forked from zfteam/rs97-commander-sdl2
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathresourceManager.cpp
More file actions
executable file
·85 lines (76 loc) · 2.59 KB
/
resourceManager.cpp
File metadata and controls
executable file
·85 lines (76 loc) · 2.59 KB
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
#include <iostream>
#include <SDL_image.h>
#include <SDL2_rotozoom.h>
#include "resourceManager.h"
#include "def.h"
#include "screen.h"
#include "sdlutils.h"
namespace {
SDL_Surface *LoadIcon(const char *path) {
SDL_Surface *img = IMG_Load(path);
if(img == nullptr)
{
std::cerr << "LoadIcon(\"" << path << "\"): " << IMG_GetError() << std::endl;
return nullptr;
}
SDL_Surface *scaled = nullptr;
if ((screen.ppu_x == 1 || screen.ppu_x == 2) && (screen.ppu_y == 1 || screen.ppu_y == 2)) {
scaled = shrinkSurface(img, 2 / screen.ppu_x, 2 / screen.ppu_y);
} else {
scaled = zoomSurface(img, screen.ppu_x / 2, screen.ppu_y / 2, SMOOTHING_ON);
}
SDL_FreeSurface(img);
// SDL_Surface *display = SDL_DisplayFormatAlpha(scaled);
SDL_Surface *display = SDL_ConvertSurfaceFormat(scaled, SDL_PIXELFORMAT_RGBA8888, 0);
SDL_FreeSurface(scaled);
return display;
}
} // namespace
CResourceManager& CResourceManager::instance(void)
{
static CResourceManager l_singleton;
return l_singleton;
}
CResourceManager::CResourceManager(void) :
m_font(NULL)
{
// Load images
m_surfaces[T_SURFACE_FOLDER] = LoadIcon(RES_DIR "folder.png");
m_surfaces[T_SURFACE_FILE] = LoadIcon(RES_DIR "file-text.png");
m_surfaces[T_SURFACE_FILE_IMAGE] = LoadIcon(RES_DIR "file-image.png");
m_surfaces[T_SURFACE_FILE_INSTALLABLE_PACKAGE] = LoadIcon(RES_DIR "file-ipk.png");
m_surfaces[T_SURFACE_FILE_PACKAGE] = LoadIcon(RES_DIR "file-opk.png");
m_surfaces[T_SURFACE_UP] = LoadIcon(RES_DIR "up.png");
m_surfaces[T_SURFACE_CURSOR1] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1));
m_surfaces[T_SURFACE_CURSOR2] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2));
// Load font
m_font = SDL_utils::loadFont(FONT_TO_USE, FONT_SIZE);
}
void CResourceManager::sdlCleanup(void)
{
INHIBIT(std::cout << "CResourceManager::sdlCleanup" << std::endl;)
int l_i(0);
// Free surfaces
for (l_i = 0; l_i < NB_SURFACES; ++l_i)
{
if (m_surfaces[l_i] != NULL)
{
SDL_FreeSurface(m_surfaces[l_i]);
m_surfaces[l_i] = NULL;
}
}
// Free font
if (m_font != NULL)
{
TTF_CloseFont(m_font);
m_font = NULL;
}
}
SDL_Surface *CResourceManager::getSurface(const T_SURFACE p_surface) const
{
return m_surfaces[p_surface];
}
TTF_Font *CResourceManager::getFont(void) const
{
return m_font;
}