-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModuleTextures.cpp
77 lines (61 loc) · 1.36 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
#include "Globals.h"
#include "Application.h"
#include "ModuleRender.h"
#include "ModuleTextures.h"
#include "SDL.h"
#include "SDL_image/include/SDL_image.h"
using namespace std;
ModuleTextures::ModuleTextures()
{
}
// Destructor
ModuleTextures::~ModuleTextures()
{
IMG_Quit();
}
// Called before render is available
bool ModuleTextures::Init()
{
bool ret = true;
// load support for the PNG image format
int flags = IMG_INIT_PNG;
int init = IMG_Init(flags);
if ((init & flags) != flags)
ret = false;
return ret;
}
// Called before quitting
bool ModuleTextures::CleanUp()
{
for (list<SDL_Texture*>::iterator it = textures.begin(); it != textures.end(); ++it)
SDL_DestroyTexture(*it);
textures.clear();
return true;
}
// Load new texture from file path
SDL_Texture* const ModuleTextures::Load(const char* path)
{
SDL_Texture* texture = nullptr;
SDL_Surface* surface = IMG_Load(path);
if (surface != nullptr)
{
texture = SDL_CreateTextureFromSurface(App->renderer->renderer, surface);
if (texture != nullptr)
textures.push_back(texture);
SDL_FreeSurface(surface);
}
return texture;
}
// Free texture from memory
void ModuleTextures::Unload(SDL_Texture* texture)
{
for (list<SDL_Texture*>::iterator it = textures.begin(); it != textures.end(); ++it)
{
if (*it == texture)
{
SDL_DestroyTexture(*it);
textures.erase(it);
break;
}
}
}