Compare commits

...

6 Commits

Author SHA1 Message Date
0f398d0dbb actualitzat makefile 2025-03-24 17:34:22 +01:00
71f5d90efa aaara si, makes, cmakes i gitignores com toca 2025-03-24 12:17:19 +01:00
a14d7ceb0f modificat .gitignore
modificat CMakeLists.txt per a windows
afegit Makefile
2025-03-24 09:36:22 +01:00
a49297527b arreglos d'estil 2025-03-23 20:45:03 +01:00
9cddd6e03b afegit CMakeLists.txt
arreglos d'estil
2025-03-23 20:27:03 +01:00
4c9b8adfa6 migrat a SDL3 2025-03-23 20:08:55 +01:00
7 changed files with 513 additions and 15 deletions

20
.gitignore vendored
View File

@@ -1,3 +1,6 @@
demo3_pixels_bouncing
build/
# ---> C++
# Prerequisites
*.d
@@ -39,7 +42,8 @@
.LSOverride
# Icon must end with two \r
Icon
Icon
# Thumbnails
._*
@@ -86,20 +90,6 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk
# ---> CMake
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json
# ---> Linux
*~

49
CMakeLists.txt Normal file
View File

@@ -0,0 +1,49 @@
cmake_minimum_required(VERSION 3.20)
project(demo3_pixels_bouncing)
# Establecer el estándar de C++
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Opciones comunes de compilación
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Os -ffunction-sections -fdata-sections")
# Buscar SDL3 automáticamente
find_package(SDL3 REQUIRED)
# Si no se encuentra SDL3, generar un error
if (NOT SDL3_FOUND)
message(FATAL_ERROR "SDL3 no encontrado. Por favor, verifica su instalación.")
endif()
# Archivos fuente
file(GLOB SOURCE_FILES source/*.cpp)
# Comprobar si se encontraron archivos fuente
if(NOT SOURCE_FILES)
message(FATAL_ERROR "No se encontraron archivos fuente en el directorio 'source/'. Verifica la ruta.")
endif()
# Detectar la plataforma y configuraciones específicas
if(WIN32)
set(PLATFORM windows)
set(LINK_LIBS ${SDL3_LIBRARIES} mingw32 ws2_32)
elseif(UNIX AND NOT APPLE)
set(PLATFORM linux)
set(LINK_LIBS ${SDL3_LIBRARIES})
elseif(APPLE)
set(PLATFORM macos)
set(LINK_LIBS ${SDL3_LIBRARIES})
endif()
# Incluir directorios de SDL3
include_directories(${SDL3_INCLUDE_DIRS})
# Añadir el ejecutable reutilizando el nombre del proyecto
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
# Especificar la ubicación del ejecutable (en la raíz del proyecto)
set_target_properties(${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
# Enlazar las bibliotecas necesarias
target_link_libraries(${PROJECT_NAME} ${LINK_LIBS})

20
Makefile Normal file
View File

@@ -0,0 +1,20 @@
# Variables comunes
SOURCE := source/*.cpp
EXECUTABLE_NAME := demo3_pixels_bouncing
CXXFLAGS := -std=c++20 -Wall -Os -ffunction-sections -fdata-sections # Opciones comunes de compilación
LDFLAGS := -lSDL3 # Flags de enlace comunes
OUTPUT_EXT :=
# Detectar plataforma y configurar
ifeq ($(OS),Windows_NT)
LDFLAGS += -lmingw32 -lws2_32
OUTPUT_EXT := .exe
endif
# Regla principal: compilar el ejecutable
all:
$(CXX) $(SOURCE) $(CXXFLAGS) $(LDFLAGS) -o $(EXECUTABLE_NAME)$(OUTPUT_EXT)
# Regla para limpiar archivos generados
clean:
rm -f $(EXECUTABLE_NAME)$(OUTPUT_EXT)

42
source/dbgtxt.h Normal file
View File

@@ -0,0 +1,42 @@
#pragma once
namespace
{
SDL_Texture *dbg_tex = nullptr;
SDL_Renderer *dbg_ren = nullptr;
}
void dbg_init(SDL_Renderer *renderer)
{
dbg_ren = renderer;
Uint8 font[448] = {0x42, 0x4D, 0xC0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x82, 0x01, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x12, 0x0B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x18, 0xF3, 0x83, 0x83, 0xCF, 0x83, 0x87, 0x00, 0x00, 0xF3, 0x39, 0x39, 0xCF, 0x79, 0xF3, 0x00, 0x00, 0x01, 0xF9, 0x39, 0xCF, 0x61, 0xF9, 0x00, 0x00, 0x33, 0xF9, 0x03, 0xE7, 0x87, 0x81, 0x00, 0x00, 0x93, 0x03, 0x3F, 0xF3, 0x1B, 0x39, 0x00, 0x00, 0xC3, 0x3F, 0x9F, 0x39, 0x3B, 0x39, 0x00, 0x41, 0xE3, 0x03, 0xC3, 0x01, 0x87, 0x83, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xE7, 0x01, 0xC7, 0x81, 0x01, 0x83, 0x00, 0x00, 0xE7, 0x1F, 0x9B, 0xE7, 0x1F, 0x39, 0x00, 0x00, 0xE7, 0x8F, 0x39, 0xE7, 0x87, 0xF9, 0x00, 0x00, 0xC3, 0xC7, 0x39, 0xE7, 0xC3, 0xC3, 0x00, 0x00, 0x99, 0xE3, 0x39, 0xE7, 0xF1, 0xE7, 0x00, 0x00, 0x99, 0xF1, 0xB3, 0xC7, 0x39, 0xF3, 0x00, 0x00, 0x99, 0x01, 0xC7, 0xE7, 0x83, 0x81, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x83, 0xE7, 0x83, 0xEF, 0x39, 0x39, 0x00, 0x00, 0x39, 0xE7, 0x39, 0xC7, 0x11, 0x11, 0x00, 0x00, 0xF9, 0xE7, 0x39, 0x83, 0x01, 0x83, 0x00, 0x00, 0x83, 0xE7, 0x39, 0x11, 0x01, 0xC7, 0x00, 0x00, 0x3F, 0xE7, 0x39, 0x39, 0x29, 0x83, 0x00, 0x00, 0x33, 0xE7, 0x39, 0x39, 0x39, 0x11, 0x00, 0x00, 0x87, 0x81, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x39, 0x83, 0x3F, 0x85, 0x31, 0x00, 0x00, 0x39, 0x31, 0x39, 0x3F, 0x33, 0x23, 0x00, 0x00, 0x29, 0x21, 0x39, 0x03, 0x21, 0x07, 0x00, 0x00, 0x01, 0x01, 0x39, 0x39, 0x39, 0x31, 0x00, 0x00, 0x01, 0x09, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x11, 0x19, 0x39, 0x39, 0x39, 0x39, 0x00, 0x00, 0x39, 0x39, 0x83, 0x03, 0x83, 0x03, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0xC1, 0x39, 0x81, 0x83, 0x31, 0x01, 0x00, 0x00, 0x99, 0x39, 0xE7, 0x39, 0x23, 0x3F, 0x00, 0x00, 0x39, 0x39, 0xE7, 0xF9, 0x07, 0x3F, 0x00, 0x00, 0x31, 0x01, 0xE7, 0xF9, 0x0F, 0x3F, 0x00, 0x00, 0x3F, 0x39, 0xE7, 0xF9, 0x27, 0x3F, 0x00, 0x00, 0x9F, 0x39, 0xE7, 0xF9, 0x33, 0x3F, 0x00, 0x00, 0xC1, 0x39, 0x81, 0xF9, 0x39, 0x3F, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x39, 0x03, 0xC3, 0x07, 0x01, 0x3F, 0x00, 0x00, 0x39, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0x01, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x39, 0x03, 0x3F, 0x39, 0x03, 0x03, 0x00, 0x00, 0x39, 0x39, 0x3F, 0x39, 0x3F, 0x3F, 0x00, 0x00, 0x93, 0x39, 0x99, 0x33, 0x3F, 0x3F, 0x00, 0x00, 0xC7, 0x03, 0xC3, 0x07, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00};
dbg_tex = SDL_CreateTextureFromSurface(dbg_ren, SDL_LoadBMP_IO(SDL_IOFromMem(font, 448), 1));
}
void dbg_print(int x, int y, const char *text, Uint8 r, Uint8 g, Uint8 b)
{
int cc = 0;
SDL_SetTextureColorMod(dbg_tex, r, g, b);
SDL_FRect src = {0, 0, 8, 8};
SDL_FRect dst = {static_cast<float>(x), static_cast<float>(y), 8, 8};
while (text[cc] != 0)
{
if (text[cc] != 32)
{
if (text[cc] >= 65)
{
src.x = ((text[cc] - 65) % 6) * 8;
src.y = ((text[cc] - 65) / 6) * 8;
}
else
{
src.x = ((text[cc] - 22) % 6) * 8;
src.y = ((text[cc] - 22) / 6) * 8;
}
SDL_RenderTexture(dbg_ren, dbg_tex, &src, &dst);
}
cc++;
dst.x += 8;
}
}

16
source/defines.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
constexpr char WINDOW_CAPTION[] = "demo3_pixels_bouncing";
constexpr int WIDTH = 320;
constexpr int HEIGHT = 240;
constexpr int ZOOM = 3;
constexpr float GRAVITY = 0.2f;
constexpr int DOT_SIZE = 1;
constexpr int MAX_DOTS = 200000;
constexpr Uint64 TEXT_DURATION = 2000;
constexpr Uint64 DEMO_SPEED = 1000/60;
struct Color
{
int r, g, b;
};

120
source/dot.h Normal file
View File

@@ -0,0 +1,120 @@
#pragma once
namespace Dot
{
struct DotData
{
float x; // Posición x
float y; // Posición y
int w; // Ancho
int h; // Alto
float vx, vy; // Velocidad
float g; // Gravedad
Color color; // Color de la pelota
bool on_floor; // Indica si la pelota está ya en el suelo
bool stopped; // Indica si la pelota ha terminado de moverse;
float loss; // Coeficiente de rebote. Pérdida de energía en cada rebote
};
// Inicializa la estructura
DotData ini(float x, float vx, float vy, Color color);
// Actualiza la lógica de la clase
SDL_FPoint update(DotData &dot);
// Modifica la velocidad
void modVel(DotData &dot, float vx, float vy);
// Constructor
DotData ini(float x, float vx, float vy, Color color)
{
DotData dot;
dot.x = x;
dot.y = 0.0f;
dot.vx = vx;
dot.vy = vy;
dot.color = color;
dot.g = GRAVITY;
dot.on_floor = false;
dot.stopped = false;
dot.loss = ((rand() % 30) * 0.01f) + 0.6f;
return dot;
}
// Actualiza la lógica de la clase
SDL_FPoint update(DotData &dot)
{
if (dot.stopped)
{
return {dot.x, dot.y};
}
// Aplica la gravedad a la velocidad
if (!dot.on_floor && (dot.y - HEIGHT) < DOT_SIZE * 2)
{
dot.vy += dot.g;
}
// Actualiza la posición en función de la velocidad
dot.x += dot.vx;
dot.y += dot.vy;
// Comprueba las colisiones con el lateral izquierdo
if (dot.x < 0)
{
dot.x = 0;
dot.vx = -dot.vx;
}
// Comprueba las colisiones con el lateral derecho
if (dot.x >= WIDTH)
{
dot.x = WIDTH - 1;
dot.vx = -dot.vx;
}
// Comprueba las colisiones con la parte superior
if (dot.y < 0)
{
dot.y = 0;
dot.vy = -dot.vy;
}
// Comprueba las colisiones con la parte inferior
if (dot.y >= HEIGHT)
{
dot.y = HEIGHT - 1;
dot.vy = -dot.vy * dot.loss;
if (abs(dot.vy) < 0.1f)
{
dot.vy = 0.0f;
dot.on_floor = true;
}
}
// Aplica rozamiento al rodar por el suelo
if (dot.on_floor)
{
dot.vx = dot.vx * 0.97f;
if (abs(dot.vx) < 0.1f)
{
dot.vx = 0.0f;
dot.stopped = true;
}
}
return {dot.x, dot.y};
}
// Modifica la velocidad
void modVel(DotData &dot, float vx, float vy)
{
if (dot.stopped)
{
dot.vx = dot.vx + vx;
}
dot.vy = dot.vy + vy;
dot.on_floor = false;
dot.stopped = false;
}
}

261
source/main.cpp Normal file
View File

@@ -0,0 +1,261 @@
#include <SDL3/SDL_error.h> // Para SDL_GetError
#include <SDL3/SDL_events.h> // Para SDL_EventType, SDL_PollEvent, SDL_Event
#include <SDL3/SDL_init.h> // Para SDL_Init, SDL_Quit, SDL_INIT_VIDEO
#include <SDL3/SDL_keycode.h> // Para SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5
#include <SDL3/SDL_rect.h> // Para SDL_FPoint
#include <SDL3/SDL_render.h> // Para SDL_SetRenderDrawColor, SDL_CreateRend...
#include <SDL3/SDL_stdinc.h> // Para Uint64
#include <SDL3/SDL_timer.h> // Para SDL_GetTicks
#include <SDL3/SDL_video.h> // Para SDL_CreateWindow, SDL_DestroyWindow
#include <stdlib.h> // Para rand, srand
#include <time.h> // Para time
#include <iostream> // Para basic_ostream, char_traits, operator<<
#include <string> // Para operator+, string, to_string
#include "dbgtxt.h" // Para dbg_init, dbg_print
#include "defines.h" // Para WIDTH, MAX_DOTS, Color, HEIGHT, ZOOM
#include "dot.h" // Para DotData, ini, modVel, update
// Ventana y renderizador
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
// Datos de los puntos y su representación gráfica
Dot::DotData dots[MAX_DOTS];
SDL_FPoint dot_points[MAX_DOTS];
// Escenarios y configuración
int test[8] = {1, 10, 100, 500, 1000, 10000, 50000, MAX_DOTS};
int scenario = 0;
std::string text = "";
int text_pos = 0;
bool show_text = true;
Uint64 text_init_time;
bool should_exit = false;
Uint64 ticks = 0;
// Actualiza el texto a mostrar en pantalla
void setText()
{
const std::string TEXT2 = test[scenario] == 1 ? " PIXEL" : " PIXELES";
text = std::to_string(test[scenario]) + TEXT2;
const int SIZE = text.size() * 8;
text_pos = WIDTH / 2 - SIZE / 2;
text_init_time = SDL_GetTicks();
show_text = true;
}
// Inicializa los puntos para el escenario actual
void initDots()
{
for (int i = 0; i < test[scenario]; ++i)
{
const int SIGN = ((rand() % 2) * 2) - 1;
const float X = (rand() % (WIDTH / 2)) + (WIDTH / 4);
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
const float VY = ((rand() % 60) - 30) * 0.1f;
const Color COLOR = {(rand() % 192) + 32, (rand() % 192) + 32, (rand() % 192) + 32};
dots[i] = Dot::ini(X, VX, VY, COLOR);
}
setText();
}
// Empuja los puntos hacia arriba con una fuerza aleatoria
void pushUpDots()
{
for (int i = 0; i < test[scenario]; ++i)
{
const int SIGN = ((rand() % 2) * 2) - 1;
const float VX = (((rand() % 20) + 10) * 0.1f) * SIGN;
const float VY = ((rand() % 40) * 0.1f) + 5;
Dot::modVel(dots[i], VX, -VY);
}
}
// Inicializa SDL y configura la ventana y el renderizador
bool init()
{
bool success = true;
// Inicializar SDL
if (!SDL_Init(SDL_INIT_VIDEO))
{
std::cout << "¡SDL no se pudo inicializar! Error de SDL: " << SDL_GetError() << std::endl;
success = false;
}
else
{
// Crear ventana
window = SDL_CreateWindow(WINDOW_CAPTION, WIDTH * ZOOM, HEIGHT * 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
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
{
// Inicializar el color del renderizador
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
// Establecer el tamaño lógico del renderizador
SDL_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
}
}
}
ticks = SDL_GetTicks();
srand(time(nullptr));
dbg_init(renderer);
initDots();
setText();
return success;
}
// Libera los recursos y cierra SDL
void close()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
// Finalizar SDL
SDL_Quit();
}
// Maneja los eventos del teclado y del sistema
void checkEvents()
{
SDL_Event event;
while (SDL_PollEvent(&event) != 0)
{
// Evento de salida
if (event.type == SDL_EVENT_QUIT)
{
should_exit = true;
break;
}
// Eventos de teclado
if (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0)
{
switch (event.key.key)
{
case SDLK_ESCAPE:
should_exit = true;
break;
case SDLK_SPACE:
pushUpDots();
break;
case SDLK_1:
scenario = 0;
initDots();
break;
case SDLK_2:
scenario = 1;
initDots();
break;
case SDLK_3:
scenario = 2;
initDots();
break;
case SDLK_4:
scenario = 3;
initDots();
break;
case SDLK_5:
scenario = 4;
initDots();
break;
case SDLK_6:
scenario = 5;
initDots();
break;
case SDLK_7:
scenario = 6;
initDots();
break;
case SDLK_8:
scenario = 7;
initDots();
break;
default:
break;
}
}
}
}
// Actualiza la posición de los puntos y gestiona el temporizador del texto
void update()
{
if (SDL_GetTicks() - ticks > DEMO_SPEED)
{
ticks = SDL_GetTicks();
for (int i = 0; i < test[scenario]; ++i)
{
dot_points[i] = Dot::update(dots[i]);
}
if (show_text)
{
show_text = !(SDL_GetTicks() - text_init_time > TEXT_DURATION);
}
}
}
// Renderiza el contenido en la pantalla
void render()
{
SDL_SetRenderDrawColor(renderer, 32, 32, 32, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 224, 224, 224, 255);
SDL_RenderPoints(renderer, dot_points, test[scenario]);
if (show_text)
{
dbg_print(text_pos, 8, text.c_str(), 255, 32, 32);
}
SDL_RenderPresent(renderer);
}
// Función principal
int main(int argc, char *args[])
{
if (!init())
{
return -1;
}
while (!should_exit)
{
update();
checkEvents();
render();
}
close();
return 0;
}