Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
2288fb8d15 | ||
|
c14d6e6c59 |
@ -1,6 +1,6 @@
|
||||
{
|
||||
"window":
|
||||
{
|
||||
"size": [1280,720]
|
||||
"size": -1
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
cpp = g++
|
||||
build_directory = build
|
||||
cppflags = -Wall
|
||||
libs = -lSDL2
|
||||
libs = -lSDL2 -lSDL2_image
|
||||
|
BIN
spritesheet.bmp
BIN
spritesheet.bmp
Binary file not shown.
Before Width: | Height: | Size: 58 B |
BIN
spritesheet.png
Normal file
BIN
spritesheet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 683 B |
@ -22,7 +22,7 @@ Config::Config(string filename)
|
||||
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
|
||||
and this->jsonConfig["window"]["size"].size() == 2
|
||||
|
@ -16,7 +16,7 @@ class Config
|
||||
int genDefConf();
|
||||
|
||||
public:
|
||||
int width = 0, height = 0, sdlOption = 0;
|
||||
int width = 0, height = 0, windowFlags = 0;
|
||||
|
||||
private:
|
||||
Json::Value jsonConfig;
|
||||
|
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 };
|
||||
|
||||
|
@ -4,7 +4,7 @@ int main()
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
@ -1,61 +1,58 @@
|
||||
#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)
|
||||
{
|
||||
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, sdlOption);
|
||||
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()
|
||||
|
@ -2,24 +2,27 @@
|
||||
#define WINDOW
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include "../game/GameObject.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
Window(int width, int height, int sdlOption);
|
||||
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
|
Loading…
Reference in New Issue
Block a user