diff --git a/Capter1/Game.cpp b/Capter1/Game.cpp.old similarity index 100% rename from Capter1/Game.cpp rename to Capter1/Game.cpp.old diff --git a/Capter1/Game.h b/Capter1/Game.h.old similarity index 91% rename from Capter1/Game.h rename to Capter1/Game.h.old index 93705ef..0ea9834 100644 --- a/Capter1/Game.h +++ b/Capter1/Game.h.old @@ -1,5 +1,7 @@ #pragma once -#include "SDL2/SDL.h" +//#include "SDL2/SDL.h" +#include "SDL3/SDL.h" +#include "SDL3/SDL_main.h" // Vector2 struct just stores x/y coordinates // (for now) diff --git a/Capter1/ax_game b/Capter1/ax_game index 56e1b9c..5b163cd 100755 Binary files a/Capter1/ax_game and b/Capter1/ax_game differ diff --git a/Capter1/ax_game.cpp b/Capter1/ax_game.cpp index 112882f..396067b 100644 --- a/Capter1/ax_game.cpp +++ b/Capter1/ax_game.cpp @@ -30,32 +30,54 @@ ax_game::~ax_game() } bool ax_game::Init(){ - int sdlResult = SDL_Init(SDL_INIT_VIDEO); - if(sdlResult != 0){ + if(!SDL_Init(SDL_INIT_VIDEO)){ SDL_Log("Fehler bei SDL_INIT: %s", SDL_GetError()); return false; } - ax_Window = SDL_CreateWindow("Ersted Fenster", 100,100,1024,768,0); + ax_Window = SDL_CreateWindow("Ersted Fenster", 1024,768,0); + if (!ax_Window) { + SDL_Log("Fehler bei SDL_CreateWindow: %s", SDL_GetError()); + SDL_Quit(); + return false; + } - ax_Renderer = SDL_CreateRenderer(ax_Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + ax_Renderer = SDL_CreateRenderer(ax_Window, nullptr); if(!ax_Renderer){ SDL_Log("Fehler bei SDL_CreateRenderer: %s", SDL_GetError()); + SDL_DestroyWindow(ax_Window); + SDL_Quit(); return false; } - + SDL_ShowWindow(ax_Window); return true; } void ax_game::RunLoop(){ + const float targetFrameTime = 1.0f / 30.0f; // 30 FPS = 33.333 ms pro Frame + Uint64 freq = SDL_GetPerformanceFrequency(); + while(ax_IsRunning){ + Uint64 frameStart = SDL_GetPerformanceCounter(); + + // ======= 1. Logic ========== Input(); Update(); Output(); + + // ======= 2. Timing ======= + Uint64 frameEnd = SDL_GetPerformanceCounter(); + float frameDuration = (frameEnd - frameStart) / (float)freq; + + if (frameDuration < targetFrameTime) { + // Zeit, die noch übrig ist, in Sekunden → in Millisekunden umrechnen + float delaySec = targetFrameTime - frameDuration; + SDL_Delay((Uint32)(delaySec * 1000.0f)); + } } } @@ -70,15 +92,16 @@ void ax_game::Input(){ while(SDL_PollEvent(&event)){ switch (event.type) { - case SDL_QUIT: + case SDL_EVENT_QUIT: ax_IsRunning = false; break; } } //Tastatur auslesen - const Uint8 *state = SDL_GetKeyboardState(NULL); - //if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false; + SDL_PumpEvents(); + const bool *state = SDL_GetKeyboardState(NULL); + if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false; //if(state[SDL_SCANCODE_S]) ax_balliste[0].vely += 10; //if(state[SDL_SCANCODE_W]) ax_balliste[0].vely -= 10; //if(state[SDL_SCANCODE_D]) ax_balliste[0].velx += 10; @@ -87,29 +110,21 @@ void ax_game::Input(){ } -void ax_game::Update(){ - while(!SDL_TICKS_PASSED(SDL_GetTicks(), ax_TickCounter + 16)); - float DeltaTime = (SDL_GetTicks() - ax_TickCounter) / 1000.0f; - if (DeltaTime > 0.05f) - { - DeltaTime = 0.05f; - } - ax_TickCounter = SDL_GetTicks(); - - // Update Ball - for (auto &n : ax_balliste){ - n.Update(DeltaTime, ax_balliste); - } +void ax_game::Update() { + static Uint64 lastTick = SDL_GetPerformanceCounter(); + Uint64 now = SDL_GetPerformanceCounter(); + float freq = (float)SDL_GetPerformanceFrequency(); + + float delta = (now - lastTick) / freq; + + if (delta > 0.05f) delta = 0.05f; + lastTick = now; + + for (auto &n : ax_balliste) + n.Update(delta, ax_balliste); - // clean up for (auto it = ax_balliste.begin(); it != ax_balliste.end(); ) - { - if (!it->alive) - it = ax_balliste.erase(it); // erase gibt neuen gültigen Iterator zurück - else - ++it; // nur erhöhen, wenn nichts gelöscht wurde - } - + it = (!it->alive) ? ax_balliste.erase(it) : ++it; } void ax_game::Output(){ diff --git a/Capter1/ax_game.hpp b/Capter1/ax_game.hpp index b3b8c12..c35cfdb 100644 --- a/Capter1/ax_game.hpp +++ b/Capter1/ax_game.hpp @@ -1,5 +1,7 @@ #pragma once -#include "SDL2/SDL.h" +//#include "SDL2/SDL.h" +#include "SDL3/SDL.h" +#include "SDL3/SDL_main.h" #include "ball.hpp" class ax_game diff --git a/Capter1/ball b/Capter1/ball deleted file mode 100755 index 3677ebf..0000000 Binary files a/Capter1/ball and /dev/null differ diff --git a/Capter1/ball.cpp b/Capter1/ball.cpp index 3c21606..18567cb 100644 --- a/Capter1/ball.cpp +++ b/Capter1/ball.cpp @@ -23,7 +23,7 @@ ball::ball(int x, int y, int vx, int vy) alive = true; - rect = {x,y,20,20}; + rect = {(float)x,(float)y,20,20}; color = {255,0,0,255}; } @@ -53,9 +53,9 @@ void ball::Update(float f_DeltaTime, std::vector &list){ this->alive = false; } - color.r += r_dir; - color.g += g_dir; - color.b += b_dir; + color.r += r_dir * (255.0f / 2.0f) * f_DeltaTime; // Richtung * (MaxWert / ZeitfensterFürAnimation) * Vergangene Zeit seit der Letzten Berechnung + color.g += g_dir * (255.0f / 2.0f) * f_DeltaTime; + color.b += b_dir * (255.0f / 2.0f) * f_DeltaTime; //=================ColorCycle====================== // Clamp values between 1 and 254 diff --git a/Capter1/ball.hpp b/Capter1/ball.hpp index 3f04672..cd48523 100644 --- a/Capter1/ball.hpp +++ b/Capter1/ball.hpp @@ -1,4 +1,4 @@ -#include "SDL2/SDL.h" +#include "SDL3/SDL.h" #include #include @@ -9,7 +9,7 @@ private: int g_dir; int b_dir; public: - SDL_Rect rect; + SDL_FRect rect; SDL_Color color; float velx; float vely; diff --git a/Capter1/main b/Capter1/main index e6ee5f5..2e8a485 100755 Binary files a/Capter1/main and b/Capter1/main differ diff --git a/Capter2/game.hpp b/Capter2/game.hpp index dfcd96c..f6d91e6 100644 --- a/Capter2/game.hpp +++ b/Capter2/game.hpp @@ -1,5 +1,7 @@ #pragma once -#include "SDL2/SDL.h" +//#include "SDL2/SDL.h" +#include "SDL3/SDL.h" +#include "SDL3/SDL_main.h" #include class game diff --git a/Capter2/main b/Capter2/main new file mode 100755 index 0000000..4b4b600 Binary files /dev/null and b/Capter2/main differ