Änderung von Capter1 auf SDL3
This commit is contained in:
@@ -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(){
|
||||
|
||||
Reference in New Issue
Block a user