189 lines
4.4 KiB
C++
189 lines
4.4 KiB
C++
#include "texture.h"
|
|
#include "ball.h"
|
|
#include "defines.h"
|
|
#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()
|
|
{
|
|
// Indicador de inicialización exitosa
|
|
bool success = true;
|
|
|
|
// 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);
|
|
|
|
// Configurar el renderizador para usar una presentación lógica
|
|
SDL_SetRenderLogicalPresentation(renderer, DEMO_WIDTH, DEMO_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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);
|
|
}
|
|
|
|
// Configuración inicial de tiempos
|
|
ticks = SDL_GetTicks();
|
|
srand(time(nullptr));
|
|
|
|
return success;
|
|
}
|
|
|
|
// Limpia y libera todos los recursos
|
|
void close()
|
|
{
|
|
// Destruir el renderizador y la ventana
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
|
|
// Eliminar la textura
|
|
if (texture)
|
|
{
|
|
delete texture;
|
|
}
|
|
|
|
// Eliminar todas las bolas
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
if (ball[i])
|
|
{
|
|
delete ball[i];
|
|
}
|
|
}
|
|
|
|
// Finalizar SDL
|
|
SDL_Quit();
|
|
}
|
|
|
|
// Gestiona los eventos del usuario
|
|
void checkEvents()
|
|
{
|
|
SDL_Event event;
|
|
|
|
// 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;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Actualiza el estado de las bolas
|
|
void update()
|
|
{
|
|
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
|
{
|
|
ticks = SDL_GetTicks();
|
|
|
|
// 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()
|
|
{
|
|
// Limpiar la pantalla con el color de fondo
|
|
SDL_SetRenderDrawColor(renderer, BG_R, BG_G, BG_B, 255);
|
|
SDL_RenderClear(renderer);
|
|
|
|
// Dibujar todas las bolas
|
|
for (int i = 0; i < NUM_BALLS; ++i)
|
|
{
|
|
ball[i]->render();
|
|
}
|
|
|
|
// Presentar el renderizador
|
|
SDL_RenderPresent(renderer);
|
|
}
|
|
|
|
// Función principal
|
|
int main(int argc, char *args[])
|
|
{
|
|
if (!init())
|
|
{
|
|
return -1; // Salir si la inicialización falla
|
|
}
|
|
|
|
// Bucle principal del juego
|
|
while (!should_exit)
|
|
{
|
|
update();
|
|
checkEvents();
|
|
render();
|
|
}
|
|
|
|
// Limpiar y cerrar la aplicación
|
|
close();
|
|
|
|
return 0;
|
|
}
|