Created Capter2 for Test with componentbased objects
This commit is contained in:
9
Capter2/Actor.cpp
Normal file
9
Capter2/Actor.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "Actor.hpp"
|
||||
|
||||
Actor::Actor(/* args */)
|
||||
{
|
||||
}
|
||||
|
||||
Actor::~Actor()
|
||||
{
|
||||
}
|
||||
14
Capter2/Actor.hpp
Normal file
14
Capter2/Actor.hpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
class Actor
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
Actor(/* args */);
|
||||
~Actor();
|
||||
|
||||
void Update(float);
|
||||
void Draw(SDL_Renderer *);
|
||||
|
||||
};
|
||||
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)
|
||||
{
|
||||
|
||||
}
|
||||
33
Capter2/game.hpp
Normal file
33
Capter2/game.hpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
#include "SDL2/SDL.h"
|
||||
#include <vector>
|
||||
|
||||
class game
|
||||
{
|
||||
private:
|
||||
void input(); //Process input
|
||||
void update(); //update game
|
||||
void output(); // generate output
|
||||
|
||||
SDL_Window *ptr_MainWindow;
|
||||
SDL_Renderer *ptr_MainRenderer;
|
||||
Uint32 GameTick;
|
||||
|
||||
bool GameIsRunning;
|
||||
bool UpdateActors;
|
||||
|
||||
std::vector<class Actor *> ActorList;
|
||||
|
||||
public:
|
||||
game(); //constructor
|
||||
~game(); //destructor
|
||||
|
||||
bool Init(int x, int y, int w, int h);
|
||||
void RunLoop();
|
||||
void Shutdown();
|
||||
|
||||
void AddActor(class Actor *ptr_actor);
|
||||
void RemActor(class Actor *ptr_actor);
|
||||
|
||||
};
|
||||
|
||||
20
Capter2/main.cpp
Normal file
20
Capter2/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "game.hpp"
|
||||
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
game game;
|
||||
|
||||
bool success = game.Init(100,100,1024,768);
|
||||
|
||||
while (success)
|
||||
{
|
||||
game.RunLoop();
|
||||
}
|
||||
|
||||
game.Shutdown();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user