diff --git a/source/game.cpp b/source/game.cpp index 414c818..0d3af22 100644 --- a/source/game.cpp +++ b/source/game.cpp @@ -1,10 +1,83 @@ #include "game.h" // Constructor -Game::Game() +Game::Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input) { + + this->renderer = renderer; + this->asset = asset; + this->screen = screen; + this->input = input; + + eventHandler = new SDL_Event(); } +// Destructor Game::~Game() { + delete eventHandler; } + +// Bucle para el juego +section_t Game::run() +{ + init(); + + while (section.name == SECTION_PROG_GAME) + { + // Sección juego jugando + if (section.subsection == SUBSECTION_GAME_PLAY) + { + update(); + render(); + } + } + + return section; +} + +// Inicializa las variables necesarias para la sección 'Game' +void Game::init() +{ + // Carga los recursos + // loadMedia(); + + ticks = 0; + ticksSpeed = 15; + + section.name = SECTION_PROG_GAME; + section.subsection = SUBSECTION_GAME_PLAY; +} + +// Actualiza el juego, las variables, comprueba la entrada, etc. +void Game::update() +{ + // Comprueba que la diferencia de ticks sea mayor a la velocidad del juego + if (SDL_GetTicks() - ticks > ticksSpeed) + { + // Actualiza el contador de ticks + ticks = SDL_GetTicks(); + + // Comprueba los eventos que hay en la cola + while (SDL_PollEvent(eventHandler) != 0) + { + // Evento de salida de la aplicación + if (eventHandler->type == SDL_QUIT) + { + section.name = SECTION_PROG_QUIT; + break; + } + } + } +} + +// Pinta los objetos en pantalla +void Game::render() +{ + // Prepara para dibujar el frame + screen->start(); + screen->clean(); + + // Actualiza la pantalla + screen->blit(); +} \ No newline at end of file diff --git a/source/game.h b/source/game.h index f8142de..fd7577e 100644 --- a/source/game.h +++ b/source/game.h @@ -1,5 +1,9 @@ #pragma once #include +#include "utils.h" +#include "asset.h" +#include "screen.h" +#include "input.h" #ifndef GAME_H #define GAME_H @@ -7,14 +11,33 @@ class Game { private: - + SDL_Renderer *renderer; // El renderizador de la ventana + Asset *asset; // Objeto encargado de gestionar los ficheros de recursos + Screen *screen; // Objeto encargado de dibujar en pantalla + Input *input; // Objeto Input para gestionar las entradas + SDL_Event *eventHandler; // Manejador de eventos + section_t section; // Seccion actual dentro del programa + int ticks; // Contador de ticks para ajustar la velocidad del programa + int ticksSpeed; // Velocidad a la que se repiten los bucles del programa + + // Actualiza el juego, las variables, comprueba la entrada, etc. + void update(); + + // Pinta los objetos en pantalla + void render(); + + // Inicializa las variables necesarias para la sección 'Game' + void init(); public: // Constructor - Game(); + Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input); // Destructor ~Game(); + + // Bucle para el juego + section_t run(); }; #endif diff --git a/source/main.cpp b/source/main.cpp index f25648e..0bfee80 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -19,6 +19,8 @@ Repres un 14 de febrer de 2021 int main(int argc, char *args[]) { + printf("Booting up the game...\n"); + // Inicia el generador de numeros aleatorios srand(time(nullptr)); diff --git a/source/prog.cpp b/source/prog.cpp index 9bbdb14..09b5ecc 100644 --- a/source/prog.cpp +++ b/source/prog.cpp @@ -6,31 +6,38 @@ Prog::Prog(std::string executablePath) // Establece las opciones por defecto options = new options_t; options->fullScreenMode = 0; - options->windowSize = 1; + options->windowSize = 3; options->filter = FILTER_NEAREST; options->vSync = true; - options->screenWidth = 320; - options->screenHeight = 240; + options->screenWidth = GAME_WIDTH * options->windowSize; + options->screenHeight = GAME_HEIGHT * options->windowSize; options->integerScale = true; options->keepAspect = true; - // Crea los objetos - asset = new Asset(executablePath); - input = new Input(asset->get("gamecontrollerdb.txt")); - screen = new Screen(renderer,320,240,320,240); - - // Inicializa las variables - section.name = PROG_SECTION_GAME; - + // Inicia las librerias initSDL(); initJailAudio(); + + // Crea los objetos + asset = new Asset(executablePath.substr(0, executablePath.find_last_of("\\/"))); + if (!setFileList()) + { + section.name = SECTION_PROG_QUIT; + } + else + { + section.name = SECTION_PROG_GAME; + } + input = new Input(asset->get("gamecontrollerdb.txt")); + screen = new Screen(window, renderer, options->screenWidth, options->screenWidth, GAME_WIDTH, GAME_HEIGHT); } Prog::~Prog() { + delete options; delete asset; delete input; - delete options; + delete screen; SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); @@ -67,7 +74,7 @@ bool Prog::initSDL() } // Crea la ventana - window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth * options->windowSize, options->screenHeight * options->windowSize, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); + window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, options->screenWidth, options->screenHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI); if (window == NULL) { printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError()); @@ -100,7 +107,7 @@ bool Prog::initSDL() } } - printf("\n"); + printf("SDL is running...\n"); return success; } @@ -154,30 +161,28 @@ void Prog::setSection(section_t section) void Prog::runGame() { - game = new Game(); - //setSection(game->run()); - section.name = PROG_SECTION_QUIT; - setSection(section); + game = new Game(renderer, asset, screen, input); + setSection(game->run()); delete game; } void Prog::run() { // Bucle principal - while (!(getSection() == PROG_SECTION_QUIT)) + while (!(getSection() == SECTION_PROG_QUIT)) { switch (getSection()) { - case PROG_SECTION_LOGO: - //runLogo(); + case SECTION_PROG_LOGO: + // runLogo(); break; - case PROG_SECTION_INTRO: - //runIntro(); + case SECTION_PROG_INTRO: + // runIntro(); break; - case PROG_SECTION_TITLE: - //runTitle(); + case SECTION_PROG_TITLE: + // runTitle(); break; - case PROG_SECTION_GAME: + case SECTION_PROG_GAME: runGame(); break; } diff --git a/source/prog.h b/source/prog.h index f51a715..45002c3 100644 --- a/source/prog.h +++ b/source/prog.h @@ -12,12 +12,9 @@ #define PROG_H #define WINDOW_CAPTION "Volcano" -#define WINDOW_WIDTH 320 -#define WINDOW_HEIGHT 240 #define GAME_WIDTH 320 #define GAME_HEIGHT 240 - class Prog { private: @@ -25,8 +22,8 @@ private: SDL_Renderer *renderer; // El renderizador de la ventana Asset *asset; // Objeto encargado de gestionar los ficheros de recursos Screen *screen; // Objeto encargado de dibujar en pantalla - Game *game; // Objeto para la sección del juego Input *input; // Objeto Input para gestionar las entradas + Game *game; // Objeto para la sección del juego section_t section; // Sección y subsección actual del programa; struct options_t *options; // Contiene las opciones del programa diff --git a/source/screen.cpp b/source/screen.cpp index 7842991..e9f4a0e 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -4,56 +4,56 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, int scr_w, int scr_h, int gc_w, int gc_h) { // Inicializa variables - mWindow = window; - mRenderer = renderer; + this->window = window; + this->renderer = renderer; - mScreenWidth = scr_w; - mScreenHeight = scr_h; - mGameCanvasWidth = gc_w; - mGameCanvasHeight = gc_h; - mGameCanvasPosX = (scr_w - gc_w) / 2; - mGameCanvasPosY = (scr_h - gc_h) / 2; - mDest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth, mGameCanvasHeight}; - mBorderColor = {0x27, 0x27, 0x36}; + screenWidth = scr_w; + screenHeight = scr_h; + gameCanvasWidth = gc_w; + gameCanvasHeight = gc_h; + gameCanvasPosX = (scr_w - gc_w) / 2; + gameCanvasPosY = (scr_h - gc_h) / 2; + dest = {gameCanvasPosX, gameCanvasPosY, gameCanvasWidth, gameCanvasHeight}; + borderColor = {0x27, 0x27, 0x36}; // Crea la textura donde se dibujan los graficos del juego - mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mGameCanvasWidth, mGameCanvasHeight); - if (mGameCanvas == NULL) + gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight); + if (gameCanvas == NULL) printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError()); } // Destructor Screen::~Screen() { - mRenderer = nullptr; + renderer = nullptr; } // Limpia la pantalla void Screen::clean(color_t color) { - SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF); - SDL_RenderClear(mRenderer); + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF); + SDL_RenderClear(renderer); } // Prepara para empezar a dibujar en la textura de juego void Screen::start() { - SDL_SetRenderTarget(mRenderer, mGameCanvas); + SDL_SetRenderTarget(renderer, gameCanvas); } // Vuelca el contenido del renderizador en pantalla void Screen::blit() { // Vuelve a dejar el renderizador en modo normal - SDL_SetRenderTarget(mRenderer, NULL); + SDL_SetRenderTarget(renderer, NULL); // Borra el contenido previo - SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF); - SDL_RenderClear(mRenderer); + SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF); + SDL_RenderClear(renderer); // Copia la textura de juego en el renderizador en la posición adecuada - SDL_RenderCopy(mRenderer, mGameCanvas, NULL, &mDest); + SDL_RenderCopy(renderer, gameCanvas, NULL, &dest); // Muestra por pantalla el renderizador - SDL_RenderPresent(mRenderer); + SDL_RenderPresent(renderer); } \ No newline at end of file diff --git a/source/screen.h b/source/screen.h index 14aef16..014b1bc 100644 --- a/source/screen.h +++ b/source/screen.h @@ -9,23 +9,23 @@ class Screen { private: - SDL_Window *mWindow; // Ventana de la aplicación - SDL_Renderer *mRenderer; // El renderizador de la ventana - SDL_Texture *mGameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa + SDL_Window *window; // Ventana de la aplicación + SDL_Renderer *renderer; // El renderizador de la ventana + SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa - int mScreenWidth; // Ancho de la pantalla - int mScreenHeight; // Alto de la pantalla - int mGameCanvasWidth; // Ancho de la textura donde se dibuja el juego - int mGameCanvasHeight; // Alto de la textura donde se dibuja el juego - int mGameCanvasPosX; // Posicion en el eje X donde se dibujará la textura del juego dentro de la pantalla - int mGameCanvasPosY; // Posicion en el eje Y donde se dibujará la textura del juego dentro de la pantalla + int screenWidth; // Ancho de la pantalla + int screenHeight; // Alto de la pantalla + int gameCanvasWidth; // Ancho de la textura donde se dibuja el juego + int gameCanvasHeight; // Alto de la textura donde se dibuja el juego + int gameCanvasPosX; // Posicion en el eje X donde se dibujará la textura del juego dentro de la pantalla + int gameCanvasPosY; // Posicion en el eje Y donde se dibujará la textura del juego dentro de la pantalla - SDL_Rect mDest; // Rectangulo de destino donde se dibujarà la textura con el juego - color_t mBorderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla + SDL_Rect dest; // Rectangulo de destino donde se dibujarà la textura con el juego + color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla public: // Constructor - Screen(SDL_Window *windows, SDL_Renderer *renderer, int scr_w = 0, int scr_h = 0, int gc_w = 0, int gc_h = 0); + Screen(SDL_Window *window, SDL_Renderer *renderer, int scr_w = 0, int scr_h = 0, int gc_w = 0, int gc_h = 0); // Destructor ~Screen(); diff --git a/source/utils.h b/source/utils.h index 95e563b..90d1af4 100644 --- a/source/utils.h +++ b/source/utils.h @@ -10,11 +10,20 @@ #define FILTER_LINEAL 1 // Secciones del programa -#define PROG_SECTION_LOGO 0 -#define PROG_SECTION_INTRO 1 -#define PROG_SECTION_TITLE 2 -#define PROG_SECTION_GAME 3 -#define PROG_SECTION_QUIT 4 +#define SECTION_PROG_LOGO 0 +#define SECTION_PROG_INTRO 1 +#define SECTION_PROG_TITLE 2 +#define SECTION_PROG_GAME 3 +#define SECTION_PROG_QUIT 4 + +// Subsecciones +#define SUBSECTION_GAME_PLAY 0 +#define SUBSECTION_GAME_PAUSE 1 +#define SUBSECTION_GAME_GAMEOVER 2 +#define SUBSECTION_TITLE_1 3 +#define SUBSECTION_TITLE_2 4 +#define SUBSECTION_TITLE_3 5 +#define SUBSECTION_TITLE_INSTRUCTIONS 6 // Estructura para definir un circulo struct circle_t