Actualizada la clase screen con nuevos procedimientos. Incluído el fichero const.h. Cambio de tamaño de ventana y pantalla completa

This commit is contained in:
2022-08-30 17:59:10 +02:00
parent 16b7bcc091
commit a5c983df9d
9 changed files with 162 additions and 54 deletions

View File

@@ -1,18 +1,47 @@
#pragma once
#include "utils.h"
#ifndef CONST_H
#define CONST_H
// Tamaño de bloque
#define BLOCK 16
#define HALF_BLOCK 8
// Textos
#define WINDOW_CAPTION "Volcano"
#define TEXT_COPYRIGHT "2016,2022 JAILDESIGNER & JAILBROTHER"
#define VERSION "0.5"
// Tamaño de la pantalla real
#define SCREEN_WIDTH 320
#define SCREEN_HEIGHT 240
// Tamaño de bloque
#define BLOCK 8
#define HALF_BLOCK 4
// Tamaño de la pantalla virtual
#define GAMECANVAS_WIDTH 320
#define GAMECANVAS_HEIGHT 240
// Zona de juego
const int PLAY_AREA_TOP = (0 * BLOCK);
const int PLAY_AREA_BOTTOM = (16 * BLOCK);
const int PLAY_AREA_LEFT = (0 * BLOCK);
const int PLAY_AREA_RIGHT = (32 * BLOCK);
const int PLAY_AREA_WIDTH = PLAY_AREA_RIGHT - PLAY_AREA_LEFT;
const int PLAY_AREA_HEIGHT = PLAY_AREA_BOTTOM - PLAY_AREA_TOP;
const int PLAY_AREA_CENTER_X = PLAY_AREA_LEFT + (PLAY_AREA_WIDTH / 2);
const int PLAY_AREA_CENTER_FIRST_QUARTER_X = (PLAY_AREA_WIDTH / 4);
const int PLAY_AREA_CENTER_THIRD_QUARTER_X = (PLAY_AREA_WIDTH / 4) * 3;
const int PLAY_AREA_CENTER_Y = PLAY_AREA_TOP + (PLAY_AREA_HEIGHT / 2);
const int PLAY_AREA_FIRST_QUARTER_Y = PLAY_AREA_HEIGHT / 4;
const int PLAY_AREA_THIRD_QUARTER_Y = (PLAY_AREA_HEIGHT / 4) * 3;
// Anclajes de pantalla
const int GAMECANVAS_CENTER_X = GAMECANVAS_WIDTH / 2;
const int GAMECANVAS_FIRST_QUARTER_X = GAMECANVAS_WIDTH / 4;
const int GAMECANVAS_THIRD_QUARTER_X = (GAMECANVAS_WIDTH / 4) * 3;
const int GAMECANVAS_CENTER_Y = GAMECANVAS_HEIGHT / 2;
const int GAMECANVAS_FIRST_QUARTER_Y = GAMECANVAS_HEIGHT / 4;
const int GAMECANVAS_THIRD_QUARTER_Y = (GAMECANVAS_HEIGHT / 4) * 3;
// Colores
const color_t borderColor = {0x27, 0x27, 0x36};
const color_t black = {0xFF, 0xFF, 0xFF};
#endif

View File

