primer commit

This commit is contained in:
2025-03-23 19:06:25 +01:00
commit ca305d70ea
5 changed files with 176 additions and 0 deletions

36
.gitignore vendored Normal file
View File

@@ -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

39
CMakeLists.txt Normal file
View File

@@ -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})

34
Makefile Normal file
View File

@@ -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)*

3
README.md Normal file
View File

@@ -0,0 +1,3 @@
# demo2_pixels_noise
Demo rapideta de pintat de pixels en SDL

64
demo2_pixels_noise.cpp Normal file
View File

@@ -0,0 +1,64 @@
#include <SDL3/SDL.h>
#include <cstdlib>
#include <ctime>
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<unsigned>(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;
}