143 lines
3.4 KiB
C++
143 lines
3.4 KiB
C++
#include "ax_game.hpp"
|
|
#include <vector>
|
|
|
|
ax_game::ax_game()
|
|
{
|
|
ax_Window = nullptr;
|
|
ax_Renderer = nullptr;
|
|
ax_TickCounter = 0;
|
|
ax_IsRunning = true;
|
|
|
|
|
|
srand(time(0));
|
|
|
|
|
|
//ax_ball_one = ball(500, 400, 0, 0);
|
|
//ax_ball_two = ball(500, 500, 0, 0);
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
{
|
|
ax_balliste.emplace_back(ball(rand() % 1024, rand() % 768, (rand() % 200) -100 , (rand() % 200) -100));
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
ax_game::~ax_game()
|
|
{
|
|
ax_ball_one.~ball();
|
|
}
|
|
|
|
bool ax_game::Init(){
|
|
if(!SDL_Init(SDL_INIT_VIDEO)){
|
|
SDL_Log("Fehler bei SDL_INIT: %s", SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
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, 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));
|
|
}
|
|
}
|
|
}
|
|
|
|
void ax_game::Shutdown(){
|
|
SDL_DestroyRenderer(ax_Renderer);
|
|
SDL_DestroyWindow(ax_Window);
|
|
SDL_Quit();
|
|
}
|
|
|
|
void ax_game::Input(){
|
|
SDL_Event event;
|
|
while(SDL_PollEvent(&event)){
|
|
switch (event.type)
|
|
{
|
|
case SDL_EVENT_QUIT:
|
|
ax_IsRunning = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Tastatur auslesen
|
|
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;
|
|
//if(state[SDL_SCANCODE_A]) ax_balliste[0].velx -= 10;
|
|
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
for (auto it = ax_balliste.begin(); it != ax_balliste.end(); )
|
|
it = (!it->alive) ? ax_balliste.erase(it) : ++it;
|
|
}
|
|
|
|
void ax_game::Output(){
|
|
SDL_SetRenderDrawColor(ax_Renderer,0,0,255,255); // BG Color
|
|
SDL_RenderClear(ax_Renderer); //Puffer leeren
|
|
|
|
|
|
//ax_ball_one.Draw(ax_Renderer);
|
|
//ax_ball_two.Draw(ax_Renderer);
|
|
|
|
for (auto &n : ax_balliste){
|
|
n.Draw(ax_Renderer);
|
|
}
|
|
|
|
SDL_RenderPresent(ax_Renderer);
|
|
} |