Files
coffee_crisis_arcade_edition/source/tiledbg.cpp

110 lines
2.7 KiB
C++

#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();
}