@@ -83,6 +83,55 @@ void Game::update()
section.name = SECTION_PROG_QUIT;
break;
}
else if ((eventHandler->type == SDL_KEYDOWN) and (eventHandler->key.repeat == 0))
{
switch (eventHandler->key.keysym.scancode)
{
case SDL_SCANCODE_ESCAPE:
section.name = SECTION_PROG_QUIT;
break;
case SDL_SCANCODE_D:
debug = !debug;
musicEnabled = !debug;
if (musicEnabled)
{
JA_PlayMusic(music);
}
else
{
JA_StopMusic();
}
break;
case SDL_SCANCODE_R:
delete map;
map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
delete player;
player = new Player(renderer, asset, input, map);
break;
case SDL_SCANCODE_F:
screen->switchVideoMode();
break;
case SDL_SCANCODE_F1:
screen->setWindowSize(1);
break;
case SDL_SCANCODE_F2:
screen->setWindowSize(2);
break;
case SDL_SCANCODE_F3:
screen->setWindowSize(3);
break;
case SDL_SCANCODE_F4:
screen->setWindowSize(4);
break;
}
}
}
map->update();
@@ -113,32 +162,6 @@ void Game::render()
// Comprueba la entrada
void Game::checkInput()
{
if (input->checkInput(INPUT_BUTTON_2, REPEAT_FALSE))
{
debug = !debug;
musicEnabled = !debug;
if (musicEnabled)
{
JA_PlayMusic(music);
}
else
{
JA_StopMusic();
}
}
if (input->checkInput(INPUT_BUTTON_3, REPEAT_FALSE))
{
delete map;
map = new Map(asset->get("01.map"), renderer, asset, itemTracker);
delete player;
player = new Player(renderer, asset, input, map);
}
if (input->checkInput(INPUT_BUTTON_ESCAPE, REPEAT_FALSE))
{
section.name = SECTION_PROG_QUIT;
}
}
// Muestra información de depuración

View File

@@ -12,10 +12,10 @@ Logo::Logo(SDL_Renderer *renderer, Screen *screen, Asset *asset)
eventHandler = new SDL_Event();
texture = new LTexture();
loadTextureFromFile(texture, asset->get("logo.png"), renderer);
sprite = new Sprite(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, texture, renderer);
sprite = new Sprite(0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT, texture, renderer);
// Crea un backbuffer para el renderizador
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
backbuffer = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
if (backbuffer == NULL)
printf("Backbuffer could not be created!\nSDL Error: %s\n", SDL_GetError());
@@ -105,7 +105,7 @@ void Logo::render()
// Dibuja el fade
if (counter >= initFade)
{
const SDL_Rect rect = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
const SDL_Rect rect = {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT};
const color_t color = {0xFF, 0xFF, 0xFF};
const int fadeLenght = endLogo - initFade;

View File

@@ -7,6 +7,7 @@
#include "screen.h"
#include "asset.h"
#include "jail_audio.h"
#include "const.h"
#ifndef LOGO_H
#define LOGO_H

View File

@@ -9,8 +9,8 @@ Prog::Prog(std::string executablePath)
options->windowSize = 2;
options->filter = FILTER_NEAREST;
options->vSync = true;
options->screenWidth = GAME_WIDTH * options->windowSize;
options->screenHeight = GAME_HEIGHT * options->windowSize;
options->screenWidth = GAMECANVAS_WIDTH * options->windowSize;
options->screenHeight = GAMECANVAS_HEIGHT * options->windowSize;
options->integerScale = true;
options->keepAspect = true;
@@ -29,7 +29,8 @@ Prog::Prog(std::string executablePath)
section.name = SECTION_PROG_GAME;
}
input = new Input(asset->get("gamecontrollerdb.txt"));
screen = new Screen(window, renderer, options);
screen = new Screen(window, renderer, options, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
screen->setBorderColor(borderColor);
// Controles
input->bindKey(INPUT_UP, SDL_SCANCODE_UP);

View File

@@ -12,14 +12,11 @@
#include "intro.h"
#include "title.h"
#include "prog.h"
#include "const.h"
#ifndef PROG_H
#define PROG_H
#define WINDOW_CAPTION "Volcano"
#define GAME_WIDTH 320
#define GAME_HEIGHT 240
class Prog
{
private:

View File

@@ -1,18 +1,17 @@
#include "screen.h"
#include "const.h"
#include <string>
#include <stdio.h>
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY)
{
// Inicializa variables
this->window = window;
this->renderer = renderer;
this->options = options;
gameCanvasWidth = SCREEN_WIDTH;
gameCanvasHeight = SCREEN_HEIGHT;
gameCanvasWidth = gameInternalResX;
gameCanvasHeight = gameInternalResY;
// Establece el modo de video
setVideoMode(options->fullScreenMode);
@@ -25,6 +24,14 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options)
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());
// Calcula los anclajes
anchor.left = 0;
anchor.right = gameCanvasWidth;
anchor.center = gameCanvasWidth / 2;
anchor.top = 0;
anchor.bottom = gameCanvasHeight;
anchor.middle = gameCanvasHeight / 2;
}
// Destructor
@@ -82,7 +89,7 @@ void Screen::setVideoMode(int fullScreenMode)
}
// Si está activo el modo de pantalla completa añade el borde
if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
else if (fullScreenMode == SDL_WINDOW_FULLSCREEN_DESKTOP)
{
// Obten el alto y el ancho de la ventana
SDL_GetWindowSize(window, &screenWidth, &screenHeight);
@@ -130,4 +137,33 @@ void Screen::setVideoMode(int fullScreenMode)
// Modifica el tamaño del renderizador
SDL_RenderSetLogicalSize(renderer, screenWidth, screenHeight);
}
// Actualiza el valor de la variable
options->fullScreenMode = fullScreenMode;
}
// Camibia entre pantalla completa y ventana
void Screen::switchVideoMode()
{
if (options->fullScreenMode == 0)
{
setVideoMode(SDL_WINDOW_FULLSCREEN_DESKTOP);
}
else
{
setVideoMode(0);
}
}
// Cambia el tamaño de la ventana
void Screen::setWindowSize(int size)
{
options->windowSize = size;
setVideoMode(0);
}
// Cambia el color del borde
void Screen::setBorderColor(color_t color)
{
borderColor = color;
}

