actualitzat Makefile

This commit is contained in:
2025-03-23 09:03:50 +01:00
parent 578549b122
commit 111cbb2217
3 changed files with 113 additions and 74 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.exe *.exe
*.dll *.dll
demo_pelotas2 *.out
build/

View File

@@ -1,11 +1,43 @@
# Variables comunes
source := source/*.cpp source := source/*.cpp
executable_name := demo_pelotas2 executable_name := demo_pelotas2
CXX := g++ # Cambiar a clang++ si lo prefieres
CXXFLAGS := -std=c++11 -Wall # Opciones comunes de compilación
LDFLAGS := -lSDL2 # Opciones comunes de enlace
# Detectar plataforma
ifeq ($(OS),Windows_NT)
PLATFORM := windows
LDFLAGS += -lmingw32 -lws2_32 -lSDL2main
OUTPUT_EXT := .exe
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
PLATFORM := linux
OUTPUT_EXT := .out
endif
ifeq ($(UNAME_S),Darwin) # macOS
PLATFORM := macos
CXX := clang++ # Usar clang++ en macOS
OUTPUT_EXT := .out
endif
endif
# Regla principal: compilar según la plataforma detectada
all: $(PLATFORM)
# Regla para Windows
windows: windows:
g++ $(source) -std=c++11 -Wall -lmingw32 -lws2_32 -lSDL2main -lSDL2 -o $(executable_name).exe $(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
# Regla para Linux
linux: linux:
g++ $(source) -std=c++11 -Wall -lSDL2 -o $(executable_name) $(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
# Regla para macOS
macos: macos:
g++ $(source) -std=c++11 -Wall -lSDL2 -o $(executable_name) $(CXX) $(source) $(CXXFLAGS) $(LDFLAGS) -o $(executable_name)$(OUTPUT_EXT)
# Regla para limpiar los archivos generados
clean:
rm -f $(executable_name)*

View File

@@ -6,50 +6,52 @@
#include <iostream> #include <iostream>
#include <vector> #include <vector>
// Variables globales
SDL_Window *window = nullptr; SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr; SDL_Renderer *renderer = nullptr;
SDL_Event *event;
Texture *texture = nullptr; Texture *texture = nullptr;
std::vector<Ball *> balls; std::vector<Ball *> balls;
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000}; int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, 100000};
bool shouldExit = false; bool shouldExit = false; // Controla si la aplicación debe cerrarse
Uint32 ticks = 0; Uint32 ticks = 0; // Tiempo en milisegundos para controlar la actualización
int index = 0; int scenario = 0; // Escenario actual basado en el número de bolas
std::string text = ""; std::string text = ""; // Texto a mostrar en pantalla
int textPos = 0; int textPos = 0; // Posición del texto en la pantalla
bool showText = true; bool showText = true; // Determina si el texto se debe mostrar
int counter = 0; int counter = 0; // Temporizador para mostrar el texto
void deleteBalls(); 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() void setText()
{ {
const std::string text2 = test[index] == 1 ? " PELOTA" : " PELOTAS"; const std::string text2 = test[scenario] == 1 ? " PELOTA" : " PELOTAS";
text = std::to_string(test[index]) + text2; text = std::to_string(test[scenario]) + text2;
const int size = text.size() * 8; const int size = text.size() * 8;
textPos = SCREEN_WIDTH / 2 - size / 2; textPos = SCREEN_WIDTH / 2 - size / 2;
counter = TEXT_TIME; counter = TEXT_TIME;
showText = true; showText = true;
} }
// Inicializa las bolas según el escenario seleccionado
void initBalls(int value) void initBalls(int value)
{ {
deleteBalls(); deleteBalls(); // Limpia las bolas actuales
for (int i = 0; i < test[value]; ++i) for (int i = 0; i < test[value]; ++i)
{ {
const int sign = ((rand() % 2) * 2) - 1; 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 x = (rand() % (SCREEN_WIDTH / 2)) + (SCREEN_WIDTH / 4); const float vx = (((rand() % 20) + 10) * 0.1f) * sign; // Velocidad en X
const float vx = (((rand() % 20) + 10) * 0.1f) * sign; const float vy = ((rand() % 60) - 30) * 0.1f; // Velocidad en Y
const float vy = ((rand() % 60) - 30) * 0.1f; const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32}; // Color aleatorio
const color_t color = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
Ball *b = new Ball(x, vx, vy, color, texture); Ball *b = new Ball(x, vx, vy, color, texture);
balls.push_back(b); balls.push_back(b); // Añadir la nueva bola al vector
} }
setText(); setText(); // Actualizar el texto
} }
// Aumenta la velocidad vertical de las bolas "hacia arriba"
void pushUpBalls() void pushUpBalls()
{ {
for (auto ball : balls) for (auto ball : balls)
@@ -57,10 +59,11 @@ void pushUpBalls()
const int sign = ((rand() % 2) * 2) - 1; const int sign = ((rand() % 2) * 2) - 1;
const float vx = (((rand() % 20) + 10) * 0.1f) * sign; const float vx = (((rand() % 20) + 10) * 0.1f) * sign;
const float vy = ((rand() % 40) * 0.1f) + 5; const float vy = ((rand() % 40) * 0.1f) + 5;
ball->modVel(vx, -vy); ball->modVel(vx, -vy); // Modifica la velocidad de la bola
} }
} }
// Cambia la gravedad de todas las bolas
void switchBallsGravity() void switchBallsGravity()
{ {
for (auto ball : balls) for (auto ball : balls)
@@ -68,11 +71,11 @@ void switchBallsGravity()
if (ball) if (ball)
{ {
ball->switchGravity(); ball->switchGravity();
;
} }
} }
} }
// Elimina todas las bolas y libera memoria
void deleteBalls() void deleteBalls()
{ {
for (auto ball : balls) for (auto ball : balls)
@@ -83,101 +86,95 @@ void deleteBalls()
ball = nullptr; ball = nullptr;
} }
} }
balls.clear(); balls.clear(); // Limpia el vector
} }
// Inicializa SDL y configura los componentes principales
bool init() bool init()
{ {
// Initialization flag bool success = true; // Bandera de éxito
bool success = true;
// Initialize SDL // Inicializa SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) if (SDL_Init(SDL_INIT_VIDEO) < 0)
{ {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError()); printf("¡SDL no se pudo inicializar! Error de SDL: %s\n", SDL_GetError());
success = false; success = false;
} }
else else
{ {
// Create window // 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, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * WINDOW_SIZE, SCREEN_HEIGHT * WINDOW_SIZE, SDL_WINDOW_SHOWN);
if (window == nullptr) if (window == nullptr)
{ {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError()); printf("¡No se pudo crear la ventana! Error de SDL: %s\n", SDL_GetError());
success = false; success = false;
} }
else else
{ {
// Create renderer for window // Crear renderizador
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == nullptr) if (renderer == nullptr)
{ {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError()); printf("¡No se pudo crear el renderizador! Error de SDL: %s\n", SDL_GetError());
success = false; success = false;
} }
else else
{ {
// Initialize renderer color // Establecer color inicial del renderizador
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF); SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// Establece el tamaño del renderizador // Establecer tamaño lógico para el renderizado
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT); SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
} }
} }
} }
event = new SDL_Event(); // Inicializar otros componentes
texture = new Texture(renderer, "resources/pelota.png"); texture = new Texture(renderer, "resources/pelota.png");
ticks = SDL_GetTicks(); ticks = SDL_GetTicks();
srand(time(nullptr)); srand(time(nullptr));
dbg_init(renderer); dbg_init(renderer); // Inicializar herramientas de depuración
initBalls(index); initBalls(scenario);
return success; return success;
} }
// Limpia todos los recursos y cierra SDL
void close() void close()
{ {
// Destroy window
SDL_DestroyRenderer(renderer); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
window = nullptr; window = nullptr;
renderer = nullptr; renderer = nullptr;
if (event)
{
delete event;
event = nullptr;
}
if (texture) if (texture)
{ {
delete texture; delete texture;
texture = nullptr; texture = nullptr;
} }
deleteBalls(); deleteBalls(); // Liberar memoria de las bolas
// Quit SDL subsystems SDL_Quit(); // Finalizar SDL
SDL_Quit();
} }
// Verifica los eventos en la cola
void checkEvents() void checkEvents()
{ {
// Comprueba los eventos que hay en la cola SDL_Event event;
while (SDL_PollEvent(event) != 0) while (SDL_PollEvent(&event) != 0)
{ {
// Evento de salida de la aplicación // Evento de salida
if (event->type == SDL_QUIT) if (event.type == SDL_QUIT)
{ {
shouldExit = true; shouldExit = true;
break; break;
} }
if (event->type == SDL_KEYDOWN && event->key.repeat == 0) // Procesar eventos de teclado
if (event.type == SDL_KEYDOWN && event.key.repeat == 0)
{ {
switch (event->key.keysym.sym) switch (event.key.keysym.sym)
{ {
case SDLK_ESCAPE: case SDLK_ESCAPE:
shouldExit = true; shouldExit = true;
@@ -192,49 +189,50 @@ void checkEvents()
break; break;
case SDLK_1: case SDLK_1:
index = 0; scenario = 0;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_2: case SDLK_2:
index = 1; scenario = 1;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_3: case SDLK_3:
index = 2; scenario = 2;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_4: case SDLK_4:
index = 3; scenario = 3;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_5: case SDLK_5:
index = 4; scenario = 4;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_6: case SDLK_6:
index = 5; scenario = 5;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_7: case SDLK_7:
index = 6; scenario = 6;
initBalls(index); initBalls(scenario);
break; break;
case SDLK_8: case SDLK_8:
index = 7; scenario = 7;
initBalls(index); initBalls(scenario);
break; break;
} }
} }
} }
} }
// Actualiza la lógica del juego
void update() void update()
{ {
if (SDL_GetTicks() - ticks > 15) if (SDL_GetTicks() - ticks > 15)
@@ -247,12 +245,17 @@ void update()
} }
if (counter > 0) if (counter > 0)
{
counter--; counter--;
}
else else
{
showText = false; showText = false;
}
} }
} }
// Renderiza el contenido en la pantalla
void render() void render()
{ {
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255); SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
@@ -264,11 +267,14 @@ void render()
} }
if (showText) if (showText)
{
dbg_print(textPos, 8, text.c_str(), 255, 255, 255); dbg_print(textPos, 8, text.c_str(), 255, 255, 255);
}
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
} }
// Función principal
int main(int argc, char *args[]) int main(int argc, char *args[])
{ {
init(); init();