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 412acbc..0000000 Binary files a/spritesheet.bmp and /dev/null differ diff --git a/spritesheet.png b/spritesheet.png new file mode 100644 index 0000000..4569c49 Binary files /dev/null and b/spritesheet.png differ 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