fix: dos fallos tontos

This commit is contained in:
2025-03-23 23:10:18 +01:00
parent 45b763791a
commit a7c0b7d8c6
2 changed files with 139 additions and 122 deletions

View File

@@ -4,53 +4,60 @@
#include <iostream> #include <iostream>
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
// Ventana principal y renderizador
SDL_Window *window = nullptr; SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr; SDL_Renderer *renderer = nullptr;
// Textura y bolas
Texture *texture = nullptr; Texture *texture = nullptr;
Ball *ball[NUM_BALLS]; Ball *ball[NUM_BALLS];
// Control de la aplicación
bool should_exit = false; bool should_exit = false;
Uint64 ticks = 0; Uint64 ticks = 0;
// Inicializa SDL, la ventana, el renderizador y los elementos del juego
bool init() bool init()
{ {
// Initialization flag // Indicador de inicialización exitosa
bool success = true; bool success = true;
// Initialize SDL // Inicializar SDL
if (SDL_Init(SDL_INIT_VIDEO)) if (!SDL_Init(SDL_INIT_VIDEO))
{ {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); std::cout << "¡SDL no pudo inicializarse! Error de SDL: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
{ {
// Create window // Crear ventana
window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL); window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL);
if (window == NULL) if (window == nullptr)
{ {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); std::cout << "¡No se pudo crear la ventana! Error de SDL: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
{ {
// Create renderer for window // Crear renderizador para la ventana
renderer = SDL_CreateRenderer(window, nullptr); renderer = SDL_CreateRenderer(window, nullptr);
if (renderer == NULL) if (renderer == nullptr)
{ {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); std::cout << "¡No se pudo crear el renderizador! Error de SDL: " << SDL_GetError() << std::endl;
success = false; success = false;
} }
else else
{ {
// Initialize renderer color // Establecer el color inicial del renderizador
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// Configurar el renderizador para usar una presentación lógica
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
} }
} }
} }
// Crear textura y bolas
texture = new Texture(renderer, TEXTURE_FILE); texture = new Texture(renderer, TEXTURE_FILE);
for (int i = 0; i < NUM_BALLS; ++i) for (int i = 0; i < NUM_BALLS; ++i)
{ {
@@ -62,23 +69,27 @@ bool init()
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture); ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
} }
// Configuración inicial de tiempos
ticks = SDL_GetTicks(); ticks = SDL_GetTicks();
srand(time(nullptr)); srand(time(nullptr));
return success; return success;
} }
// Limpia y libera todos los recursos
void close() void close()
{ {
// Destroy window // Destruir el renderizador y la ventana
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
// Eliminar la textura
if (texture) if (texture)
{ {
delete texture; delete texture;
} }
// Eliminar todas las bolas
for (int i = 0; i < NUM_BALLS; ++i) for (int i = 0; i < NUM_BALLS; ++i)
{ {
if (ball[i]) if (ball[i])
@@ -87,29 +98,31 @@ void close()
} }
} }
// Quit SDL subsystems // Finalizar SDL
SDL_Quit(); SDL_Quit();
} }
// Gestiona los eventos del usuario
void checkEvents() void checkEvents()
{ {
SDL_Event event; SDL_Event event;
// Comprueba los eventos que hay en la cola // Procesar todos los eventos en la cola
while (SDL_PollEvent(&event) != 0) while (SDL_PollEvent(&event) != 0)
{ {
// Evento de salida de la aplicación // Detectar si el usuario quiere cerrar la aplicación
if (event.type == SDL_EVENT_QUIT) if (event.type == SDL_EVENT_QUIT)
{ {
should_exit = true; should_exit = true;
break; break;
} }
// Manejar eventos de teclado
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0) if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
{ {
switch (event.key.key) switch (event.key.key)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE: // Salir de la aplicación
should_exit = true; should_exit = true;
break; break;
@@ -120,12 +133,14 @@ void checkEvents()
} }
} }
// Actualiza el estado de las bolas
void update() void update()
{ {
if (SDL_GetTicks() - ticks > DEMO_SPEED) if (SDL_GetTicks() - ticks > DEMO_SPEED)
{ {
ticks = SDL_GetTicks(); ticks = SDL_GetTicks();
// Actualizar la posición de cada bola
for (int i = 0; i < NUM_BALLS; ++i) for (int i = 0; i < NUM_BALLS; ++i)
{ {
ball[i]->update(); ball[i]->update();
@@ -133,23 +148,32 @@ void update()
} }
} }
// Dibuja el estado actual en la pantalla
void render() void render()
{ {
// Limpiar la pantalla con el color de fondo
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255); SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
// Dibujar todas las bolas
for (int i = 0; i < NUM_BALLS; ++i) for (int i = 0; i < NUM_BALLS; ++i)
{ {
ball[i]->render(); ball[i]->render();
} }
// Presentar el renderizador
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
// Función principal
int main(int argc, char *args[]) int main(int argc, char *args[])
{ {
init(); if (!init())
{
return -1; // Salir si la inicialización falla
}
// Bucle principal del juego
while (!should_exit) while (!should_exit)
{ {
update(); update();
@@ -157,6 +181,7 @@ int main(int argc, char *args[])
render(); render();
} }
// Limpiar y cerrar la aplicación
close(); close();
return 0; return 0;

View File

@@ -5,19 +5,11 @@
#include "texture.h" #include "texture.h"
Texture::Texture(SDL_Renderer *renderer) Texture::Texture(SDL_Renderer *renderer)
{ : renderer_(renderer), texture_(nullptr), width_(0), height_(0) {}
renderer_ = renderer;
texture_ = nullptr;
width_ = 0;
height_ = 0;
}
Texture::Texture(SDL_Renderer *renderer, std::string file_path) Texture::Texture(SDL_Renderer *renderer, std::string file_path)
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
{ {
renderer_ = renderer;
texture_ = nullptr;
width_ = 0;
height_ = 0;
loadFromFile(file_path); loadFromFile(file_path);
} }
@@ -65,7 +57,7 @@ bool Texture::loadFromFile(std::string file_path)
SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch); SDL_Surface *loaded_surface = SDL_CreateSurfaceFrom(width, height, pixel_format, (void *)data, pitch);
if (loaded_surface == nullptr) if (loaded_surface == nullptr)
{ {
std::cout << "Unable to load image " << file_path.c_str() << std::endl; std::cout << "Unable to load image " << file_path << std::endl;
} }
else else
{ {
@@ -73,7 +65,7 @@ bool Texture::loadFromFile(std::string file_path)
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface); new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
if (new_texture == nullptr) if (new_texture == nullptr)
{ {
std::cout << "Unable to create texture from " << file_path.c_str() << "! SDL Error: " << SDL_GetError() << std::endl; std::cout << "Unable to create texture from " << file_path << "! SDL Error: " << SDL_GetError() << std::endl;
} }
else else
{ {