actualitzat a SDL3
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
|
build/
|
||||||
|
|
||||||
# ---> C++
|
# ---> C++
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
|||||||
39
CMakeLists.txt
Normal file
39
CMakeLists.txt
Normal file
@@ -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})
|
||||||
34
Makefile
Normal file
34
Makefile
Normal 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:=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)*
|
||||||
84
demo1_pixels_wave.cpp
Normal file
84
demo1_pixels_wave.cpp
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user