engine.h
This commit is contained in:
parent
5813da7e97
commit
4b37c3afb8
27
include/engine.h
Normal file
27
include/engine.h
Normal file
@ -0,0 +1,27 @@
|
||||
#include "input.h"
|
||||
#include "text.h"
|
||||
#include "sprite.h"
|
||||
#include "vector.h"
|
||||
#include <SDL.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Engine
|
||||
{
|
||||
private:
|
||||
const char* ctitle;
|
||||
int frameStart = 0;
|
||||
|
||||
public:
|
||||
Input input;
|
||||
int targetFrameRate = 60;
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
|
||||
Engine(std::string title, int width, int height);
|
||||
void clear(SDL_Color color);
|
||||
void startFrame();
|
||||
void render();
|
||||
void update();
|
||||
~Engine();
|
||||
};
|
6
makefile
6
makefile
@ -7,14 +7,14 @@ TARGET = build/x86_64/main.x86_64
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
dirs:
|
||||
@mkdir -p build/x86_64
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
@$(CXX) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
build/%.o: src/%.cpp
|
||||
@$(CXX) -c -o $@ $< $(CXXFLAGS)
|
||||
|
||||
dirs:
|
||||
@mkdir -p build/x86_64
|
||||
|
||||
clean:
|
||||
@rm -f $(OBJ) $(TARGET)
|
58
src/engine.cpp
Normal file
58
src/engine.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
#include "engine.h"
|
||||
|
||||
Engine::Engine(std::string title, int width, int height)
|
||||
{
|
||||
// convert title to char*
|
||||
ctitle = title.c_str();
|
||||
|
||||
// Initialize SDL and TFF;
|
||||
TTF_Init();
|
||||
SDL_Init(SDL_INIT_EVERYTHING);
|
||||
|
||||
// Create window and renderer
|
||||
window = SDL_CreateWindow(ctitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_SHOWN);
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
}
|
||||
|
||||
void Engine::clear(SDL_Color color)
|
||||
{
|
||||
// Set color
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
|
||||
|
||||
// Clear screen
|
||||
SDL_RenderClear(renderer);
|
||||
}
|
||||
|
||||
void Engine::startFrame()
|
||||
{
|
||||
// Start frame
|
||||
frameStart = SDL_GetTicks();
|
||||
}
|
||||
|
||||
void Engine::render()
|
||||
{
|
||||
// Render screen
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
if (1000 / targetFrameRate > SDL_GetTicks() - frameStart)
|
||||
{
|
||||
SDL_Delay(1000 / targetFrameRate - (SDL_GetTicks() - frameStart));
|
||||
}
|
||||
}
|
||||
|
||||
void Engine::update()
|
||||
{
|
||||
// Update input
|
||||
input.update();
|
||||
}
|
||||
|
||||
Engine::~Engine()
|
||||
{
|
||||
// Free renderer and window
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
// Quit SDL and TTF
|
||||
SDL_Quit();
|
||||
TTF_Quit();
|
||||
}
|
111
src/main.cpp
111
src/main.cpp
@ -1,31 +1,17 @@
|
||||
// Inludes
|
||||
#include <SDL.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "input.h"
|
||||
#include "text.h"
|
||||
#include "sprite.h"
|
||||
#include "engine.h"
|
||||
|
||||
// Variables
|
||||
SDL_Surface* assets;
|
||||
int startTime = 0;
|
||||
Input input;
|
||||
TTF_Font *font;
|
||||
SDL_Window *window;
|
||||
SDL_Renderer *renderer;
|
||||
const int targetFps = 60;
|
||||
Engine *engine;
|
||||
|
||||
// Functions
|
||||
// Quit function
|
||||
void quit()
|
||||
{
|
||||
// Free assets
|
||||
TTF_CloseFont(font);
|
||||
SDL_FreeSurface(assets);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
TTF_Quit();
|
||||
SDL_FreeSurface(assets);
|
||||
delete engine;
|
||||
// Exit program
|
||||
exit(0);
|
||||
}
|
||||
@ -33,63 +19,98 @@ void quit()
|
||||
// Main function
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
// Initialize SDL and TFF;
|
||||
TTF_Init();
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
// Create window and renderer
|
||||
window = SDL_CreateWindow("test game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 600, 400, SDL_WINDOW_SHOWN);
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
|
||||
engine = new Engine("Game", 800, 600);
|
||||
engine->targetFrameRate = 240;
|
||||
|
||||
// Load assets
|
||||
assets = SDL_LoadBMP("assets/sprites.bmp");
|
||||
|
||||
// frame map
|
||||
std::map<std::string, SDL_Rect> frames = {
|
||||
{ "idle", { 4, 456, 68, 125 } },
|
||||
{ "idle2", { 76, 456, 68, 128 } },
|
||||
{ "idle3", { 148, 456, 68, 125 } }
|
||||
std::map<std::string, SDL_Rect> idle = {
|
||||
{ "idle", { 76, 456, 68, 128 } },
|
||||
};
|
||||
|
||||
std::map<std::string, SDL_Rect> front = {
|
||||
{ "front", { 4, 456, 68, 125 } },
|
||||
{ "front2", { 76, 456, 68, 128 } },
|
||||
{ "front3", { 148, 456, 68, 125 } }
|
||||
};
|
||||
|
||||
std::map<std::string, SDL_Rect> back = {
|
||||
{ "back", { 662, 456, 68, 125 } },
|
||||
{ "back2", { 734, 456, 68, 128 } },
|
||||
{ "back3", { 806, 456, 68, 125 } }
|
||||
};
|
||||
|
||||
std::map<std::string, SDL_Rect> walk = {
|
||||
{ "walk", { 220, 456, 68, 124 } },
|
||||
{ "walk2", { 292, 456, 73, 127 } },
|
||||
{ "walk3", { 369, 456, 68, 124 } }
|
||||
};
|
||||
|
||||
// Create sprite and animation
|
||||
Sprite* sprite = new Sprite(renderer, assets);
|
||||
Sprite* sprite = new Sprite(engine->renderer, assets);
|
||||
AnimationManager* animations = new AnimationManager(sprite);
|
||||
animations->addAnimation("idle", frames, 10, false, true, true, true);
|
||||
animations->addAnimation("idle", idle, 10, false, true, false, false);
|
||||
animations->addAnimation("walk_left", walk, 10, false, true, true, false);
|
||||
animations->addAnimation("walk_right", walk, 10, true, true, true, false);
|
||||
animations->addAnimation("walk_back", back, 10, false, true, true, false);
|
||||
animations->addAnimation("walk_front", front, 10, false, true, true, true);
|
||||
animations->setAnimation("idle");
|
||||
|
||||
// Main loop
|
||||
while (true)
|
||||
{
|
||||
// Start time
|
||||
startTime = SDL_GetTicks();
|
||||
engine->startFrame();
|
||||
|
||||
// Update input
|
||||
input.update();
|
||||
engine->update();
|
||||
|
||||
// Exit if window is closed
|
||||
if (input.exit)
|
||||
if (engine->input.exit)
|
||||
{
|
||||
quit();
|
||||
}
|
||||
|
||||
if (engine->input.activeKeys[SDLK_d])
|
||||
{
|
||||
sprite->dstrect.x += 1;
|
||||
animations->setAnimation("walk_right");
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_a])
|
||||
{
|
||||
sprite->dstrect.x -= 1;
|
||||
animations->setAnimation("walk_left");
|
||||
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_w])
|
||||
{
|
||||
sprite->dstrect.y -= 1;
|
||||
animations->setAnimation("walk_back");
|
||||
}
|
||||
if (engine->input.activeKeys[SDLK_s])
|
||||
{
|
||||
sprite->dstrect.y += 1;
|
||||
animations->setAnimation("walk_front");
|
||||
|
||||
}
|
||||
if (!engine->input.activeKeys[SDLK_d] && !engine->input.activeKeys[SDLK_a] && !engine->input.activeKeys[SDLK_w] && !engine->input.activeKeys[SDLK_s])
|
||||
{
|
||||
animations->setAnimation("idle");
|
||||
}
|
||||
|
||||
// Update animation
|
||||
animations->update();
|
||||
|
||||
// Clear screen
|
||||
SDL_SetRenderDrawColor(renderer, 100, 100, 255, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
engine->clear({ 100, 150, 255, 255 });
|
||||
|
||||
// Draw sprite
|
||||
animations->draw(renderer);
|
||||
animations->draw(engine->renderer);
|
||||
|
||||
// Update screen
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
// if frame is faster than target fps, delay
|
||||
if (1000 / targetFps > SDL_GetTicks() - startTime)
|
||||
{
|
||||
SDL_Delay(1000 / targetFps - (SDL_GetTicks() - startTime));
|
||||
}
|
||||
engine->render();
|
||||
}
|
||||
return 0;
|
||||
}
|
@ -74,7 +74,12 @@ void Animation::update()
|
||||
frameIndex += frameDirection;
|
||||
if (frameIndex >= frames.size() - 1 || frameIndex <= 0)
|
||||
{
|
||||
frameDirection *= -1;
|
||||
if (!loop)
|
||||
{
|
||||
frameDirection = 0;
|
||||
} else {
|
||||
frameDirection *= -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -82,7 +87,12 @@ void Animation::update()
|
||||
frameIndex++;
|
||||
if (frameIndex >= frames.size())
|
||||
{
|
||||
frameIndex = 0;
|
||||
if (!loop)
|
||||
{
|
||||
frameIndex = frames.size() - 1;
|
||||
} else {
|
||||
frameIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user