Empezada la sección del titulo del juego

This commit is contained in:
2022-08-23 12:35:56 +02:00
parent 6b4926efb8
commit b61677bda9
8 changed files with 180 additions and 11 deletions

View File

@@ -1,9 +1,8 @@
#include "game.h"
// Constructor
Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input)
Game::Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
{
this->renderer = renderer;
this->asset = asset;
this->screen = screen;

View File

@@ -19,14 +19,14 @@ private:
Screen *screen; // Objeto encargado de dibujar en pantalla
Input *input; // Objeto Input para gestionar las entradas
SDL_Event *eventHandler; // Manejador de eventos
JA_Music music; // Contiene la musica que se reproduce durante el juego
Text *debugText; // Objeto para escribir texto con información de debug
Map *map; // Objeto encargado de gestionar el mapeado del juego
Player *player; // Objeto para gestionar el jugador
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
JA_Music music; // Contiene la musica que se reproduce durante el juego
// Actualiza el juego, las variables, comprueba la entrada, etc.
void update();
@@ -51,7 +51,7 @@ private:
public:
// Constructor
Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input);
Game(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input);
// Destructor
~Game();

View File

@@ -33,6 +33,9 @@ Intro::~Intro()
texture->unload();
delete texture;
texture = nullptr;
delete sprite;
sprite = nullptr;
}
// Actualiza las variables
@@ -57,15 +60,16 @@ void Intro::update()
// Cualquier tecla pulsada
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
section.name = SECTION_PROG_GAME;
section.name = SECTION_PROG_TITLE;
section.subsection = 0;
}
}
sprite->animate();
// Comprueba si ha terminado la animación de la intro
if (sprite->animationIsCompleted())
{
section.name = SECTION_PROG_GAME;
section.name = SECTION_PROG_TITLE;
}
}
}

View File

@@ -204,6 +204,7 @@ void Prog::setSection(section_t section)
this->section = section;
}
// Ejecuta la seccion de juego con el logo
void Prog::runLogo()
{
logo = new Logo(renderer, screen, asset);
@@ -219,9 +220,18 @@ void Prog::runIntro()
delete intro;
}
// Ejecuta la seccion de juego con el titulo y los menus
void Prog::runTitle()
{
title = new Title(renderer, screen, asset, input);
setSection(title->run());
delete title;
}
// Ejecuta la seccion de juego donde se juega
void Prog::runGame()
{
game = new Game(renderer, asset, screen, input);
game = new Game(renderer, screen, asset, input);
setSection(game->run());
delete game;
}
@@ -240,7 +250,7 @@ void Prog::run()
runIntro();
break;
case SECTION_PROG_TITLE:
// runTitle();
runTitle();
break;
case SECTION_PROG_GAME:
runGame();

View File

@@ -9,6 +9,8 @@
#include "screen.h"
#include "logo.h"
#include "intro.h"
#include "title.h"
#include "prog.h"
#ifndef PROG_H
#define PROG_H
@@ -28,6 +30,7 @@ private:
Game *game; // Objeto para la sección del juego
Intro *intro; // Objeto encargado de gestionar la intro del juego
Logo *logo; // Objeto encargado de gestionar el logo del juego
Title *title; // Objeto encargado de gestionar el titulo del juego, con el menu principal
section_t section; // Sección y subsección actual del programa;
struct options_t *options; // Contiene las opciones del programa

View File

@@ -51,7 +51,7 @@ public:
// Escribe el texto con sombra
void writeShadowed(int x, int y, std::string text, color_t color, Uint8 shadowDistance = 1, int kerning = 1, int lenght = -1);
// Escribe el texto centrado en un punto x y con kerning
// Escribe el texto centrado en un punto x
void writeCentered(int x, int y, std::string text, int kerning = 1, int lenght = -1);
// Escribe texto con extras

104
source/title.cpp Normal file
View File

@@ -0,0 +1,104 @@
#include "title.h"
// Constructor
Title::Title(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input)
{
// Copia los punteros
this->renderer = renderer;
this->screen = screen;
this->asset = asset;
this->input = input;
// Reserva memoria para los punteros
eventHandler = new SDL_Event();
texture = new LTexture();
loadTextureFromFile(texture, asset->get("intro.png"), renderer);
sprite = new AnimatedSprite(texture, renderer, asset->get("intro.ani"));
sprite->setCurrentAnimation("menu");
text = new Text(asset->get("debug.png"), asset->get("debug.txt"), renderer);
// Inicializa variables
section = {SECTION_PROG_TITLE, 0};
ticks = 0;
ticksSpeed = 15;
}
// Destructor
Title::~Title()
{
renderer = nullptr;
screen = nullptr;
asset = nullptr;
delete eventHandler;
eventHandler = nullptr;
texture->unload();
delete texture;
texture = nullptr;
delete sprite;
sprite = nullptr;
delete text;
text = nullptr;
}
// Actualiza las variables
void Title::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;
}
// Cualquier tecla pulsada
if ((eventHandler->type == SDL_KEYDOWN) || (eventHandler->type == SDL_JOYBUTTONDOWN))
{
section.name = SECTION_PROG_GAME;
section.subsection = 0;
}
}
sprite->animate();
}
}
// Dibuja en pantalla
void Title::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
// Limpia la pantalla
screen->clean();
// Dibuja los objetos
sprite->render();
text->writeCentered(160, 200, "@2016,2022 JailDesigner (v0.6)", -1);
// Vuelca el contenido del renderizador en pantalla
screen->blit();
}
// Bucle principal
section_t Title::run()
{
while (section.name == SECTION_PROG_TITLE)
{
update();
render();
}
return section;
}

49
source/title.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include <SDL2/SDL.h>
#include "const.h"
#include "asset.h"
#include "utils.h"
#include "input.h"
#include "screen.h"
#include "text.h"
#include "animatedsprite.h"
#include "jail_audio.h"
#ifndef TITLE_H
#define TITLE_H
// Clase Intro
class Title
{
private:
SDL_Renderer *renderer; // El renderizador de la ventana
Screen *screen; // Objeto encargado de dibujar en pantalla
LTexture *texture; // Textura con los graficos
SDL_Event *eventHandler; // Manejador de eventos
Asset *asset; // Objeto con los ficheros de recurso
Input *input; // Objeto para gestionar las entradas
Text *text; // Objeto para escribir texto en pantalla
AnimatedSprite *sprite; // Sprite para dibujar los graficos de la intro
section_t section; // Estado del bucle principal para saber si continua o se sale
int ticks; // Contador de ticks para ajustar la velocidad del programa
int ticksSpeed; // Velocidad a la que se repiten los bucles del programa
// Actualiza las variables
void update();
// Dibuja en pantalla
void render();
public:
// Constructor
Title(SDL_Renderer *renderer, Screen *screen, Asset *asset, Input *input);
// Destructor
~Title();
// Bucle principal
section_t run();
};
#endif