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
|
#include "Game.h"
Game * Game::s_pInstance = 0;
// SDL Initialize *************************************************************
bool Game::init(const char * title, int xpos, int ypos, int width,
int height, bool fullscreen)
{
// attepmt to initialize sdl
if(SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "SDL Init success.\n";
// Init the window
m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, fullscreen);
if(m_pWindow != 0)
{
std::cout << "SDL Window success.\n";
m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);
if(m_pRenderer != 0)
{
std::cout << "SDL Renderer success.\n";
SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
}else
{
std::cout << "SDL Renderer fail.\n";
return false;
}
}else
{
std::cout << "SDL Window fail.\n";
return false;
}
}else
{
std::cout << "SDL Init fail.\n";
return false;
}
std::cout << "SDL Success.\n";
m_bRunning = true;
if(!theTextureManager::Instance()->load("images/animate.bmp", "animate", m_pRenderer))
{
return false;
}
TheInputHandler::Instance()->initializeJoysticks();
m_pGameStateMachine = new GameStateMachine();
m_pGameStateMachine->changeState(new MenuState());
// m_gameObjects.push_back(new Player(new LoaderParams(100, 100, 128, 82, "animate")));
// m_gameObjects.push_back(new Enemy(new LoaderParams(300, 300, 128, 82, "animate")));
return true;
}
// Renderer Function **********************************************************
void Game::render()
{
SDL_RenderClear(m_pRenderer);
m_pGameStateMachine->render();
SDL_RenderPresent(m_pRenderer);
}
void Game::update()
{
m_pGameStateMachine->update();
}
// Cleanup Function ***********************************************************
void Game::clean()
{
std::cout << "SDL Cleanup complete.\n";
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
TheInputHandler::Instance()->clean();
SDL_Quit();
}
// Handle Events function *****************************************************
void Game::handleEvents()
{
TheInputHandler::Instance()->update();
if(TheInputHandler::Instance()->isKeyDown(SDL_SCANCODE_RETURN))
{
m_pGameStateMachine->changeState(new PlayState());
}
}
|