forked from zfteam/rs97-commander-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsdlutils.cpp
executable file
·164 lines (149 loc) · 6.42 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
#include "sdlutils.h"
#include <algorithm>
#include <iostream>
#include <SDL_image.h>
#include <SDL2_rotozoom.h>
#include "def.h"
#include "fileutils.h"
#include "resourceManager.h"
#include "screen.h"
extern SDL_Surface *ScreenSurface;
bool SDL_utils::isSupportedImageExt(const std::string &ext) {
return ext == "jpg" || ext == "jpeg" || ext == "png" || ext == "ico" || ext == "bmp" || ext == "xcf";
}
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)
std::cerr << "SDL_utils::loadImageToFit: " << IMG_GetError() << std::endl;
SDL_ClearError();
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_w *= screen.ppu_x;
target_h *= screen.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 = File_utils::getLowercaseFileExtension(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_Surface *l_img3 = SDL_ConvertSurfaceFormat(l_img2, SDL_PIXELFORMAT_RGBA8888, 0);
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 * screen.ppu_x;
l_offset.y = p_y * screen.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 * screen.ppu_x, 72 * screen.ppu_y);
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, const SDL_Color &p_bg)
{
SDL_Surface *result = TTF_RenderUTF8_Shaded(p_font, p_text.c_str(), p_fg, p_bg);
if (result == nullptr) {
std::cerr << "TTF_RenderUTF8_Shaded: " << SDL_GetError() << std::endl;
SDL_ClearError();
}
return result;
}
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 / screen.ppu_x, p_y, l_text, p_destination);
break;
case T_TEXT_ALIGN_CENTER:
applySurface(p_x - l_text->w / 2 / screen.ppu_x, p_y, l_text, p_destination);
break;
default:
break;
}
SDL_FreeSurface(l_text);
}
SDL_Surface *SDL_utils::createSurface(int width, int height)
{
return SDL_CreateRGBSurface(SURFACE_FLAGS, width, 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);
}
SDL_Surface *SDL_utils::createImage(const int p_width, const int p_height, const Uint32 p_color)
{
SDL_Surface *l_ret = createSurface(p_width, p_height);
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();
IMG_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 = Rect(
(screen.w * screen.ppu_x - (l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * screen.ppu_x)) / 2,
(screen.h * screen.ppu_y - (l_surfaceTmp->h + 9)) / 2,
l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * screen.ppu_x,
l_surfaceTmp->h + 4 * screen.ppu_y);
SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER));
l_rect.x += DIALOG_BORDER * screen.ppu_x;
l_rect.y += DIALOG_BORDER * screen.ppu_y;
l_rect.w -= 2 * DIALOG_BORDER * screen.ppu_x;
l_rect.h -= 2 * DIALOG_BORDER * screen.ppu_y;
SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1));
applySurface((screen.w - l_surfaceTmp->w / screen.ppu_x) / 2, (screen.h - l_surfaceTmp->h / screen.ppu_y) / 2, l_surfaceTmp, Globals::g_screen);
SDL_FreeSurface(l_surfaceTmp);
//SDL_Flip(Globals::g_screen);
//SDL_Flip(Globals::g_screen);
// SDL_Flip(ScreenSurface);
SDL_UpdateWindowSurface(Globals::g_sdlwindow);
SDL_UpdateWindowSurface(Globals::g_sdlwindow);
}