Ä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
#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)

Binary file not shown.

View File

@@ -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(){

View File

@@ -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

Binary file not shown.

View File

@@ -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<ball> &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

View File

@@ -1,4 +1,4 @@
#include "SDL2/SDL.h"
#include "SDL3/SDL.h"
#include <iostream>
#include <vector>
@@ -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;

Binary file not shown.