Created SpriteSheet

This commit is contained in:
Kayne Ruse
2013-04-29 04:18:20 +10:00
parent d72a3f4fb5
commit 392ef1ef32
7 changed files with 125 additions and 3 deletions
+24
View File
@@ -0,0 +1,24 @@
#ifndef DELTA_H_
#define DELTA_H_
#include <ctime>
class Delta {
public:
Delta() {
time = tick = 0;
}
int Calculate() {
int c = clock();
time = c - tick;
tick = c;
return time;
}
int GetDelta() const {
return time;
};
private:
int time, tick;
};
#endif
+2 -2
View File
@@ -1,7 +1,7 @@
CXXFLAGS+=-std=c++11 -DDEBUG
LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi
OBJ=base_scene.o scene_manager.o main.o
SRC=test_systems.cpp surface_manager.cpp image.cpp
SRC=test_systems.cpp
all: debug
@@ -15,4 +15,4 @@ clean:
-$(RM) *.o *.a *.exe
unit:
$(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp -lmingw32 -lSDLmain -lSDL
$(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp -lmingw32 -lSDLmain -lSDL
+20
View File
@@ -0,0 +1,20 @@
#include "sprite_sheet.h"
SpriteSheet::SpriteSheet(SDL_Surface* s, int w, int h)
: Image(s, {0, 0, (Uint16)w, (Uint16)h})
{
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW();
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH();
interval = ticks = 0;
}
void SpriteSheet::Update(int delta) {
if ((ticks += delta) > interval) {
if (++currentFrame >= maxFrames) {
currentFrame = 0;
}
ticks = 0;
SetClipX(currentFrame * GetClipW());
SetClipY(currentStrip * GetClipH());
}
}
+38
View File
@@ -0,0 +1,38 @@
#ifndef SPRITESHEET_H_
#define SPRITESHEET_H_
#include "image.h"
#include "SDL/SDL.h"
class SpriteSheet : public Image {
public:
SpriteSheet(SDL_Surface*, int w, int h);
virtual ~SpriteSheet() {}
void Update(int delta);
int SetWidth(int i) { return SetClipW(i); };
int SetHeight(int i) { return SetClipH(i); };
int SetFrames(int i) { currentFrame = 0; return maxFrames = i; };
int SetStrips(int i) { currentStrip = 0; return maxStrips = i; };
int GetWidth() const { return GetClipW(); }
int GetHeight() const { return GetClipH(); }
int GetFrames() const { return maxFrames; }
int GetStrips() const { return maxStrips; }
int SetCurrentFrame(int i) { return currentFrame = i; };
int SetCurrentStrip(int i) { return currentStrip = i; };
int SetInterval(int i) { ticks = 0; return interval = i; }
int GetCurrentFrame() const { return currentFrame; };
int GetCurrentStrip() const { return currentStrip; };
int GetInterval() const { return interval; };
private:
int currentFrame, maxFrames;
int currentStrip, maxStrips;
int interval, ticks;
};
#endif
+41 -1
View File
@@ -1,3 +1,5 @@
#include "delta.h"
#include "sprite_sheet.h"
#include "image.h"
#include "surface_manager.h"
@@ -5,6 +7,8 @@
#include <exception>
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
@@ -17,11 +21,23 @@ int go(int, char**) {
//load all resources
sMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp");
sMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp");
sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp");
Image player(sMgr.Get("player"));
SpriteSheet player(sMgr.Get("player"), 32, 48);
Image tiles(sMgr.Get("tileset"));
player.SetInterval(200);
vector<SpriteSheet> flowerVector;
for (int i = 0; i < 100; i++) {
SpriteSheet ss(sMgr.Get("flower"), 32, 32);
ss.SetInterval(i%5 + 95);
flowerVector.push_back(ss);
}
Delta delta;
bool running = true;
while (running) {
SDL_Event event;
@@ -35,15 +51,39 @@ int go(int, char**) {
case SDLK_ESCAPE:
running = false;
break;
case SDLK_1:
player.SetCurrentStrip(0);
break;
case SDLK_2:
player.SetCurrentStrip(1);
break;
case SDLK_3:
player.SetCurrentStrip(2);
break;
case SDLK_4:
player.SetCurrentStrip(3);
break;
}
}
}
delta.Calculate();
player.Update(delta.GetDelta());
for (int i = 0; i < 100; i++) {
flowerVector[i].Update(delta.GetDelta());
}
SDL_FillRect(screen, 0, 0);
tiles.DrawTo(screen, 0, 0);
player.DrawTo(screen, 0, 0);
for (int i = 0; i < 100; i++) {
flowerVector[i].DrawTo(screen, 20+i*4, i*4);
}
SDL_Flip(screen);
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 869 B