128 lines
2.7 KiB
C++
128 lines
2.7 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(){
|
|
int sdlResult = SDL_Init(SDL_INIT_VIDEO);
|
|
if(sdlResult != 0){
|
|
SDL_Log("Fehler bei SDL_INIT: %s", SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
ax_Window = SDL_CreateWindow("Ersted Fenster", 100,100,1024,768,0);
|
|
|
|
ax_Renderer = SDL_CreateRenderer(ax_Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
|
|
if(!ax_Renderer){
|
|
SDL_Log("Fehler bei SDL_CreateRenderer: %s", SDL_GetError());
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
void ax_game::RunLoop(){
|
|
while(ax_IsRunning){
|
|
Input();
|
|
Update();
|
|
Output();
|
|
}
|
|
}
|
|
|
|
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_QUIT:
|
|
ax_IsRunning = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//Tastatur auslesen
|
|
const Uint8 *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(){
|
|
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);
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
}
|
|
|
|
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);
|
|
} |