pasaeta loca de clang-format (despres m'arrepentiré pero bueno)
This commit is contained in:
@@ -1,17 +1,18 @@
|
||||
#define _USE_MATH_DEFINES
|
||||
#include "background.h"
|
||||
|
||||
#include <SDL3/SDL.h> // Para SDL_SetRenderTarget, SDL_FRect, SDL_Creat...
|
||||
#include <SDL3/SDL.h> // Para SDL_SetRenderTarget, SDL_FRect, SDL_Creat...
|
||||
|
||||
#include <algorithm> // Para clamp, max
|
||||
#include <cmath> // Para cos, sin, M_PI
|
||||
#include <string> // Para basic_string
|
||||
|
||||
#include "moving_sprite.h" // Para MovingSprite
|
||||
#include "param.h" // Para Param, ParamBackground, param
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "texture.h" // Para Texture
|
||||
#include "moving_sprite.h" // Para MovingSprite
|
||||
#include "param.h" // Para Param, ParamBackground, param
|
||||
#include "resource.h" // Para Resource
|
||||
#include "screen.h" // Para Screen
|
||||
#include "sprite.h" // Para Sprite
|
||||
#include "texture.h" // Para Texture
|
||||
|
||||
// Constructor
|
||||
Background::Background()
|
||||
@@ -47,8 +48,7 @@ Background::Background()
|
||||
|
||||
const float TOP_CLOUDS_TEXTURE_HEIGHT = top_clouds_texture_->getHeight() / 4;
|
||||
const float BOTTOM_CLOUDS_TEXTURE_HEIGHT = bottom_clouds_texture_->getHeight() / 4;
|
||||
for (int i = 0; i < 4; ++i)
|
||||
{
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
top_clouds_rect_[i] = {0, i * TOP_CLOUDS_TEXTURE_HEIGHT, static_cast<float>(top_clouds_texture_->getWidth()), TOP_CLOUDS_TEXTURE_HEIGHT};
|
||||
bottom_clouds_rect_[i] = {0, i * BOTTOM_CLOUDS_TEXTURE_HEIGHT, static_cast<float>(bottom_clouds_texture_->getWidth()), BOTTOM_CLOUDS_TEXTURE_HEIGHT};
|
||||
}
|
||||
@@ -107,15 +107,13 @@ Background::Background()
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Background::~Background()
|
||||
{
|
||||
Background::~Background() {
|
||||
SDL_DestroyTexture(canvas_);
|
||||
SDL_DestroyTexture(color_texture_);
|
||||
}
|
||||
|
||||
// Actualiza la lógica del objeto
|
||||
void Background::update()
|
||||
{
|
||||
void Background::update() {
|
||||
// Actualiza el valor de alpha_
|
||||
updateAlphaColorTexture();
|
||||
|
||||
@@ -140,8 +138,7 @@ void Background::update()
|
||||
}
|
||||
|
||||
// Dibuja el gradiente de fondo
|
||||
void Background::renderGradient()
|
||||
{
|
||||
void Background::renderGradient() {
|
||||
// Dibuja el gradiente de detras
|
||||
gradients_texture_->setAlpha(255);
|
||||
gradient_sprite_->setSpriteClip(gradient_rect_[(gradient_number_ + 1) % 4]);
|
||||
@@ -154,8 +151,7 @@ void Background::renderGradient()
|
||||
}
|
||||
|
||||
// Dibuja las nubes de arriba
|
||||
void Background::renderTopClouds()
|
||||
{
|
||||
void Background::renderTopClouds() {
|
||||
// Dibuja el primer conjunto de nubes, las de detras
|
||||
top_clouds_texture_->setAlpha(255);
|
||||
top_clouds_sprite_a_->setSpriteClip(top_clouds_rect_[(gradient_number_ + 1) % 4]);
|
||||
@@ -172,8 +168,7 @@ void Background::renderTopClouds()
|
||||
}
|
||||
|
||||
// Dibuja las nubes de abajo
|
||||
void Background::renderBottomClouds()
|
||||
{
|
||||
void Background::renderBottomClouds() {
|
||||
// Dibuja el primer conjunto de nubes, las de detras
|
||||
bottom_clouds_texture_->setAlpha(255);
|
||||
bottom_clouds_sprite_a_->setSpriteClip(bottom_clouds_rect_[(gradient_number_ + 1) % 4]);
|
||||
@@ -190,8 +185,7 @@ void Background::renderBottomClouds()
|
||||
}
|
||||
|
||||
// Compone todos los elementos del fondo en la textura
|
||||
void Background::fillCanvas()
|
||||
{
|
||||
void Background::fillCanvas() {
|
||||
// Cambia el destino del renderizador
|
||||
auto temp = SDL_GetRenderTarget(renderer_);
|
||||
SDL_SetRenderTarget(renderer_, canvas_);
|
||||
@@ -220,8 +214,7 @@ void Background::fillCanvas()
|
||||
}
|
||||
|
||||
// Dibuja el objeto
|
||||
void Background::render()
|
||||
{
|
||||
void Background::render() {
|
||||
// Fondo
|
||||
SDL_RenderTexture(renderer_, canvas_, &src_rect_, &dst_rect_);
|
||||
|
||||
@@ -230,26 +223,22 @@ void Background::render()
|
||||
}
|
||||
|
||||
// Ajusta el valor de la variable
|
||||
void Background::setCloudsSpeed(float value)
|
||||
{
|
||||
void Background::setCloudsSpeed(float value) {
|
||||
clouds_speed_ = value;
|
||||
}
|
||||
|
||||
// Ajusta el valor de la variable
|
||||
void Background::setGradientNumber(int value)
|
||||
{
|
||||
void Background::setGradientNumber(int value) {
|
||||
gradient_number_ = value % 4;
|
||||
}
|
||||
|
||||
// Ajusta el valor de la variable
|
||||
void Background::setTransition(float value)
|
||||
{
|
||||
void Background::setTransition(float value) {
|
||||
transition_ = std::clamp(value, 0.0f, 1.0f);
|
||||
}
|
||||
|
||||
// Establece la posición del objeto
|
||||
void Background::setPos(SDL_FRect pos)
|
||||
{
|
||||
void Background::setPos(SDL_FRect pos) {
|
||||
dst_rect_ = pos;
|
||||
|
||||
// Si cambian las medidas del destino, hay que cambiar las del origen para evitar deformar la imagen
|
||||
@@ -260,8 +249,7 @@ void Background::setPos(SDL_FRect pos)
|
||||
}
|
||||
|
||||
// Establece el color_ de atenuación
|
||||
void Background::setColor(Color color)
|
||||
{
|
||||
void Background::setColor(Color color) {
|
||||
attenuate_color_ = color;
|
||||
|
||||
// Colorea la textura
|
||||
@@ -275,8 +263,7 @@ void Background::setColor(Color color)
|
||||
}
|
||||
|
||||
// Establece la transparencia de la atenuación
|
||||
void Background::setAlpha(int alpha)
|
||||
{
|
||||
void Background::setAlpha(int alpha) {
|
||||
// Evita que se asignen valores fuera de rango
|
||||
alpha_ = std::clamp(alpha, 0, 255);
|
||||
|
||||
@@ -286,22 +273,17 @@ void Background::setAlpha(int alpha)
|
||||
}
|
||||
|
||||
// Actualiza el valor de alpha_
|
||||
void Background::updateAlphaColorTexture()
|
||||
{
|
||||
if (alpha_color_text_ == alpha_color_text_temp_)
|
||||
{
|
||||
void Background::updateAlphaColorTexture() {
|
||||
if (alpha_color_text_ == alpha_color_text_temp_) {
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
alpha_color_text_ > alpha_color_text_temp_ ? ++alpha_color_text_temp_ : --alpha_color_text_temp_;
|
||||
SDL_SetTextureAlphaMod(color_texture_, alpha_color_text_temp_);
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza las nubes
|
||||
void Background::updateClouds()
|
||||
{
|
||||
void Background::updateClouds() {
|
||||
// Aplica la velocidad calculada a las nubes
|
||||
top_clouds_sprite_a_->setVelX(clouds_speed_);
|
||||
top_clouds_sprite_b_->setVelX(clouds_speed_);
|
||||
@@ -315,37 +297,31 @@ void Background::updateClouds()
|
||||
bottom_clouds_sprite_b_->update();
|
||||
|
||||
// Calcula el offset de las nubes
|
||||
if (top_clouds_sprite_a_->getPosX() < -top_clouds_sprite_a_->getWidth())
|
||||
{
|
||||
if (top_clouds_sprite_a_->getPosX() < -top_clouds_sprite_a_->getWidth()) {
|
||||
top_clouds_sprite_a_->setPosX(top_clouds_sprite_a_->getWidth());
|
||||
}
|
||||
|
||||
if (top_clouds_sprite_b_->getPosX() < -top_clouds_sprite_b_->getWidth())
|
||||
{
|
||||
if (top_clouds_sprite_b_->getPosX() < -top_clouds_sprite_b_->getWidth()) {
|
||||
top_clouds_sprite_b_->setPosX(top_clouds_sprite_b_->getWidth());
|
||||
}
|
||||
|
||||
if (bottom_clouds_sprite_a_->getPosX() < -bottom_clouds_sprite_a_->getWidth())
|
||||
{
|
||||
if (bottom_clouds_sprite_a_->getPosX() < -bottom_clouds_sprite_a_->getWidth()) {
|
||||
bottom_clouds_sprite_a_->setPosX(bottom_clouds_sprite_a_->getWidth());
|
||||
}
|
||||
|
||||
if (bottom_clouds_sprite_b_->getPosX() < -bottom_clouds_sprite_b_->getWidth())
|
||||
{
|
||||
if (bottom_clouds_sprite_b_->getPosX() < -bottom_clouds_sprite_b_->getWidth()) {
|
||||
bottom_clouds_sprite_b_->setPosX(bottom_clouds_sprite_b_->getWidth());
|
||||
}
|
||||
}
|
||||
|
||||
// Precalcula el vector con el recorrido del sol
|
||||
void Background::createSunPath()
|
||||
{
|
||||
void Background::createSunPath() {
|
||||
constexpr float CENTER_X = 170;
|
||||
const float center_y = base_ - 80;
|
||||
constexpr float RADIUS = 120;
|
||||
|
||||
// Generar puntos de la curva desde 90 a 180 grados
|
||||
for (double theta = M_PI / 2; theta <= M_PI; theta += 0.01)
|
||||
{
|
||||
for (double theta = M_PI / 2; theta <= M_PI; theta += 0.01) {
|
||||
float x = CENTER_X + (RADIUS * cos(theta));
|
||||
float y = center_y - (RADIUS * sin(theta));
|
||||
sun_path_.push_back({x, y});
|
||||
@@ -354,22 +330,19 @@ void Background::createSunPath()
|
||||
// Agregar puntos en línea recta después de la curva
|
||||
constexpr int EXTRA_PIXELS = 40;
|
||||
SDL_FPoint last_point = sun_path_.back();
|
||||
for (int i = 1; i <= EXTRA_PIXELS; ++i)
|
||||
{
|
||||
for (int i = 1; i <= EXTRA_PIXELS; ++i) {
|
||||
sun_path_.push_back({last_point.x, last_point.y + i});
|
||||
}
|
||||
}
|
||||
|
||||
// Precalcula el vector con el recorrido de la luna
|
||||
void Background::createMoonPath()
|
||||
{
|
||||
void Background::createMoonPath() {
|
||||
constexpr float CENTER_X = 100;
|
||||
const float center_y = base_ - 50;
|
||||
constexpr float RADIUS = 140;
|
||||
|
||||
// Generar puntos de la curva desde 0 a 90 grados
|
||||
for (double theta = 0; theta <= M_PI / 2; theta += 0.01)
|
||||
{
|
||||
for (double theta = 0; theta <= M_PI / 2; theta += 0.01) {
|
||||
float x = CENTER_X + (RADIUS * cos(theta));
|
||||
float y = center_y - (RADIUS * sin(theta));
|
||||
moon_path_.push_back({x, y});
|
||||
@@ -377,15 +350,13 @@ void Background::createMoonPath()
|
||||
}
|
||||
|
||||
// Establece la posición del sol
|
||||
void Background::setSunProgression(float progress)
|
||||
{
|
||||
void Background::setSunProgression(float progress) {
|
||||
progress = std::clamp(progress, 0.0f, 1.0f);
|
||||
sun_index_ = static_cast<size_t>(progress * (sun_path_.size() - 1));
|
||||
}
|
||||
|
||||
// Establece la posición de la luna
|
||||
void Background::setMoonProgression(float progress)
|
||||
{
|
||||
void Background::setMoonProgression(float progress) {
|
||||
progress = std::clamp(progress, 0.0f, 1.0f);
|
||||
moon_index_ = static_cast<size_t>(progress * (moon_path_.size() - 1));
|
||||
}
|
||||
Reference in New Issue
Block a user