wow, I created ground and fire
This commit is contained in:
parent
c14d6e6c59
commit
2288fb8d15
14 changed files with 185 additions and 48 deletions
51
src/game/DinamicObject.h
Normal file
51
src/game/DinamicObject.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#include "GameObject.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <sys/time.h>
|
||||
#include <ctime>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct LastFrame
|
||||
{
|
||||
string frameName;
|
||||
int frameIndex;
|
||||
};
|
||||
|
||||
class DinamicObject: public GameObject
|
||||
{
|
||||
public:
|
||||
DinamicObject(int x, int y, int w, int h, map<string, vector<SDL_Rect>> animations, int animationFps = 30): GameObject(x, y, w, h)
|
||||
{
|
||||
this->animations = animations;
|
||||
this->frameLenth = 1000/animationFps;
|
||||
this->lastFrameTime = -1;
|
||||
this->lastFrame = {"", 0};
|
||||
}
|
||||
Frame frame()
|
||||
{
|
||||
time_t currentTime;
|
||||
{
|
||||
struct timeval timeNow{};
|
||||
gettimeofday(&timeNow, nullptr);
|
||||
currentTime = (timeNow.tv_sec * 1000) + (timeNow.tv_usec / 1000);
|
||||
}
|
||||
|
||||
if (this->lastFrameTime == -1)
|
||||
this->lastFrameTime = currentTime;
|
||||
|
||||
int currentFrameIndex = ((this->lastFrame).frameIndex + (currentTime - this->lastFrameTime)/this->frameLenth) % (this->animations)[(this->lastFrame).frameName].size();
|
||||
|
||||
this->lastFrameTime += (currentFrameIndex - (this->lastFrame).frameIndex) * this->frameLenth;
|
||||
(this->lastFrame).frameIndex = currentFrameIndex;
|
||||
|
||||
return Frame(this->dstrect, animations[(this->lastFrame).frameName][currentFrameIndex]);
|
||||
}
|
||||
|
||||
protected:
|
||||
map<string, vector<SDL_Rect>> animations;
|
||||
int frameLenth;
|
||||
time_t lastFrameTime;
|
||||
LastFrame lastFrame;
|
||||
};
|
10
src/game/Fire.h
Normal file
10
src/game/Fire.h
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include "DinamicObject.h"
|
||||
|
||||
class Fire: public DinamicObject
|
||||
{
|
||||
public:
|
||||
Fire(int x, int y): DinamicObject(x, y, 128, 128, map<string, vector<SDL_Rect>>(), 5)
|
||||
{
|
||||
for (int i = 1; i <= 4; i++) (this->animations)[""].push_back((SDL_Rect){i*16,0,16,16});
|
||||
}
|
||||
};
|
44
src/game/GameObject.h
Normal file
44
src/game/GameObject.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#ifndef GAMEOBJECT
|
||||
#define GAMEOBJECT
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
|
||||
class Frame
|
||||
{
|
||||
public:
|
||||
Frame(SDL_Rect dstrect, SDL_Rect srcrect, bool mirror = 0, int angle = 0)
|
||||
{
|
||||
this->dstrect = dstrect;
|
||||
this->srcrect = srcrect;
|
||||
this->mirror = mirror;
|
||||
this->angle = angle;
|
||||
}
|
||||
|
||||
public:
|
||||
SDL_Rect dstrect;
|
||||
SDL_Rect srcrect;
|
||||
bool mirror;
|
||||
int angle;
|
||||
};
|
||||
|
||||
class GameObject
|
||||
{
|
||||
public:
|
||||
GameObject(int x, int y, int w, int h)
|
||||
{
|
||||
(this->dstrect).x = x;
|
||||
(this->dstrect).y = y;
|
||||
(this->dstrect).w = w;
|
||||
(this->dstrect).h = h;
|
||||
}
|
||||
virtual Frame frame()
|
||||
{
|
||||
return Frame((SDL_Rect){0,0,0,0}, (SDL_Rect){0,0,0,0});
|
||||
}
|
||||
|
||||
protected:
|
||||
SDL_Rect dstrect;
|
||||
};
|
||||
|
||||
#endif //GAMEOBJECT
|
7
src/game/Ground.h
Normal file
7
src/game/Ground.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
#include "StaticObject.h"
|
||||
|
||||
class Ground: public StaticObject
|
||||
{
|
||||
public:
|
||||
Ground(int x, int y): StaticObject(x, y, 128, 128, (SDL_Rect){0,0,16,16}){}
|
||||
};
|
17
src/game/StaticObject.h
Normal file
17
src/game/StaticObject.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#include "GameObject.h"
|
||||
|
||||
class StaticObject: public GameObject
|
||||
{
|
||||
public:
|
||||
StaticObject(int x, int y, int w, int h, SDL_Rect srcrect): GameObject(x, y, w, h)
|
||||
{
|
||||
this->srcrect = srcrect;
|
||||
}
|
||||
Frame frame()
|
||||
{
|
||||
return Frame(this->dstrect, this->srcrect);
|
||||
}
|
||||
|
||||
protected:
|
||||
SDL_Rect srcrect;
|
||||
};
|
|
@ -4,11 +4,18 @@ GameEvent game(Window &window)
|
|||
{
|
||||
Event event;
|
||||
|
||||
Ground ground(0, 200);
|
||||
Fire fire(-10,72);
|
||||
|
||||
while(true)
|
||||
{
|
||||
if (event == Quit) window.quit();
|
||||
|
||||
|
||||
window.clear();
|
||||
|
||||
window.draw(ground);
|
||||
window.draw(fire);
|
||||
|
||||
window.update();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#ifndef GAME
|
||||
#define GAME
|
||||
|
||||
#include "../window/window.h"
|
||||
#include "Ground.h"
|
||||
#include "Fire.h"
|
||||
|
||||
enum GameEvent { None, Quit, Up, Left, Down, Right };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue