-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleTextures.cpp
More file actions
103 lines (85 loc) · 2.03 KB
/
ModuleTextures.cpp
File metadata and controls
103 lines (85 loc) · 2.03 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//=================================
// forward declared dependencies
//=================================
// included dependencies
#include "Application.h"
#include "ModuleTextures.h"
#include "ModuleRender.h"
#include "SDL_image/include/SDL_image.h"
#pragma comment( lib, "SDL_image/libx86/SDL2_image.lib" )
//=================================
// the actual code
ModuleTextures::ModuleTextures(Application *app, bool start_enabled) : Module(app, start_enabled)
{ }
// 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. JPG and TIF
// can be also loaded.
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 quitting
bool ModuleTextures::cleanUp()
{
LOG("Freeing textures and Image Library");
doubleNode<SDL_Texture*>* item = textures.getFirst();
while (item != NULL)
{
SDL_DestroyTexture(item->data);
item = item->next;
}
textures.clear();
IMG_Quit();
return true;
}
SDL_Texture* const ModuleTextures::load(const char *path)
{
SDL_Texture *texture = NULL;
// Load file for use as an image in a new surface
SDL_Surface *surface = IMG_Load(path);
if (surface == NULL)
{
LOG("Could not load surface with path: %s. IMG_Load: %s", path, IMG_GetError());
}
else
{
texture = SDL_CreateTextureFromSurface(app->renderer->renderer, surface);
if (texture == NULL)
{
LOG("Unable to create texture form surface! SDL_Error: %s", SDL_GetError());
}
else
{
textures.add(texture);
}
SDL_FreeSurface(surface);
}
return texture;
}
// Free texture from memory
void ModuleTextures::unload(SDL_Texture *texture)
{
doubleNode<SDL_Texture*>* item = textures.getFirst();
while (item != NULL)
{
if (item->data == texture)
{
SDL_DestroyTexture(item->data);
textures.del(item);
break;
}
item = item->next;
}
}