cmasmasizat el codi

This commit is contained in:
2025-03-24 13:28:54 +01:00
parent 4a2e5c27e2
commit e2e3b7c779
6 changed files with 75 additions and 111 deletions
+36 -59
View File
@@ -5,31 +5,33 @@
#include "dbgtxt.h"
#include <iostream>
#include <vector>
#include <array>
#include <cstdlib>
#include <ctime>
#include <memory>
// Variables globales
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
Texture *texture = nullptr;
std::vector<Ball *> balls;
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
std::shared_ptr<Texture> texture = nullptr;
std::vector<std::unique_ptr<Ball>> balls;
std::array<int, 8> test = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
bool should_exit = false; // Controla si la aplicación debe cerrarse
Uint64 ticks = 0; // Tiempo en milisegundos para controlar la actualización
int scenario = 0; // Escenario actual basado en el número de bolas
std::string text = ""; // Texto a mostrar en pantalla
std::string text; // Texto a mostrar en pantalla
int text_pos = 0; // Posición del texto en la pantalla
bool show_text = true; // Determina si el texto se debe mostrar
Uint64 text_init_time = 0; // Temporizador para mostrar el texto
void deleteBalls(); // Declaración de una función para eliminar bolas
// Establece el texto en pantalla mostrando el número de bolas actuales
void setText()
{
const std::string TEXT2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
text = std::to_string(test[scenario]) + TEXT2;
const int SIZE = text.size() * 8;
text_pos = SCREEN_WIDTH / 2 - SIZE / 2;
const std::string TEXT_NUMBER = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
text = std::to_string(test[scenario]) + TEXT_NUMBER;
const int TEXT_SIZE = static_cast<int>(text.size() * 8);
text_pos = SCREEN_WIDTH / 2 - TEXT_SIZE / 2;
text_init_time = SDL_GetTicks();
show_text = true;
}
@@ -37,27 +39,26 @@ void setText()
// Inicializa las bolas según el escenario seleccionado
void initBalls(int value)
{
deleteBalls(); // Limpia las bolas actuales
for (int i = 0; i < test[value]; ++i)
balls.clear();
for (int i = 0; i < test.at(value); ++i)
{
const int SIGN = ((rand() % 2) * 2) - 1; // Genera un signo aleatorio (+ o -)
const float X = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); // Posición inicial en X
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN; // Velocidad en X
const float VY = ((rand() % 60) - 30) * 0.1f; // Velocidad en Y
const Color COLOR = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; // Color aleatorio
Ball *b = new Ball(X, VX, VY, COLOR, texture);
balls.push_back(b); // Añadir la nueva bola al vector
balls.emplace_back(std::make_unique<Ball>(X, VX, VY, COLOR, texture));
}
setText(); // Actualizar el texto
setText(); // Actualiza el texto
}
// Aumenta la velocidad vertical de las bolas "hacia arriba"
void pushUpBalls()
{
for (auto ball : balls)
for (auto &ball : balls)
{
const int SIGN = ((rand() % 2) * 2) - 1;
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
const int SIGNO = ((rand() % 2) * 2) - 1;
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGNO;
const float VY = ((rand() % 40) * 0.1f) + 5;
ball->modVel(VX, -VY); // Modifica la velocidad de la bola
}
@@ -66,38 +67,21 @@ void pushUpBalls()
// Cambia la gravedad de todas las bolas
void switchBallsGravity()
{
for (auto ball : balls)
for (auto &ball : balls)
{
if (ball)
{
ball->switchGravity();
}
ball->switchGravity();
}
}
// Elimina todas las bolas y libera memoria
void deleteBalls()
{
for (auto ball : balls)
{
if (ball)
{
delete ball;
ball = nullptr;
}
}
balls.clear(); // Limpia el vector
}
// Inicializa SDL y configura los componentes principales
bool init()
{
bool success = true; // Bandera de éxito
bool success = true;
// Inicializa SDL
if (!SDL_Init(SDL_INIT_VIDEO))
{
printf("¡SDL no se pudo inicializar! Error de SDL: %s\n", SDL_GetError());
std::cout << "¡SDL no se pudo inicializar! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
}
else
@@ -106,7 +90,7 @@ bool init()
window = SDL_CreateWindow(WINDOW_CAPTION, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_OPENGL);
if (window == nullptr)
{
printf("¡No se pudo crear la ventana! Error de SDL: %s\n", SDL_GetError());
std::cout << "¡No se pudo crear la ventana! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
}
else
@@ -115,7 +99,7 @@ bool init()
renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == nullptr)
{
printf("¡No se pudo crear el renderizador! Error de SDL: %s\n", SDL_GetError());
std::cout << "¡No se pudo crear el renderizador! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
}
else
@@ -130,10 +114,10 @@ bool init()
}
// Inicializar otros componentes
texture = new Texture(renderer, "resources/pelota.png");
texture = std::make_shared<Texture>(renderer, "resources/ball.png");
ticks = SDL_GetTicks();
srand(time(nullptr));
dbg_init(renderer); // Inicializar herramientas de depuración
srand(static_cast<unsigned>(time(nullptr)));
dbg_init(renderer);
initBalls(scenario);
return success;
@@ -144,18 +128,7 @@ void close()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = nullptr;
renderer = nullptr;
if (texture)
{
delete texture;
texture = nullptr;
}
deleteBalls(); // Liberar memoria de las bolas
SDL_Quit(); // Finalizar SDL
SDL_Quit();
}
// Verifica los eventos en la cola
@@ -239,7 +212,7 @@ void update()
{
ticks = SDL_GetTicks();
for (auto ball : balls)
for (auto &ball : balls)
{
ball->update();
}
@@ -257,7 +230,7 @@ void render()
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
SDL_RenderClear(renderer);
for (auto ball : balls)
for (auto &ball : balls)
{
ball->render();
}
@@ -273,7 +246,11 @@ void render()
// Función principal
int main(int argc, char *args[])
{
init();
if (!init())
{
std::cout << "Ocurrió un error durante la inicialización." << std::endl;
return 1;
}
while (!should_exit)
{