fix: dos fallos tontos
This commit is contained in:
245
source/main.cpp
245
source/main.cpp
@@ -4,160 +4,185 @@
|
||||
#include <iostream>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
// Ventana principal y renderizador
|
||||
SDL_Window *window = nullptr;
|
||||
SDL_Renderer *renderer = nullptr;
|
||||
|
||||
// Textura y bolas
|
||||
Texture *texture = nullptr;
|
||||
Ball *ball[NUM_BALLS];
|
||||
|
||||
// Control de la aplicación
|
||||
bool should_exit = false;
|
||||
Uint64 ticks = 0;
|
||||
|
||||
// Inicializa SDL, la ventana, el renderizador y los elementos del juego
|
||||
bool init()
|
||||
{
|
||||
// Initialization flag
|
||||
bool success = true;
|
||||
// Indicador de inicialización exitosa
|
||||
bool success = true;
|
||||
|
||||
// Initialize SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create window
|
||||
window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL);
|
||||
if (window == NULL)
|
||||
{
|
||||
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create renderer for window
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == NULL)
|
||||
{
|
||||
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Initialize renderer color
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
// Inicializar SDL
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
std::cout << "¡SDL no pudo inicializarse! Error de SDL: " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Crear ventana
|
||||
window = SDL_CreateWindow("demo_pelotas1", DEMO_WIDTH * WINDOW_ZOOM, DEMO_HEIGHT * WINDOW_ZOOM, SDL_WINDOW_OPENGL);
|
||||
if (window == nullptr)
|
||||
{
|
||||
std::cout << "¡No se pudo crear la ventana! Error de SDL: " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Crear renderizador para la ventana
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == nullptr)
|
||||
{
|
||||
std::cout << "¡No se pudo crear el renderizador! Error de SDL: " << SDL_GetError() << std::endl;
|
||||
success = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Establecer el color inicial del renderizador
|
||||
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);
|
||||
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 X = rand() % DEMO_WIDTH;
|
||||
const int Y = rand() % DEMO_HEIGHT;
|
||||
constexpr int SIZE = BALL_SIZE;
|
||||
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
||||
}
|
||||
// Crear textura y bolas
|
||||
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 X = rand() % DEMO_WIDTH;
|
||||
const int Y = rand() % DEMO_HEIGHT;
|
||||
constexpr int SIZE = BALL_SIZE;
|
||||
ball[i] = new Ball(X, Y, SIZE, SIZE, VX, VY, texture);
|
||||
}
|
||||
|
||||
ticks = SDL_GetTicks();
|
||||
srand(time(nullptr));
|
||||
// Configuración inicial de tiempos
|
||||
ticks = SDL_GetTicks();
|
||||
srand(time(nullptr));
|
||||
|
||||
return success;
|
||||
return success;
|
||||
}
|
||||
|
||||
// Limpia y libera todos los recursos
|
||||
void close()
|
||||
{
|
||||
// Destroy window
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
// Destruir el renderizador y la ventana
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
|
||||
if (texture)
|
||||
{
|
||||
delete texture;
|
||||
}
|
||||
// Eliminar la textura
|
||||
if (texture)
|
||||
{
|
||||
delete texture;
|
||||
}
|
||||
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
if (ball[i])
|
||||
{
|
||||
delete ball[i];
|
||||
}
|
||||
}
|
||||
// Eliminar todas las bolas
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
if (ball[i])
|
||||
{
|
||||
delete ball[i];
|
||||
}
|
||||
}
|
||||
|
||||
// Quit SDL subsystems
|
||||
SDL_Quit();
|
||||
// Finalizar SDL
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Gestiona los eventos del usuario
|
||||
void checkEvents()
|
||||
{
|
||||
SDL_Event event;
|
||||
SDL_Event event;
|
||||
|
||||
// Comprueba los eventos que hay en la cola
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Evento de salida de la aplicación
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
// Procesar todos los eventos en la cola
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Detectar si el usuario quiere cerrar la aplicación
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
should_exit = true;
|
||||
break;
|
||||
// Manejar eventos de teclado
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_ESCAPE: // Salir de la aplicación
|
||||
should_exit = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Actualiza el estado de las bolas
|
||||
void update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
ball[i]->update();
|
||||
}
|
||||
}
|
||||
// Actualizar la posición de cada bola
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
ball[i]->update();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Dibuja el estado actual en la pantalla
|
||||
void render()
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
// Limpiar la pantalla con el color de fondo
|
||||
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
ball[i]->render();
|
||||
}
|
||||
// Dibujar todas las bolas
|
||||
for (int i = 0; i < NUM_BALLS; ++i)
|
||||
{
|
||||
ball[i]->render();
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
// Presentar el renderizador
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
// Función principal
|
||||
int main(int argc, char *args[])
|
||||
{
|
||||
init();
|
||||
if (!init())
|
||||
{
|
||||
return -1; // Salir si la inicialización falla
|
||||
}
|
||||
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
// Bucle principal del juego
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
render();
|
||||
}
|
||||
|
||||
close();
|
||||
// Limpiar y cerrar la aplicación
|
||||
close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -5,19 +5,11 @@
|
||||
#include "texture.h"
|
||||
|
||||
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)
|
||||
: renderer_(renderer), texture_(nullptr), width_(0), height_(0)
|
||||
{
|
||||
renderer_ = renderer;
|
||||
texture_ = nullptr;
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
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);
|
||||
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
|
||||
{
|
||||
@@ -73,7 +65,7 @@ bool Texture::loadFromFile(std::string file_path)
|
||||
new_texture = SDL_CreateTextureFromSurface(renderer_, loaded_surface);
|
||||
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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user