-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsoSprites.cpp
67 lines (59 loc) · 1.61 KB
/
IsoSprites.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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "IsoSprites.hpp"
#include "IsoEng.hpp"
IsoSprite::IsoSprite(std::string name, bool delimg) : name(name), delimg(delimg)
{
loadImg();
}
IsoSprite::IsoSprite(std::string name, SDL_Surface* surface, bool delimg) : name(name), delimg(delimg), img(img)
{
}
IsoSprite::~IsoSprite()
{
if(delimg) SDL_FreeSurface(img);
}
int IsoSprite::loadImg()
{
std::string lname = IsoEng::getImgPath() + name + IsoEng::getImgExtension();
img = IMG_Load(lname.c_str());
if(img == NULL)
{
IsoEng::printErr("Couldn't load image at \"" + lname + "\"");
return 0;
}
return 1;
}
IsoSprite_Animated::IsoSprite_Animated(std::string name, unsigned int fps, unsigned int nframes, bool delimg) : IsoSprite(name, delimg), fps(fps), nframes(nframes)
{
if(img != NULL)
{
horizAnim = img->h <= img->w;
framesize = horizAnim ? img->w / nframes : img->h / nframes;
}
}
IsoSprite_Animated::IsoSprite_Animated(std::string name, SDL_Surface* surface, unsigned int fps, unsigned int nframes, bool delimg) : IsoSprite(name, delimg), fps(fps), nframes(nframes)
{
horizAnim = img->h <= img->w;
framesize = horizAnim ? img->w / nframes : img->h / nframes;
}
void IsoSprite_Animated::render(SDL_Surface* screen, SDL_Rect* pos, unsigned int frameid, unsigned int time)
{
frameid %= nframes;
SDL_Rect img_rect;
if(horizAnim)
{
img_rect.x = frameid * framesize;
img_rect.y = 0;
img_rect.w = framesize;
img_rect.h = img->h;
}
else
{
img_rect.x = 0;
img_rect.y = frameid * framesize;
img_rect.w = img->w;
img_rect.h = framesize;
}
SDL_BlitSurface(img, &img_rect, screen, pos);
}