Compare commits

..

2 Commits

Author SHA1 Message Date
elwld
2288fb8d15 wow, I created ground and fire 2022-07-10 01:12:20 +05:00
elwld
c14d6e6c59 sdlOption -> windowFlags 2022-07-09 00:37:28 +05:00
16 changed files with 187 additions and 50 deletions

View File

@ -1,6 +1,6 @@
{ {
"window": "window":
{ {
"size": [1280,720] "size": -1
} }
} }

View File

@ -1,4 +1,4 @@
cpp = g++ cpp = g++
build_directory = build build_directory = build
cppflags = -Wall cppflags = -Wall
libs = -lSDL2 libs = -lSDL2 -lSDL2_image

Binary file not shown.

Before

Width:  |  Height:  |  Size: 58 B

BIN
spritesheet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 683 B

View File

@ -22,7 +22,7 @@ Config::Config(string filename)
and this->jsonConfig["window"]["size"].asInt() == -1) and this->jsonConfig["window"]["size"].asInt() == -1)
{ {
this->sdlOption = SDL_WINDOW_FULLSCREEN_DESKTOP; this->windowFlags = SDL_WINDOW_FULLSCREEN_DESKTOP;
} }
else if(this->jsonConfig["window"]["size"].type() == Json::arrayValue else if(this->jsonConfig["window"]["size"].type() == Json::arrayValue
and this->jsonConfig["window"]["size"].size() == 2 and this->jsonConfig["window"]["size"].size() == 2

View File

@ -16,7 +16,7 @@ class Config
int genDefConf(); int genDefConf();
public: public:
int width = 0, height = 0, sdlOption = 0; int width = 0, height = 0, windowFlags = 0;
private: private:
Json::Value jsonConfig; Json::Value jsonConfig;

51
src/game/DinamicObject.h Normal file
View 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
View 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
View 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
View 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
View 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;
};

View File

@ -4,11 +4,18 @@ GameEvent game(Window &window)
{ {
Event event; Event event;
Ground ground(0, 200);
Fire fire(-10,72);
while(true) while(true)
{ {
if (event == Quit) window.quit(); if (event == Quit) window.quit();
window.clear(); window.clear();
window.draw(ground);
window.draw(fire);
window.update(); window.update();
} }

View File

@ -1,7 +1,8 @@
#ifndef GAME #ifndef GAME
#define GAME #define GAME
#include "../window/window.h" #include "../window/window.h"
#include "Ground.h"
#include "Fire.h"
enum GameEvent { None, Quit, Up, Left, Down, Right }; enum GameEvent { None, Quit, Up, Left, Down, Right };

View File

@ -4,7 +4,7 @@ int main()
{ {
Config config("./config.json"); Config config("./config.json");
Window window(config.width, config.height, config.sdlOption); Window window(config.width, config.height, 30, config.windowFlags);
while(game(window) != Quit); while(game(window) != Quit);
} }

View File

@ -1,61 +1,58 @@
#include "window.h" #include "window.h"
Window::Window(int width, int height, int sdlOption) Window::Window(int width, int height, int windowFps, int windowFlags)
{ {
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) if (SDL_Init(SDL_INIT_VIDEO) < 0)
{ {
cout << "Failed to initialize the SDL2 library\n"; cout << "Couldn't initialize SDL: " << SDL_GetError() << endl;
this->quit(); exit(1);
} }
this->window = SDL_CreateWindow("Spirit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, sdlOption); IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG);
if(!(this->window)) this->window = SDL_CreateWindow("Spirit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, windowFlags);
{
cout << "Failed to create window: " << SDL_GetError() << "\n";
this->quit();
}
SDL_SetWindowResizable(this->window, SDL_TRUE); SDL_SetWindowResizable(this->window, SDL_TRUE);
this->spritesheet = SDL_LoadBMP("./spritesheet.bmp");
if(!spritesheet) if (!this->window)
{ {
cout << "Failed to open spritesheet file\n"; cout << "Failed to open window: " << SDL_GetError() << endl;
this->quit(); 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) if (!this->renderer)
{ {
cout << "Failed to get the surface from the window" << SDL_GetError() << "\n"; cout << "Failed to create renderer: " << SDL_GetError() << endl;
this->quit(); exit(1);
} }
};
this->spritesheet = IMG_LoadTexture(this->renderer, "./spritesheet.png");
SDL_SetTextureScaleMode(this->spritesheet, SDL_ScaleModeNearest);
this->windowDelay = 1000/windowFps;
}
void Window::clear() 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() void Window::update()
{ {
SDL_UpdateWindowSurface(this->window); SDL_RenderPresent(this->renderer);
SDL_Delay(this->windowDelay);
this->surface = SDL_GetWindowSurface(this->window);
if(!surface)
{
cout << "Failed to get the surface from the window" << SDL_GetError() << "\n";
this->quit();
}
} }
void Window::quit() void Window::quit()

View File

@ -2,24 +2,27 @@
#define WINDOW #define WINDOW
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include "../game/GameObject.h"
using namespace std; using namespace std;
class Window class Window
{ {
public: public:
Window(int width, int height, int sdlOption); Window(int width, int height, int windowFps, int windowFlags);
void clear(); void clear();
void draw(); void draw(GameObject &draw);
void update(); void update();
void quit(); void quit();
private: private:
SDL_Window *window; SDL_Window *window;
SDL_Surface *spritesheet; SDL_Renderer *renderer;
SDL_Surface *surface; SDL_Texture* spritesheet;
int windowDelay;
}; };
#endif //WINDOW #endif //WINDOW