migrat a SDL3
This commit is contained in:
+40
-44
@@ -1,4 +1,4 @@
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include "texture.h"
|
||||
#include "ball.h"
|
||||
#include "defines.h"
|
||||
@@ -13,25 +13,25 @@ Texture *texture = nullptr;
|
||||
std::vector<Ball *> balls;
|
||||
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
|
||||
|
||||
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
|
||||
bool should_exit = false; // Controla si la aplicación debe cerrarse
|
||||
Uint64 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 text_pos = 0; // Posición del texto en la pantalla
|
||||
bool show_text = true; // Determina si el texto se debe mostrar
|
||||
Uint64 text_init_time = 0; // Temporizador para mostrar el texto
|
||||
|
||||
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[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;
|
||||
const std::string TEXT2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
|
||||
text = std::to_string(test[scenario]) + TEXT2;
|
||||
const int SIZE = text.size() * 8;
|
||||
text_pos = SCREEN_WIDTH / 2 - SIZE / 2;
|
||||
text_init_time = SDL_GetTicks();
|
||||
show_text = true;
|
||||
}
|
||||
|
||||
// Inicializa las bolas según el escenario seleccionado
|
||||
@@ -40,12 +40,12 @@ void initBalls(int value)
|
||||
deleteBalls(); // Limpia las bolas actuales
|
||||
for (int i = 0; i < test[value]; ++i)
|
||||
{
|
||||
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);
|
||||
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 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); // Añadir la nueva bola al vector
|
||||
}
|
||||
setText(); // Actualizar el texto
|
||||
@@ -56,10 +56,10 @@ void pushUpBalls()
|
||||
{
|
||||
for (auto ball : balls)
|
||||
{
|
||||
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); // Modifica la velocidad de la bola
|
||||
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); // Modifica la velocidad de la bola
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ bool init()
|
||||
bool success = true; // Bandera de éxito
|
||||
|
||||
// Inicializa SDL
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0)
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
{
|
||||
printf("¡SDL no se pudo inicializar! Error de SDL: %s\n", SDL_GetError());
|
||||
success = false;
|
||||
@@ -103,7 +103,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// 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);
|
||||
window = SDL_CreateWindow(WINDOW_CAPTION, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_OPENGL);
|
||||
if (window == nullptr)
|
||||
{
|
||||
printf("¡No se pudo crear la ventana! Error de SDL: %s\n", SDL_GetError());
|
||||
@@ -112,7 +112,7 @@ bool init()
|
||||
else
|
||||
{
|
||||
// Crear renderizador
|
||||
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||
renderer = SDL_CreateRenderer(window, nullptr);
|
||||
if (renderer == nullptr)
|
||||
{
|
||||
printf("¡No se pudo crear el renderizador! Error de SDL: %s\n", SDL_GetError());
|
||||
@@ -124,7 +124,7 @@ bool init()
|
||||
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
|
||||
|
||||
// Establecer tamaño lógico para el renderizado
|
||||
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
SDL_SetRenderLogicalPresentation(renderer, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,26 +165,26 @@ void checkEvents()
|
||||
while (SDL_PollEvent(&event) != 0)
|
||||
{
|
||||
// Evento de salida
|
||||
if (event.type == SDL_QUIT)
|
||||
if (event.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
}
|
||||
|
||||
// Procesar eventos de teclado
|
||||
if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
|
||||
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
|
||||
{
|
||||
switch (event.key.keysym.sym)
|
||||
switch (event.key.key)
|
||||
{
|
||||
case SDLK_ESCAPE:
|
||||
shouldExit = true;
|
||||
should_exit = true;
|
||||
break;
|
||||
|
||||
case SDLK_SPACE:
|
||||
pushUpBalls();
|
||||
break;
|
||||
|
||||
case SDLK_g:
|
||||
case SDLK_G:
|
||||
switchBallsGravity();
|
||||
break;
|
||||
|
||||
@@ -235,7 +235,7 @@ void checkEvents()
|
||||
// Actualiza la lógica del juego
|
||||
void update()
|
||||
{
|
||||
if (SDL_GetTicks() - ticks > 15)
|
||||
if (SDL_GetTicks() - ticks > DEMO_SPEED)
|
||||
{
|
||||
ticks = SDL_GetTicks();
|
||||
|
||||
@@ -244,13 +244,9 @@ void update()
|
||||
ball->update();
|
||||
}
|
||||
|
||||
if (counter > 0)
|
||||
if (show_text)
|
||||
{
|
||||
counter--;
|
||||
}
|
||||
else
|
||||
{
|
||||
showText = false;
|
||||
show_text = !(SDL_GetTicks() - text_init_time > TEXT_DURATION);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -266,9 +262,9 @@ void render()
|
||||
ball->render();
|
||||
}
|
||||
|
||||
if (showText)
|
||||
if (show_text)
|
||||
{
|
||||
dbg_print(textPos, 8, text.c_str(), 255, 255, 255);
|
||||
dbg_print(text_pos, 8, text.c_str(), 255, 255, 255);
|
||||
}
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
@@ -279,7 +275,7 @@ int main(int argc, char *args[])
|
||||
{
|
||||
init();
|
||||
|
||||
while (!shouldExit)
|
||||
while (!should_exit)
|
||||
{
|
||||
update();
|
||||
checkEvents();
|
||||
|
||||
Reference in New Issue
Block a user