actualitzat Makefile

This commit is contained in:
2025-03-23 09:03:50 +01:00
parent 578549b122
commit 111cbb2217
3 changed files with 113 additions and 74 deletions

View File

@@ -6,50 +6,52 @@
#include <iostream>
#include <vector>
// Variables globales
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
SDL_Event *event;
Texture *texture = nullptr;
std::vector<Ball *> balls;
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
bool shouldExit = false;
Uint32 ticks = 0;
int index = 0;
std::string text = "";
int textPos = 0;
bool showText = true;
int counter = 0;
bool shouldExit = false; // Controla si la aplicación debe cerrarse
Uint32 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
int textPos = 0; // Posición del texto en la pantalla
bool showText = true; // Determina si el texto se debe mostrar
int counter = 0; // Temporizador para mostrar el texto
void deleteBalls();
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[index] == 1 ? " PELOTA" : " PELOTAS";
text = std::to_string(test[index]) + text2;
const std::string text2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
text = std::to_string(test[scenario]) + text2;
const int size = text.size() * 8;
textPos = SCREEN_WIDTH / 2 - size / 2;
counter = TEXT_TIME;
showText = true;
}
// Inicializa las bolas según el escenario seleccionado
void initBalls(int value)
{
deleteBalls();
deleteBalls(); // Limpia las bolas actuales
for (int i = 0; i < test[value]; ++i)
{
const int sign = ((rand() % 2) * 2) - 1;
;
const float x = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4);
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
const float vy = ((rand() % 60) - 30) * 0.1f;
const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
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_t 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);
balls.push_back(b); // Añadir la nueva bola al vector
}
setText();
setText(); // Actualizar el texto
}
// Aumenta la velocidad vertical de las bolas "hacia arriba"
void pushUpBalls()
{
for (auto ball : balls)
@@ -57,10 +59,11 @@ void pushUpBalls()
const int sign = ((rand() % 2) * 2) - 1;
const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
const float vy = ((rand() % 40) * 0.1f) + 5;
ball->modVel(vx, -vy);
ball->modVel(vx, -vy); // Modifica la velocidad de la bola
}
}
// Cambia la gravedad de todas las bolas
void switchBallsGravity()
{
for (auto ball : balls)
@@ -68,11 +71,11 @@ void switchBallsGravity()
if (ball)
{
ball->switchGravity();
;
}
}
}
// Elimina todas las bolas y libera memoria
void deleteBalls()
{
for (auto ball : balls)
@@ -83,101 +86,95 @@ void deleteBalls()
ball = nullptr;
}
}
balls.clear();
balls.clear(); // Limpia el vector
}
// Inicializa SDL y configura los componentes principales
bool init()
{
// Initialization flag
bool success = true;
bool success = true; // Bandera de éxito
// Initialize SDL
// Inicializa SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
printf("¡SDL no se pudo inicializar! Error de SDL: %s\n", SDL_GetError());
success = false;
}
else
{
// Create window
// Crear ventana principal
window = SDL_CreateWindow(WINDOW_CAPTION, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_SHOWN);
if (window == nullptr)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
printf("¡No se pudo crear la ventana! Error de SDL: %s\n", SDL_GetError());
success = false;
}
else
{
// Create renderer for window
// Crear renderizador
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr)
{
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
printf("¡No se pudo crear el renderizador! Error de SDL: %s\n", SDL_GetError());
success = false;
}
else
{
// Initialize renderer color
// Establecer color inicial del renderizador
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// Establece el tamaño del renderizador
// Establecer tamaño lógico para el renderizado
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
}
}
}
event = new SDL_Event();
// Inicializar otros componentes
texture = new Texture(renderer, "resources/pelota.png");
ticks = SDL_GetTicks();
srand(time(nullptr));
dbg_init(renderer);
initBalls(index);
dbg_init(renderer); // Inicializar herramientas de depuración
initBalls(scenario);
return success;
}
// Limpia todos los recursos y cierra SDL
void close()
{
// Destroy window
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window = nullptr;
renderer = nullptr;
if (event)
{
delete event;
event = nullptr;
}
if (texture)
{
delete texture;
texture = nullptr;
}
deleteBalls();
deleteBalls(); // Liberar memoria de las bolas
// Quit SDL subsystems
SDL_Quit();
SDL_Quit(); // Finalizar SDL
}
// Verifica los eventos en la cola
void checkEvents()
{
// Comprueba los eventos que hay en la cola
while (SDL_PollEvent(event) != 0)
SDL_Event event;
while (SDL_PollEvent(&event) != 0)
{
// Evento de salida de la aplicación
if (event->type == SDL_QUIT)
// Evento de salida
if (event.type == SDL_QUIT)
{
shouldExit = true;
break;
}
if (event->type == SDL_KEYDOWN && event->key.repeat == 0)
// Procesar eventos de teclado
if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
{
switch (event->key.keysym.sym)
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
shouldExit = true;
@@ -192,49 +189,50 @@ void checkEvents()
break;
case SDLK_1:
index = 0;
initBalls(index);
scenario = 0;
initBalls(scenario);
break;
case SDLK_2:
index = 1;
initBalls(index);
scenario = 1;
initBalls(scenario);
break;
case SDLK_3:
index = 2;
initBalls(index);
scenario = 2;
initBalls(scenario);
break;
case SDLK_4:
index = 3;
initBalls(index);
scenario = 3;
initBalls(scenario);
break;
case SDLK_5:
index = 4;
initBalls(index);
scenario = 4;
initBalls(scenario);
break;
case SDLK_6:
index = 5;
initBalls(index);
scenario = 5;
initBalls(scenario);
break;
case SDLK_7:
index = 6;
initBalls(index);
scenario = 6;
initBalls(scenario);
break;
case SDLK_8:
index = 7;
initBalls(index);
scenario = 7;
initBalls(scenario);
break;
}
}
}
}
// Actualiza la lógica del juego
void update()
{
if (SDL_GetTicks() - ticks > 15)
@@ -247,12 +245,17 @@ void update()
}
if (counter > 0)
{
counter--;
}
else
{
showText = false;
}
}
}
// Renderiza el contenido en la pantalla
void render()
{
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
@@ -264,11 +267,14 @@ void render()
}
if (showText)
{
dbg_print(textPos, 8, text.c_str(), 255, 255, 255);
}
SDL_RenderPresent(renderer);
}
// Función principal
int main(int argc, char *args[])
{
init();
@@ -283,4 +289,4 @@ int main(int argc, char *args[])
close();
return 0;
}
}