Creada la clase tiledbg para gestionar el mosaico de fondo de la clase titulo

This commit is contained in:
2024-06-12 12:25:14 +02:00
parent 90bfd8349b
commit 832cd12d5f
5 changed files with 176 additions and 97 deletions

View File

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