118 lines
2.4 KiB
C++
118 lines
2.4 KiB
C++
#include "game.h"
|
|
|
|
// Constructor
|
|
Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input)
|
|
{
|
|
|
|
this->renderer = renderer;
|
|
this->asset = asset;
|
|
this->screen = screen;
|
|
this->input = input;
|
|
|
|
eventHandler = new SDL_Event();
|
|
map = new Map(asset->get("01.map"), renderer, asset);
|
|
player = new Player(renderer, asset, input, map);
|
|
debugText = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer);
|
|
}
|
|
|
|
// Destructor
|
|
Game::~Game()
|
|
{
|
|
delete eventHandler;
|
|
delete map;
|
|
delete player;
|
|
delete debugText;
|
|
}
|
|
|
|
// Bucle para el juego
|
|
section_t Game::run()
|
|
{
|
|
init();
|
|
|
|
while (section.name == SECTION_PROG_GAME)
|
|
{
|
|
// Sección juego jugando
|
|
if (section.subsection == SUBSECTION_GAME_PLAY)
|
|
{
|
|
update();
|
|
render();
|
|
}
|
|
}
|
|
|
|
return section;
|
|
}
|
|
|
|
// Inicializa las variables necesarias para la sección 'Game'
|
|
void Game::init()
|
|
{
|
|
ticks = 0;
|
|
ticksSpeed = 15;
|
|
|
|
section.name = SECTION_PROG_GAME;
|
|
section.subsection = SUBSECTION_GAME_PLAY;
|
|
|
|
debug = true;
|
|
}
|
|
|
|
// Actualiza el juego, las variables, comprueba la entrada, etc.
|
|
void Game::update()
|
|
{
|
|
// Comprueba que la diferencia de ticks sea mayor a la velocidad del juego
|
|
if (SDL_GetTicks() - ticks > ticksSpeed)
|
|
{
|
|
// Actualiza el contador de ticks
|
|
ticks = SDL_GetTicks();
|
|
|
|
// Comprueba los eventos que hay en la cola
|
|
while (SDL_PollEvent(eventHandler) != 0)
|
|
{
|
|
// Evento de salida de la aplicación
|
|
if (eventHandler->type == SDL_QUIT)
|
|
{
|
|
section.name = SECTION_PROG_QUIT;
|
|
break;
|
|
}
|
|
}
|
|
|
|
player->update();
|
|
checkInput();
|
|
}
|
|
}
|
|
|
|
// Pinta los objetos en pantalla
|
|
void Game::render()
|
|
{
|
|
// Prepara para dibujar el frame
|
|
screen->start();
|
|
screen->clean();
|
|
|
|
// Dibuja los objetos
|
|
map->render();
|
|
player->render();
|
|
renderDebugInfo();
|
|
|
|
// Actualiza la pantalla
|
|
screen->blit();
|
|
}
|
|
|
|
// Comprueba la entrada
|
|
void Game::checkInput()
|
|
{
|
|
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
|
|
debug = !debug;
|
|
}
|
|
|
|
// Muestra información de depuración
|
|
void Game::renderDebugInfo()
|
|
{
|
|
if (!debug)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int line = 0;
|
|
std::string text = "";
|
|
|
|
text = std::to_string((int)player->sprite->getPosX()) + "," + std::to_string((int)player->sprite->getPosY());
|
|
debugText->write(0, line, text, -1);
|
|
} |