-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpriteSheet.cpp
192 lines (183 loc) · 4.98 KB
/
SpriteSheet.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#ifdef WIN32
#define _USE_MATH_DEFINES
#include <cmath>
#endif
#include <apollo/Apollo.h>
#include <apollo/Graphics/SpriteSheet.h>
#include <apollo/Utilities/ResourceManager.h>
#include <TinyXML/tinyxml.h>
#include <apollo/Logging.h>
#include <apollo/Graphics/ImageLoader.h>
namespace Graphics
{
void SpriteSheet::MakeResident ()
{
texID = ImageLoader::CreateTexture(surface, false);
}
SpriteSheet::SpriteSheet ( const std::string& name )
: texID(0)
{
std::string configPath, imagePath;
if (name[0] == '+')
{
configPath = "";
imagePath = "Images/" + name.substr(1, std::string::npos) + ".png";
}
else
{
if (Preferences::Get("Sprite/Original", "blah") == "blah") //[ALISTAIR]: Why doesn't this work?
{
configPath = "NewResources/Sprites/" + name + ".xml";
imagePath = "NewResources/Sprites/" + name + ".png";
SDL_RWops* imageRWOps = ResourceManager::OpenFile(imagePath);
if (!imageRWOps)
{
configPath = "Sprites/" + name + ".xml";
imagePath = "Sprites/" + name + ".png";
}
} else
{
configPath = "Sprites/" + name + ".xml";
imagePath = "Sprites/" + name + ".png";
}
}
SDL_RWops* configRWOps = ResourceManager::OpenFile(configPath);
surface = ImageLoader::LoadImage(imagePath);
if (!surface)
{
// TODO: make this work gracefully
LOG("Graphics::SpriteSheet", LOG_ERROR, "Failed to load image: %s", name.c_str());
abort();
delete configRWOps;
}
if (configRWOps)
{
// load config
size_t confLength;
char* confData = (char*)ResourceManager::ReadFull(&confLength, configRWOps, 1);
TiXmlDocument* xmlDoc = new TiXmlDocument(name + ".xml");
confData = (char*)realloc(confData, confLength + 1);
confData[confLength] = '\0';
xmlDoc->Parse(confData);
free(confData);
if (xmlDoc->Error())
{
LOG("Graphics::SpriteSheet", LOG_ERROR, "XML error: %s", xmlDoc->ErrorDesc());
abort();
}
TiXmlElement* root = xmlDoc->RootElement();
assert(root);
TiXmlElement* dimensions = root->FirstChild("dimensions")->ToElement();
assert(dimensions);
TiXmlElement* scale = root->FirstChild("scale")->ToElement();
int rc;
if (scale)
{
rc = scale->QueryFloatAttribute("factor", &scaleFactor);
if (rc != TIXML_SUCCESS)
scaleFactor = 1.0f;
}
else
{
scaleFactor = 1.0f;
}
rc = dimensions->QueryIntAttribute("x", &sheetTilesX);
assert(rc == TIXML_SUCCESS);
rc = dimensions->QueryIntAttribute("y", &sheetTilesY);
assert(rc == TIXML_SUCCESS);
assert(sheetTilesX > 0);
assert(sheetTilesY > 0);
tileSizeX = surface->w / sheetTilesX;
tileSizeY = surface->h / sheetTilesY;
if (root->FirstChild("rotational"))
rotational = true;
else
rotational = false;
delete xmlDoc;
}
else
{
// assume it's just one sprite
sheetTilesX = 1;
sheetTilesY = 1;
rotational = false;
tileSizeX = surface->w;
tileSizeY = surface->h;
scaleFactor = 1.0f;
}
}
SpriteSheet::~SpriteSheet ()
{
if (texID)
glDeleteTextures(1, &texID);
SDL_FreeSurface(surface);
}
void SpriteSheet::Draw ( int x, int y, const vec2& size )
{
vec2 halfSize = size / 2.0f;
if (texID)
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texID);
else
MakeResident();
if (scaleFactor <= 0)
{
printf("Trouble with scaleFactor value.");
abort();
}
float vx = halfSize.X() * scaleFactor, vy = halfSize.Y() * scaleFactor;
GLfloat vertices[] = { -vx, -vy, vx, -vy, vx, vy, -vx, vy };
float texBLX, texBLY, texWidth = tileSizeX, texHeight = tileSizeY;
texBLX = (tileSizeX * x);
texBLY = (tileSizeY * y);
GLfloat texCoords[] = { texBLX, texBLY + texHeight,
texBLX + texWidth, texBLY + texHeight,
texBLX + texWidth, texBLY,
texBLX, texBLY };
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_QUADS, 0, 4);
}
/* [ADAMLATER]
void SpriteSheet::DrawTile ( int x, int y, float vx, float vy, const vec2& size )
{
vec2 halfSize = size / 2.0f;
if (texID)
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texID);
else
MakeResident();
if (scaleFactor <= 0)
{
printf("Trouble with scaleFactor value.");
abort();
}
GLfloat vertices[] = { -vx, -vy, vx, -vy, vx, vy, -vx, vy };
float texBLX, texBLY, texWidth = tileSizeX, texHeight = tileSizeY;
texBLX = (tileSizeX * x);
texBLY = (tileSizeY * y);
GLfloat texCoords[] = { texBLX, texBLY + texHeight,
texBLX + texWidth, texBLY + texHeight,
texBLX + texWidth, texBLY,
texBLX, texBLY };
glVertexPointer(2, GL_FLOAT, 0, vertices);
glTexCoordPointer(2, GL_FLOAT, 0, texCoords);
glDrawArrays(GL_QUADS, 0, 4);
}*/
void SpriteSheet::DrawRotation ( const vec2& size, float angle )
{
int numObjects = sheetTilesX * sheetTilesY;
angle += (M_PI / float(numObjects));
angle -= M_PI / 2.0;
if (angle < 0.0)
angle += 2.0*M_PI;
// angle is now 0 = north, 2π = north, anticlockwise
angle /= 2.0*M_PI;
// angle is now 0=north, 1=north, anticlockwise
angle = 1.0f - angle;
int index = (int)((angle - 0.00001f) * numObjects) + 1;
if (index == sheetTilesX * sheetTilesY)
index = 0;
int x = index % sheetTilesX;
int y = (index - x) / sheetTilesX;
Draw(x, y, size);
}
}