View File

@@ -6,6 +6,16 @@
#ifndef SCREEN_H
#define SCREEN_H
struct anchor_t
{
int left; // Parte izquierda de la pantalla de juego
int right; // Parte drecha de la pantalla de juego
int center; // Parte central horizontal de la pantalla de juego
int top; // Parte superior de la pantalla de juego
int bottom; // Parte infoerior de la pantalla de juego
int middle; // Parte central vertical de la pantalla de juego
};
// Clase Screen
class Screen
{
@@ -15,16 +25,17 @@ private:
SDL_Texture *gameCanvas; // Textura para completar la ventana de juego hasta la pantalla completa
options_t *options; // Variable con todas las opciones del programa
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
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego
int screenWidth; // Ancho de la pantalla o ventana
int screenHeight; // Alto de la pantalla o ventana
int gameCanvasWidth; // Resolución interna del juego. Es el ancho de la textura donde se dibuja el juego
int gameCanvasHeight; // Resolución interna del juego. Es el alto de la textura donde se dibuja el juego
anchor_t anchor; // Variable con los anclajes de la pantalla
SDL_Rect dest; // Coordenadas donde se va a dibujar la textura del juego sobre la pantalla o ventana
color_t borderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
public:
// Constructor
Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options);
Screen(SDL_Window *window, SDL_Renderer *renderer, options_t *options, int gameInternalResX, int gameInternalResY);
// Destructor
~Screen();
@@ -40,6 +51,15 @@ public:
// Establece el modo de video
void setVideoMode(int fullScreenMode);
// Camibia entre pantalla completa y ventana
void switchVideoMode();
// Cambia el tamaño de la ventana
void setWindowSize(int size);
// Cambia el color del borde
void setBorderColor(color_t color);
};
#endif

View File

@@ -94,7 +94,8 @@ void Title::render()
// Dibuja los objetos
sprite->render();
text->writeDX(TXT_CENTER | TXT_COLOR, 160, 205, "@2016,2022 JAILDESIGNER & JAILBROTHER (v0.4)", 0, {255, 93, 4});
//const std::string text = TEXT_COPYRIGHT + " (" + VERSION + ")";
this->text->writeDX(TXT_CENTER | TXT_COLOR, GAMECANVAS_CENTER_X, 205, TEXT_COPYRIGHT, 0, {255, 93, 4});
menu->render();
// Vuelca el contenido del renderizador en pantalla