diff --git a/.gitignore b/.gitignore index e257658..c248bb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +build/ + # ---> C++ # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c88fd77 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.20) +project(demo1_pixels_wave) + +# Establecer el estándar de C++ +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Definir las fuentes y el ejecutable +set(SOURCE demo1_pixels_wave.cpp) +set(EXECUTABLE demo1_pixels_wave) + +# Buscar SDL3 automáticamente +find_package(SDL3 REQUIRED) +include_directories(${SDL3_INCLUDE_DIRS}) +link_directories(${SDL3_LIBRARY_DIRS}) + +# Detectar la plataforma y ajustar configuraciones específicas +if(WIN32) + set(PLATFORM windows) + set(LDFLAGS "-lmingw32 -lws2_32 ${SDL3_LIBRARIES}") + set(EXE_EXT ".exe") +elseif(UNIX AND NOT APPLE) + set(PLATFORM linux) + set(EXE_EXT ".out") + set(LDFLAGS "${SDL3_LIBRARIES}") +elseif(APPLE) + set(PLATFORM macos) + set(EXE_EXT ".out") + set(LDFLAGS "${SDL3_LIBRARIES}") +endif() + +# Añadir el ejecutable +add_executable(${EXECUTABLE}${EXE_EXT} ${SOURCE}) + +# Enlazar las bibliotecas +target_link_libraries(${EXECUTABLE}${EXE_EXT} ${LDFLAGS}) + +# Colocar el ejecutable en la raíz del proyecto +set_target_properties(${EXECUTABLE}${EXE_EXT} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d56743e --- /dev/null +++ b/Makefile @@ -0,0 +1,34 @@ +# Makefile sin espacios adicionales y con sintaxis minimalista +CXXFLAGS:=-std=c++20 -Wall -Os -ffunction-sections -fdata-sections +LDFLAGS:=-lSDL3 +SOURCE:=demo1_pixels_wave.cpp +EXECUTABLE:=demo1_pixels_wave +RM:=rm -f + +# Detectar plataforma +ifeq ($(OS),Windows_NT) + PLATFORM:=windows + LDFLAGS+=-lmingw32 -lws2_32 + EXE_EXT:=.exe +else + UNAME_S:=$(shell uname -s) +ifeq ($(UNAME_S),Linux) + PLATFORM:=linux + EXE_EXT:=.out +endif +ifeq ($(UNAME_S),Darwin) + PLATFORM:=macos + EXE_EXT:=.out +endif +endif + +# Regla principal +all: $(EXECUTABLE) + +# Compilar +$(EXECUTABLE): + $(CXX) $(SOURCE) $(CXXFLAGS) $(LDFLAGS) -o $(EXECUTABLE)$(EXE_EXT) + +# Limpiar +clean: + $(RM) $(EXECUTABLE)* \ No newline at end of file diff --git a/demo1_pixels_wave.cpp b/demo1_pixels_wave.cpp new file mode 100644 index 0000000..6806449 --- /dev/null +++ b/demo1_pixels_wave.cpp @@ -0,0 +1,84 @@ +#include +#include + +int main(int argc, char *argv[]) +{ + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); + + constexpr int WIDTH = 160; + constexpr int HEIGHT = 160; + constexpr int SIZE = WIDTH * HEIGHT; + constexpr int ZOOM = 4; + + SDL_Window *window = SDL_CreateWindow("pixels", WIDTH * ZOOM, HEIGHT * ZOOM, SDL_WINDOW_OPENGL); + SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr); + SDL_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE); + SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT); + + Uint32 *pixels; + int pitch; + int rad = 96; + int dx = (WIDTH - (rad * 2)) / 2; + int dy = (HEIGHT - (rad * 2)) / 2; + + Uint8 surface[SIZE]; + Uint32 paleta[2]; + + paleta[0] = 0xFF000000; + paleta[1] = 0xFFFFFFFF; + + SDL_Event sdlEvent; + bool exit = false; + while (!exit) + { + while (SDL_PollEvent(&sdlEvent)) + { + if (sdlEvent.type == SDL_EVENT_QUIT) + { + exit = true; + break; + } + } + + SDL_LockTexture(texture, nullptr, (void **)&pixels, &pitch); + + // Limpia la surface + for (int i = 0; i < SIZE; ++i) + { + surface[i] = 0; + } + + // Dibuja el efecto + float time = SDL_GetTicks() / 1000.0f; + for (int j = -rad; j <= rad; j += 3) + for (int i = -rad; i <= rad; i += 2) + { + float dist = sqrt(i * i + j * j); + float z = cos((dist / 40 - time) * M_PI * 2) * 6; + const int x = rad + i + dx; + const int y = rad + j - z + dy; + if (x < WIDTH && y < HEIGHT && x >= 0 && y >= 0) + surface[x + y * WIDTH] = 1; + } + + // Vuelca la surface a la textura + for (int i = 0; i < SIZE; ++i) + { + pixels[i] = paleta[surface[i]]; + } + + SDL_UnlockTexture(texture); + + SDL_RenderTexture(renderer, texture, nullptr, nullptr); + + SDL_RenderPresent(renderer); + } + + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + SDL_Quit(); + + return 0; +} \ No newline at end of file