#include "director.h" #include "utils.h" #include "const.h" #include "options.h" #include #include #include #include #include #include "dbgtxt.h" #ifndef _WIN32 #include #endif // Constructor Director::Director(int argc, char *argv[]) { // Inicializa variables section = new section_t(); #ifdef RECORDING section->name = SECTION_PROG_GAME; section->options = SECTION_OPTIONS_GAME_PLAY_1P; #elif DEBUG section->name = SECTION_PROG_LOGO; #else section->name = SECTION_PROG_LOGO; #endif // Comprueba los parametros del programa checkProgramArguments(argc, argv); // Crea la carpeta del sistema donde guardar datos createSystemFolder("jailgames"); createSystemFolder("jailgames/coffee_crisis_arcade_edition"); // Crea el objeto que controla los ficheros de recursos asset = new Asset(executablePath); // Si falta algún fichero no inicia el programa if (!setFileList()) { exit(EXIT_FAILURE); } // Carga el fichero de configuración loadOptionsFile(asset->get("config.txt")); // Carga los parametros para configurar el juego #ifdef ANBERNIC const std::string paramFilePath = asset->get("param_320x240.txt"); #else const std::string paramFilePath = paramFileArgument == "--320x240" ? asset->get("param_320x240.txt") : asset->get("param_320x256.txt"); #endif loadParams(paramFilePath); // Carga el fichero de puntuaciones ManageHiScoreTable *manager = new ManageHiScoreTable(&options.game.hiScoreTable); manager->loadFromFile(asset->get("score.bin")); delete manager; // Inicializa SDL initSDL(); // Inicializa JailAudio initJailAudio(); // Inicializa el texto de debug dbg_init(renderer); // Crea los objetos lang::loadFromFile(getLangFile((lang::lang_e)options.game.language)); input = new Input(asset->get("gamecontrollerdb.txt")); initInput(); screen = new Screen(window, renderer, asset, input); // Carga los sonidos del juego loadSounds(); // Carga las musicas del juego loadMusics(); } Director::~Director() { saveOptionsFile(asset->get("config.txt")); delete asset; delete input; delete screen; delete section; deleteSounds(); deleteMusics(); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } // Inicializa el objeto input void Director::initInput() { // Establece si ha de mostrar mensajes #ifdef VERBOSE input->setVerbose(true); #else input->setVerbose(options.console); #endif // Busca si hay mandos conectados input->discoverGameControllers(); // Teclado - Movimiento del jugador input->bindKey(input_up, SDL_SCANCODE_UP); input->bindKey(input_down, SDL_SCANCODE_DOWN); input->bindKey(input_left, SDL_SCANCODE_LEFT); input->bindKey(input_right, SDL_SCANCODE_RIGHT); input->bindKey(input_fire_left, SDL_SCANCODE_Q); input->bindKey(input_fire_center, SDL_SCANCODE_W); input->bindKey(input_fire_right, SDL_SCANCODE_E); input->bindKey(input_start, SDL_SCANCODE_RETURN); // Teclado - Control del programa input->bindKey(input_exit, SDL_SCANCODE_ESCAPE); input->bindKey(input_pause, SDL_SCANCODE_P); input->bindKey(input_window_dec_size, SDL_SCANCODE_F1); input->bindKey(input_window_inc_size, SDL_SCANCODE_F2); input->bindKey(input_window_fullscreen, SDL_SCANCODE_F3); input->bindKey(input_video_shaders, SDL_SCANCODE_F4); input->bindKey(input_mute, SDL_SCANCODE_F5); input->bindKey(input_showinfo, SDL_SCANCODE_F6); input->bindKey(input_reset, SDL_SCANCODE_F10); // Asigna botones a inputs const int numGamePads = input->getNumControllers(); for (int i = 0; i < numGamePads; ++i) { // Mando - Movimiento del jugador input->bindGameControllerButton(i, input_up, SDL_CONTROLLER_BUTTON_DPAD_UP); input->bindGameControllerButton(i, input_down, SDL_CONTROLLER_BUTTON_DPAD_DOWN); input->bindGameControllerButton(i, input_left, SDL_CONTROLLER_BUTTON_DPAD_LEFT); input->bindGameControllerButton(i, input_right, SDL_CONTROLLER_BUTTON_DPAD_RIGHT); input->bindGameControllerButton(i, input_fire_left, SDL_CONTROLLER_BUTTON_X); input->bindGameControllerButton(i, input_fire_center, SDL_CONTROLLER_BUTTON_Y); input->bindGameControllerButton(i, input_fire_right, SDL_CONTROLLER_BUTTON_B); input->bindGameControllerButton(i, input_start, SDL_CONTROLLER_BUTTON_START); // Mando - Control del programa input->bindGameControllerButton(i, input_service, SDL_CONTROLLER_BUTTON_BACK); input->bindGameControllerButton(i, input_exit, input_start); input->bindGameControllerButton(i, input_pause, input_fire_right); input->bindGameControllerButton(i, input_video_shaders, input_fire_left); input->bindGameControllerButton(i, input_mute, input_left); input->bindGameControllerButton(i, input_showinfo, input_right); input->bindGameControllerButton(i, input_reset, input_fire_center); input->bindGameControllerButton(i, input_config, input_down); input->bindGameControllerButton(i, input_swap_controllers, input_up); } // Mapea las asignaciones a los botones desde el archivo de configuración, si se da el caso for (int i = 0; i < numGamePads; ++i) for (int index = 0; index < (int)options.controller.size(); ++index) if (input->getControllerName(i) == options.controller[index].name) for (int j = 0; j < (int)options.controller[index].inputs.size(); ++j) { input->bindGameControllerButton(i, options.controller[index].inputs[j], options.controller[index].buttons[j]); } // Asigna botones a inputs desde otros inputs for (int i = 0; i < numGamePads; ++i) { input->bindGameControllerButton(i, input_exit, input_start); input->bindGameControllerButton(i, input_reset, input_fire_center); input->bindGameControllerButton(i, input_pause, input_fire_right); input->bindGameControllerButton(i, input_video_shaders, input_fire_left); input->bindGameControllerButton(i, input_mute, input_left); input->bindGameControllerButton(i, input_showinfo, input_right); input->bindGameControllerButton(i, input_config, input_down); input->bindGameControllerButton(i, input_swap_controllers, input_up); } // Guarda las asignaciones de botones en las opciones for (int i = 0; i < numGamePads; ++i) { options.controller[i].name = input->getControllerName(i); for (int j = 0; j < (int)options.controller[i].inputs.size(); ++j) { options.controller[i].buttons[j] = input->getControllerBinding(i, options.controller[i].inputs[j]); } } } // Inicializa JailAudio void Director::initJailAudio() { JA_Init(48000, AUDIO_S16, 2); JA_EnableMusic(options.audio.music.enabled); JA_EnableSound(options.audio.sound.enabled); JA_SetMusicVolume(options.audio.music.volume); JA_SetSoundVolume(options.audio.sound.volume); } // Arranca SDL y crea la ventana bool Director::initSDL() { // Indicador de éxito bool success = true; // Inicializa SDL if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { if (options.console) { std::cout << "SDL could not initialize!\nSDL Error: " << SDL_GetError() << std::endl; } success = false; } else { // Inicia el generador de numeros aleatorios std::srand(static_cast(SDL_GetTicks())); // Muestra información de la pantalla if (options.console) { /*std::cout << "\nDisplay modes list:" << std::endl; for (int i = 0; i < SDL_GetNumDisplayModes(0); ++i) { SDL_DisplayMode DM; SDL_GetDisplayMode(0,i,&DM); std::cout << " - " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl; }*/ SDL_DisplayMode DM; SDL_GetCurrentDisplayMode(0, &DM); std::cout << "\nCurrent display mode: " + std::to_string(DM.w) + "x" + std::to_string(DM.h) + " @ " + std::to_string(DM.refresh_rate) + "Hz" << std::endl; std::cout << "Window resolution : " + std::to_string(param.game.width) + "x" + std::to_string(param.game.height) + " x" + std::to_string(options.video.window.size) << std::endl; } // Establece el filtro de la textura if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, std::to_string(options.video.filter).c_str())) { if (options.console) { std::cout << "Warning: texture filtering not enabled!\n"; } } #ifndef NO_SHADERS if (!SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl")) { if (options.console) { std::cout << "Warning: opengl not enabled!\n"; } } #endif // Crea la ventana window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, param.game.width * options.video.window.size, param.game.height * options.video.window.size, SDL_WINDOW_HIDDEN); if (window == nullptr) { if (options.console) { std::cout << "Window could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } success = false; } else { // Crea un renderizador para la ventana. El vsync se activa en funcion de las opciones Uint32 flags = 0; if (options.video.vSync) { flags = flags | SDL_RENDERER_PRESENTVSYNC; } #ifndef NO_SHADERS // La aceleración se activa según el define flags = flags | SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE; #endif renderer = SDL_CreateRenderer(window, -1, flags); if (renderer == nullptr) { if (options.console) { std::cout << "Renderer could not be created!\nSDL Error: " << SDL_GetError() << std::endl; } success = false; } else { // Inicializa el color de renderizado SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); // Establece el tamaño del buffer de renderizado SDL_RenderSetLogicalSize(renderer, param.game.width, param.game.height); // Establece el modo de mezcla SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); } } } if (options.console) { std::cout << std::endl; } return success; } // Crea el indice de ficheros bool Director::setFileList() { #ifdef MACOS_BUNDLE const std::string prefix = "/../Resources"; #else const std::string prefix = ""; #endif // Ficheros de configuración asset->add(systemFolder + "/config.txt", t_data, false, true); asset->add(systemFolder + "/score.bin", t_data, false, true); asset->add(prefix + "/data/config/param_320x240.txt", t_data); asset->add(prefix + "/data/config/param_320x256.txt", t_data); asset->add(prefix + "/data/config/demo1.bin", t_data); asset->add(prefix + "/data/config/demo2.bin", t_data); asset->add(prefix + "/data/config/gamecontrollerdb.txt", t_data); // Notificaciones asset->add(prefix + "/data/notification/notify.png", t_bitmap); // Musicas asset->add(prefix + "/data/music/intro.ogg", t_music); asset->add(prefix + "/data/music/playing.ogg", t_music); asset->add(prefix + "/data/music/title.ogg", t_music); // Sonidos asset->add(prefix + "/data/sound/balloon.wav", t_sound); asset->add(prefix + "/data/sound/bubble1.wav", t_sound); asset->add(prefix + "/data/sound/bubble2.wav", t_sound); asset->add(prefix + "/data/sound/bubble3.wav", t_sound); asset->add(prefix + "/data/sound/bubble4.wav", t_sound); asset->add(prefix + "/data/sound/bullet.wav", t_sound); asset->add(prefix + "/data/sound/coffeeout.wav", t_sound); asset->add(prefix + "/data/sound/hiscore.wav", t_sound); asset->add(prefix + "/data/sound/itemdrop.wav", t_sound); asset->add(prefix + "/data/sound/itempickup.wav", t_sound); asset->add(prefix + "/data/sound/player_collision.wav", t_sound); asset->add(prefix + "/data/sound/stage_change.wav", t_sound); asset->add(prefix + "/data/sound/title.wav", t_sound); asset->add(prefix + "/data/sound/clock.wav", t_sound); asset->add(prefix + "/data/sound/powerball.wav", t_sound); asset->add(prefix + "/data/sound/notify.wav", t_sound); // Shaders asset->add(prefix + "/data/shaders/crtpi.glsl", t_data); // Texturas asset->add(prefix + "/data/gfx/balloon1.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon1.ani", t_animation); asset->add(prefix + "/data/gfx/balloon2.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon2.ani", t_animation); asset->add(prefix + "/data/gfx/balloon3.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon3.ani", t_animation); asset->add(prefix + "/data/gfx/balloon4.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon4.ani", t_animation); asset->add(prefix + "/data/gfx/explosion1.png", t_bitmap); asset->add(prefix + "/data/gfx/explosion1.ani", t_animation); asset->add(prefix + "/data/gfx/explosion2.png", t_bitmap); asset->add(prefix + "/data/gfx/explosion2.ani", t_animation); asset->add(prefix + "/data/gfx/explosion3.png", t_bitmap); asset->add(prefix + "/data/gfx/explosion3.ani", t_animation); asset->add(prefix + "/data/gfx/explosion4.png", t_bitmap); asset->add(prefix + "/data/gfx/explosion4.ani", t_animation); asset->add(prefix + "/data/gfx/powerball.png", t_bitmap); asset->add(prefix + "/data/gfx/powerball.ani", t_animation); asset->add(prefix + "/data/gfx/bullet.png", t_bitmap); asset->add(prefix + "/data/gfx/game_buildings.png", t_bitmap); asset->add(prefix + "/data/gfx/game_clouds1.png", t_bitmap); asset->add(prefix + "/data/gfx/game_clouds2.png", t_bitmap); asset->add(prefix + "/data/gfx/game_grass.png", t_bitmap); asset->add(prefix + "/data/gfx/game_power_meter.png", t_bitmap); asset->add(prefix + "/data/gfx/game_sky_colors.png", t_bitmap); asset->add(prefix + "/data/gfx/game_text.png", t_bitmap); asset->add(prefix + "/data/gfx/intro.png", t_bitmap); asset->add(prefix + "/data/gfx/logo_jailgames.png", t_bitmap); asset->add(prefix + "/data/gfx/logo_jailgames_mini.png", t_bitmap); asset->add(prefix + "/data/gfx/logo_since_1998.png", t_bitmap); asset->add(prefix + "/data/gfx/item_points1_disk.png", t_bitmap); asset->add(prefix + "/data/gfx/item_points1_disk.ani", t_animation); asset->add(prefix + "/data/gfx/item_points2_gavina.png", t_bitmap); asset->add(prefix + "/data/gfx/item_points2_gavina.ani", t_animation); asset->add(prefix + "/data/gfx/item_points3_pacmar.png", t_bitmap); asset->add(prefix + "/data/gfx/item_points3_pacmar.ani", t_animation); asset->add(prefix + "/data/gfx/item_clock.png", t_bitmap); asset->add(prefix + "/data/gfx/item_clock.ani", t_animation); asset->add(prefix + "/data/gfx/item_coffee.png", t_bitmap); asset->add(prefix + "/data/gfx/item_coffee.ani", t_animation); asset->add(prefix + "/data/gfx/item_coffee_machine.png", t_bitmap); asset->add(prefix + "/data/gfx/item_coffee_machine.ani", t_animation); asset->add(prefix + "/data/gfx/title_bg_tile.png", t_bitmap); asset->add(prefix + "/data/gfx/title_coffee.png", t_bitmap); asset->add(prefix + "/data/gfx/title_crisis.png", t_bitmap); asset->add(prefix + "/data/gfx/title_arcade_edition.png", t_bitmap); asset->add(prefix + "/data/gfx/title_dust.png", t_bitmap); asset->add(prefix + "/data/gfx/title_dust.ani", t_animation); asset->add(prefix + "/data/gfx/player1.gif", t_bitmap); asset->add(prefix + "/data/gfx/player1_pal1.gif", t_palette); asset->add(prefix + "/data/gfx/player1_pal2.gif", t_palette); asset->add(prefix + "/data/gfx/player1_pal3.gif", t_palette); asset->add(prefix + "/data/gfx/player2.gif", t_bitmap); asset->add(prefix + "/data/gfx/player2_pal1.gif", t_palette); asset->add(prefix + "/data/gfx/player2_pal2.gif", t_palette); asset->add(prefix + "/data/gfx/player2_pal3.gif", t_palette); asset->add(prefix + "/data/gfx/player.ani", t_animation); asset->add(prefix + "/data/gfx/player_power.gif", t_bitmap); asset->add(prefix + "/data/gfx/player_power_pal.gif", t_palette); asset->add(prefix + "/data/gfx/player_power.ani", t_animation); // Fuentes de texto asset->add(prefix + "/data/font/8bithud.png", t_font); asset->add(prefix + "/data/font/8bithud.txt", t_font); asset->add(prefix + "/data/font/nokia.png", t_font); asset->add(prefix + "/data/font/nokia_big2.png", t_font); asset->add(prefix + "/data/font/nokia.txt", t_font); asset->add(prefix + "/data/font/nokia2.png", t_font); asset->add(prefix + "/data/font/nokia2.txt", t_font); asset->add(prefix + "/data/font/nokia_big2.txt", t_font); asset->add(prefix + "/data/font/smb2_big.png", t_font); asset->add(prefix + "/data/font/smb2_big.txt", t_font); asset->add(prefix + "/data/font/smb2.gif", t_font); asset->add(prefix + "/data/font/smb2_pal1.gif", t_palette); asset->add(prefix + "/data/font/smb2.txt", t_font); // Textos asset->add(prefix + "/data/lang/es_ES.txt", t_lang); asset->add(prefix + "/data/lang/en_UK.txt", t_lang); asset->add(prefix + "/data/lang/ba_BA.txt", t_lang); return asset->check(); } // Carga los parametros para configurar el juego void Director::loadParams(std::string filepath) { loadParamsFromFile(filepath); // Modifica las opciones desde el fichero de parametros options.video.window.width = options.video.window.size * param.game.width; options.video.window.height = options.video.window.size * param.game.height; options.video.gameWidth = param.game.width; options.video.gameHeight = param.game.height; } // Comprueba los parametros del programa void Director::checkProgramArguments(int argc, char *argv[]) { // Establece la ruta del programa executablePath = argv[0]; // Valores por defecto paramFileArgument = ""; // Comprueba el resto de parametros for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "--console") == 0) { options.console = true; } if (strcmp(argv[i], "--320x240") == 0) { paramFileArgument = argv[i]; } } } // Crea la carpeta del sistema donde guardar datos void Director::createSystemFolder(std::string folder) { #ifdef _WIN32 systemFolder = std::string(getenv("APPDATA")) + "/" + folder; #elif __APPLE__ struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; systemFolder = std::string(homedir) + "/Library/Application Support" + "/" + folder; #elif __linux__ struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; systemFolder = std::string(homedir) + "/." + folder; #endif struct stat st = {0}; if (stat(systemFolder.c_str(), &st) == -1) { errno = 0; #ifdef _WIN32 int ret = mkdir(systemFolder.c_str()); #else int ret = mkdir(systemFolder.c_str(), S_IRWXU); #endif if (ret == -1) { switch (errno) { case EACCES: printf("the parent directory does not allow write"); exit(EXIT_FAILURE); case EEXIST: printf("pathname already exists"); exit(EXIT_FAILURE); case ENAMETOOLONG: printf("pathname is too long"); exit(EXIT_FAILURE); default: perror("mkdir"); exit(EXIT_FAILURE); } } } } // Carga los sonidos del juego void Director::loadSounds() { // Obtiene la lista con las rutas a los ficheros de sonidos std::vector list = asset->getListByType(t_sound); sounds.clear(); for (auto l : list) { const size_t lastIndex = l.find_last_of("/") + 1; const std::string name = l.substr(lastIndex, std::string::npos); sound_file_t temp; temp.name = name; // Añade el nombre del fichero temp.file = JA_LoadSound(l.c_str()); // Carga el fichero de audio sounds.push_back(temp); } } // Carga las musicas del juego void Director::loadMusics() { // Obtiene la lista con las rutas a los ficheros musicales std::vector list = asset->getListByType(t_music); musics.clear(); for (auto l : list) { const size_t lastIndex = l.find_last_of("/") + 1; const std::string name = l.substr(lastIndex, std::string::npos); music_file_t temp; temp.name = name; // Añade el nombre del fichero temp.file = JA_LoadMusic(l.c_str()); // Carga el fichero de audio musics.push_back(temp); } } // Libera la memoria usada por los sonidos del juego void Director::deleteSounds() { for (auto s : sounds) { JA_DeleteSound(s.file); } sounds.clear(); } // Libera la memoria usada por las músicas del juego void Director::deleteMusics() { for (auto m : musics) { JA_DeleteMusic(m.file); } musics.clear(); } // Ejecuta la sección con el logo void Director::runLogo() { logo = new Logo(screen, asset, input, section); logo->run(); delete logo; } // Ejecuta la sección con la secuencia de introducción void Director::runIntro() { intro = new Intro(screen, asset, input, section, getMusic(musics, "intro.ogg")); intro->run(); delete intro; } // Ejecuta la sección con el titulo del juego void Director::runTitle() { title = new Title(screen, asset, input, section, getMusic(musics, "title.ogg")); title->run(); delete title; } // Ejecuta la sección donde se juega al juego void Director::runGame() { const int playerID = section->options; const int currentStage = 0; game = new Game(playerID, currentStage, GAME_MODE_DEMO_OFF, screen, asset, input, section, getMusic(musics, "playing.ogg")); game->run(); delete game; } // Ejecuta la sección donde se muestran las instrucciones void Director::runInstructions() { instructions = new Instructions(screen, asset, input, section, getMusic(musics, "title.ogg")); instructions->run(); delete instructions; } // Ejecuta la sección donde se muestra la tabla de puntuaciones void Director::runHiScoreTable() { hiScoreTable = new HiScoreTable(screen, asset, input, section, getMusic(musics, "title.ogg")); hiScoreTable->run(); delete hiScoreTable; } // Ejecuta el juego en modo demo void Director::runDemoGame() { const int playerID = (rand() % 2) + 1; const int currentStage = 0; demoGame = new Game(playerID, currentStage, GAME_MODE_DEMO_ON, screen, asset, input, section, nullptr); demoGame->run(); delete demoGame; } int Director::run() { // Bucle principal while (section->name != SECTION_PROG_QUIT) { switch (section->name) { case SECTION_PROG_INIT: section->name = SECTION_PROG_LOGO; break; case SECTION_PROG_LOGO: runLogo(); break; case SECTION_PROG_INTRO: runIntro(); break; case SECTION_PROG_TITLE: runTitle(); break; case SECTION_PROG_GAME: runGame(); break; case SECTION_PROG_HI_SCORE_TABLE: runHiScoreTable(); break; case SECTION_PROG_GAME_DEMO: runDemoGame(); break; case SECTION_PROG_INSTRUCTIONS: runInstructions(); break; } } const int returnCode = section->options == SECTION_OPTIONS_QUIT_NORMAL ? 0 : 1; return returnCode; } // Obtiene una fichero a partir de un lang_e std::string Director::getLangFile(lang::lang_e lang) { switch (lang) { case lang::ba_BA: return asset->get("ba_BA.txt"); break; case lang::es_ES: return asset->get("es_ES.txt"); break; case lang::en_UK: return asset->get("en_UK.txt"); break; default: break; } return asset->get("en_UK.txt"); }