This commit is contained in:
Alfie King 2024-09-06 13:38:39 +01:00
parent 5813da7e97
commit 4b37c3afb8
5 changed files with 166 additions and 50 deletions

27
include/engine.h Normal file
View 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();
};

View File

@ -7,14 +7,14 @@ TARGET = build/x86_64/main.x86_64
all: $(TARGET) all: $(TARGET)
dirs:
@mkdir -p build/x86_64
$(TARGET): $(OBJ) $(TARGET): $(OBJ)
@$(CXX) -o $@ $^ $(LDFLAGS) @$(CXX) -o $@ $^ $(LDFLAGS)
build/%.o: src/%.cpp build/%.o: src/%.cpp
@$(CXX) -c -o $@ $< $(CXXFLAGS) @$(CXX) -c -o $@ $< $(CXXFLAGS)
dirs:
@mkdir -p build/x86_64
clean: clean:
@rm -f $(OBJ) $(TARGET) @rm -f $(OBJ) $(TARGET)

58
src/engine.cpp Normal file
View 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();
}

View File

@ -1,31 +1,17 @@
// Inludes // Inludes
#include <SDL.h> #include "engine.h"
#include <string>
#include <vector>
#include "input.h"
#include "text.h"
#include "sprite.h"
// Variables // Variables
SDL_Surface* assets; SDL_Surface* assets;
int startTime = 0; Engine *engine;
Input input;
TTF_Font *font;
SDL_Window *window;
SDL_Renderer *renderer;
const int targetFps = 60;
// Functions // Functions
// Quit function // Quit function
void quit() void quit()
{ {
// Free assets // Free assets
TTF_CloseFont(font); SDL_FreeSurface(assets);
SDL_FreeSurface(assets); delete engine;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
TTF_Quit();
// Exit program // Exit program
exit(0); exit(0);
} }
@ -33,63 +19,98 @@ void quit()
// Main function // Main function
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
// Initialize SDL and TFF; engine = new Engine("Game", 800, 600);
TTF_Init(); engine->targetFrameRate = 240;
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);
// Load assets // Load assets
assets = SDL_LoadBMP("assets/sprites.bmp"); assets = SDL_LoadBMP("assets/sprites.bmp");
// frame map // frame map
std::map<std::string, SDL_Rect> frames = { std::map<std::string, SDL_Rect> idle = {
{ "idle", { 4, 456, 68, 125 } }, { "idle", { 76, 456, 68, 128 } },
{ "idle2", { 76, 456, 68, 128 } }, };
{ "idle3", { 148, 456, 68, 125 } }
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 // Create sprite and animation
Sprite* sprite = new Sprite(renderer, assets); Sprite* sprite = new Sprite(engine->renderer, assets);
AnimationManager* animations = new AnimationManager(sprite); 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"); animations->setAnimation("idle");
// Main loop // Main loop
while (true) while (true)
{ {
// Start time // Start time
startTime = SDL_GetTicks(); engine->startFrame();
// Update input // Update input
input.update(); engine->update();
// Exit if window is closed // Exit if window is closed
if (input.exit) if (engine->input.exit)
{ {
quit(); 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 // Update animation
animations->update(); animations->update();
// Clear screen // Clear screen
SDL_SetRenderDrawColor(renderer, 100, 100, 255, 255); engine->clear({ 100, 150, 255, 255 });
SDL_RenderClear(renderer);
// Draw sprite // Draw sprite
animations->draw(renderer); animations->draw(engine->renderer);
// Update screen // Update screen
SDL_RenderPresent(renderer); engine->render();
// if frame is faster than target fps, delay
if (1000 / targetFps > SDL_GetTicks() - startTime)
{
SDL_Delay(1000 / targetFps - (SDL_GetTicks() - startTime));
}
} }
return 0; return 0;
} }

View File

@ -74,7 +74,12 @@ void Animation::update()
frameIndex += frameDirection; frameIndex += frameDirection;
if (frameIndex >= frames.size() - 1 || frameIndex <= 0) if (frameIndex >= frames.size() - 1 || frameIndex <= 0)
{ {
frameDirection *= -1; if (!loop)
{
frameDirection = 0;
} else {
frameDirection *= -1;
}
} }
} }
else else
@ -82,7 +87,12 @@ void Animation::update()
frameIndex++; frameIndex++;
if (frameIndex >= frames.size()) if (frameIndex >= frames.size())
{ {
frameIndex = 0; if (!loop)
{
frameIndex = frames.size() - 1;
} else {
frameIndex = 0;
}
} }
} }