-
Notifications
You must be signed in to change notification settings - Fork 6
/
GUILetter.cpp
90 lines (69 loc) · 1.94 KB
/
GUILetter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//
// Flyweight.cpp
// Design Patterns
//
// Created by Nathan Daly on 4/24/12.
// Copyright 2012 Lions Entertainment. All rights reserved.
//
#include "GUILetter.h"
#include "GameDisplay.h"
#include "GUIUtility.h"
using std::string;
#include <map>
#include <iostream>
using std::map; using std::pair;
using std::cout; using std::endl;
using GUI::Error; using GUI::DispPoint;
const char* const letters_file_path = "fonts/black/";
#include "GUIView.h"
#include "GUIImage.h"
using GUI::View;
void GUILetter::drawself(SDL_Surface *dest, int x, int y) const{
display_image(image, dest, x, y, 0);
}
void GUILetter::drawself(View *dest, int x, int y) const{
dest->draw_onto_self((GUIImage&)image, DispPoint(x,y));
}
int GUILetter::get_height() const {
return image->h;
}
int GUILetter::get_width() const {
// SUPER HACKY
if (image->w == 0) { return 5; } // for space ' '
return image->w;
}
bool operator< (const SDL_Color& a, const SDL_Color& b) {
if (a.r == b.r) {
if (a.g == b.g) {
return a.b < b.b;
}
else return a.g < b.g;
}
else return a.r < b.r;
}
GUILetter* GUILetter::get_letter(char letter_, int size_, SDL_Color color_){
static letter_map_t letters;
static fonts_map_t fonts;
GUILetter* &letter = letters[letter_][size_][color_];
if (!letter){
TTF_Font* &font = fonts[size_];
if (!font){
font = TTF_OpenFont("GUIFonts/arial.ttf", size_);
if (!font) throw Error("Couldn't open font!");
}
letter = new GUILetter(letter_, font, size_, color_);
}
return letter;
}
GUILetter::GUILetter(char letter_, TTF_Font* font, int size_, SDL_Color color_){
string ch(1,letter_);
image = TTF_RenderText_Solid(font, ch.c_str(), color_);
}
GUILetter::Print_GUILetter::Print_GUILetter(SDL_Surface *dest, int x_, int y_)
: x(x_), y(y_), surface(dest)
{ }
void GUILetter::Print_GUILetter::operator() (const GUILetter* letter){
int width = letter->get_width();
letter->drawself(surface, x, y);
x+= width;
}