Corregits 8.000.000 de segmentation faults en intro.cpp

This commit is contained in:
2024-10-07 18:37:08 +02:00
parent 0330fe6b74
commit bf945ef14b
5 changed files with 48 additions and 53 deletions

View File

@@ -19,18 +19,15 @@
struct JA_Music_t;
// Constructor
Intro::Intro(JA_Music_t *music):music(music)
Intro::Intro(JA_Music_t *music) : music(music)
{
// Copia los punteros
input = Input::get();
asset = Asset::get();
screen = Screen::get();
SDL_Renderer *renderer = screen->getRenderer();
SDL_Renderer *renderer = Screen::get()->getRenderer();
// Reserva memoria para los objetos
eventHandler = std::make_unique<SDL_Event>();
texture = std::make_unique<Texture>(renderer, asset->get("intro.png"));
text = std::make_unique<Text>(asset->get("nokia.png"), asset->get("nokia.txt"), renderer);
texture = std::make_unique<Texture>(renderer, Asset::get()->get("intro.png"));
text = std::make_unique<Text>(Asset::get()->get("nokia.png"), Asset::get()->get("nokia.txt"), renderer);
// Inicializa variables
section::name = section::NAME_INTRO;
@@ -49,7 +46,7 @@ Intro::Intro(JA_Music_t *music):music(music)
ss->setEnabledCounter(20);
ss->setDestX(param.game.gameArea.centerX - 64);
ss->setDestY(param.game.gameArea.firstQuarterY - 24);
bitmaps.push_back(ss.get());
bitmaps.push_back(std::move(ss));
}
bitmaps[0]->setPosX(-128);
@@ -111,7 +108,7 @@ Intro::Intro(JA_Music_t *music):music(music)
w->setKerning(-1);
w->setEnabled(false);
w->setEnabledCounter(180);
texts.push_back(w.get());
texts.push_back(std::move(w));
}
// Un dia qualsevol de l'any 2000
@@ -150,7 +147,7 @@ Intro::Intro(JA_Music_t *music):music(music)
texts[8]->setCaption(lang::getText(35));
texts[8]->setSpeed(16);
for (auto text : texts)
for (auto &text : texts)
{
text->center(param.game.gameArea.centerX);
}
@@ -191,7 +188,7 @@ void Intro::checkEvents()
void Intro::checkInput()
{
// Comprueba si se ha pulsado cualquier botón (de los usados para jugar)
if (input->checkAnyButtonPressed())
if (Input::get()->checkAnyButtonPressed())
{
JA_StopMusic();
section::name = section::NAME_TITLE;
@@ -200,7 +197,7 @@ void Intro::checkInput()
}
// Comprueba el input para el resto de objetos
screen->checkInput();
Screen::get()->checkInput();
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
globalInputs::check();
@@ -371,15 +368,15 @@ void Intro::update()
ticks = SDL_GetTicks();
// Actualiza el objeto screen
screen->update();
Screen::get()->update();
// Actualiza los objetos
for (auto bitmap : bitmaps)
for (auto &bitmap : bitmaps)
{
bitmap->update();
}
for (auto text : texts)
for (auto &text : texts)
{
text->update();
}
@@ -393,24 +390,24 @@ void Intro::update()
void Intro::render()
{
// Prepara para empezar a dibujar en la textura de juego
screen->start();
Screen::get()->start();
// Limpia la pantalla
screen->clean(bgColor);
Screen::get()->clean(bgColor);
// Dibuja los objetos
for (auto bitmap : bitmaps)
for (auto &bitmap : bitmaps)
{
bitmap->render();
}
for (auto text : texts)
for (auto &text : texts)
{
text->render();
}
// Vuelca el contenido del renderizador en pantalla
screen->blit();
Screen::get()->blit();
}
// Bucle principal

View File

@@ -1,16 +1,13 @@
#pragma once
#include <SDL2/SDL_events.h> // for SDL_Event
#include <SDL2/SDL_stdinc.h> // for Uint32, Uint8
#include <vector> // for vector
#include <SDL2/SDL_events.h> // for SDL_Event
#include <SDL2/SDL_stdinc.h> // for Uint32, Uint8
#include <vector> // for vector
#include <memory>
class Asset;
class Input;
class Screen;
class SmartSprite;
#include "smart_sprite.h"
#include "texture.h"
#include "text.h"
class Writer;
#include "writer.h"
struct JA_Music_t;
/*
@@ -22,17 +19,13 @@ struct JA_Music_t;
class Intro
{
private:
// Objetos y punteros
Screen *screen; // Objeto encargado de dibujar en pantalla
Asset *asset; // Objeto que gestiona todos los ficheros de recursos
Input *input; // Objeto pata gestionar la entrada
std::unique_ptr<Texture> texture; // Textura con los graficos
std::unique_ptr<SDL_Event> eventHandler; // Manejador de eventos
std::unique_ptr<Text> text; // Textos de la intro
std::vector<SmartSprite *> bitmaps; // Vector con los sprites inteligentes para los dibujos de la intro
std::vector<Writer *> texts; // Textos de la intro
// Objetos
std::unique_ptr<Texture> texture; // Textura con los graficos
std::unique_ptr<SDL_Event> eventHandler; // Manejador de eventos
std::unique_ptr<Text> text; // Textos de la intro
std::vector<std::unique_ptr<SmartSprite>> bitmaps; // Vector con los sprites inteligentes para los dibujos de la intro
std::vector<std::unique_ptr<Writer>> texts; // Textos de la intro
// Variables
Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa

View File

@@ -1,6 +1,6 @@
#pragma once
#include "animated_sprite.h" // for AnimatedSprite
#include "animated_sprite.h" // for AnimatedSprite
class Texture;
// Clase SmartSprite
@@ -25,6 +25,9 @@ public:
// Constructor
SmartSprite(Texture *texture);
// Destructor
~SmartSprite() = default;
// Inicializa el objeto
void init();

View File

@@ -2,11 +2,8 @@
#include "text.h" // for Text
// Constructor
Writer::Writer(Text *text)
Writer::Writer(Text *text) : text(text)
{
// Copia los punteros
this->text = text;
// Inicializa variables
posX = 0;
posY = 0;
@@ -28,7 +25,8 @@ void Writer::update()
if (enabled)
{
if (!completed)
{ // No completado
{
// No completado
if (writingCounter > 0)
{
writingCounter--;
@@ -46,8 +44,9 @@ void Writer::update()
}
}
if (completed)
{ // Completado
else
{
// Completado
if (enabledCounter > 0)
{
enabledCounter--;
@@ -108,7 +107,7 @@ void Writer::setEnabled(bool value)
}
// Obtiene el valor de la variable
bool Writer::IsEnabled()
bool Writer::IsEnabled() const
{
return enabled;
}
@@ -120,7 +119,7 @@ void Writer::setEnabledCounter(int time)
}
// Obtiene el valor de la variable
int Writer::getEnabledCounter()
int Writer::getEnabledCounter() const
{
return enabledCounter;
}
@@ -132,7 +131,7 @@ void Writer::center(int x)
}
// Obtiene el valor de la variable
bool Writer::hasFinished()
bool Writer::hasFinished() const
{
return finished;
}

View File

@@ -28,6 +28,9 @@ public:
// Constructor
Writer(Text *text);
// Destructor
~Writer() = default;
// Actualiza el objeto
void update();
@@ -53,17 +56,17 @@ public:
void setEnabled(bool value);
// Obtiene el valor de la variable
bool IsEnabled();
bool IsEnabled() const;
// Establece el valor de la variable
void setEnabledCounter(int time);
// Obtiene el valor de la variable
int getEnabledCounter();
int getEnabledCounter() const;
// Centra la cadena de texto a un punto X
void center(int x);
// Obtiene el valor de la variable
bool hasFinished();
bool hasFinished() const;
};