1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include "GameManager.h"
#include "GameState.h"
#include "Player.h"
GameManager::GameManager() : _window(nullptr), _renderer(nullptr), _screenWidth(1024), _screenHeight(768)
{
std::cout << "GameManager created..." << std::endl;
GameState _gameState(GameState::PLAY);
}
GameManager::~GameManager()
{
std::cout << "GameManager destroyed..." << std::endl;
}
void GameManager::run()
{
initialiseSystem();
gameLoop();
}
void GameManager::initialiseSystem()
{
// Initialise SDL
SDL_Init(SDL_INIT_EVERYTHING);
// Create an SDL window
_window = SDL_CreateWindow(_gameTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_SHOWN);
// Create an SDL renderer
_renderer = SDL_CreateRenderer(_window, -1, 0);
/* Select the color for drawing. It is set to red here. */
SDL_SetRenderDrawColor(_renderer, 255, 0, 0, 255);
/* Clear the entire screen to our selected color. */
SDL_RenderClear(_renderer);
}
void GameManager::gameLoop()
{
std::cout << "gameLoop() running ..." << std::endl;
Player player1;
// print x position for debug
std::cout << player1.playerRect.x << std::endl;
std::cout << player1.playerRect.y << std::endl;
std::cout << player1.playerRect.w << std::endl;
std::cout << player1.playerRect.h << std::endl;
player1.getPlayerRect();
while (_gameState != GameState::EXIT) {
// Handle input
handleInput();
// Move player, physics, etc
// Draw
//draw(player.getPlayerRect());
}
std::cout << "gameLoop() ended ..." << std::endl;
}
void GameManager::handleInput()
{
// check for event, determine type and act accordingly
SDL_Event evnt;
while (SDL_PollEvent(&evnt))
{
switch (evnt.type) {
case SDL_QUIT: // user clicked x to close window
SDL_Quit();
_gameState = GameState::EXIT;
break;
}
}
}
void GameManager::draw(const SDL_Rect rect)
{
SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255);
SDL_RenderDrawRect(_renderer, &rect);
// Show back buffer
SDL_RenderPresent(_renderer);
}
|