From 4b37c3afb895d5c0715dd800b9889abbff763909 Mon Sep 17 00:00:00 2001 From: Alfie King Date: Fri, 6 Sep 2024 13:38:39 +0100 Subject: [PATCH] engine.h --- include/engine.h | 27 ++++++++++++ makefile | 6 +-- src/engine.cpp | 58 +++++++++++++++++++++++++ src/main.cpp | 111 ++++++++++++++++++++++++++++------------------- src/sprite.cpp | 14 +++++- 5 files changed, 166 insertions(+), 50 deletions(-) create mode 100644 include/engine.h create mode 100644 src/engine.cpp diff --git a/include/engine.h b/include/engine.h new file mode 100644 index 0000000..9cb3ac7 --- /dev/null +++ b/include/engine.h @@ -0,0 +1,27 @@ +#include "input.h" +#include "text.h" +#include "sprite.h" +#include "vector.h" +#include +#include +#include + +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(); +}; \ No newline at end of file diff --git a/makefile b/makefile index 6d6b96e..bc87ab1 100644 --- a/makefile +++ b/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) \ No newline at end of file diff --git a/src/engine.cpp b/src/engine.cpp new file mode 100644 index 0000000..60737d3 --- /dev/null +++ b/src/engine.cpp @@ -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(); +} diff --git a/src/main.cpp b/src/main.cpp index 77e4e7c..c84ab4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,31 +1,17 @@ // Inludes -#include -#include -#include -#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 frames = { - { "idle", { 4, 456, 68, 125 } }, - { "idle2", { 76, 456, 68, 128 } }, - { "idle3", { 148, 456, 68, 125 } } + std::map idle = { + { "idle", { 76, 456, 68, 128 } }, + }; + + std::map front = { + { "front", { 4, 456, 68, 125 } }, + { "front2", { 76, 456, 68, 128 } }, + { "front3", { 148, 456, 68, 125 } } + }; + + std::map back = { + { "back", { 662, 456, 68, 125 } }, + { "back2", { 734, 456, 68, 128 } }, + { "back3", { 806, 456, 68, 125 } } + }; + + std::map 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; } \ No newline at end of file diff --git a/src/sprite.cpp b/src/sprite.cpp index 8415f9e..c309f4f 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp @@ -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; + } } }