Änderung von Capter1 auf SDL3

This commit is contained in:
2025-11-02 16:26:43 +01:00
parent f4f9560fa9
commit 2aa3720d12
11 changed files with 59 additions and 38 deletions

View File

@@ -1,5 +1,7 @@
#pragma once #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 // Vector2 struct just stores x/y coordinates
// (for now) // (for now)

Binary file not shown.

View File

@@ -30,32 +30,54 @@ ax_game::~ax_game()
} }
bool ax_game::Init(){ bool ax_game::Init(){
int sdlResult = SDL_Init(SDL_INIT_VIDEO); if(!SDL_Init(SDL_INIT_VIDEO)){
if(sdlResult != 0){
SDL_Log("Fehler bei SDL_INIT: %s", SDL_GetError()); SDL_Log("Fehler bei SDL_INIT: %s", SDL_GetError());
return false; 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){ if(!ax_Renderer){
SDL_Log("Fehler bei SDL_CreateRenderer: %s", SDL_GetError()); SDL_Log("Fehler bei SDL_CreateRenderer: %s", SDL_GetError());
SDL_DestroyWindow(ax_Window);
SDL_Quit();
return false; return false;
} }
SDL_ShowWindow(ax_Window);
return true; return true;
} }
void ax_game::RunLoop(){ 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){ while(ax_IsRunning){
Uint64 frameStart = SDL_GetPerformanceCounter();
// ======= 1. Logic ==========
Input(); Input();
Update(); Update();
Output(); 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)){ while(SDL_PollEvent(&event)){
switch (event.type) switch (event.type)
{ {
case SDL_QUIT: case SDL_EVENT_QUIT:
ax_IsRunning = false; ax_IsRunning = false;
break; break;
} }
} }
//Tastatur auslesen //Tastatur auslesen
const Uint8 *state = SDL_GetKeyboardState(NULL); SDL_PumpEvents();
//if(state[SDL_SCANCODE_ESCAPE]) ax_IsRunning = false; 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_S]) ax_balliste[0].vely += 10;
//if(state[SDL_SCANCODE_W]) 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; //if(state[SDL_SCANCODE_D]) ax_balliste[0].velx += 10;
@@ -87,29 +110,21 @@ void ax_game::Input(){
} }
void ax_game::Update(){ void ax_game::Update() {
while(!SDL_TICKS_PASSED(SDL_GetTicks(), ax_TickCounter + 16)); static Uint64 lastTick = SDL_GetPerformanceCounter();
float DeltaTime = (SDL_GetTicks() - ax_TickCounter) / 1000.0f; Uint64 now = SDL_GetPerformanceCounter();
if (DeltaTime > 0.05f) float freq = (float)SDL_GetPerformanceFrequency();
{
DeltaTime = 0.05f;
}
ax_TickCounter = SDL_GetTicks();
// Update Ball float delta = (now - lastTick) / freq;
for (auto &n : ax_balliste){
n.Update(DeltaTime, ax_balliste); 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(); ) for (auto it = ax_balliste.begin(); it != ax_balliste.end(); )
{ it = (!it->alive) ? ax_balliste.erase(it) : ++it;
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
}
} }
void ax_game::Output(){ void ax_game::Output(){

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include "SDL2/SDL.h" //#include "SDL2/SDL.h"
#include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
#include "ball.hpp" #include "ball.hpp"
class ax_game class ax_game

Binary file not shown.

View File

@@ -23,7 +23,7 @@ ball::ball(int x, int y, int vx, int vy)
alive = true; alive = true;
rect = {x,y,20,20}; rect = {(float)x,(float)y,20,20};
color = {255,0,0,255}; color = {255,0,0,255};
} }
@@ -53,9 +53,9 @@ void ball::Update(float f_DeltaTime, std::vector<ball> &list){
this->alive = false; this->alive = false;
} }
color.r += r_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; color.g += g_dir * (255.0f / 2.0f) * f_DeltaTime;
color.b += b_dir; color.b += b_dir * (255.0f / 2.0f) * f_DeltaTime;
//=================ColorCycle====================== //=================ColorCycle======================
// Clamp values between 1 and 254 // Clamp values between 1 and 254

View File

@@ -1,4 +1,4 @@
#include "SDL2/SDL.h" #include "SDL3/SDL.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
@@ -9,7 +9,7 @@ private:
int g_dir; int g_dir;
int b_dir; int b_dir;
public: public:
SDL_Rect rect; SDL_FRect rect;
SDL_Color color; SDL_Color color;
float velx; float velx;
float vely; float vely;

Binary file not shown.

View File

@@ -1,5 +1,7 @@
#pragma once #pragma once
#include "SDL2/SDL.h" //#include "SDL2/SDL.h"
#include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
#include <vector> #include <vector>
class game class game

BIN
Capter2/main Executable file

Binary file not shown.