Ta muestra información de depuración

This commit is contained in:
2022-08-17 08:55:05 +02:00
parent a941f72208
commit a6d6f2854e
8 changed files with 250 additions and 8 deletions

View File

@@ -12,6 +12,7 @@ Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, 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
@@ -20,6 +21,7 @@ Game::~Game()
delete eventHandler;
delete map;
delete player;
delete debugText;
}
// Bucle para el juego
@@ -48,6 +50,8 @@ void Game::init()
section.name = SECTION_PROG_GAME;
section.subsection = SUBSECTION_GAME_PLAY;
debug = true;
}
// Actualiza el juego, las variables, comprueba la entrada, etc.
@@ -71,6 +75,7 @@ void Game::update()
}
player->update();
checkInput();
}
}
@@ -84,7 +89,30 @@ void Game::render()
// 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);
}

View File

@@ -6,6 +6,7 @@
#include "input.h"
#include "map.h"
#include "player.h"
#include "text.h"
#ifndef GAME_H
#define GAME_H
@@ -18,11 +19,13 @@ private:
Screen *screen; // Objeto encargado de dibujar en pantalla
Input *input; // Objeto Input para gestionar las entradas
SDL_Event *eventHandler; // Manejador de eventos
Text *debugText; // Objeto para escribir texto con información de debug
section_t section; // Seccion actual dentro del programa
int ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles del programa
Map *map; // Objeto encargado de gestionar el mapeado del juego
Player *player; // Objeto para gestionar el jugador
bool debug; // Indica si esta activo el modo de depuración
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();
@@ -33,6 +36,12 @@ private:
// Inicializa las variables necesarias para la sección 'Game'
void init();
// Comprueba la entrada
void checkInput();
// Muestra información de depuración
void renderDebugInfo();
public:
// Constructor
Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input);

View File

@@ -13,7 +13,7 @@
// The player
class Player
{
private:
public:
Asset *asset; // Objeto con la ruta a todos los ficheros de recursos
SDL_Renderer *renderer; // El renderizador de la ventana
Input *input; // Objeto Input para gestionar las entradas

View File

@@ -168,6 +168,10 @@ bool Prog::setFileList()
asset->add("/media/music/music_surface.ogg", music);
asset->add("/media/music/music_volcano.ogg", music);
// Texto
asset->add("/media/font/debug.png", font);
asset->add("/media/font/debug.txt", font);
return asset->check();
}

View File

@@ -4,13 +4,16 @@
#include <fstream>
// Constructor
Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
Text::Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer)
{
texture = new LTexture();
loadTextureFromFile(texture, bitmapFile, renderer);
SDL_Rect rect = {0,0,0,0};
mSprite = new Sprite(rect, texture, renderer);
mSprite->setTexture(texture);
mSprite->setRenderer(renderer);
mFile = file;
mFile = textFile;
init();
}
@@ -18,6 +21,10 @@ Text::Text(std::string file, LTexture *texture, SDL_Renderer *renderer)
// Destructor
Text::~Text()
{
texture->unload();
delete texture;
texture = nullptr;
delete mSprite;
mSprite = nullptr;
}
@@ -144,7 +151,6 @@ void Text::initOffsetFromFile()
if (rfile.is_open() && rfile.good())
{
std::string buffer;
//printf("Reading %s file\n", mFile.c_str());
// Lee los dos primeros valores del fichero
std::getline(rfile, buffer);

View File

@@ -27,20 +27,21 @@ private:
Uint8 mBoxWidth; // Anchura de la caja de cada caracter en el png
Uint8 mBoxHeight; // Altura de la caja de cada caracter en el png
std::string mFile; // Fichero con los descriptores de la fuente
LTexture *texture; // Textura con los bitmaps del texto
// Inicializador
void init();
// Inicializa el vector de offsets desde un fichero
void initOffsetFromFile();
public:
// Constructor
Text(std::string file, LTexture *texture, SDL_Renderer *renderer);
Text(std::string bitmapFile, std::string textFile, SDL_Renderer *renderer);
// Destructor
~Text();
// Inicializador
void init();
// Escribe el texto en pantalla
void write(int x, int y, std::string text, int kerning = 1, int lenght = -1);