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

View File

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

View File

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

View File

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

View File

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