229 lines
3.0 KiB
C++
229 lines
3.0 KiB
C++
#include "game.hpp"
|
|
#include "gamestate.hpp"
|
|
|
|
|
|
game::game()
|
|
{ }
|
|
|
|
game::~game()
|
|
{ }
|
|
|
|
|
|
|
|
bool game::Init()
|
|
{
|
|
if ( SDL_Init ( SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO ) == -1 )
|
|
{
|
|
cerr << "[ERROR] Cannot init SDL" << endl;
|
|
|
|
m_bRunning = false;
|
|
|
|
return false;
|
|
}
|
|
|
|
SDL_EnableUNICODE ( SDL_ENABLE );
|
|
|
|
|
|
SDL_WM_SetCaption ( "[SG]Tetris", NULL );
|
|
|
|
m_pScreen = SDL_SetVideoMode ( WIDTH * SIZE + 130, HEIGHT * SIZE, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
|
|
|
|
if ( m_pScreen == NULL )
|
|
{
|
|
cerr << "[ERROR] Cannot setup screen" << endl;
|
|
|
|
m_bRunning = false;
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
if ( TTF_Init() )
|
|
{
|
|
cerr << "[ERROR] Cannot init SDL_ttf" << endl;
|
|
|
|
m_bRunning = false;
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
m_bRunning = Load();
|
|
m_bGameOver = false;
|
|
tick = 0;
|
|
|
|
return m_bRunning;
|
|
}
|
|
|
|
bool game::Load()
|
|
{
|
|
fontMenu = TTF_OpenFont ( "data-latin.ttf", 38 );
|
|
|
|
if ( fontMenu == NULL )
|
|
{
|
|
cerr << "[ERROR] Cannot load font 'data-latin.ttf'" << endl;
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
fontGame = TTF_OpenFont ( "handelgotd.ttf", 30 );
|
|
|
|
if ( fontGame == NULL )
|
|
{
|
|
cerr << "[ERROR] Cannot load font 'handelgotd.ttf'" << endl;
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
void game::ChangeState ( GameState * state )
|
|
{
|
|
// cleanup the current state
|
|
if ( !states.empty() )
|
|
{
|
|
states.back()->Clean();
|
|
states.pop_back();
|
|
}
|
|
|
|
// store and init the new state
|
|
states.push_back ( state );
|
|
states.back()->Init();
|
|
}
|
|
|
|
void game::PushState ( GameState * state )
|
|
{
|
|
// pause current state
|
|
if ( !states.empty() )
|
|
{
|
|
states.back()->Pause();
|
|
}
|
|
|
|
// store and init the new state
|
|
states.push_back ( state );
|
|
states.back()->Init();
|
|
}
|
|
|
|
void game::PopState()
|
|
{
|
|
// cleanup the current state
|
|
if ( !states.empty() )
|
|
{
|
|
states.back()->Clean();
|
|
states.pop_back();
|
|
}
|
|
|
|
// resume previous state
|
|
if ( !states.empty() )
|
|
{
|
|
states.back()->Resume();
|
|
}
|
|
}
|
|
|
|
void game::DelStateF()
|
|
{
|
|
// cleanup the first state
|
|
if ( !states.empty() )
|
|
{
|
|
states.front()->Clean();
|
|
states.erase(states.begin());
|
|
}
|
|
}
|
|
|
|
# ifdef DEBUG
|
|
int game::CountStates()
|
|
{
|
|
return states.size();
|
|
}
|
|
# endif
|
|
|
|
void game::HandleEvents()
|
|
{
|
|
states.back()->HandleEvents ( this );
|
|
}
|
|
|
|
void game::Update()
|
|
{
|
|
states.back()->Update ( this );
|
|
}
|
|
|
|
void game::Draw()
|
|
{
|
|
states.back()->Draw ( this );
|
|
}
|
|
|
|
|
|
SDL_Surface * game::GetScreen()
|
|
{
|
|
return m_pScreen;
|
|
}
|
|
|
|
bool game::Running()
|
|
{
|
|
return m_bRunning;
|
|
}
|
|
|
|
void game::Clean()
|
|
{
|
|
# ifdef DEBUG
|
|
cout << "Cleanning..." << endl;
|
|
# endif
|
|
|
|
TTF_CloseFont ( fontMenu );
|
|
|
|
TTF_Quit();
|
|
SDL_Quit();
|
|
}
|
|
|
|
void game::Quit()
|
|
{
|
|
m_bRunning = false;
|
|
|
|
# ifdef DEBUG
|
|
cout << "Shutting down..." << endl;
|
|
# endif
|
|
}
|
|
|
|
void game::ReduceFPS()
|
|
{
|
|
if ( 1000 / FPS > ( SDL_GetTicks() - start ) )
|
|
{
|
|
SDL_Delay ( 1000 / FPS - ( SDL_GetTicks() - start ) );
|
|
}
|
|
}
|
|
|
|
void game::UpdateFPS()
|
|
{
|
|
if ( tick <= FPS )
|
|
{
|
|
tick += 1;
|
|
}
|
|
else
|
|
{
|
|
tick = 1;
|
|
}
|
|
}
|
|
|
|
Uint32 game::GetStart()
|
|
{
|
|
return start;
|
|
}
|
|
|
|
void game::SetStart ( Uint32 start )
|
|
{
|
|
this->start = start;
|
|
}
|
|
|
|
TTF_Font * game::GetfontGame()
|
|
{
|
|
return fontGame;
|
|
}
|
|
|
|
TTF_Font * game::GetfontMenu()
|
|
{
|
|
return fontMenu;
|
|
}
|