Ja dibuixa la tarjeta amb la ajuda per als botons de servei

This commit is contained in:
2024-10-01 20:58:05 +02:00
parent 3cabd5c675
commit 3a84ea792c
5 changed files with 222 additions and 29 deletions

View File

@@ -154,6 +154,7 @@ void Director::initInput()
input->bindKey(input_start, SDL_SCANCODE_RETURN); input->bindKey(input_start, SDL_SCANCODE_RETURN);
// Teclado - Control del programa // Teclado - Control del programa
input->bindKey(input_service, SDL_SCANCODE_0);
input->bindKey(input_exit, SDL_SCANCODE_ESCAPE); input->bindKey(input_exit, SDL_SCANCODE_ESCAPE);
input->bindKey(input_pause, SDL_SCANCODE_P); input->bindKey(input_pause, SDL_SCANCODE_P);
input->bindKey(input_window_dec_size, SDL_SCANCODE_F1); input->bindKey(input_window_dec_size, SDL_SCANCODE_F1);
@@ -380,6 +381,8 @@ bool Director::setFileList()
asset->add(prefix + "/data/shaders/crtpi.glsl", t_data); asset->add(prefix + "/data/shaders/crtpi.glsl", t_data);
// Texturas // Texturas
asset->add(prefix + "/data/gfx/controllers/controllers.png", t_bitmap);
asset->add(prefix + "/data/gfx/balloon/balloon1.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon/balloon1.png", t_bitmap);
asset->add(prefix + "/data/gfx/balloon/balloon1.ani", t_animation); asset->add(prefix + "/data/gfx/balloon/balloon1.ani", t_animation);
asset->add(prefix + "/data/gfx/balloon/balloon2.png", t_bitmap); asset->add(prefix + "/data/gfx/balloon/balloon2.png", t_bitmap);

View File

