-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsdlutils.cpp
129 lines (120 loc) · 4.75 KB
/
sdlutils.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
#include <iostream>
#include "sdlutils.h"
#include "SDL/SDL_image.h"
#include "def.h"
#include "resourceManager.h"
// Load an image using SDL_image
SDL_Surface *SDL_utils::loadImage(const std::string &p_filename)
{
INHIBIT(std::cout << "SDL_utils::loadImage(" << p_filename << ")" << std::endl;)
// Load image
SDL_Surface* l_img = IMG_Load(p_filename.c_str());
SDL_Surface* l_img2 = NULL;
if(l_img != NULL)
{
// Optimize the image
l_img2 = SDL_DisplayFormat(l_img);
// Free the first image
SDL_FreeSurface(l_img);
// Set color key
if (l_img2 != NULL)
SDL_SetColorKey(l_img2, SDL_SRCCOLORKEY, SDL_MapRGB(l_img2->format, COLOR_KEY));
}
// Check errors
if (l_img2 == NULL)
std::cerr << "SDL_utils::loadImage: " << SDL_GetError() << std::endl;
return l_img2;
}
void SDL_utils::applySurface(const Sint16 p_x, const Sint16 p_y, SDL_Surface* p_source, SDL_Surface* p_destination, SDL_Rect *p_clip)
{
// Rectangle to hold the offsets
SDL_Rect l_offset;
// Set offsets
l_offset.x = p_x;
l_offset.y = p_y;
//Blit the surface
SDL_BlitSurface(p_source, p_clip, p_destination, &l_offset);
}
TTF_Font *SDL_utils::loadFont(const std::string &p_font, const int p_size)
{
INHIBIT(std::cout << "SDL_utils::loadFont(" << p_font << ", " << p_size << ")" << std::endl;)
TTF_Font *l_font = TTF_OpenFont(p_font.c_str(), p_size);
if (l_font == NULL)
std::cerr << "SDL_utils::loadFont: " << SDL_GetError() << std::endl;
return l_font;
}
SDL_Surface *SDL_utils::renderText(TTF_Font *p_font, const std::string &p_text, const SDL_Color &p_fg)
{
return TTF_RenderUTF8_Solid(p_font, p_text.c_str(), p_fg);
}
void SDL_utils::applyText(const Sint16 p_x, const Sint16 p_y, SDL_Surface* p_destination, TTF_Font *p_font, const std::string &p_text, const SDL_Color &p_fg, const T_TEXT_ALIGN p_align)
{
SDL_Surface *l_text = renderText(p_font, p_text, p_fg);
switch (p_align)
{
case T_TEXT_ALIGN_LEFT:
applySurface(p_x, p_y, l_text, p_destination);
break;
case T_TEXT_ALIGN_RIGHT:
applySurface(p_x - l_text->w, p_y, l_text, p_destination);
break;
case T_TEXT_ALIGN_CENTER:
applySurface(p_x - l_text->w / 2, p_y, l_text, p_destination);
break;
default:
break;
}
SDL_FreeSurface(l_text);
}
SDL_Surface *SDL_utils::createImage(const int p_width, const int p_height, const Uint32 p_color)
{
// Create image in the same format as the screen
SDL_Surface *l_ret = SDL_CreateRGBSurface(SURFACE_FLAGS, p_width, p_height, Globals::g_screen->format->BitsPerPixel, Globals::g_screen->format->Rmask, Globals::g_screen->format->Gmask, Globals::g_screen->format->Bmask, Globals::g_screen->format->Amask);
if (l_ret == NULL)
std::cerr << "SDL_utils::createImage: " << SDL_GetError() << std::endl;
// Fill image with the given color
SDL_FillRect(l_ret, NULL, p_color);
return l_ret;
}
void SDL_utils::renderAll(void)
{
if (Globals::g_windows.empty())
return;
// First window to draw is the last fullscreen
unsigned int l_i = Globals::g_windows.size() - 1;
while (l_i && !Globals::g_windows[l_i]->isFullScreen())
--l_i;
// Draw windows
for (std::vector<CWindow *>::iterator l_it = Globals::g_windows.begin() + l_i; l_it != Globals::g_windows.end(); ++l_it)
(*l_it)->render(l_it + 1 == Globals::g_windows.end());
}
void SDL_utils::hastalavista(void)
{
// Destroy all dialogs except the first one (the commander)
while (Globals::g_windows.size() > 1)
delete Globals::g_windows.back();
// Free resources
CResourceManager::instance().sdlCleanup();
// Quit SDL
TTF_Quit();
SDL_Quit();
}
void SDL_utils::pleaseWait(void)
{
SDL_Surface *l_surfaceTmp = renderText(CResourceManager::instance().getFont(), "Please wait...", Globals::g_colorTextNormal);
SDL_Rect l_rect;
l_rect.x = (SCREEN_WIDTH - (l_surfaceTmp->w + 2 * DIALOG_MARGIN + 2 * DIALOG_BORDER)) >> 1;
l_rect.y = (SCREEN_HEIGHT - (l_surfaceTmp->h + 9)) >> 1;
l_rect.w = l_surfaceTmp->w + 2 * DIALOG_MARGIN + 2 * DIALOG_BORDER;
l_rect.h = l_surfaceTmp->h + 9;
SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER));
l_rect.x += DIALOG_BORDER;
l_rect.y += DIALOG_BORDER;
l_rect.w -= 2 * DIALOG_BORDER;
l_rect.h -= 2 * DIALOG_BORDER;
SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1));
applySurface((SCREEN_WIDTH - l_surfaceTmp->w) >> 1, (SCREEN_HEIGHT - l_surfaceTmp->h) >> 1, l_surfaceTmp, Globals::g_screen);
SDL_FreeSurface(l_surfaceTmp);
SDL_Flip(Globals::g_screen);
SDL_Flip(Globals::g_screen);
}