104 lines
2.1 KiB
C++
104 lines
2.1 KiB
C++
#include "game.h"
|
|
|
|
// Constructor
|
|
Game::Game(SDL_Renderer *renderer, std::string *filelist, Lang *lang, Input *input)
|
|
{
|
|
// Copia los punteros
|
|
mRenderer = renderer;
|
|
mFileList = filelist;
|
|
mLang = lang;
|
|
mInput = input;
|
|
|
|
mEventHandler = new SDL_Event();
|
|
mTextureText = new LTexture();
|
|
mText = new Text(mFileList[5], mTextureText, mRenderer);
|
|
mFade = new Fade(mRenderer);
|
|
}
|
|
|
|
Game::~Game()
|
|
{
|
|
mRenderer = nullptr;
|
|
mFileList = nullptr;
|
|
mLang = nullptr;
|
|
mInput = nullptr;
|
|
|
|
delete mEventHandler;
|
|
mEventHandler = nullptr;
|
|
|
|
mTextureText->unload();
|
|
delete mTextureText;
|
|
mTextureText = nullptr;
|
|
|
|
delete mText;
|
|
mText = nullptr;
|
|
|
|
delete mFade;
|
|
mFade = nullptr;
|
|
}
|
|
|
|
// Inicializa las variables necesarias para la sección 'Game'
|
|
void Game::init()
|
|
{
|
|
// Carga los recursos
|
|
loadMedia();
|
|
|
|
mTicks = 0;
|
|
mTicksSpeed = 15;
|
|
|
|
mSection.name = PROG_SECTION_GAME;
|
|
mSection.subsection = GAME_SECTION_PLAY_1P;
|
|
}
|
|
|
|
// Carga los recursos necesarios para la sección 'Game'
|
|
bool Game::loadMedia()
|
|
{
|
|
bool success = true;
|
|
|
|
// Texturas
|
|
success &= loadTextureFromFile(mTextureText, mFileList[4], mRenderer);
|
|
|
|
return success;
|
|
}
|
|
|
|
// Bucle para el juego
|
|
section_t Game::run()
|
|
{
|
|
init();
|
|
|
|
while (mSection.name == PROG_SECTION_GAME)
|
|
{
|
|
// Sección juego jugando
|
|
if ((mSection.subsection == GAME_SECTION_PLAY_1P) || (mSection.subsection == GAME_SECTION_PLAY_2P))
|
|
{
|
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
|
if (SDL_GetTicks() - mTicks > mTicksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
mTicks = SDL_GetTicks();
|
|
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(mEventHandler) != 0)
|
|
{
|
|
// Evento de salida de la aplicación
|
|
if (mEventHandler->type == SDL_QUIT)
|
|
{
|
|
mSection.name = PROG_SECTION_QUIT;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Limpia la pantalla
|
|
SDL_SetRenderDrawColor(mRenderer, 0xAA, 0x44, 0x44, 0xFF);
|
|
SDL_RenderClear(mRenderer);
|
|
|
|
mText->writeCentered(SCREEN_CENTER_X, SCREEN_CENTER_Y, "Pepe el Cazavampiros", 0);
|
|
|
|
// Actualiza la pantalla
|
|
SDL_RenderPresent(mRenderer);
|
|
}
|
|
}
|
|
|
|
return mSection;
|
|
}
|