Precàrrega de tots els recursos al inici del joc
8.000.000 de cherrypickings que he anat fent pel codi
This commit is contained in:
@@ -1,105 +1,84 @@
|
||||
#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
|
||||
#include <SDL2/SDL_pixels.h> // for SDL_PIXELFORMAT_RGBA8888
|
||||
#include <stdlib.h> // for rand
|
||||
#include <memory> // for unique_ptr, make_shared, make_unique
|
||||
#include <cmath> // for sinf
|
||||
#include "resource.h" // for Resource
|
||||
#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)
|
||||
TiledBG::TiledBG(SDL_Rect pos, int mode)
|
||||
: renderer_(Screen::get()->getRenderer()),
|
||||
pos_(pos),
|
||||
counter_(0),
|
||||
mode_(mode == TILED_MODE_RANDOM ? rand() % 2 : 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;
|
||||
window_ = {128, 96, pos_.w, 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);
|
||||
sin_[i] = std::sinf(i * 3.14159f / 180.0f); // Convierte grados a radianes y calcula el seno
|
||||
}
|
||||
}
|
||||
|
||||
// Destructor
|
||||
TiledBG::~TiledBG()
|
||||
{
|
||||
SDL_DestroyTexture(canvas_);
|
||||
}
|
||||
|
||||
// Rellena la textura con el contenido
|
||||
void Tiledbg::fillTexture()
|
||||
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_});
|
||||
auto tile = std::make_unique<Sprite>(Resource::get()->getTexture("title_bg_tile.png"), (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_);
|
||||
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->setX(i * tile_width_);
|
||||
tile->setY(j * tile_height_);
|
||||
tile->setX(i * TILE_WIDTH_);
|
||||
tile->setY(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()
|
||||
void TiledBG::render()
|
||||
{
|
||||
SDL_RenderCopy(renderer_, canvas_, &window_, &pos_);
|
||||
}
|
||||
|
||||
// Actualiza la lógica de la clase
|
||||
void Tiledbg::update()
|
||||
void TiledBG::update()
|
||||
{
|
||||
if (mode_ == TILED_MODE_DIAGONAL)
|
||||
{ // El tileado de fondo se desplaza en diagonal
|
||||
++window_.x %= tile_width_;
|
||||
++window_.y %= tile_height_;
|
||||
++window_.x %= TILE_WIDTH_;
|
||||
++window_.y %= TILE_HEIGHT_;
|
||||
}
|
||||
else if (mode_ == TILED_MODE_CIRCLE)
|
||||
{ // El tileado de fondo se desplaza en circulo
|
||||
@@ -110,7 +89,7 @@ void Tiledbg::update()
|
||||
}
|
||||
|
||||
// Recarga las texturas
|
||||
void Tiledbg::reLoad()
|
||||
void TiledBG::reLoad()
|
||||
{
|
||||
fillTexture();
|
||||
}
|
||||
Reference in New Issue
Block a user