From ca305d70ea5c774ee04a6205682e1cd9e2bce003 Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Sun, 23 Mar 2025 19:06:25 +0100 Subject: [PATCH] primer commit --- .gitignore | 36 ++++++++++++++++++++++++ CMakeLists.txt | 39 +++++++++++++++++++++++++ Makefile | 34 ++++++++++++++++++++++ README.md | 3 ++ demo2_pixels_noise.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 Makefile create mode 100644 README.md create mode 100644 demo2_pixels_noise.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c248bb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,36 @@ +build/ + +# ---> C++ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fab36bf --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,39 @@ +cmake_minimum_required(VERSION 3.20) +project(demo2_pixels_noise) + +# 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 demo2_pixels_noise.cpp) +set(EXECUTABLE demo2_pixels_noise) + +# 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..c0b7481 --- /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:=demo2_pixels_noise.cpp +EXECUTABLE:=demo2_pixels_noise +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/README.md b/README.md new file mode 100644 index 0000000..b0bb0ea --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# demo2_pixels_noise + +Demo rapideta de pintat de pixels en SDL \ No newline at end of file diff --git a/demo2_pixels_noise.cpp b/demo2_pixels_noise.cpp new file mode 100644 index 0000000..a8c993d --- /dev/null +++ b/demo2_pixels_noise.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +struct ARGB +{ + Uint8 b, g, r, a; + + // Constructor + ARGB(Uint8 blue = 0, Uint8 green = 0, Uint8 red = 0, Uint8 alpha = 255) + : b(blue), g(green), r(red), a(alpha) {} +}; + +int main(int argc, char *argv[]) +{ + constexpr int WIDTH = 320; + constexpr int HEIGHT = 240; + constexpr int ZOOM = 2; + + srand(static_cast(time(nullptr))); + + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS); + + SDL_Window *window = SDL_CreateWindow("pixels_white_noise", 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); + + ARGB *pixels; + int pitch; + + 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); + + for (int i = 0; i < 76800; ++i) + { + pixels[i] = ARGB(rand() % 256, rand() % 256, rand() % 256, 255); + } + + 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; +}