-
Notifications
You must be signed in to change notification settings - Fork 6
/
Letter.cpp
97 lines (76 loc) · 1.76 KB
/
Letter.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
90
91
92
93
94
95
96
//
// Flyweight.cpp
// Design Patterns
//
// Created by Nathan Daly on 4/24/12.
// Copyright 2012 Lions Entertainment. All rights reserved.
//
#include "Letter.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;
namespace GUI {
const char* const letters_file_path = "fonts/black/";
void Letter::drawself(SDL_Surface *dest, int x, int y) const{
display_image(image, dest, x, y, 0);
}
int Letter::get_height() const {
return image->h;
}
int Letter::get_width() const {
return image->w;
}
Letter* Letter::get_letter(char letter_){
static letter_map_t letters;
Letter* letter(letters[letter_]);
if (!letter){
string filename = letters_file_path;
string spec_char = special_chars(letter_);
if (spec_char != ""){
filename += spec_char;
}
else if (!isalnum(letter_)) return 0;
else {
filename += letter_;
}
Letter* result(new Letter);
filename += ".bmp";
result->set_image (filename);
letter = result;
}
return letter;
}
string Letter::special_chars(char letter_){
switch (letter_) {
case ' ':
return "_space";
break;
case ':':
return "_colon";
break;
default:
return "";
break;
}
}
void Letter::set_image(const std::string& filename) {
// USE TTF! :D
image = loadBMP(filename);
if (!image) {
string error_msg = "Couldn't load " + filename +": "+ SDL_GetError() +"\n";
throw Error(error_msg.c_str());
}
}
Letter::Print_Letter::Print_Letter(SDL_Surface *dest, int x_, int y_)
: x(x_), y(y_), surface(dest)
{ }
void Letter::Print_Letter::operator() (const Letter* letter){
int width = letter->get_width();
letter->drawself(surface, x, y);
x+= width;
}
} // namespace GUI