diff --git a/source/tiledbg.cpp b/source/tiledbg.cpp new file mode 100644 index 0000000..5f3460f --- /dev/null +++ b/source/tiledbg.cpp @@ -0,0 +1,110 @@ +#include "tiledbg.h" + +// Constructor +Tiledbg::Tiledbg(SDL_Renderer *renderer, Screen *screen, Asset *asset, SDL_Rect pos) +{ + // Copia los punteros + this->renderer = renderer; + this->screen = screen; + this->asset = asset; + this->pos = pos; + + // Crea la textura para el mosaico de fondo + canvas = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, pos.w * 2, pos.h * 2); + + // Inicializa las variables + init(); +} + +// Destructor +Tiledbg::~Tiledbg() +{ + SDL_DestroyTexture(canvas); +} + +// Inicializa las variables +void Tiledbg::init() +{ + counter = 0; + mode = rand() % 2; + tileWidth = 64; + tileHeight = 64; + + // Rellena la textura con el contenido + fillTexture(); + + // Coloca la ventana que recorre el mosaico de fondo de manera que coincida + // con el mosaico que hay pintado en el titulo al iniciar + window.x = 128; + window.y = 96; + window.w = pos.w; + window.h = pos.h; + + // Inicializa los valores del vector con los valores del seno + for (int i = 0; i < 360; ++i) + { + sin[i] = SDL_sinf((float)i * 3.14f / 180.0f); + } +} + +// Rellena la textura con el contenido +void Tiledbg::fillTexture() +{ + // Crea los objetos para pintar en la textura de fondo + Texture *bgTileTexture = new Texture(renderer, asset->get("title_bg_tile.png")); + Sprite *tile = new Sprite({0, 0, tileWidth, tileHeight}, bgTileTexture, renderer); + + // Prepara para dibujar sobre la textura + SDL_Texture *temp = SDL_GetRenderTarget(renderer); + SDL_SetRenderTarget(renderer, canvas); + + // Rellena la textura con el tile + const int iMax = pos.w * 2 / tileWidth; + const int jMax = pos.h * 2 / tileHeight; + tile->setSpriteClip(0, 0, tileWidth, tileHeight); + for (int i = 0; i < iMax; ++i) + { + for (int j = 0; j < jMax; ++j) + { + tile->setPosX(i * tileWidth); + tile->setPosY(j * tileHeight); + tile->render(); + } + } + + // Vuelve a colocar el renderizador como estaba + SDL_SetRenderTarget(renderer, temp); + + // Libera la memoria utilizada por los objetos + bgTileTexture->unload(); + delete bgTileTexture; + delete tile; +} + +// Pinta la clase en pantalla +void Tiledbg::render() +{ + SDL_RenderCopy(renderer, canvas, &window, &pos); +} + +// Actualiza la lógica de la clase +void Tiledbg::update() +{ + if (mode == 0) + { // El tileado de fondo se desplaza en diagonal + ++window.x %= tileWidth; + ++window.y %= tileHeight; + } + else + { // El tileado de fondo se desplaza en circulo + ++counter %= 360; + window.x = 128 + (int(sin[(counter + 270) % 360] * 128)); + window.y = 96 + (int(sin[(360 - counter) % 360] * 96)); + } +} + +// Recarga las texturas +void Tiledbg::reLoad() +{ + fillTexture(); +} \ No newline at end of file diff --git a/source/tiledbg.h b/source/tiledbg.h new file mode 100644 index 0000000..7ac5da3 --- /dev/null +++ b/source/tiledbg.h @@ -0,0 +1,54 @@ +#pragma once + +#include +#include "common/asset.h" +#include "common/screen.h" +#include "common/sprite.h" +#include "const.h" + +#ifndef TILEDBG_H +#define TILEDBG_H + +// Clase Tiledbg +class Tiledbg +{ +private: + // Objetos y punteros + SDL_Renderer *renderer; // El renderizador de la ventana + Screen *screen; // Objeto encargado de dibujar en pantalla + Asset *asset; // Objeto que gestiona todos los ficheros de recursos + SDL_Rect window; // Ventana visible para la textura de fondo del titulo + SDL_Texture *canvas; // Textura dibujar el fondo del titulo + + // Variables + SDL_Rect pos; // Posición y tamaña del mosaico + int counter; // Contador + Uint8 mode; // Tipo de movimiento del mosaico + float sin[360]; // Vector con los valores del seno precalculados + int tileWidth; // Ancho del tile + int tileHeight; // Alto del tile + + // Inicializa las variables + void init(); + + // Rellena la textura con el contenido + void fillTexture(); + +public: + // Constructor + Tiledbg(SDL_Renderer *renderer, Screen *screen, Asset *asset, SDL_Rect pos); + + // Destructor + ~Tiledbg(); + + // Pinta la clase en pantalla + void render(); + + // Actualiza la lógica de la clase + void update(); + + // Recarga las texturas + void reLoad(); +}; + +#endif \ No newline at end of file diff --git a/source/title.cpp b/source/title.cpp index b500e95..a7df137 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -35,6 +35,8 @@ Title::Title(SDL_Renderer *renderer, Screen *screen, Input *input, Asset *asset, backgroundObj->setGradientNumber(1); backgroundObj->setTransition(0.8f); + tiledbg = new Tiledbg(renderer, screen, asset, {0, 0, GAMECANVAS_WIDTH, GAMECANVAS_HEIGHT}); + // Sonidos crashSound = JA_LoadSound(asset->get("title.wav").c_str()); @@ -70,11 +72,10 @@ Title::~Title() delete text2; delete backgroundObj; + delete tiledbg; JA_DeleteSound(crashSound); JA_DeleteMusic(titleMusic); - - SDL_DestroyTexture(background); } // Inicializa los valores de las variables @@ -83,8 +84,6 @@ void Title::init() // Inicializa variables section->subsection = SUBSECTION_TITLE_1; counter = TITLE_COUNTER; - backgroundCounter = 0; - backgroundMode = rand() % 2; menuVisible = false; nextSection.name = SECTION_PROG_GAME; postFade = 0; @@ -169,22 +168,6 @@ void Title::init() dustBitmapL->setPosY(47); dustBitmapL->setWidth(16); dustBitmapL->setHeight(16); - - // Crea el mosaico de fondo del titulo - createTiledBackground(); - - // Coloca la ventana que recorre el mosaico de fondo de manera que coincida - // con el mosaico que hay pintado en el titulo al iniciar - backgroundWindow.x = 128; - backgroundWindow.y = 96; - backgroundWindow.w = GAMECANVAS_WIDTH; - backgroundWindow.h = GAMECANVAS_HEIGHT; - - // Inicializa los valores del vector con los valores del seno - for (int i = 0; i < 360; ++i) - { - sin[i] = SDL_sinf((float)i * 3.14f / 180.0f); - } } // Actualiza las variables del objeto @@ -310,8 +293,8 @@ void Title::update() } } - // Actualiza el tileado de fondo - updateBG(); + // Actualiza el mosaico de fondo + tiledbg->update(); } else if (counter == 0) { @@ -362,8 +345,8 @@ void Title::render() // Limpia la pantalla screen->clean(bgColor); - // Dibuja el tileado de fondo - SDL_RenderCopy(renderer, background, &backgroundWindow, nullptr); + // Dibuja el mosacico de fondo + tiledbg->render(); // backgroundObj->render(); // Dibuja el logo @@ -440,22 +423,6 @@ void Title::checkInput() } } -// Actualiza el tileado de fondo -void Title::updateBG() -{ - if (backgroundMode == 0) - { // El tileado de fondo se desplaza en diagonal - ++backgroundWindow.x %= 64; - ++backgroundWindow.y %= 64; - } - else - { // El tileado de fondo se desplaza en circulo - ++backgroundCounter %= 360; - backgroundWindow.x = 128 + (int(sin[(backgroundCounter + 270) % 360] * 128)); - backgroundWindow.y = 96 + (int(sin[(360 - backgroundCounter) % 360] * 96)); - } -} - // Cambia el valor de la variable de modo de pantalla completa void Title::switchFullScreenModeVar() { @@ -574,49 +541,6 @@ bool Title::updatePlayerInputs(int numPlayer) } } -// Crea el mosaico de fondo del titulo -void Title::createTiledBackground() -{ - // Crea la textura para el mosaico de fondo - background = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, GAMECANVAS_WIDTH * 2, GAMECANVAS_HEIGHT * 2); - if (background == nullptr) - { - if (options->console) - { - std::cout << "TitleSurface could not be created!\nSDL Error: " << SDL_GetError() << std::endl; - } - } - - // Crea los objetos para pintar en la textura de fondo - Texture *bgTileTexture = new Texture(renderer, asset->get("title_bg_tile.png")); - Sprite *tile = new Sprite({0, 0, 64, 64}, bgTileTexture, renderer); - - // Prepara para dibujar sobre la textura - SDL_SetRenderTarget(renderer, background); - SDL_SetRenderDrawColor(renderer, 0x43, 0x43, 0x4F, 0xFF); - SDL_RenderClear(renderer); - - // Rellena la textura con el tile - tile->setSpriteClip(0, 0, 64, 64); - for (int i = 0; i < 8; ++i) - { - for (int j = 0; j < 6; ++j) - { - tile->setPosX(i * 64); - tile->setPosY(j * 64); - tile->render(); - } - } - - // Vuelve a colocar el renderizador apuntando a la pantalla - SDL_SetRenderTarget(renderer, nullptr); - - // Libera la memoria utilizada por los objetos - bgTileTexture->unload(); - delete bgTileTexture; - delete tile; -} - // Comprueba cuantos mandos hay conectados para gestionar el menu de opciones void Title::checkInputDevices() { @@ -661,5 +585,5 @@ void Title::reLoadTextures() dustTexture->reLoad(); coffeeTexture->reLoad(); crisisTexture->reLoad(); - createTiledBackground(); + tiledbg->reLoad(); } \ No newline at end of file diff --git a/source/title.h b/source/title.h index 6875687..20d3754 100644 --- a/source/title.h +++ b/source/title.h @@ -19,6 +19,7 @@ #include "item.h" #include "lang.h" #include "background.h" +#include "tiledbg.h" #ifndef TITLE_H #define TITLE_H @@ -47,14 +48,12 @@ private: SDL_Event *eventHandler; // Manejador de eventos section_t *section; // Indicador para el bucle del titulo Background *backgroundObj; // Objeto para dibujar el fondo del juego + Tiledbg *tiledbg; // Objeto para dibujar el mosaico animado de fondo Texture *dustTexture; // Textura con los graficos del polvo Texture *coffeeTexture; // Textura con los graficos de la palabra coffee Texture *crisisTexture; // Textura con los graficos de la plabra crisis - SDL_Rect backgroundWindow; // Ventana visible para la textura de fondo del titulo - SDL_Texture *background; // Textura dibujar el fondo del titulo - AnimatedSprite *dustBitmapL; // Sprite con la el polvo que aparece al colisionar el texto de la pantalla de titulo AnimatedSprite *dustBitmapR; // Sprite con la el polvo que aparece al colisionar el texto de la pantalla de titulo @@ -68,11 +67,8 @@ private: // Variable JA_Music_t *titleMusic; // Musica para el titulo JA_Sound_t *crashSound; // Sonido con el impacto del título - int backgroundCounter; // Temporizador para el fondo de tiles de la pantalla de titulo int counter; // Temporizador para la pantalla de titulo Uint32 ticks; // Contador de ticks para ajustar la velocidad del programa - Uint8 backgroundMode; // Variable para almacenar el tipo de efecto que hará el fondo de la pantalla de titulo - float sin[360]; // Vector con los valores del seno precalculados bool menuVisible; // Indicador para saber si se muestra el menu del titulo o la frase intermitente bool demo; // Indica si el modo demo estará activo section_t nextSection; // Indica cual es la siguiente sección a cargar cuando termine el contador del titulo @@ -98,9 +94,6 @@ private: // Comprueba las entradas void checkInput(); - // Actualiza el tileado de fondo - void updateBG(); - // Cambia el valor de la variable de modo de pantalla completa void switchFullScreenModeVar(); @@ -116,9 +109,6 @@ private: // Modifica las opciones para los controles de los jugadores bool updatePlayerInputs(int numPlayer); - // Crea el mosaico de fondo del titulo - void createTiledBackground(); - // Comprueba cuantos mandos hay conectados para gestionar el menu de opciones void checkInputDevices(); diff --git a/todo.txt b/todo.txt index 5ff3664..4424c64 100644 --- a/todo.txt +++ b/todo.txt @@ -7,4 +7,5 @@ [] Valorar la opcion de que write devuelva texturas con el texto en lugar de pintar letra a letra [] Arreglar los anclajes en la pantalla de game over [] Al poner pausa, que se sigan moviendo las nubes -[] Revisar la clase Fade \ No newline at end of file +[] Revisar la clase Fade +[] Quitar los static de title / crear clase para el logo de coffee crisis \ No newline at end of file