feat added entities and components and added failing textcomponent

This commit is contained in:
2025-07-14 00:14:45 +02:00
parent d04c0a6e15
commit f2958fa9fa
98 changed files with 4006 additions and 180 deletions

View File

@ -1,10 +1,14 @@
#include "Engine.h"
#include "Engine.hpp"
#include "WindowManager.hpp"
#include "../systems/Renderer.hpp"
#include "../game/components/TextComponent.hpp"
#include <iostream>
#include <SDL3/SDL.h>
#include "WindowManager.h"
#include <SDL3_ttf/SDL_ttf.h>
bool running = true;
WindowManager *wm = nullptr;
Renderer renderer;
bool Engine::Init() {
if (!SDL_Init(SDL_INIT_VIDEO)) {
@ -12,6 +16,11 @@ bool Engine::Init() {
return false;
}
if (!TTF_Init()) {
std::cerr << "TTF_Init: failed" << SDL_GetError() << "\n";
return false;
}
wm = new WindowManager();
if (!wm->InitWindow("ShadowCrest", 800, 600)) {
@ -19,25 +28,66 @@ bool Engine::Init() {
return false;
}
if (!renderer.Init(wm->GetWindow())) return false;
return true;
}
void Engine::Run() {
SDL_Event event;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_EVENT_QUIT) {
running = false;
}
}
SDL_Delay(16);
renderer.Clear();
// game logic here
TextComponent* text = new TextComponent(
"Hallo Welt", "assets/fonts/Roboto.ttf", 24, SDL_Color{ 255, 255, 255, 255 }
);
text->Init(renderer.GetSDLRenderer());
renderer.Present();
}
}
/*
GetFPS: calculates FPS the engine can execute at call
*/
int Engine::GetFPS() {
this->oldTime = newTime;
this->newTime = SDL_GetTicks();
this->FPS = 1.f / (float(this->newTime - this->oldTime) / 1000.f);
// prevent nois (fps numbers below 0) from beeing written to the FPS counter and do rewrite it when the value wouldn't change
if (this->FPS < 0) this->FPS = this->lastFPS;
if (this->lastFPS != this->FPS) this->lastFPS = this->FPS;
return this->FPS;
}
void Engine::Shutdown() {
// empty variables that have been set by the engine itself
this->FPS = 0;
this->lastFPS = 0;
this->newTime = 0;
this->oldTime = 0;
// safely close down every system
if (wm != nullptr) {
wm->Close();
}
// stop sdl systems
SDL_Quit();
}

View File

@ -1,8 +0,0 @@
#pragma once
class Engine {
public:
bool Init();
void Run();
void Shutdown();
};

17
src/core/Engine.hpp Normal file
View File

@ -0,0 +1,17 @@
#pragma once
#include <iostream>
#include <SDL3/SDL.h>
class Engine {
public:
bool Init();
void Run();
void Shutdown();
int GetFPS();
private:
int lastFPS = 0;
int FPS = 0;
Uint64 oldTime = 0;
Uint64 newTime = 0;
};

View File

@ -1,4 +1,4 @@
#include "WindowManager.h"
#include "WindowManager.hpp"
#include <iostream>
#include <SDL3/SDL.h>
@ -16,4 +16,9 @@ void WindowManager::Draw() {
void WindowManager::Close() {
SDL_DestroyWindow(window);
}
SDL_Window* WindowManager::GetWindow()
{
return window;
}

View File

@ -1,8 +1,12 @@
#pragma once
#include <SDL3/SDL.h>
class WindowManager {
public:
bool InitWindow(const char* name, int width, int height);
void Draw();
void Close();
SDL_Window* GetWindow();
};