-
Notifications
You must be signed in to change notification settings - Fork 6
/
GameDisplay.cpp
325 lines (240 loc) · 8.21 KB
/
GameDisplay.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "GameDisplay.h"
#include "GUIUtility.h"
#include "Compatibility.h"
#include <cctype>
#include <vector>
#include <iostream>
using std::isspace;
using std::vector;
using std::list;
using std::string;
using std::cout; using std::endl;
using GUI::Error;
using GUI::DispPoint;
const int c_char_image_length = 17;
const int c_char_image_height = 19;
const SDL_Color default_color_key_c = {0xFF, 0, 0xFF, 0};
SDL_Surface* load_letter(char ch_, string text_image_file, string extension);
void updateScreen(SDL_Surface *screen){
if (SDL_Flip( screen ) == -1){
throw Error("Failed Screen Flip");
}
}
SDL_Surface* createDisplay(SDL_Surface *screen, int width, int height, int bpp,
SDL_Color colors[], const int numColors, const Uint32 flags){
/* Create a display surface with a grayscale palette */
if (width <= 0 || height <= 0 || bpp <= 0){
throw Error("non-positive values in createDisplay not allowed!");
}
// /* Fill colors with color information */
// for(int i = 0; i < numColors; i++){
// colors[i].r = i;
// colors[i].g = i;
// colors[i].b = i;
// }
/* Create display */
screen=SDL_SetVideoMode(width, height, bpp, flags);
if(!screen){
throw Error("Couldn't set video mode: \n" + string(SDL_GetError()));
}
// /* Set palette */
// if (!SDL_SetPalette(screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, numColors)) {
// throw Error("Couldn't set color palette: \n" + string(SDL_GetError()));
// }
return screen;
}
SDL_Surface * loadBMP(string file) {
//Temporary storage for the image that's loaded
SDL_Surface* loadedImage = SDL_LoadBMP( file.c_str() );
//If nothing went wrong in loading the image
if( !loadedImage ) {
string error_msg = "Couldn't load " + file +": "+ SDL_GetError() +"\n";
throw Error(error_msg);
}
//The optimized image that will be used
SDL_Surface* optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
if (!optimizedImage) {
string error_msg = "Couldn't optimize " + file +": "+ SDL_GetError() +"\n";
throw Error(error_msg);
}
//Return the optimized image
return optimizedImage;
}
SDL_Surface * loadBMPAlpha(string file, SDL_Color color_key)
{
SDL_Surface *image = loadBMP(file);
//Map the color key
Uint32 colorkey = SDL_MapRGB( image->format, color_key.r, color_key.g, color_key.b);
//Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
SDL_SetColorKey( image, SDL_SRCCOLORKEY, colorkey );
return image;
}
void ShowBMP(string file, SDL_Surface *screen, int x, int y, bool update)
{
SDL_Surface *image;
image = loadBMP(file);
if ( image == NULL ) {
return;
}
display_image(image, screen, x, y, update);
SDL_FreeSurface(image);
}
void ShowBMP(string file, SDL_Surface *screen, SDL_Rect rect, bool update)
{
ShowBMP(file, screen, rect.x, rect.y, update);
}
void display_image (const SDL_Surface *src, SDL_Surface *dest, DispPoint pos, bool update, SDL_Rect rect){
display_image (src, dest, pos.x, pos.y, update, rect);
}
void display_image (const SDL_Surface *src, SDL_Surface *dest, int x, int y, bool update, SDL_Rect rect){
SDL_Rect dest_rect = {x, y, rect.w, rect.h};
// if (rect.w != def_rect_c.w && rect.h != def_rect_c.h){
// SDL_BlitSurface(src, &rect, dest, &dest_rect);
// }
SDL_BlitSurface((SDL_Surface *)src, 0, dest, &dest_rect);
if (update) SDL_UpdateRects(dest, 1, &dest_rect);
}
void displayToScreen (const SDL_Surface *src, int x, int y, bool update, SDL_Rect rect) {
display_image(src, SDL_GetVideoSurface(), x, y, update, rect);
}
void displayTextToScreen (const string &a, int x, int y, int size, bool update){
static TTF_Font* font = TTF_OpenFont("GUIFonts/arial.ttf", size);
if (!font) throw Error("Couldn't load font: arial.ttf");
SDL_Color textColor = {255, 255, 255};
SDL_Surface* message = TTF_RenderText_Solid(font, a.c_str(), textColor);
display_image(message, SDL_GetVideoSurface(), x, y, update);
SDL_FreeSurface(message);
}
SDL_Surface* createText2 (const string &a, int size, SDL_Color textColor){
TTF_Font* font = TTF_OpenFont("GUIFonts//arial.ttf", size);
if (!font) throw Error("Couldn't load font: arial.ttf");
SDL_Surface* message = TTF_RenderText_Solid(font, a.c_str(), textColor);
TTF_CloseFont(font);
return message;
}
SDL_Surface* createText (const string &a){
string alphaImage = "fonts/";
string fileExtension = ".bmp";
if (a.size() == 0) {
return 0;
}
vector<SDL_Surface *> letters;
int x = 0;
string filePath = alphaImage;
for (size_t i = 0; i < a.size(); i++) {
SDL_Surface *letter = load_letter(a[i], alphaImage, fileExtension);
if (!letter) {
return 0;
}
x += letter->w;
letters.push_back(letter);
}
SDL_Surface *surface = create_SDL_Surface(x, letters[0]->h);
x = 0;
for (size_t i = 0; i < letters.size(); i++) {
SDL_Rect rect = {x, 0, letters[i]->w, letters[i]->h};
SDL_BlitSurface(letters[i], NULL, surface, &rect);
x += letters[i]->w;
SDL_FreeSurface(letters[i]);
}
return surface;
}
SDL_Surface* load_letter(char ch_, string text_image_file, string extension){
SDL_Surface *letter;
string filePath = text_image_file;
if (isspace(ch_)) filePath += "_space" + extension;
else if (ch_ == ':') filePath += "_colon" + extension;
else filePath += ch_ + extension;
letter = SDL_LoadBMP(filePath.c_str());
return letter;
}
SDL_Surface* create_SDL_Surface(int w, int h){
/* Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,
as expected by OpenGL for textures */
SDL_Surface *surface;
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
surface = SDL_CreateRGBSurface(SDL_HWSURFACE, w, h, 32,
rmask, gmask, bmask, amask);
if(surface == NULL) {
cout << "CreateRGBSurface failed: \n" + string(SDL_GetError()) << endl;
return 0;
}
SDL_Surface* updatedimage = SDL_DisplayFormat(surface);
if(!updatedimage) {
cout << "updateimage in CreateRGBSurface failed: \n" + string(SDL_GetError());
return 0;
}
return updatedimage;
}
Uint32 getpixel(const SDL_Surface *surface, int x, int y)
{
if (x < 0 || y <0 || x >= surface->w || y >= surface->h) {
throw Error("Trying to read out-of-bounds pixel!");
}
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
break;
case 2:
return *(Uint16 *)p;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
break;
case 4:
return *(Uint32 *)p;
break;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
if (x < 0 || y <0 || x >= surface->w || y >= surface->h) {
return;
}
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}