500 lines
11 KiB
C++
500 lines
11 KiB
C++
#include "const.h"
|
|
#include "struct.h"
|
|
#include "game.h"
|
|
#include <iostream>
|
|
|
|
const Uint8 *keystates;
|
|
|
|
// Constructor
|
|
Game::Game()
|
|
{
|
|
}
|
|
|
|
Game::~Game()
|
|
{
|
|
}
|
|
|
|
// Iniciador
|
|
void Game::init()
|
|
{
|
|
}
|
|
|
|
// Arranca SDL y crea la ventana
|
|
bool Game::initSDL()
|
|
{
|
|
// Indicador de inicialización
|
|
bool success = true;
|
|
|
|
// Inicializa SDL
|
|
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_HAPTIC) < 0)
|
|
{
|
|
printf("SDL could not initialize!\nSDL Error: %s\n", SDL_GetError());
|
|
success = false;
|
|
}
|
|
else
|
|
{
|
|
// Establece el filtro de la textura a nearest
|
|
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0"))
|
|
{
|
|
printf("Warning: Nearest texture filtering not enabled!\n");
|
|
}
|
|
|
|
// Inicializa jail_audio
|
|
JA_Init(48000, AUDIO_S16, 2);
|
|
|
|
// Comprueba si hay algun mando conectado
|
|
if (SDL_NumJoysticks() < 1)
|
|
{
|
|
printf("Warning: No joysticks connected!\n");
|
|
mGameControllerFound = false;
|
|
}
|
|
else
|
|
{
|
|
// Carga el mando
|
|
mGameController = SDL_JoystickOpen(0);
|
|
mGameControllerFound = true;
|
|
|
|
if (mGameController == NULL)
|
|
{
|
|
printf("Warning: Unable to open game controller!\nSDL Error: %s\n", SDL_GetError());
|
|
mGameControllerFound = false;
|
|
}
|
|
else
|
|
{
|
|
printf("%i joysticks were found.\n", SDL_NumJoysticks());
|
|
std::cout << SDL_JoystickNumButtons(mGameController) << " buttons\n";
|
|
|
|
//Get controller haptic device
|
|
mControllerHaptic = SDL_HapticOpenFromJoystick(mGameController);
|
|
if (mControllerHaptic == NULL)
|
|
{
|
|
printf("Warning: Controller does not support haptics!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
else
|
|
{
|
|
printf("Haptics detected\n");
|
|
|
|
//Get initialize rumble
|
|
if (SDL_HapticRumbleInit(mControllerHaptic) < 0)
|
|
{
|
|
printf("Warning: Unable to initialize rumble!\nSDL Error: %s\n", SDL_GetError());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Crea la ventana
|
|
mWindow = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, VIEW_WIDTH, VIEW_HEIGHT, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
|
|
if (mWindow == NULL)
|
|
{
|
|
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
success = false;
|
|
}
|
|
else
|
|
{
|
|
// Crea un renderizador para la ventana con vsync
|
|
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
if (mRenderer == NULL)
|
|
{
|
|
printf("Renderer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
success = false;
|
|
}
|
|
else
|
|
{
|
|
// Inicializa el color de renderizado
|
|
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
|
|
|
|
// Establece el tamaño del buffer de renderizado
|
|
SDL_RenderSetLogicalSize(mRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
|
|
// Establece el modo de mezcla
|
|
SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_BLEND);
|
|
}
|
|
|
|
// Crea un backbuffer para el renderizador
|
|
mBackbuffer = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
|
if (mBackbuffer == NULL)
|
|
{
|
|
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
|
|
success = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
printf("\n");
|
|
return success;
|
|
}
|
|
|
|
// Crea el indice de ficheros
|
|
void Game::setFileList()
|
|
{
|
|
// Ficheros binarios
|
|
mResource.file[FILE_MAP_VOLCANO].file = mGame.path + "/" + "../data/volcano.map";
|
|
mResource.file[FILE_CONFIG].file = mGame.path + "/" + "../data/config.bin";
|
|
|
|
// Texturas
|
|
mResource.texture[TEXTURE_ACTORS].file = mGame.path + "/" + "../media/gfx/actors.png";
|
|
mResource.texture[TEXTURE_BKG_SURFACE].file = mGame.path + "/" + "../media/gfx/bkg_surface.png";
|
|
mResource.texture[TEXTURE_FILTER].file = mGame.path + "/" + "../media/gfx/filter.png";
|
|
mResource.texture[TEXTURE_HUD].file = mGame.path + "/" + "../media/gfx/hud.png";
|
|
mResource.texture[TEXTURE_MENU_ANIMATION].file = mGame.path + "/" + "../media/gfx/menu_animation.png";
|
|
mResource.texture[TEXTURE_MENU].file = mGame.path + "/" + "../media/gfx/menu.png";
|
|
mResource.texture[TEXTURE_PLAYER].file = mGame.path + "/" + "../media/gfx/player.png";
|
|
mResource.texture[TEXTURE_TILES_SURFACE].file = mGame.path + "/" + "../media/gfx/tiles_surface.png";
|
|
mResource.texture[TEXTURE_TILES_VOLCANO].file = mGame.path + "/" + "../media/gfx/tiles_volcano.png";
|
|
|
|
// Sonidos
|
|
mResource.sound[SOUND_COIN].file = mGame.path + "/" + "../media/sound/sound_player_coin.wav";
|
|
mResource.sound[SOUND_DEATH].file = mGame.path + "/" + "../media/sound/sound_player_death.wav";
|
|
mResource.sound[SOUND_DROP_ENEMY].file = mGame.path + "/" + "../media/sound/sound_drop_enemy.wav";
|
|
mResource.sound[SOUND_DROP_SPLAT].file = mGame.path + "/" + "../media/sound/sound_drop_splat.wav";
|
|
mResource.sound[SOUND_JUMP].file = mGame.path + "/" + "../media/sound/sound_player_jump.wav";
|
|
mResource.sound[SOUND_MENU_LOGO].file = mGame.path + "/" + "../media/sound/sound_menu_logo.wav";
|
|
mResource.sound[SOUND_MENU_START].file = mGame.path + "/" + "../media/sound/sound_menu_start.wav";
|
|
|
|
// Musicas
|
|
mResource.music[MUSIC_MENU].file = mGame.path + "/" + "../media/music/music_menu.ogg";
|
|
mResource.music[MUSIC_SURFACE].file = mGame.path + "/" + "../media/music/music_surface.ogg";
|
|
mResource.music[MUSIC_VOLCANO].file = mGame.path + "/" + "../media/music/music_volcano.ogg";
|
|
}
|
|
|
|
// Comprueba que todos los ficheros existen
|
|
bool Game::checkFileList()
|
|
{
|
|
bool success = true;
|
|
std::string p;
|
|
std::string filename;
|
|
SDL_RWops *file;
|
|
|
|
// Comprueba los ficheros de musica
|
|
printf("\n>> MUSIC FILES\n");
|
|
for (Uint8 i = 0; i < TOTAL_MUSIC; i++)
|
|
{
|
|
p = mResource.music[i].file.c_str();
|
|
filename = p.substr(p.find_last_of("\\/") + 1);
|
|
file = SDL_RWFromFile(p.c_str(), "r+b");
|
|
if (file != NULL)
|
|
{
|
|
printf("Checking file %-20s [OK]\n", filename.c_str());
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
|
}
|
|
SDL_RWclose(file);
|
|
}
|
|
|
|
// Comprueba los ficheros de sonidos
|
|
printf("\n>> SOUND FILES\n");
|
|
for (Uint8 i = 0; i < TOTAL_SOUND; i++)
|
|
{
|
|
p = mResource.sound[i].file.c_str();
|
|
filename = p.substr(p.find_last_of("\\/") + 1);
|
|
file = SDL_RWFromFile(p.c_str(), "r+b");
|
|
if (file != NULL)
|
|
{
|
|
printf("Checking file %-20s [OK]\n", filename.c_str());
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
|
}
|
|
SDL_RWclose(file);
|
|
}
|
|
|
|
// Comprueba los ficheros con texturas
|
|
printf("\n>> TEXTURE FILES\n");
|
|
for (Uint8 i = 0; i < TOTAL_TEXTURE; i++)
|
|
{
|
|
p = mResource.texture[i].file.c_str();
|
|
filename = p.substr(p.find_last_of("\\/") + 1);
|
|
file = SDL_RWFromFile(p.c_str(), "r+b");
|
|
if (file != NULL)
|
|
{
|
|
printf("Checking file %-20s [OK]\n", filename.c_str());
|
|
}
|
|
else
|
|
{
|
|
success = false;
|
|
printf("Checking file %-20s [ERROR]\n", filename.c_str());
|
|
}
|
|
SDL_RWclose(file);
|
|
}
|
|
|
|
// Resultado
|
|
if (success)
|
|
{
|
|
printf("\n** All files OK.\n\n");
|
|
}
|
|
else
|
|
{
|
|
printf("\n** File is missing. Exiting.\n\n");
|
|
}
|
|
return success;
|
|
}
|
|
|
|
// Carga un archivo de imagen en una textura
|
|
bool Game::loadTextureFromFile(LTexture *texture, std::string path, SDL_Renderer *renderer)
|
|
{
|
|
bool success = true;
|
|
if (!texture->loadFromFile(path, renderer))
|
|
{
|
|
printf("Failed to load %s texture!\n", path.c_str());
|
|
success = false;
|
|
}
|
|
return success;
|
|
}
|
|
|
|
// Carga los recursos necesarios
|
|
bool Game::loadMedia(Uint8 section)
|
|
{
|
|
// Indicador de éxito en la carga
|
|
bool success = true;
|
|
std::string path = mGame.path + "/";
|
|
std::string p;
|
|
|
|
switch (section)
|
|
{
|
|
case GAME_SECTION_INIT:
|
|
{
|
|
p = mResource.file[FILE_CONFIG].file.c_str();
|
|
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
|
filename = p.substr(p.find_last_of("\\/") + 1);
|
|
|
|
// Abre el fichero con la configuracion de las opciones para leer en binario
|
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "r+b");
|
|
|
|
// El fichero no existe
|
|
if (file == NULL)
|
|
{
|
|
printf("Warning: Unable to open %s file\n", filename.c_str());
|
|
|
|
// Crea el fichero para escribir
|
|
file = SDL_RWFromFile(p.c_str(), "w+b");
|
|
if (file != NULL)
|
|
{
|
|
printf("New file (%s) created!\n", filename.c_str());
|
|
|
|
// Inicializa los datos
|
|
mGame.options.fullScreenMode = 0;
|
|
SDL_RWwrite(file, &mGame.options.fullScreenMode, sizeof(mGame.options.fullScreenMode), 1);
|
|
|
|
mGame.options.windowSize = 3;
|
|
SDL_RWwrite(file, &mGame.options.windowSize, sizeof(mGame.options.windowSize), 1);
|
|
|
|
// Cierra el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
else
|
|
{
|
|
printf("Error: Unable to create file %s\n", filename.c_str());
|
|
success = false;
|
|
}
|
|
}
|
|
// El fichero existe
|
|
else
|
|
{
|
|
// Carga los datos
|
|
printf("Reading file %s\n", filename.c_str());
|
|
SDL_RWread(file, &mGame.options.fullScreenMode, sizeof(mGame.options.fullScreenMode), 1);
|
|
SDL_SetWindowFullscreen(mWindow, mGame.options.fullScreenMode);
|
|
SDL_RWread(file, &mGame.options.windowSize, sizeof(mGame.options.windowSize), 1);
|
|
SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mGame.options.windowSize, SCREEN_HEIGHT * mGame.options.windowSize);
|
|
|
|
// Cierra el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
printf("\n");
|
|
|
|
// Texturas
|
|
|
|
// Sonidos
|
|
|
|
// Musicas
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_TITLE:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_PLAYING:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_GAME_OVER_SCREEN:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_INTRO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_DEMO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_INSTRUCTIONS:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_LOGO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
default:
|
|
{
|
|
}
|
|
break;
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
// Descrga los recursos necesarios
|
|
bool Game::unLoadMedia(Uint8 section)
|
|
{
|
|
// Indicador de éxito en la carga
|
|
bool success = true;
|
|
std::string path = mGame.path + "/";
|
|
std::string p;
|
|
|
|
switch (section)
|
|
{
|
|
case GAME_SECTION_INIT:
|
|
{
|
|
p = mResource.file[FILE_CONFIG].file;
|
|
std::string filename = p.substr(p.find_last_of("\\/") + 1);
|
|
|
|
// Abre el fichero de puntuación para escribir
|
|
SDL_RWops *file = SDL_RWFromFile(p.c_str(), "w+b");
|
|
|
|
if (file != NULL)
|
|
{
|
|
// Guardamos los datos
|
|
SDL_RWwrite(file, &mGame.options.fullScreenMode, sizeof(Uint32), 1);
|
|
SDL_RWwrite(file, &mGame.options.windowSize, sizeof(Uint8), 1);
|
|
|
|
printf("Writing file %s\n", filename.c_str());
|
|
|
|
// Cerramos el fichero
|
|
SDL_RWclose(file);
|
|
}
|
|
else
|
|
{
|
|
printf("Error: Unable to save %s file! %s\n", filename.c_str(), SDL_GetError());
|
|
}
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_TITLE:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_PLAYING:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_GAME_OVER_SCREEN:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_INTRO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_DEMO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_INSTRUCTIONS:
|
|
{
|
|
}
|
|
break;
|
|
|
|
case GAME_SECTION_LOGO:
|
|
{
|
|
}
|
|
break;
|
|
|
|
default:
|
|
{
|
|
}
|
|
break;
|
|
}
|
|
|
|
return success;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Game::setPath(std::string path)
|
|
{
|
|
mGame.path = path.substr(0, path.find_last_of("\\/"));
|
|
}
|
|
|
|
// Obtiene el valor de la variable
|
|
Uint8 Game::getGameSection()
|
|
{
|
|
return mGame.section;
|
|
}
|
|
|
|
// Establece el valor de la variable
|
|
void Game::setGameSection(Uint8 section)
|
|
{
|
|
mGame.section = section;
|
|
}
|
|
|
|
// Cambia el valor de la variable de modo de pantalla completa
|
|
void Game::changeFullScreenMode()
|
|
{
|
|
switch (mGame.options.fullScreenMode)
|
|
{
|
|
case 0:
|
|
mGame.options.fullScreenMode = SDL_WINDOW_FULLSCREEN;
|
|
break;
|
|
case SDL_WINDOW_FULLSCREEN:
|
|
mGame.options.fullScreenMode = SDL_WINDOW_FULLSCREEN_DESKTOP;
|
|
break;
|
|
case SDL_WINDOW_FULLSCREEN_DESKTOP:
|
|
mGame.options.fullScreenMode = 0;
|
|
break;
|
|
|
|
default:
|
|
mGame.options.fullScreenMode = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
// Bucle para el logo del juego
|
|
void Game::runLogo()
|
|
{
|
|
}
|
|
|
|
// Bucle para la intro del juego
|
|
void Game::runIntro()
|
|
{
|
|
}
|
|
|
|
// Bucle para el titulo del juego
|
|
void Game::runTitle()
|
|
{
|
|
}
|
|
|
|
// Bucle para el juego
|
|
void Game::runGame()
|
|
{
|
|
} |