Trabajando en la clase screen

This commit is contained in:
2022-08-11 18:39:56 +02:00
parent 09ad78b02e
commit 89a43b3529
8 changed files with 180 additions and 71 deletions

View File

@@ -1,10 +1,83 @@
#include "game.h" #include "game.h"
// Constructor // 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() 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();
}

View File

@@ -1,5 +1,9 @@
#pragma once #pragma once
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include "utils.h"
#include "asset.h"
#include "screen.h"
#include "input.h"
#ifndef GAME_H #ifndef GAME_H
#define GAME_H #define GAME_H
@@ -7,14 +11,33 @@
class Game class Game
{ {
private: 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: public:
// Constructor // Constructor
Game(); Game(SDL_Renderer *renderer, Asset *asset, Screen *screen, Input *input);
// Destructor // Destructor
~Game(); ~Game();
// Bucle para el juego
section_t run();
}; };
#endif #endif

View File

@@ -19,6 +19,8 @@ Repres un 14 de febrer de 2021
int main(int argc, char *args[]) int main(int argc, char *args[])
{ {
printf("Booting up the game...\n");
// Inicia el generador de numeros aleatorios // Inicia el generador de numeros aleatorios
srand(time(nullptr)); srand(time(nullptr));

View File

@@ -6,31 +6,38 @@ Prog::Prog(std::string executablePath)
// Establece las opciones por defecto // Establece las opciones por defecto
options = new options_t; options = new options_t;
options->fullScreenMode = 0; options->fullScreenMode = 0;
options->windowSize = 1; options->windowSize = 3;
options->filter = FILTER_NEAREST; options->filter = FILTER_NEAREST;
options->vSync = true; options->vSync = true;
options->screenWidth = 320; options->screenWidth = GAME_WIDTH * options->windowSize;
options->screenHeight = 240; options->screenHeight = GAME_HEIGHT * options->windowSize;
options->integerScale = true; options->integerScale = true;
options->keepAspect = true; options->keepAspect = true;
// Crea los objetos // Inicia las librerias
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;
initSDL(); initSDL();
initJailAudio(); 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() Prog::~Prog()
{ {
delete options;
delete asset; delete asset;
delete input; delete input;
delete options; delete screen;
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
@@ -67,7 +74,7 @@ bool Prog::initSDL()
} }
// Crea la ventana // 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) if (window == NULL)
{ {
printf("Window could not be created!\nSDL Error: %s\n", SDL_GetError()); 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; return success;
} }
@@ -154,30 +161,28 @@ void Prog::setSection(section_t section)
void Prog::runGame() void Prog::runGame()
{ {
game = new Game(); game = new Game(renderer, asset, screen, input);
//setSection(game->run()); setSection(game->run());
section.name = PROG_SECTION_QUIT;
setSection(section);
delete game; delete game;
} }
void Prog::run() void Prog::run()
{ {
// Bucle principal // Bucle principal
while (!(getSection() == PROG_SECTION_QUIT)) while (!(getSection() == SECTION_PROG_QUIT))
{ {
switch (getSection()) switch (getSection())
{ {
case PROG_SECTION_LOGO: case SECTION_PROG_LOGO:
//runLogo(); // runLogo();
break; break;
case PROG_SECTION_INTRO: case SECTION_PROG_INTRO:
//runIntro(); // runIntro();
break; break;
case PROG_SECTION_TITLE: case SECTION_PROG_TITLE:
//runTitle(); // runTitle();
break; break;
case PROG_SECTION_GAME: case SECTION_PROG_GAME:
runGame(); runGame();
break; break;
} }

View File

@@ -12,12 +12,9 @@
#define PROG_H #define PROG_H
#define WINDOW_CAPTION "Volcano" #define WINDOW_CAPTION "Volcano"
#define WINDOW_WIDTH 320
#define WINDOW_HEIGHT 240
#define GAME_WIDTH 320 #define GAME_WIDTH 320
#define GAME_HEIGHT 240 #define GAME_HEIGHT 240
class Prog class Prog
{ {
private: private:
@@ -25,8 +22,8 @@ private:
SDL_Renderer *renderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
Asset *asset; // Objeto encargado de gestionar los ficheros de recursos Asset *asset; // Objeto encargado de gestionar los ficheros de recursos
Screen *screen; // Objeto encargado de dibujar en pantalla 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 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; section_t section; // Sección y subsección actual del programa;
struct options_t *options; // Contiene las opciones del programa struct options_t *options; // Contiene las opciones del programa

View File

@@ -4,56 +4,56 @@
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, int scr_w, int scr_h, int gc_w, int gc_h) Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, int scr_w, int scr_h, int gc_w, int gc_h)
{ {
// Inicializa variables // Inicializa variables
mWindow = window; this->window = window;
mRenderer = renderer; this->renderer = renderer;
mScreenWidth = scr_w; screenWidth = scr_w;
mScreenHeight = scr_h; screenHeight = scr_h;
mGameCanvasWidth = gc_w; gameCanvasWidth = gc_w;
mGameCanvasHeight = gc_h; gameCanvasHeight = gc_h;
mGameCanvasPosX = (scr_w - gc_w) / 2; gameCanvasPosX = (scr_w - gc_w) / 2;
mGameCanvasPosY = (scr_h - gc_h) / 2; gameCanvasPosY = (scr_h - gc_h) / 2;
mDest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth, mGameCanvasHeight}; dest = {gameCanvasPosX, gameCanvasPosY, gameCanvasWidth, gameCanvasHeight};
mBorderColor = {0x27, 0x27, 0x36}; borderColor = {0x27, 0x27, 0x36};
// Crea la textura donde se dibujan los graficos del juego // Crea la textura donde se dibujan los graficos del juego
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, mGameCanvasWidth, mGameCanvasHeight); gameCanvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, gameCanvasWidth, gameCanvasHeight);
if (mGameCanvas == NULL) if (gameCanvas == NULL)
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError()); printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
} }
// Destructor // Destructor
Screen::~Screen() Screen::~Screen()
{ {
mRenderer = nullptr; renderer = nullptr;
} }
// Limpia la pantalla // Limpia la pantalla
void Screen::clean(color_t color) void Screen::clean(color_t color)
{ {
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF); SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, 0xFF);
SDL_RenderClear(mRenderer); SDL_RenderClear(renderer);
} }
// Prepara para empezar a dibujar en la textura de juego // Prepara para empezar a dibujar en la textura de juego
void Screen::start() void Screen::start()
{ {
SDL_SetRenderTarget(mRenderer, mGameCanvas); SDL_SetRenderTarget(renderer, gameCanvas);
} }
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
void Screen::blit() void Screen::blit()
{ {
// Vuelve a dejar el renderizador en modo normal // Vuelve a dejar el renderizador en modo normal
SDL_SetRenderTarget(mRenderer, NULL); SDL_SetRenderTarget(renderer, NULL);
// Borra el contenido previo // Borra el contenido previo
SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF); SDL_SetRenderDrawColor(renderer, borderColor.r, borderColor.g, borderColor.b, 0xFF);
SDL_RenderClear(mRenderer); SDL_RenderClear(renderer);
// Copia la textura de juego en el renderizador en la posición adecuada // 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 // Muestra por pantalla el renderizador
SDL_RenderPresent(mRenderer); SDL_RenderPresent(renderer);
} }

