-
Notifications
You must be signed in to change notification settings - Fork 5
/
sprite.cpp
213 lines (176 loc) · 4.3 KB
/
sprite.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include "sprite.h"
#include "global.h"
SpriteSheet::SpriteSheet(const char* filename, int tileDim, bool flip)
{
_numberOfSprites = 0;
_indexBuffers = NULL;
if (flip)
{
SDL_Surface* surf = loadSurfaceFromImage(filename);
if (!surf)
{
fprintf(stderr, "Fatal: Failed to load %s\n", filename);
_tex = 0;
return;
}
surf = reverseSpriteSheet(surf, tileDim);
_tex = createTextureFromSurface(surf);
SDL_FreeSurface(surf);
}
else
{
_tex = loadTexture(filename);
if (_tex == 0)
{
fprintf(stderr, "Fatal: Failed to load %s\n", filename);
return;
}
}
_tileDim = tileDim;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &_width);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &_height);
_widthInSprites = _width / tileDim;
unsigned int heightInSprites = _height / tileDim;
_numberOfSprites = _widthInSprites * heightInSprites;
_indexBuffers = new GLuint[_numberOfSprites];
_clips.resize(_numberOfSprites);
unsigned int i, j;
for (i = 0; i < _widthInSprites; i++)
{
for (j = 0; j < heightInSprites; j++)
{
_clips[j * _widthInSprites + i].w = tileDim;
_clips[j * _widthInSprites + i].h = tileDim;
_clips[j * _widthInSprites + i].x = i * tileDim;
_clips[j * _widthInSprites + i].y = j * tileDim;
}
}
LOGF((stdout, "%s is %ix%i pixels and has %i %ix%i sprites.\n", filename, _width, _height, _numberOfSprites, tileDim, tileDim));
}
SpriteSheet::~SpriteSheet()
{
LOGF((stdout, "Deleting a Sprite Resource with texture ID %i.\n", _tex));
glDeleteTextures(1, &_tex);
_width = 0;
_height = 0;
if (_indexBuffers)
{
delete [] _indexBuffers;
}
}
GLint SpriteSheet::getWidth()
{
return _width;
}
GLint SpriteSheet::getHeight()
{
return _height;
}
GLuint SpriteSheet::getTexId()
{
return _tex;
}
int SpriteSheet::getTileDim()
{
return _tileDim;
}
void SpriteSheet::getClipPosition(unsigned int index, float* x, float* y)
{
*x = _clips[index].x;
*y = _clips[index].y;
}
unsigned int SpriteSheet::getNumberOfSprites()
{
return _numberOfSprites;
}
GLuint* SpriteSheet::getIndexBuffers()
{
return _indexBuffers;
}
GLuint SpriteSheet::getVertexBuffer()
{
return _vertexBuffer;
}
void SpriteSheet::setVertexBuffer(GLuint vbo)
{
_vertexBuffer = vbo;
}
AnimationSequence::AnimationSequence(bool looping)
{
_looping = looping;
_finished = false;
_framesPerSecond = 1;
_millisecondsPerFrame = (float)(1.0f / _framesPerSecond) * 1000;
_savedMsPerFrame = _millisecondsPerFrame;
currentSpriteIndex = 0;
}
AnimationSequence::~AnimationSequence()
{
sequence.clear();
}
void AnimationSequence::addSpriteNumber(unsigned int index)
{
sequence.push_back(index);
}
unsigned int AnimationSequence::getSpriteAt(size_t i)
{
return sequence[i];
}
void AnimationSequence::setSpriteIndex(unsigned int index)
{
currentSpriteIndex = index;
}
bool AnimationSequence::readFromFile(const char* filename)
{
std::ifstream input(filename);
if (!input)
{
LOGF((stderr, "Failed to read animation file %s.\n", filename));
return false;
}
int anim = 0;
int numFrames = 0;
char string[32];
input.getline(string, 32);
_framesPerSecond = atoi(string);
_millisecondsPerFrame = (float)(1.0f / _framesPerSecond) * 1000;
_savedMsPerFrame = _millisecondsPerFrame;
input.getline(string, 32);
numFrames = atoi(string);
for (int i = 0; i < numFrames; i++)
{
input.getline(string, 32);
anim = atoi(string);
addSpriteNumber(anim);
}
return true;
}
size_t AnimationSequence::getNumSprites()
{
return sequence.size();
}
float AnimationSequence::getMsPerFrame()
{
return _savedMsPerFrame;
}
bool AnimationSequence::isLooping()
{
return _looping;
}