-
Notifications
You must be signed in to change notification settings - Fork 5
/
ImageManager.cpp
98 lines (83 loc) · 2.73 KB
/
ImageManager.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
//=============================================================================
// Author: Arvind
// Purpose: Contains the image manager class - used to manage in-game images
//=============================================================================
#include "master.h"
#include "ImageManager.h"
using namespace pyrodactyl;
//Stuff we use throughout the game
namespace pyrodactyl
{
ImageManager gImageManager;
}
//------------------------------------------------------------------------
// Purpose: Load assets here.
//------------------------------------------------------------------------
void ImageManager::LoadMap(const std::string &filename)
{
for (auto it = map.begin(); it != map.end(); ++it)
it->second->Destroy();
map.clear();
XMLDoc image_list(filename);
if (image_list.ready())
{
rapidxml::xml_node<char> *node = image_list.Doc()->first_node("res");
for (auto n = node->first_node("image"); n != nullptr; n = n->next_sibling("image"))
{
ImageKey key;
if (LoadImgKey(key, "name", n))
{
std::string path;
LoadStr(path, "path", n, false);
map[key] = TextureFromName(path);
}
}
}
}
bool ImageManager::Init()
{
//Load common assets
LoadMap("core/data/common.xml");
invalid_img = map[0];
return true;
}
//------------------------------------------------------------------------
// Purpose: Get texture for a particular id
//------------------------------------------------------------------------
Texture* ImageManager::GetTexture(const ImageKey &id)
{
if (map.count(id) > 0)
return map[id];
return invalid_img;
}
bool ImageManager::ValidTexture(const ImageKey &id)
{
if (id != 0 && map.count(id) > 0)
return true;
return false;
}
//------------------------------------------------------------------------
// Purpose: Draw
//------------------------------------------------------------------------
void ImageManager::Draw(const int &x, const int &y, const int &w, const int &h, const ImageKey &id, const SDL_Rect* clip)
{
RenderTexture(GetTexture(id), x, y, w, h, clip);
}
void ImageManager::CircleDraw(const int &x, const int &y, const ImageKey &id, const int &start_angle, const int &end_angle, const float &radius)
{
GLcoord2 pos(x, y);
RenderCircularBar(GetTexture(id), start_angle, end_angle, radius, pos);
}
void ImageManager::Draw(const int &x, const int &y, const int &w, const int &h, const ImageKey &id, const float &fade, const SDL_Rect* clip)
{
RenderTexture(GetTexture(id), x, y, w, h, fade, clip);
}
//------------------------------------------------------------------------
// Purpose: free resources
//------------------------------------------------------------------------
void ImageManager::Quit()
{
for (auto it = map.begin(); it != map.end(); ++it)
it->second->Destroy();
map.clear();
}