diff --git a/source/gamedirector.cpp b/source/gamedirector.cpp index e66207e..2e330d1 100644 --- a/source/gamedirector.cpp +++ b/source/gamedirector.cpp @@ -43,6 +43,70 @@ GameDirector::GameDirector(SDL_Renderer *gRenderer) bullet[i] = new Bullet(gRenderer); } + // Carga la música del titulo + gTitleMusic = JA_LoadMusic("media/music/title.ogg"); + + // Carga la música del juego + gPlayingMusic = JA_LoadMusic("media/music/playing.ogg"); + + // Carga los efectos de sonido para la explosión de los globos + gPopBalloonFX = JA_LoadSound("media/sound/balloon.wav"); + + // Carga los efectos de sonido para los disparos del jugador + gBulletFX = JA_LoadSound("media/sound/bullet.wav"); + + // Carga los gráficos del fondo del juego + gGameBackgroundTexture = new LTexture(gRenderer); + if (!gGameBackgroundTexture->loadFromFile("media/gfx/background.png")) + { + printf("Failed to load game background texture!\n"); + } + + // Carga los gráficos del fondo de la pantalla de titulo + gTitleBackgroundTexture = new LTexture(gRenderer); + if (!gTitleBackgroundTexture->loadFromFile("media/gfx/title.png")) + { + printf("Failed to load title texture!\n"); + } + + // Carga varios gráficos para varios propósitos + gMiscTexture = new LTexture(gRenderer); + if (!gMiscTexture->loadFromFile("media/gfx/misc.png")) + { + printf("Failed to load misc texture!\n"); + } + + // Carga los gráficos para el texto blanco + gWhiteFontTexture = new LTexture(gRenderer); + if (!gWhiteFontTexture->loadFromFile("media/gfx/white_font.png")) + { + printf("Failed to load white font texture!\n"); + } + + // Carga los gráficos para el texto negro + gBlackFontTexture = new LTexture(gRenderer); + if (!gBlackFontTexture->loadFromFile("media/gfx/black_font.png")) + { + printf("Failed to load black font texture!\n"); + } + + // 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"; + } + init(); } @@ -50,18 +114,10 @@ GameDirector::GameDirector(SDL_Renderer *gRenderer) GameDirector::~GameDirector() { // Libera los efectos de sonido - // Mix_FreeChunk(gPopBalloonFX); - // Mix_FreeChunk(gBulletFX); - // gPopBalloonFX = NULL; - // gBulletFX = NULL; JA_DeleteSound(gPopBalloonFX); JA_DeleteSound(gBulletFX); // Libra la música - // Mix_FreeMusic(gTitleMusic); - // gTitleMusic = NULL; - // Mix_FreeMusic(gPlayingMusic); - // gPlayingMusic = NULL; JA_DeleteMusic(gTitleMusic); JA_DeleteMusic(gPlayingMusic); @@ -95,91 +151,6 @@ GameDirector::~GameDirector() // 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()); - //} - - gTitleMusic = JA_LoadMusic("media/music/title.ogg"); - gPlayingMusic = JA_LoadMusic("media/music/playing.ogg"); - gPopBalloonFX = JA_LoadSound("media/sound/balloon.wav"); - gBulletFX = JA_LoadSound("media/sound/bullet.wav"); - - gGameBackgroundTexture = new LTexture(gRenderer); - // Carga los gráficos del fondo del juego - if (!gGameBackgroundTexture->loadFromFile("media/gfx/background.png")) - { - printf("Failed to load game background texture!\n"); - } - - gTitleBackgroundTexture = new LTexture(gRenderer); - // Carga los gráficos del fondo de la pantalla de titulo - if (!gTitleBackgroundTexture->loadFromFile("media/gfx/title.png")) - { - printf("Failed to load title texture!\n"); - } - - gMiscTexture = new LTexture(gRenderer); - // Carga varios gráficos para varios propósitos - if (!gMiscTexture->loadFromFile("media/gfx/misc.png")) - { - printf("Failed to load misc texture!\n"); - } - - gWhiteFontTexture = new LTexture(gRenderer); - // Carga los gráficos para el texto blanco - if (!gWhiteFontTexture->loadFromFile("media/gfx/white_font.png")) - { - printf("Failed to load white font texture!\n"); - } - - gBlackFontTexture = new LTexture(gRenderer); - // Carga los gráficos para el texto negro - if (!gBlackFontTexture->loadFromFile("media/gfx/black_font.png")) - { - printf("Failed to load black font texture!\n"); - } - - // 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; @@ -199,24 +170,12 @@ void GameDirector::init() // Establece a cero todos los valores del vector de objetos globo resetBalloons(); - // Crea dos objetos globo y los centra en el area de juego - // balloon[0].init(0, BLOCK, BALLOON_4, BALLON_VELX_POSITIVE, 0); - // balloon[0].allignTo(PLAY_AREA_WIDTH / 2); - // balloon[1].init(0, BLOCK, BALLOON_4, BALLON_VELX_NEGATIVE, 0); - // balloon[1].allignTo(PLAY_AREA_WIDTH / 2); - // Con los globos creados, calcula el nivel de amenaza calculateMenaceLevel(); // Establece a cero todos los valores del vector de objetos bala resetBullets(); -#ifdef TEST - balloonTest.init(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, BALLOON_4, 0); - balloonTest.stop(); - bulletTest.init(SCREEN_WIDTH / 4, SCREEN_HEIGHT / 2, BULLET_UP); -#endif - // Los fondos gameBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - (0 * BLOCK), gGameBackgroundTexture); titleBackground.init(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, gTitleBackgroundTexture); @@ -242,16 +201,6 @@ void GameDirector::init() menuPause->centerMenuOnScreen(); } -// Hace una pausa de milisegundos -void GameDirector::sleep(Uint16 time) -{ - Uint32 ticks = SDL_GetTicks(); - while (SDL_GetTicks() - ticks < time) - { - /* code */ - } -} - // Establece el valor de la variable void GameDirector::setScore(Uint32 score) { @@ -301,6 +250,7 @@ std::string GameDirector::updateScoreText(Uint32 num) case 100000 ... 999999: return (std::to_string(num)); break; + default: return (std::to_string(num)); break; @@ -681,7 +631,6 @@ void GameDirector::checkGameInput() setGameStatus(GAME_STATE_PAUSED); // Detiene la música - //Mix_HaltMusic(); JA_StopMusic(); } } @@ -707,88 +656,6 @@ void GameDirector::checkMenuInput(Menu *menu) { menu->checkInput(INPUT_FIRE); } - -#ifdef TEST - if (SDL_JoystickGetButton(gGameController, 1)) - { - std::cout << "button1\n"; - } - - if (SDL_JoystickGetButton(gGameController, 1)) - { - std::cout << "button1\n"; - } - - if (SDL_JoystickGetButton(gGameController, 2)) - { - std::cout << "button2\n"; - } - - if (SDL_JoystickGetButton(gGameController, 3)) - { - std::cout << "button3\n"; - } - - if (SDL_JoystickGetButton(gGameController, 4)) - { - std::cout << "button4\n"; - } - - if (SDL_JoystickGetButton(gGameController, 5)) - { - std::cout << "button5\n"; - } - - if (SDL_JoystickGetButton(gGameController, 6)) - { - std::cout << "button6\n"; - } - - if (SDL_JoystickGetButton(gGameController, 7)) - { - std::cout << "button7\n"; - } - - if (SDL_JoystickGetButton(gGameController, 8)) - { - std::cout << "button8\n"; - } - - if (SDL_JoystickGetButton(gGameController, 9)) - { - std::cout << "button9\n"; - } - - if (SDL_JoystickGetButton(gGameController, 10)) - { - std::cout << "button10\n"; - } - - if (SDL_JoystickGetButton(gGameController, 11)) - { - std::cout << "button11\n"; - } - - if (SDL_JoystickGetButton(gGameController, 12)) - { - std::cout << "button12\n"; - } - - if (SDL_JoystickGetButton(gGameController, 13)) - { - std::cout << "button13\n"; - } - - if (SDL_JoystickGetButton(gGameController, 14)) - { - std::cout << "button14\n"; - } - - if (SDL_JoystickGetButton(gGameController, 15)) - { - std::cout << "button15\n"; - } -#endif } // Obtiene el valor de la variable @@ -898,11 +765,9 @@ void GameDirector::renderGetReady() void GameDirector::runTitle() { // Si la música no está sonando - //if (Mix_PlayingMusic() == 0) if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { // Reproduce la música - //Mix_PlayMusic(gTitleMusic, -1); JA_PlayMusic(gTitleMusic); } @@ -937,17 +802,17 @@ void GameDirector::runTitle() setGameStatus(GAME_STATE_PLAYING); menuTitle->resetMenu(); renderTransition(1); - //Mix_HaltMusic(); JA_StopMusic(); SDL_Delay(1200); break; + case 1: setGameStatus(GAME_STATE_QUIT); menuTitle->resetMenu(); renderTransition(1); - //Mix_HaltMusic(); JA_StopMusic(); break; + default: break; } @@ -957,11 +822,9 @@ void GameDirector::runTitle() void GameDirector::runGame() { // Si la música no está sonando - //if (Mix_PlayingMusic() == 0) if ((JA_GetMusicState() == JA_MUSIC_INVALID) || (JA_GetMusicState() == JA_MUSIC_STOPPED)) { // Reproduce la música - //Mix_PlayMusic(gPlayingMusic, -1); JA_PlayMusic(gPlayingMusic); } @@ -991,39 +854,6 @@ void GameDirector::runGame() popBalloon(0); break; } -#ifdef TEST - // W key pressed - if (eventHandler.key.keysym.sym == SDLK_w) - { - bulletTest.setPosY(bulletTest.getPosY() - 1); - bulletTest.testMove(); - break; - } - - // S key pressed - if (eventHandler.key.keysym.sym == SDLK_s) - { - bulletTest.setPosY(bulletTest.getPosY() + 1); - bulletTest.testMove(); - break; - } - - // A key pressed - if (eventHandler.key.keysym.sym == SDLK_a) - { - bulletTest.setPosX(bulletTest.getPosX() - 1); - bulletTest.testMove(); - break; - } - - // D key pressed - if (eventHandler.key.keysym.sym == SDLK_d) - { - bulletTest.setPosX(bulletTest.getPosX() + 1); - bulletTest.testMove(); - break; - } -#endif } // Actualiza el jugador @@ -1032,10 +862,6 @@ void GameDirector::runGame() // Mueve los globos moveBalloons(); -#ifdef TEST - balloonTest.move(); -#endif - // Mueve las balas moveBullets(); @@ -1059,16 +885,6 @@ void GameDirector::runGame() // Dibuja los objetos gameBackground.render(); renderBalloons(); -#ifdef TEST - balloonTest.render(); - bulletTest.render(); - if (checkCollision(balloonTest.getCollider(), bulletTest.getCollider())) - { - whiteText.write(0, 0, "X"); - } -#endif - // whiteText.write(0, 0, std::to_string(mMenaceLevelThreshold)); - // whiteText.write(0, BLOCK, std::to_string(player->getPosX() + player->getWidth())); renderBullets(); player->render(); renderScoreBoard(whiteText); @@ -1112,12 +928,14 @@ void GameDirector::runPausedGame() setGameStatus(GAME_STATE_PLAYING); menuPause->resetMenu(); break; + case 1: setGameStatus(GAME_STATE_TITLE); menuPause->resetMenu(); renderTransition(1); init(); break; + default: break; } diff --git a/source/gamedirector.h b/source/gamedirector.h index 68c66df..8fe185a 100644 --- a/source/gamedirector.h +++ b/source/gamedirector.h @@ -27,9 +27,6 @@ public: // Iniciador void init(); - // Hace una pausa de milisegundos - void sleep(Uint16 time); - // Establece el valor de la variable void setScore(Uint32 score); diff --git a/source/main.cpp b/source/main.cpp index 11318f2..70c53e8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -35,7 +35,6 @@ Los objetos globo tienen varios contadores para alternar de un estado a otro. En los vectores que contienen objetos, se considera activos los objetos que tienen un tipo asociado diferente a NO_KIND */ -#define TEST_ #include "background.h" #include "balloon.h" @@ -78,7 +77,7 @@ bool init() JA_Init(44100, AUDIO_S16, 2); // Inicializa SDL - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO) < 0) + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); success = false; @@ -91,13 +90,6 @@ bool init() printf("Warning: Nearest texture filtering not enabled!"); } - // Inicializa SDL_mixer - //if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) - //{ - // printf("SDL_mixer could not initialize! SDL_mixer Error: %s\n", Mix_GetError()); - // success = false; - //} - // 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) @@ -121,14 +113,6 @@ bool init() // Establece el tamaño del buffer de renderizado SDL_RenderSetLogicalSize(gRenderer, SCREEN_WIDTH, SCREEN_HEIGHT); - - // Inicializa el cargador de PNG - /*int imgFlags = IMG_INIT_PNG; - if (!(IMG_Init(imgFlags) & imgFlags)) - { - printf("SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError()); - success = false; - }*/ } } } @@ -146,7 +130,6 @@ void close() gRenderer = NULL; // Sal del subsistema SDL - //IMG_Quit(); SDL_Quit(); } @@ -164,13 +147,6 @@ int main(int argc, char *args[]) // Crea el objeto gameDirector GameDirector gameDirector(gRenderer); - // Inicializa el objeto gameDirector - gameDirector.init(); - -#ifdef TEST - gameDirector.resetBalloons(); -#endif - // Mientras no se quiera salir del juego while (!(gameDirector.getGameStatus() == GAME_STATE_QUIT)) {