View File

@@ -9,23 +9,23 @@
class Screen class Screen
{ {
private: private:
SDL_Window *mWindow; // Ventana de la aplicación SDL_Window *window; // Ventana de la aplicación
SDL_Renderer *mRenderer; // El renderizador de la ventana SDL_Renderer *renderer; // El renderizador de la ventana
SDL_Texture *mGameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
int mScreenWidth; // Ancho de la pantalla int screenWidth; // Ancho de la pantalla
int mScreenHeight; // Alto de la pantalla int screenHeight; // Alto de la pantalla
int mGameCanvasWidth; // Ancho de la textura donde se dibuja el juego int gameCanvasWidth; // Ancho de la textura donde se dibuja el juego
int mGameCanvasHeight; // Alto de la textura donde se dibuja el juego int gameCanvasHeight; // 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 gameCanvasPosX; // 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 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 SDL_Rect dest; // 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 color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
public: public:
// Constructor // 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 // Destructor
~Screen(); ~Screen();

View File

@@ -10,11 +10,20 @@
#define FILTER_LINEAL 1 #define FILTER_LINEAL 1
// Secciones del programa // Secciones del programa
#define PROG_SECTION_LOGO 0 #define SECTION_PROG_LOGO 0
#define PROG_SECTION_INTRO 1 #define SECTION_PROG_INTRO 1
#define PROG_SECTION_TITLE 2 #define SECTION_PROG_TITLE 2
#define PROG_SECTION_GAME 3 #define SECTION_PROG_GAME 3
#define PROG_SECTION_QUIT 4 #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 // Estructura para definir un circulo
struct circle_t struct circle_t