fix: dos fallos tontos
This commit is contained in:
245
source/main.cpp
245
source/main.cpp
@@ -4,160 +4,185 @@
|
|||||||
#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);
|
||||||
|
|
||||||
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
// Configurar el renderizador para usar una presentación lógica
|
||||||
}
|
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
texture = new Texture(renderer, TEXTURE_FILE);
|
// Crear textura y bolas
|
||||||
for (int i = 0; i < NUM_BALLS; ++i)
|
texture = new Texture(renderer, TEXTURE_FILE);
|
||||||
{
|
for (int i = 0; i < NUM_BALLS; ++i)
|
||||||
const int VX = rand() % 2 == 0 ? 1 : -1;
|
{
|
||||||
const int VY = rand() % 2 == 0 ? 1 : -1;
|
const int VX = rand() % 2 == 0 ? 1 : -1;
|
||||||
const int X = rand() % DEMO_WIDTH;
|
const int VY = rand() % 2 == 0 ? 1 : -1;
|
||||||
const int Y = rand() % DEMO_HEIGHT;
|
const int X = rand() % DEMO_WIDTH;
|
||||||
constexpr int SIZE = BALL_SIZE;
|
const int Y = rand() % DEMO_HEIGHT;
|
||||||
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
constexpr int SIZE = BALL_SIZE;
|
||||||
}
|
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
||||||
|
}
|
||||||
|
|
||||||
ticks = SDL_GetTicks();
|
// Configuración inicial de tiempos
|
||||||
srand(time(nullptr));
|
ticks = SDL_GetTicks();
|
||||||
|
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);
|
||||||
|
|
||||||
if (texture)
|
// Eliminar la textura
|
||||||
{
|
if (texture)
|
||||||
delete texture;
|
{
|
||||||
}
|
delete texture;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < NUM_BALLS; ++i)
|
// Eliminar todas las bolas
|
||||||
{
|
for (int i = 0; i < NUM_BALLS; ++i)
|
||||||
if (ball[i])
|
{
|
||||||
{
|
if (ball[i])
|
||||||
delete ball[i];
|
{
|
||||||
}
|
delete ball[i];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
// Manejar eventos de teclado
|
||||||
{
|
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||||
switch (event.key.key)
|
{
|
||||||
{
|
switch (event.key.key)
|
||||||
case SDLK_ESCAPE:
|
{
|
||||||
should_exit = true;
|
case SDLK_ESCAPE: // Salir de la aplicación
|
||||||
break;
|
should_exit = true;
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
for (int i = 0; i < NUM_BALLS; ++i)
|
// Actualizar la posición de cada bola
|
||||||
{
|
for (int i = 0; i < NUM_BALLS; ++i)
|
||||||
ball[i]->update();
|
{
|
||||||
}
|
ball[i]->update();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dibuja el estado actual en la pantalla
|
||||||
void render()
|
void render()
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
// Limpiar la pantalla con el color de fondo
|
||||||
SDL_RenderClear(renderer);
|
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
for (int i = 0; i < NUM_BALLS; ++i)
|
// Dibujar todas las bolas
|
||||||
{
|
for (int i = 0; i < NUM_BALLS; ++i)
|
||||||
ball[i]->render();
|
{
|
||||||
}
|
ball[i]->render();
|
||||||
|
}
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
// Presentar el renderizador
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
while (!should_exit)
|
// Bucle principal del juego
|
||||||
{
|
while (!should_exit)
|
||||||
update();
|
{
|
||||||
checkEvents();
|
update();
|
||||||
render();
|
checkEvents();
|
||||||
}
|
render();
|
||||||
|
}
|
||||||
|
|
||||||
close();
|
// Limpiar y cerrar la aplicación
|
||||||
|
close();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user