-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModuleTextures.cpp
139 lines (115 loc) · 2.89 KB
/
ModuleTextures.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
#include "Globals.h"
#include "Application.h"
#include "ModuleRender.h"
#include "ModuleTextures.h"
#include "SDL/include/SDL.h"
#include "SDL_image/include/SDL_image.h"
#pragma comment( lib, "SDL_image/libx86/SDL2_image.lib" )
ModuleTextures::ModuleTextures() : Module()
{
for (uint i = 0; i < MAX_TEXTURES; ++i) {
textures[i] = nullptr;
surfaces[i] = nullptr;
}
}
// Destructor
ModuleTextures::~ModuleTextures()
{}
// Called before render is available
bool ModuleTextures::Init()
{
LOG("Init Image library");
bool ret = true;
// load support for the PNG image format
int flags = IMG_INIT_PNG;
int init = IMG_Init(flags);
if((init & flags) != flags)
{
LOG("Could not initialize Image lib. IMG_Init: %s", IMG_GetError());
ret = false;
}
return ret;
}
// Called before q uitting
bool ModuleTextures::CleanUp()
{
LOG("Freeing textures and Image library\n");
for (int i = (MAX_TEXTURES - 1); i >= 0; --i) {
if (textures[i] != nullptr)
SDL_DestroyTexture(textures[i]);
textures[i] = nullptr;
if (surfaces[i] != nullptr)
SDL_FreeSurface(surfaces[i]);
surfaces[i] = nullptr;
}
IMG_Quit();
return true;
}
// Load new texture from file path
SDL_Texture* const ModuleTextures::Load(const char* path)
{
SDL_Surface* image = IMG_Load(path);
SDL_Texture* ret = nullptr;
if (image == NULL) {
LOG("Failed to load image \"%s\" IMG_Load: %s\n", path, IMG_GetError());
}
else {
last_texture = MAX_TEXTURES;
for (int i = 0; i < MAX_TEXTURES; ++i) {
if (textures[i] == nullptr) {
last_texture = i;
break;
}
}
if (last_texture == MAX_TEXTURES) {
LOG("Overflow error: Overwriting textures. \n");
last_texture = 0;
}
if (textures[last_texture] != nullptr)
SDL_DestroyTexture(textures[last_texture]);
textures[last_texture] = nullptr;
textures[last_texture] = SDL_CreateTextureFromSurface(App->render->renderer, image);
if (textures[last_texture] == NULL) {
LOG("Failed to create texture from surface SDL_CreateTextureFromSurface: %s\n", SDL_GetError());
}
else
ret = textures[last_texture];
SDL_FreeSurface(image);
}
return ret;
}
SDL_Surface * const ModuleTextures::LoadSurface(const char * path)
{
SDL_Surface* ret = nullptr;
ret = IMG_Load(path);
if (ret == NULL)
LOG("Failed to load image \"%s\" IMG_Load: %s\n", path, IMG_GetError());
return ret;
}
SDL_Texture * const ModuleTextures::SurfaceToTexture(SDL_Surface * surface)
{
SDL_Texture* ret = SDL_CreateTextureFromSurface(App->render->renderer, surface);
return ret;
}
//// Load new texture from file path
bool ModuleTextures::Unload(SDL_Texture* texture)
{
bool ret = false;
for (uint i = 0; i < MAX_TEXTURES; ++i)
{
if (texture == textures[i])
{
SDL_DestroyTexture(textures[i]);
textures[i] = nullptr;
ret = true;
break;
}
}
return ret;
}
bool ModuleTextures::UnloadSurface(SDL_Surface * surface)
{
bool ret = true;
SDL_FreeSurface(surface);
return ret;
}