Tratando de eliminar las variables globales

This commit is contained in:
2022-10-20 17:19:59 +02:00
parent 11ec100878
commit 596bf2c4a5
4 changed files with 262 additions and 257 deletions

View File

@@ -32,9 +32,74 @@ GameDirector::GameDirector()
init();
}
// Destructor
GameDirector::~GameDirector()
{
// Libera los efectos de sonido
Mix_FreeChunk(gPopBalloonFX);
Mix_FreeChunk(gBulletFX);
gPopBalloonFX = NULL;
gBulletFX = NULL;
// Libra la música
Mix_FreeMusic(gTitleMusic);
gTitleMusic = NULL;
Mix_FreeMusic(gPlayingMusic);
gPlayingMusic = NULL;
// Libera el mando
SDL_JoystickClose(gGameController);
gGameController = NULL;
}
// Iniciador
void GameDirector::init()
{
// Carga la música del titulo
gTitleMusic = Mix_LoadMUS("media/music/title.ogg");
if (gTitleMusic == NULL)
{
printf("Failed to load title music! SDL_mixer Error: %s\n", Mix_GetError());
}
// Carga la música del juego
gPlayingMusic = Mix_LoadMUS("media/music/playing.ogg");
if (gPlayingMusic == NULL)
{
printf("Failed to load playing music! SDL_mixer Error: %s\n", Mix_GetError());
}
// Carga los efectos de sonido para la explosión de los globos
gPopBalloonFX = Mix_LoadWAV("media/sound/balloon.wav");
if (gPopBalloonFX == NULL)
{
printf("Failed to load balloon sound effect! SDL_mixer Error: %s\n", Mix_GetError());
}
// Carga los efectos de sonido para los disparos del jugador
gBulletFX = Mix_LoadWAV("media/sound/bullet.wav");
if (gBulletFX == NULL)
{
printf("Failed to load bullet sound effect! SDL_mixer Error: %s\n", Mix_GetError());
}
// Comprueba los mandos
if (SDL_NumJoysticks() < 1)
{
printf("Warning: No joysticks connected!\n");
}
else
{
// Carga el mando
gGameController = SDL_JoystickOpen(0);
if (gGameController == NULL)
{
printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
printf("%i joysticks were found.\n", SDL_NumJoysticks());
std::cout << SDL_JoystickNumButtons(gGameController) << " buttons\n";
}
// Variables
mGameStatus = GAME_STATE_TITLE;
mOldTicks = 0;

View File

@@ -20,6 +20,9 @@ public:
// Constructor
GameDirector();
// Destructor
~GameDirector();
// Iniciador
void init();
@@ -126,6 +129,17 @@ public:
void runPausedGame();
private:
// Objetos con la música del juego
Mix_Music *gTitleMusic = NULL;
Mix_Music *gPlayingMusic = NULL;
// Objetos con los efectos de sonido del juego
Mix_Chunk *gPopBalloonFX = NULL;
Mix_Chunk *gBulletFX = NULL;
// Manejador para el mando 1
SDL_Joystick *gGameController = NULL;
// Manejador de eventos
SDL_Event eventHandler;

View File

@@ -15,21 +15,9 @@ struct Circle
Uint8 r;
};
//La ventana donde dibujamos
SDL_Window *gWindow = NULL;
//El renderizador de la ventana
SDL_Renderer *gRenderer = NULL;
//Manejador para el mando 1
SDL_Joystick* gGameController = NULL;
//Objetos con la música del juego
Mix_Music *gTitleMusic = NULL;
Mix_Music *gPlayingMusic = NULL;
//Objetos con los efectos de sonido del juego
Mix_Chunk *gPopBalloonFX = NULL;
Mix_Chunk *gBulletFX = NULL;
#endif

View File

@@ -54,6 +54,9 @@ un tipo asociado diferente a NO_KIND
#include <stdio.h>
#include <string>
//La ventana donde dibujamos
SDL_Window *gWindow = NULL;
//Arranca SDL y crea la ventana
bool init();
@@ -90,23 +93,6 @@ bool init()
success = false;
}
//Comprueba los mandos
if (SDL_NumJoysticks() < 1)
{
printf("Warning: No joysticks connected!\n");
}
else
{
//Carga el mando
gGameController = SDL_JoystickOpen(0);
if (gGameController == NULL)
{
printf("Warning: Unable to open game controller! SDL Error: %s\n", SDL_GetError());
}
printf("%i joysticks were found.\n", SDL_NumJoysticks());
std::cout << SDL_JoystickNumButtons(gGameController) << " buttons\n";
}
//Crea la ventana
gWindow = SDL_CreateWindow("Super Popping (Like Loc) in Jailers World", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
@@ -214,38 +200,6 @@ bool loadMedia()
success = false;
}
//Carga la música del titulo
gTitleMusic = Mix_LoadMUS("media/music/title.ogg");
if (gTitleMusic == NULL)
{
printf("Failed to load title music! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
//Carga la música del juego
gPlayingMusic = Mix_LoadMUS("media/music/playing.ogg");
if (gPlayingMusic == NULL)
{
printf("Failed to load playing music! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
//Carga los efectos de sonido para la explosión de los globos
gPopBalloonFX = Mix_LoadWAV("media/sound/balloon.wav");
if (gPopBalloonFX == NULL)
{
printf("Failed to load balloon sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
//Carga los efectos de sonido para los disparos del jugador
gBulletFX = Mix_LoadWAV("media/sound/bullet.wav");
if (gBulletFX == NULL)
{
printf("Failed to load bullet sound effect! SDL_mixer Error: %s\n", Mix_GetError());
success = false;
}
return success;
}
@@ -262,22 +216,6 @@ void close()
gBalloonTexture.free();
gMiscTexture.free();
//Libera los efectos de sonido
Mix_FreeChunk(gPopBalloonFX);
Mix_FreeChunk(gBulletFX);
gPopBalloonFX = NULL;
gBulletFX = NULL;
//Libra la música
Mix_FreeMusic(gTitleMusic);
gTitleMusic = NULL;
Mix_FreeMusic(gPlayingMusic);
gPlayingMusic = NULL;
//Libera el mando
SDL_JoystickClose(gGameController);
gGameController = NULL;
//Destruye la ventana
SDL_DestroyRenderer(gRenderer);
SDL_DestroyWindow(gWindow);