@@ -1,12 +1,13 @@
#include "global_inputs.h" #include "global_inputs.h"
#include <string> // for basic_string, operator+ #include <string> // for basic_string, operator+
#include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_REPEAT #include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_REPEAT
#include "jail_audio.h" // for JA_EnableMusic, JA_EnableSound #include "jail_audio.h" // for JA_EnableMusic, JA_EnableSound
#include "lang.h" // for getText #include "lang.h" // for getText
#include "options.h" // for options #include "options.h" // for options
#include "screen.h" // for Screen #include "on_screen_help.h"
#include "section.h" // for options_e, name, name_e, options #include "screen.h" // for Screen
#include "utils.h" // for op_audio_t, options_t, op_music_t, boolToOnOff #include "section.h" // for options_e, name, name_e, options
#include "utils.h" // for op_audio_t, options_t, op_music_t, boolToOnOff
// Termina // Termina
void quit(section::options_e code) void quit(section::options_e code)
@@ -61,6 +62,12 @@ void checkGlobalInputs()
return; return;
} }
else if (Input::get()->checkInput(input_service, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
OnScreenHelp::get()->toggleState();
return;
}
for (int i = 0; i < Input::get()->getNumControllers(); ++i) for (int i = 0; i < Input::get()->getNumControllers(); ++i)
{ {
// Comprueba si se sale con el mando // Comprueba si se sale con el mando

View File

@@ -1,8 +1,36 @@
#include "on_screen_help.h" #include "on_screen_help.h"
#include "screen.h"
#include "asset.h"
#include "input.h"
#include "text.h"
#include "sprite.h"
#include "texture.h"
#include "lang.h"
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
OnScreenHelp *OnScreenHelp::onScreenHelp = nullptr; OnScreenHelp *OnScreenHelp::onScreenHelp = nullptr;
// Constantes evaluables en tiempo de compilación
constexpr int ICONSIZE = 16;
constexpr SDL_Point PADDING = {8, 8};
constexpr SDL_Point DESP = {ICONSIZE + 4, 5};
constexpr SDL_Rect CONTROLLER_UP = {16, 16, ICONSIZE, ICONSIZE};
constexpr SDL_Rect CONTROLLER_DOWN = {48, 16, ICONSIZE, ICONSIZE};
constexpr SDL_Rect CONTROLLER_LEFT = {64, 16, ICONSIZE, ICONSIZE};
constexpr SDL_Rect LEFT_BUTTON = {48, 144, ICONSIZE, ICONSIZE};
constexpr SDL_Rect TOP_BUTTON = {0, 144, ICONSIZE, ICONSIZE};
constexpr SDL_Rect RIGHT_BUTTON = {16, 144, ICONSIZE, ICONSIZE};
constexpr SDL_Rect START_BUTTON = {128, 320, ICONSIZE, ICONSIZE};
constexpr SDL_Rect CONTROLLER_UP_POS = {PADDING.x, PADDING.y + 18 * 0, ICONSIZE, ICONSIZE};
constexpr SDL_Rect CONTROLLER_DOWN_POS = {PADDING.x, PADDING.y + 18 * 1, ICONSIZE, ICONSIZE};
constexpr SDL_Rect CONTROLLER_LEFT_POS = {PADDING.x, PADDING.y + 18 * 2, ICONSIZE, ICONSIZE};
constexpr SDL_Rect LEFT_BUTTON_POS = {PADDING.x, PADDING.y + 18 * 3, ICONSIZE, ICONSIZE};
constexpr SDL_Rect TOP_BUTTON_POS = {PADDING.x, PADDING.y + 18 * 4, ICONSIZE, ICONSIZE};
constexpr SDL_Rect RIGHT_BUTTON_POS = {PADDING.x, PADDING.y + 18 * 5, ICONSIZE, ICONSIZE};
constexpr SDL_Rect START_BUTTON_POS = {PADDING.x, PADDING.y + 18 * 6, ICONSIZE, ICONSIZE};
// [SINGLETON] Crearemos el objeto onScreenHelp con esta función estática // [SINGLETON] Crearemos el objeto onScreenHelp con esta función estática
void OnScreenHelp::init() void OnScreenHelp::init()
{ {
@@ -22,11 +50,143 @@ OnScreenHelp *OnScreenHelp::get()
} }
// Constructor // Constructor
OnScreenHelp::OnScreenHelp() OnScreenHelp::OnScreenHelp() : state(OnScreenHelpStatus::showing)
{ {
setSize();
texture = SDL_CreateTexture(Screen::get()->getRenderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, dest.w, dest.h);
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
fillTexture();
} }
// Destructor // Destructor
OnScreenHelp::~OnScreenHelp() OnScreenHelp::~OnScreenHelp()
{ {
SDL_DestroyTexture(texture);
}
// Actualiza la lógica interna
void OnScreenHelp::update()
{
}
// Muestra el objeto en pantalla
void OnScreenHelp::render()
{
if (state != OnScreenHelpStatus::hidden)
{
SDL_RenderCopy(Screen::get()->getRenderer(), texture, nullptr, &dest);
}
}
// Rellena la textura con los gráficos y texto
void OnScreenHelp::fillTexture()
{
// Cambia el renderizador a la textura
SDL_Texture *temp = SDL_GetRenderTarget(Screen::get()->getRenderer());
SDL_SetRenderTarget(Screen::get()->getRenderer(), texture);
// Crea el objeto para el texto
Text *text = new Text(Asset::get()->get("8bithud.png"), Asset::get()->get("8bithud.txt"), Screen::get()->getRenderer());
// Crea la textura con los gráficos
Texture *controllersTexture = new Texture(Screen::get()->getRenderer(), Asset::get()->get("controllers.png"));
// Crea el sprite para dibujar los gráficos
Sprite *sprite = new Sprite({0, 0, 16, 16}, controllersTexture);
// Borra la textura
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0, 0, 0, 0);
SDL_RenderClear(Screen::get()->getRenderer());
// Pon el color de fondo con el bisel
SDL_SetRenderDrawColor(Screen::get()->getRenderer(), 0x55, 0x5D, 0x77, 255);
SDL_Rect rect;
rect = {4, 0, dest.w - (4 * 2), dest.h};
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
rect = {4 / 2, 1, dest.w - 4, dest.h - 2};
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
rect = {1, 4 / 2, dest.w - 2, dest.h - 4};
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
rect = {0, 4, dest.w, dest.h - (4 * 2)};
SDL_RenderFillRect(Screen::get()->getRenderer(), &rect);
// Dibuja los iconos y el texto
sprite->setSpriteClip(CONTROLLER_UP);
sprite->setPos(CONTROLLER_UP_POS);
sprite->render();
text->write(CONTROLLER_UP_POS.x + DESP.x, CONTROLLER_UP_POS.y + DESP.y, lang::getText(107));
sprite->setSpriteClip(CONTROLLER_DOWN);
sprite->setPos(CONTROLLER_DOWN_POS);
sprite->render();
text->write(CONTROLLER_DOWN_POS.x + DESP.x, CONTROLLER_DOWN_POS.y + DESP.y, lang::getText(108));
sprite->setSpriteClip(CONTROLLER_LEFT);
sprite->setPos(CONTROLLER_LEFT_POS);
sprite->render();
text->write(CONTROLLER_LEFT_POS.x + DESP.x, CONTROLLER_LEFT_POS.y + DESP.y, lang::getText(109));
sprite->setSpriteClip(LEFT_BUTTON);
sprite->setPos(LEFT_BUTTON_POS);
sprite->render();
text->write(LEFT_BUTTON_POS.x + DESP.x, LEFT_BUTTON_POS.y + DESP.y, lang::getText(110));
sprite->setSpriteClip(TOP_BUTTON);
sprite->setPos(TOP_BUTTON_POS);
sprite->render();
text->write(TOP_BUTTON_POS.x + DESP.x, TOP_BUTTON_POS.y + DESP.y, lang::getText(111));
sprite->setSpriteClip(RIGHT_BUTTON);
sprite->setPos(RIGHT_BUTTON_POS);
sprite->render();
text->write(RIGHT_BUTTON_POS.x + DESP.x, RIGHT_BUTTON_POS.y + DESP.y, lang::getText(112));
sprite->setSpriteClip(START_BUTTON);
sprite->setPos(START_BUTTON_POS);
sprite->render();
text->write(START_BUTTON_POS.x + DESP.x, START_BUTTON_POS.y + DESP.y, lang::getText(113));
// text->write(0, 0, "hola");
// Restaura el destino de renderizado
SDL_SetRenderTarget(Screen::get()->getRenderer(), temp);
// Elimina los objetos
delete text;
delete controllersTexture;
delete sprite;
}
// Define el ancho y alto de la textura
void OnScreenHelp::setSize()
{
const int textSize = getLargestStringSize();
dest = (SDL_Rect){8, 8, PADDING.x + DESP.x + textSize + PADDING.x, PADDING.y + (7 * 18) + PADDING.y};
}
// Activa o desactiva el objeto
void OnScreenHelp::toggleState()
{
state = state == OnScreenHelpStatus::showing ? OnScreenHelpStatus::hidden : OnScreenHelpStatus::showing;
}
// Calcula la longitud en pixels del texto más largo
int OnScreenHelp::getLargestStringSize() const
{
Text *text = new Text(Asset::get()->get("8bithud.png"), Asset::get()->get("8bithud.txt"), Screen::get()->getRenderer());
int size = 0;
for (int i = 107; i <= 113; ++i)
{
const int textSize = text->lenght(lang::getText(i));
size = textSize > size ? textSize : size;
}
delete text;
return size;
} }

View File

@@ -27,6 +27,9 @@ private:
Input *input; // Objeto pata gestionar la entrada Input *input; // Objeto pata gestionar la entrada
SDL_Texture *texture; // Textura donde dibujar SDL_Texture *texture; // Textura donde dibujar
SDL_Rect dest; // Posición donde dibujar la textura;
OnScreenHelpStatus state; // Estado del objeto
// [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera // [SINGLETON] Ahora el constructor y el destructor son privados, para no poder crear objetos screen desde fuera
@@ -36,6 +39,15 @@ private:
// Destructor // Destructor
~OnScreenHelp(); ~OnScreenHelp();
// Rellena la textura con los gráficos y texto
void fillTexture();
// Define el ancho y alto de la textura
void setSize();
// Calcula la longitud en pixels del texto más largo
int getLargestStringSize() const;
public: public:
// [SINGLETON] Crearemos el objeto screen con esta función estática // [SINGLETON] Crearemos el objeto screen con esta función estática
static void init(); static void init();
@@ -45,4 +57,13 @@ public:
// [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él // [SINGLETON] Con este método obtenemos el objeto screen y podemos trabajar con él
static OnScreenHelp *get(); static OnScreenHelp *get();
// Actualiza la lógica interna
void update();
// Muestra el objeto en pantalla
void render();
// Activa o desactiva el objeto
void toggleState();
}; };

