Finalizada la primera implementación funcional de la clase screen
This commit is contained in:
@@ -15,22 +15,22 @@
|
||||
#define HALF_BLOCK 4
|
||||
|
||||
// Tamaño de la pantalla real
|
||||
#define REAL_SCREEN_WIDTH 320
|
||||
#define REAL_SCREEN_HEIGHT 240
|
||||
#define SCREEN_WIDTH 320
|
||||
#define SCREEN_HEIGHT 240
|
||||
|
||||
// Tamaño de la pantalla virtual
|
||||
#define SCREEN_WIDTH 256
|
||||
#define SCREEN_HEIGHT 192
|
||||
#define GAMECANVAS_WIDTH 256
|
||||
#define GAMECANVAS_HEIGHT 192
|
||||
|
||||
// Tamaño de la pantalla que se muestra
|
||||
const int VIEW_WIDTH = REAL_SCREEN_WIDTH * 3;
|
||||
const int VIEW_HEIGHT = REAL_SCREEN_HEIGHT * 3;
|
||||
const int VIEW_WIDTH = SCREEN_WIDTH * 3;
|
||||
const int VIEW_HEIGHT = SCREEN_HEIGHT * 3;
|
||||
|
||||
// Zona de juego
|
||||
const int PLAY_AREA_TOP = (0 * BLOCK);
|
||||
const int PLAY_AREA_BOTTOM = SCREEN_HEIGHT - (4 * BLOCK);
|
||||
const int PLAY_AREA_BOTTOM = GAMECANVAS_HEIGHT - (4 * BLOCK);
|
||||
const int PLAY_AREA_LEFT = (0 * BLOCK);
|
||||
const int PLAY_AREA_RIGHT = SCREEN_WIDTH - (0 * BLOCK);
|
||||
const int PLAY_AREA_RIGHT = GAMECANVAS_WIDTH - (0 * 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);
|
||||
@@ -41,12 +41,12 @@ 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 SCREEN_CENTER_X = SCREEN_WIDTH / 2;
|
||||
const int SCREEN_FIRST_QUARTER_X = SCREEN_WIDTH / 4;
|
||||
const int SCREEN_THIRD_QUARTER_X = (SCREEN_WIDTH / 4) * 3;
|
||||
const int SCREEN_CENTER_Y = SCREEN_HEIGHT / 2;
|
||||
const int SCREEN_FIRST_QUARTER_Y = SCREEN_HEIGHT / 4;
|
||||
const int SCREEN_THIRD_QUARTER_Y = (SCREEN_HEIGHT / 4) * 3;
|
||||
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;
|
||||
|
||||
// Secciones del programa
|
||||
#define PROG_SECTION_LOGO 0
|
||||
|
||||
@@ -49,7 +49,7 @@ Director::Director(std::string path)
|
||||
|
||||
// Aplica las opciones
|
||||
SDL_SetWindowFullscreen(mWindow, mOptions->fullScreenMode);
|
||||
SDL_SetWindowSize(mWindow, REAL_SCREEN_WIDTH * mOptions->windowSize, REAL_SCREEN_HEIGHT * mOptions->windowSize);
|
||||
SDL_SetWindowSize(mWindow, SCREEN_WIDTH * mOptions->windowSize, SCREEN_HEIGHT * mOptions->windowSize);
|
||||
mLang->setLang(mOptions->language);
|
||||
|
||||
// Inicializa el resto de variables
|
||||
@@ -172,7 +172,7 @@ bool Director::initSDL()
|
||||
SDL_SetRenderDrawColor(mRenderer, 0x00, 0x00, 0x00, 0xFF);
|
||||
|
||||
// Establece el tamaño del buffer de renderizado
|
||||
SDL_RenderSetLogicalSize(mRenderer, REAL_SCREEN_WIDTH, REAL_SCREEN_HEIGHT);
|
||||
SDL_RenderSetLogicalSize(mRenderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
|
||||
// Establece el modo de mezcla
|
||||
SDL_SetRenderDrawBlendMode(mRenderer, SDL_BLENDMODE_BLEND);
|
||||
|
||||
@@ -92,15 +92,17 @@ section_t Game::run()
|
||||
}
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
mScreen->clean();
|
||||
// Prepara para dibujar el frame
|
||||
const color_t color = {0xAA, 0x55, 0x55};
|
||||
mScreen->start();
|
||||
mScreen->clean(color);
|
||||
|
||||
// Medidas de ancho y alto de la pantalla
|
||||
mText->writeCentered(SCREEN_CENTER_X, 0, std::to_string(SCREEN_WIDTH), -1);
|
||||
mText->write(0, SCREEN_CENTER_Y - (mText->getCharacterWidth() / 2), std::to_string(SCREEN_HEIGHT), -1);
|
||||
// Escribe las medidas de ancho y alto de la pantalla
|
||||
mText->writeCentered(GAMECANVAS_CENTER_X, 0, std::to_string(GAMECANVAS_WIDTH), -1);
|
||||
mText->write(0, GAMECANVAS_CENTER_Y - (mText->getCharacterWidth() / 2), std::to_string(GAMECANVAS_HEIGHT), -1);
|
||||
|
||||
// Texto en el centro de la pantalla
|
||||
mText->writeCentered(SCREEN_CENTER_X, SCREEN_CENTER_Y - (mText->getCharacterWidth() / 2), "Pepe el Cazavampiros", -1);
|
||||
mText->writeCentered(GAMECANVAS_CENTER_X, GAMECANVAS_CENTER_Y - (mText->getCharacterWidth() / 2), "Pepe el Cazavampiros", -1);
|
||||
|
||||
// Actualiza la pantalla
|
||||
mScreen->blit();
|
||||
|
||||
@@ -4,17 +4,21 @@
|
||||
// Constructor
|
||||
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
|
||||
{
|
||||
// Inicializa variables
|
||||
mWindow = window;
|
||||
mRenderer = renderer;
|
||||
|
||||
mRealScreenWidth = REAL_SCREEN_WIDTH;
|
||||
mRealScreenHeight = REAL_SCREEN_HEIGHT;
|
||||
mScreenWidth = SCREEN_WIDTH;
|
||||
mScreenHeight = SCREEN_HEIGHT;
|
||||
mBorderX = (REAL_SCREEN_WIDTH - SCREEN_WIDTH) / 2;
|
||||
mBorderY = (REAL_SCREEN_HEIGHT - SCREEN_HEIGHT) / 2;
|
||||
mGameCanvasWidth = GAMECANVAS_WIDTH;
|
||||
mGameCanvasHeight = GAMECANVAS_HEIGHT;
|
||||
mGameCanvasPosX = (SCREEN_WIDTH - GAMECANVAS_WIDTH) / 2;
|
||||
mGameCanvasPosY = (SCREEN_HEIGHT - GAMECANVAS_HEIGHT) / 2;
|
||||
|
||||
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
mBorderColor = {0x27, 0x27, 0x36};
|
||||
|
||||
// Crea la textura donde se dibujan los graficos del juego
|
||||
mGameCanvas = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT);
|
||||
if (mGameCanvas == NULL)
|
||||
printf("TitleSurface could not be created!\nSDL Error: %s\n", SDL_GetError());
|
||||
}
|
||||
@@ -26,25 +30,34 @@ Screen::~Screen()
|
||||
}
|
||||
|
||||
// Limpia la pantalla
|
||||
void Screen::clean()
|
||||
void Screen::clean(color_t color)
|
||||
{
|
||||
SDL_SetRenderDrawColor(mRenderer, 0x27, 0x27, 0x36, 0xFF);
|
||||
SDL_SetRenderDrawColor(mRenderer, color.r, color.g, color.b, 0xFF);
|
||||
SDL_RenderClear(mRenderer);
|
||||
}
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void Screen::start()
|
||||
{
|
||||
SDL_SetRenderTarget(mRenderer, mGameCanvas);
|
||||
SDL_SetRenderDrawColor(mRenderer, 0xAA, 0x44, 0x44, 0xFF);
|
||||
SDL_RenderClear(mRenderer);
|
||||
}
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void Screen::blit()
|
||||
{
|
||||
SDL_Rect src = {0, 0, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
SDL_Rect dest = {mBorderX, mBorderY, SCREEN_WIDTH, SCREEN_HEIGHT};
|
||||
|
||||
// Vuelve a dejar el renderizador en modo normal
|
||||
SDL_SetRenderTarget(mRenderer, NULL);
|
||||
|
||||
|
||||
// Borra el contenido previo
|
||||
SDL_SetRenderDrawColor(mRenderer, mBorderColor.r, mBorderColor.g, mBorderColor.b, 0xFF);
|
||||
SDL_RenderClear(mRenderer);
|
||||
|
||||
// Rectangulo de destino donde se dibujarà la textura con el juego
|
||||
SDL_Rect dest = {mGameCanvasPosX, mGameCanvasPosY, mGameCanvasWidth, mGameCanvasHeight};
|
||||
|
||||
// Copia la textura de juego en el renderizador en la posición adecuada
|
||||
SDL_RenderCopy(mRenderer, mGameCanvas, NULL, &dest);
|
||||
|
||||
// Muestra por pantalla el renderizador
|
||||
SDL_RenderPresent(mRenderer);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
#include "ifdefs.h"
|
||||
#include "utils.h"
|
||||
|
||||
#ifndef SCREEN_H
|
||||
#define SCREEN_H
|
||||
@@ -8,16 +9,18 @@
|
||||
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 *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
|
||||
|
||||
int mRealScreenWidth;
|
||||
int mRealScreenHeight;
|
||||
int mScreenWidth;
|
||||
int mScreenHeight;
|
||||
int mBorderX;
|
||||
int mBorderY;
|
||||
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
|
||||
|
||||
color_t mBorderColor; // Color del borde añadido a la textura de juego para rellenar la pantalla
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
@@ -27,7 +30,10 @@ public:
|
||||
~Screen();
|
||||
|
||||
// Limpia la pantalla
|
||||
void clean();
|
||||
void clean(color_t color);
|
||||
|
||||
// Prepara para empezar a dibujar en la textura de juego
|
||||
void start();
|
||||
|
||||
// Vuelca el contenido del renderizador en pantalla
|
||||
void blit();
|
||||
|
||||
Reference in New Issue
Block a user