This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
forked from jbanes/rs97-commander
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sdlutils.cpp
executable file
·184 lines (167 loc) · 6.64 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
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
#include "sdlutils.h"
#include <algorithm>
#include <iostream>
#include <SDL_image.h>
#include <SDL_rotozoom.h>
#include "def.h"
#include "resourceManager.h"
extern SDL_Surface *ScreenSurface;
// 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;
}
static void AsciiToLower(std::string *s)
{
for (char &c : *s)
if (c >= 'A' && c <= 'Z')
c -= ('Z' - 'z');
}
static std::string GetFileExtension(const std::string &name) {
const auto dot_pos = name.rfind('.');
if (dot_pos == std::string::npos)
return "";
std::string ext = name.substr(dot_pos + 1);
AsciiToLower(&ext);
return ext;
}
SDL_Surface *SDL_utils::loadImageToFit(const std::string &p_filename, int fit_w, int fit_h)
{
// Load image
SDL_Surface *l_img = IMG_Load(p_filename.c_str());
if (IMG_GetError() != nullptr && *IMG_GetError() != '\0') {
if (strcmp(IMG_GetError(), "Unsupported image format") == 0)
SDL_ClearError();
else
std::cerr << "SDL_utils::loadImageToFit: " << IMG_GetError() << std::endl;
return nullptr;
}
const double aspect_ratio = static_cast<double>(l_img->w) / l_img->h;
int target_w, target_h;
if (fit_w * l_img->h <= fit_h * l_img->w) {
target_w = std::min(l_img->w, fit_w);
target_h = target_w / aspect_ratio;
} else {
target_h = std::min(l_img->h, fit_h);
target_w = target_h * aspect_ratio;
}
target_h *= PPU_Y;
SDL_Surface *l_img2 = zoomSurface(l_img, static_cast<double>(target_w) / l_img->w, static_cast<double>(target_h) / l_img->h, SMOOTHING_ON);
SDL_FreeSurface(l_img);
const std::string ext = GetFileExtension(p_filename);
const bool supports_alpha = ext != "xcf" && ext != "jpg" && ext != "jpeg";
SDL_Surface *l_img3 = supports_alpha ? SDL_DisplayFormatAlpha(l_img2) : SDL_DisplayFormat(l_img2);
SDL_FreeSurface(l_img2);
return l_img3;
}
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 * PPU_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_OpenFontDPI(p_font.c_str(), p_size, 72, 72 * PPU_Y);
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, const SDL_Color &p_bg)
{
return TTF_RenderUTF8_Shaded(p_font, p_text.c_str(), p_fg, p_bg);
}
void SDL_utils::applyText(Sint16 p_x, Sint16 p_y, SDL_Surface* p_destination, TTF_Font *p_font, const std::string &p_text, const SDL_Color &p_fg, const SDL_Color &p_bg, const T_TEXT_ALIGN p_align)
{
SDL_Surface *l_text = renderText(p_font, p_text, p_fg, p_bg);
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, {COLOR_BG_1});
SDL_Rect l_rect;
l_rect.x = (SCREEN_WIDTH - (l_surfaceTmp->w + 2 * DIALOG_MARGIN + 2 * DIALOG_BORDER)) >> 1;
l_rect.y = ((SCREEN_HEIGHT * PPU_Y - (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 * PPU_Y;
l_rect.w -= 2 * DIALOG_BORDER;
l_rect.h -= 2 * DIALOG_BORDER * PPU_Y;
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 * PPU_Y - l_surfaceTmp->h) >> 1) / PPU_Y, l_surfaceTmp, Globals::g_screen);
SDL_FreeSurface(l_surfaceTmp);
//SDL_Flip(Globals::g_screen);
//SDL_Flip(Globals::g_screen);
SDL_Flip(ScreenSurface);
}