View File

@@ -1,20 +1,21 @@
#include "screen.h" #include "screen.h"
#include <SDL2/SDL_events.h> // for SDL_DISABLE, SDL_ENABLE #include <SDL2/SDL_events.h> // for SDL_DISABLE, SDL_ENABLE
#include <SDL2/SDL_mouse.h> // for SDL_ShowCursor #include <SDL2/SDL_mouse.h> // for SDL_ShowCursor
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888 #include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_timer.h> // for SDL_GetTicks #include <SDL2/SDL_timer.h> // for SDL_GetTicks
#include <algorithm> // for max, min #include <algorithm> // for max, min
#include <fstream> // for basic_ifstream, ifstream #include <fstream> // for basic_ifstream, ifstream
#include <iterator> // for istreambuf_iterator, operator!= #include <iterator> // for istreambuf_iterator, operator!=
#include <string> // for basic_string, operator+, to_string, cha... #include <string> // for basic_string, operator+, to_string, cha...
#include "asset.h" // for Asset #include "asset.h" // for Asset
#include "dbgtxt.h" // for dbg_print #include "dbgtxt.h" // for dbg_print
#include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_REPEAT #include "input.h" // for Input, inputs_e, INPUT_DO_NOT_ALLOW_REPEAT
#include "notify.h" // for Notify #include "notify.h" // for Notify
#include "options.h" // for options #include "options.h" // for options
#include "on_screen_help.h"
#ifndef NO_SHADERS #ifndef NO_SHADERS
#include "jail_shader.h" // for init, render #include "jail_shader.h" // for init, render
#endif #endif
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado // [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
@@ -39,11 +40,9 @@ Screen *Screen::get()
} }
// Constructor // Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) Screen::Screen(SDL_Window *window, SDL_Renderer *renderer) : window(window), renderer(renderer)
{ {
// Copia punteros // Copia punteros
this->window = window;
this->renderer = renderer;
input = Input::get(); input = Input::get();
asset = Asset::get(); asset = Asset::get();
@@ -68,7 +67,7 @@ Screen::Screen(SDL_Window *window, SDL_Renderer *renderer)
fpsCounter = 0; fpsCounter = 0;
fps = 0; fps = 0;
#ifdef DEBUG #ifdef DEBUG
showInfo = true; showInfo = false;
#else #else
showInfo = false; showInfo = false;
#endif #endif
@@ -115,14 +114,17 @@ void Screen::start()
// Vuelca el contenido del renderizador en pantalla // Vuelca el contenido del renderizador en pantalla
void Screen::blit() void Screen::blit()
{ {
// Actualiza el contador de FPS
fpsCounter++;
// Actualiza y dibuja el efecto de flash en la pantalla // Actualiza y dibuja el efecto de flash en la pantalla
doFlash(); doFlash();
// Atenua la pantalla // Atenua la pantalla
doAttenuate(); doAttenuate();
// Actualiza el contador de FPS // Muestra la ayuda por pantalla
fpsCounter++; OnScreenHelp::get()->render();
// Muestra información por pantalla // Muestra información por pantalla
displayInfo(); displayInfo();