From 2288fb8d15768863e94dc0f693cbccbd21636f21 Mon Sep 17 00:00:00 2001 From: elwld Date: Sun, 10 Jul 2022 01:12:20 +0500 Subject: [PATCH] wow, I created ground and fire --- config.json | 2 +- config.mk | 2 +- spritesheet.bmp | Bin 58 -> 0 bytes spritesheet.png | Bin 0 -> 683 bytes src/game/DinamicObject.h | 51 ++++++++++++++++++++++++++ src/game/Fire.h | 10 ++++++ src/game/GameObject.h | 44 +++++++++++++++++++++++ src/game/Ground.h | 7 ++++ src/game/StaticObject.h | 17 +++++++++ src/game/game.cpp | 9 ++++- src/game/game.h | 3 +- src/spirit.cpp | 2 +- src/window/window.cpp | 75 +++++++++++++++++++-------------------- src/window/window.h | 11 +++--- 14 files changed, 185 insertions(+), 48 deletions(-) delete mode 100644 spritesheet.bmp create mode 100644 spritesheet.png create mode 100644 src/game/DinamicObject.h create mode 100644 src/game/Fire.h create mode 100644 src/game/GameObject.h create mode 100644 src/game/Ground.h create mode 100644 src/game/StaticObject.h diff --git a/config.json b/config.json index f22beed..f0ac613 100644 --- a/config.json +++ b/config.json @@ -1,6 +1,6 @@ { "window": { - "size": [1280,720] + "size": -1 } } diff --git a/config.mk b/config.mk index 32d0a3d..e186af4 100644 --- a/config.mk +++ b/config.mk @@ -1,4 +1,4 @@ cpp = g++ build_directory = build cppflags = -Wall -libs = -lSDL2 +libs = -lSDL2 -lSDL2_image diff --git a/spritesheet.bmp b/spritesheet.bmp deleted file mode 100644 index 412acbc1839a4c76def21a9d42c1321b24389a4b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 58 lcmZ?rwPJt(Ga#h_#Eft(0hV9^l0w`N3>E+q|NsAI002&u1bzSj diff --git a/spritesheet.png b/spritesheet.png new file mode 100644 index 0000000000000000000000000000000000000000..4569c49f3a75650d633774ee50f72a512d638231 GIT binary patch literal 683 zcmV;c0#yBpP) zp>E?q5QhJ?iz-m4zO?XE|D0R+WWBmj@^~kQXSZ%1WX{mWpca z5zl6JJ+t18TX4S=#cR*(%zitwPRy_S9~L9@`{y4s#{Yj;!3US^835pA`wr>;7}*+G z88rIf&?q2Ji2p<4& z{UP4gDp;&u5&;cbxtPzU0D$>y>fq2dAJaG4^060WKz3PKvw|@zc>yBW5QI`qDXrPX z5bb(kr40yLxd@+!P&l-h9RQez#sI?=)U}0_oO;%z$^-QGyl=25qvvt*3#zj#0N`SA zhl|DCi76*aX{TKKq?VRCf##E-(%dzUFRL&9V_KF8@<3dFM0Y$%JmFXs#TK7i{jnT^*kT2iS2rlHZoVd{1V9j- zpW++?zR~zY_c|q=eS&{d3%_k1-RET8wrUbRahu!r=*tuW_r@{*vGKcS6i)=$ZlBR^ zpN9hl603-k7~{|9h#d*ak6p>Xw*irVH?jCh6__KJ7zN_+5jMJML8m_hr{wrMjOX^g_}6m1;8&jZxte%zfke{{t5sb#(n! RZCU^T002ovPDHLkV1m^NI3)l8 literal 0 HcmV?d00001 diff --git a/src/game/DinamicObject.h b/src/game/DinamicObject.h new file mode 100644 index 0000000..eeb7c21 --- /dev/null +++ b/src/game/DinamicObject.h @@ -0,0 +1,51 @@ +#include "GameObject.h" +#include +#include +#include +#include +#include + +using namespace std; + +struct LastFrame +{ + string frameName; + int frameIndex; +}; + +class DinamicObject: public GameObject +{ + public: + DinamicObject(int x, int y, int w, int h, map> 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> animations; + int frameLenth; + time_t lastFrameTime; + LastFrame lastFrame; +}; \ No newline at end of file diff --git a/src/game/Fire.h b/src/game/Fire.h new file mode 100644 index 0000000..7b19e5c --- /dev/null +++ b/src/game/Fire.h @@ -0,0 +1,10 @@ +#include "DinamicObject.h" + +class Fire: public DinamicObject +{ + public: + Fire(int x, int y): DinamicObject(x, y, 128, 128, map>(), 5) + { + for (int i = 1; i <= 4; i++) (this->animations)[""].push_back((SDL_Rect){i*16,0,16,16}); + } +}; \ No newline at end of file diff --git a/src/game/GameObject.h b/src/game/GameObject.h new file mode 100644 index 0000000..79e722f --- /dev/null +++ b/src/game/GameObject.h @@ -0,0 +1,44 @@ +#ifndef GAMEOBJECT +#define GAMEOBJECT + +#include +#include + +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 \ No newline at end of file diff --git a/src/game/Ground.h b/src/game/Ground.h new file mode 100644 index 0000000..5632700 --- /dev/null +++ b/src/game/Ground.h @@ -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}){} +}; \ No newline at end of file diff --git a/src/game/StaticObject.h b/src/game/StaticObject.h new file mode 100644 index 0000000..dbe7eaa --- /dev/null +++ b/src/game/StaticObject.h @@ -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; +}; \ No newline at end of file diff --git a/src/game/game.cpp b/src/game/game.cpp index 6f62678..e96c5a3 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -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(); } diff --git a/src/game/game.h b/src/game/game.h index e8b061b..2a93e22 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -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 }; diff --git a/src/spirit.cpp b/src/spirit.cpp index 1d93bcb..934e581 100644 --- a/src/spirit.cpp +++ b/src/spirit.cpp @@ -4,7 +4,7 @@ int main() { Config config("./config.json"); - Window window(config.width, config.height, config.windowFlags); + Window window(config.width, config.height, 30, config.windowFlags); while(game(window) != Quit); } diff --git a/src/window/window.cpp b/src/window/window.cpp index 1ddf1e8..e8cfe14 100644 --- a/src/window/window.cpp +++ b/src/window/window.cpp @@ -1,61 +1,58 @@ #include "window.h" -Window::Window(int width, int height, int windowFlags) +Window::Window(int width, int height, int windowFps, int windowFlags) { - if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) - { - cout << "Failed to initialize the SDL2 library\n"; - this->quit(); - } + if (SDL_Init(SDL_INIT_VIDEO) < 0) + { + cout << "Couldn't initialize SDL: " << SDL_GetError() << endl; + exit(1); + } - this->window = SDL_CreateWindow("Spirit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, windowFlags); + IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG); - if(!(this->window)) - { - cout << "Failed to create window: " << SDL_GetError() << "\n"; - this->quit(); - } - + this->window = SDL_CreateWindow("Spirit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, windowFlags); SDL_SetWindowResizable(this->window, SDL_TRUE); - - this->spritesheet = SDL_LoadBMP("./spritesheet.bmp"); - if(!spritesheet) - { - cout << "Failed to open spritesheet file\n"; - this->quit(); - } + if (!this->window) + { + cout << "Failed to open window: " << SDL_GetError() << endl; + exit(1); + } + + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); - this->surface = SDL_GetWindowSurface(this->window); + this->renderer = SDL_CreateRenderer(this->window, -1, SDL_RENDERER_ACCELERATED); - if(!surface) - { - cout << "Failed to get the surface from the window" << SDL_GetError() << "\n"; - this->quit(); - } -}; + if (!this->renderer) + { + cout << "Failed to create renderer: " << SDL_GetError() << endl; + exit(1); + } + + this->spritesheet = IMG_LoadTexture(this->renderer, "./spritesheet.png"); + SDL_SetTextureScaleMode(this->spritesheet, SDL_ScaleModeNearest); + + this->windowDelay = 1000/windowFps; +} void Window::clear() { - SDL_FillRect(this->surface, NULL, 0x000000); + if (SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255)) + cout << "\n"; + SDL_RenderClear(this->renderer); } -void Window::draw() +void Window::draw(GameObject &object) { - SDL_FillRect(this->surface, NULL, 0x000000); + Frame frame = object.frame(); + + SDL_RenderCopy(this->renderer, this->spritesheet, &(frame.srcrect), &(frame.dstrect)); } void Window::update() { - SDL_UpdateWindowSurface(this->window); - - this->surface = SDL_GetWindowSurface(this->window); - - if(!surface) - { - cout << "Failed to get the surface from the window" << SDL_GetError() << "\n"; - this->quit(); - } + SDL_RenderPresent(this->renderer); + SDL_Delay(this->windowDelay); } void Window::quit() diff --git a/src/window/window.h b/src/window/window.h index 9a91fb3..5244f78 100644 --- a/src/window/window.h +++ b/src/window/window.h @@ -2,24 +2,27 @@ #define WINDOW #include +#include #include #include +#include "../game/GameObject.h" using namespace std; class Window { public: - Window(int width, int height, int windowFlags); + Window(int width, int height, int windowFps, int windowFlags); void clear(); - void draw(); + void draw(GameObject &draw); void update(); void quit(); private: SDL_Window *window; - SDL_Surface *spritesheet; - SDL_Surface *surface; + SDL_Renderer *renderer; + SDL_Texture* spritesheet; + int windowDelay; }; #endif //WINDOW \ No newline at end of file