Files
coffee_crisis_arcade_edition/source/tiled_bg.cpp

116 lines
3.2 KiB
C++

#include "tiled_bg.h"
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
#include <SDL2/SDL_stdinc.h> // for SDL_sinf
#include <stdlib.h> // for rand
#include <memory> // for unique_ptr, make_shared, make_unique
#include "screen.h" // for Screen
#include "sprite.h" // for Sprite
#include "texture.h" // for Texture
// Constructor
Tiledbg::Tiledbg(std::string texture_path, SDL_Rect pos, int mode)
: texture_path_(texture_path), pos_(pos), mode_(mode)
{
// Copia los punteros
renderer_ = Screen::get()->getRenderer();
// 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;
if (mode_ == TILED_MODE_RANDOM)
{
mode_ = rand() % 2;
}
tile_width_ = 64;
tile_height_ = 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
auto bg_tile_texture = std::make_shared<Texture>(renderer_, texture_path_);
auto tile = std::make_unique<Sprite>(bg_tile_texture, (SDL_Rect){0, 0, tile_width_, tile_height_});
// Prepara para dibujar sobre la textura
auto temp = SDL_GetRenderTarget(renderer_);
SDL_SetRenderTarget(renderer_, canvas_);
// Rellena la textura con el tile
const auto i_max = pos_.w * 2 / tile_width_;
const auto j_max = pos_.h * 2 / tile_height_;
tile->setSpriteClip(0, 0, tile_width_, tile_height_);
for (int i = 0; i < i_max; ++i)
{
for (int j = 0; j < j_max; ++j)
{
tile->setPosX(i * tile_width_);
tile->setPosY(j * tile_height_);
tile->render();
}
}
// Vuelve a colocar el renderizador como estaba
SDL_SetRenderTarget(renderer_, temp);
// Libera la memoria utilizada por los objetos
bg_tile_texture->unload();
}
// 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_ == TILED_MODE_DIAGONAL)
{ // El tileado de fondo se desplaza en diagonal
++window_.x %= tile_width_;
++window_.y %= tile_height_;
}
else if (mode_ == TILED_MODE_CIRCLE)
{ // 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();
}