Created Capter2 for Test with componentbased objects
This commit is contained in:
121
Capter2/game.cpp
Normal file
121
Capter2/game.cpp
Normal file
@@ -0,0 +1,121 @@
|
||||
#include "game.hpp"
|
||||
#include "Actor.hpp"
|
||||
|
||||
|
||||
game::game()
|
||||
{
|
||||
ptr_MainWindow = nullptr;
|
||||
ptr_MainRenderer = nullptr;
|
||||
GameTick = 0;
|
||||
GameIsRunning = true;
|
||||
}
|
||||
|
||||
game::~game()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
void game::input()
|
||||
{
|
||||
SDL_Event event;
|
||||
|
||||
while (SDL_PollEvent (&event))
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
GameIsRunning = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const Uint8* state = SDL_GetKeyboardState(NULL);
|
||||
if (state[SDL_SCANCODE_ESCAPE])
|
||||
{
|
||||
GameIsRunning = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
void game::update()
|
||||
{
|
||||
while (!SDL_TICKS_PASSED(SDL_GetTicks(), GameTick + 16)); //Wait 16ms since last frame
|
||||
|
||||
float dt = (SDL_GetTicks() - GameTick)/ 1000.0f; //Zeitdifferent in Sekunden
|
||||
if(dt > 0.05f) dt = 0.05f;
|
||||
|
||||
GameTick = SDL_GetTicks();
|
||||
|
||||
//Update all Actores
|
||||
|
||||
UpdateActors = true;
|
||||
for (auto a : ActorList)
|
||||
{
|
||||
a->Update(dt);
|
||||
}
|
||||
|
||||
UpdateActors = false;
|
||||
|
||||
}
|
||||
|
||||
void game::output()
|
||||
{
|
||||
for (auto a : ActorList)
|
||||
{
|
||||
a->Draw(ptr_MainRenderer);
|
||||
}
|
||||
}
|
||||
|
||||
bool game::Init(int x, int y, int w, int h)
|
||||
{
|
||||
ptr_MainWindow = SDL_CreateWindow("ComponentBasedProgramm", x,y,w,h,0);
|
||||
|
||||
if (!ptr_MainWindow)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ptr_MainRenderer = SDL_CreateRenderer(ptr_MainWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||
|
||||
if (!ptr_MainRenderer)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
GameTick = SDL_GetTicks();
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void game::RunLoop()
|
||||
{
|
||||
while(GameIsRunning){
|
||||
input();
|
||||
update();
|
||||
output();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void game::Shutdown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void game::AddActor(class Actor *ptr_actor)
|
||||
{
|
||||
SDL_DestroyRenderer(ptr_MainRenderer);
|
||||
SDL_DestroyWindow(ptr_MainWindow);
|
||||
SDL_Quit();
|
||||
|
||||
}
|
||||
|
||||
void RemActor(class Actor *ptr_actor)
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user