reinventant la roda
This commit is contained in:
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.20)
|
|||||||
project(demo1_pixels_wave)
|
project(demo1_pixels_wave)
|
||||||
|
|
||||||
# Establecer el estándar de C++
|
# Establecer el estándar de C++
|
||||||
set(CMAKE_CXX_STANDARD 20)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
||||||
# Opciones comunes de compilación
|
# Opciones comunes de compilación
|
||||||
@@ -17,12 +17,7 @@ if (NOT SDL3_FOUND)
|
|||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Archivos fuente
|
# Archivos fuente
|
||||||
file(GLOB SOURCE_FILES source/*.cpp)
|
set(SOURCE_FILES source/demo1_raii.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
|
# Detectar la plataforma y configuraciones específicas
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
|
|||||||
20
Makefile
20
Makefile
@@ -1,20 +0,0 @@
|
|||||||
# Variables comunes
|
|
||||||
SOURCE := source/*.cpp
|
|
||||||
EXECUTABLE_NAME := demo1_pixels_wave
|
|
||||||
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)
|
|
||||||
@@ -14,8 +14,8 @@ int main(int argc, char *argv[])
|
|||||||
SDL_Window *window = SDL_CreateWindow("pixels", WIDTH * ZOOM, HEIGHT * ZOOM, 0);
|
SDL_Window *window = SDL_CreateWindow("pixels", WIDTH * ZOOM, HEIGHT * ZOOM, 0);
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
|
||||||
SDL_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
SDL_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||||
// SDL_SetDefaultTextureScaleMode(renderer, SDL_SCALEMODE_NEAREST);
|
|
||||||
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, WIDTH, HEIGHT);
|
||||||
|
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
|
||||||
|
|
||||||
Uint32 *pixels;
|
Uint32 *pixels;
|
||||||
int pitch;
|
int pitch;
|
||||||
|
|||||||
130
source/demo1_raii.cpp
Normal file
130
source/demo1_raii.cpp
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
#define SDL_MAIN_USE_CALLBACKS
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
|
#include <cmath>
|
||||||
|
#include <memory>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
class PixelApp {
|
||||||
|
public:
|
||||||
|
static constexpr int WIDTH = 160;
|
||||||
|
static constexpr int HEIGHT = 160;
|
||||||
|
static constexpr int SIZE = WIDTH * HEIGHT;
|
||||||
|
static constexpr int ZOOM = 4;
|
||||||
|
|
||||||
|
PixelApp() = default;
|
||||||
|
~PixelApp() { cleanup(); }
|
||||||
|
|
||||||
|
bool init() {
|
||||||
|
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
window.reset(SDL_CreateWindow("pixels", WIDTH * ZOOM, HEIGHT * ZOOM, 0));
|
||||||
|
renderer.reset(SDL_CreateRenderer(window.get(), nullptr));
|
||||||
|
SDL_SetRenderLogicalPresentation(renderer.get(), WIDTH, HEIGHT,
|
||||||
|
SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||||
|
|
||||||
|
texture.reset(SDL_CreateTexture(renderer.get(),
|
||||||
|
SDL_PIXELFORMAT_ARGB8888,
|
||||||
|
SDL_TEXTUREACCESS_STREAMING,
|
||||||
|
WIDTH, HEIGHT));
|
||||||
|
SDL_SetTextureScaleMode(texture.get(), SDL_SCALEMODE_NEAREST);
|
||||||
|
|
||||||
|
|
||||||
|
palette = { 0xFF000000, 0xFFFFFFFF };
|
||||||
|
surface.fill(0);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleEvent(const SDL_Event& e, SDL_AppResult& result) {
|
||||||
|
if (e.type == SDL_EVENT_QUIT ||
|
||||||
|
(e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE))
|
||||||
|
{
|
||||||
|
result = SDL_APP_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void update() {
|
||||||
|
Uint32* pixels;
|
||||||
|
int pitch;
|
||||||
|
|
||||||
|
SDL_LockTexture(texture.get(), nullptr, (void**)&pixels, &pitch);
|
||||||
|
|
||||||
|
surface.fill(0);
|
||||||
|
|
||||||
|
float time = SDL_GetTicks() / 1000.0f;
|
||||||
|
int rad = 96;
|
||||||
|
int dx = (WIDTH - rad * 2) / 2;
|
||||||
|
int dy = (HEIGHT - rad * 2) / 2;
|
||||||
|
|
||||||
|
for (int j = -rad; j <= rad; j += 3) {
|
||||||
|
for (int i = -rad; i <= rad; i += 2) {
|
||||||
|
float dist = std::sqrt(float(i * i + j * j));
|
||||||
|
float z = std::cos((dist / 40 - time) * M_PI * 2) * 6;
|
||||||
|
|
||||||
|
int X = rad + i + dx;
|
||||||
|
int Y = rad + j - z + dy;
|
||||||
|
|
||||||
|
if (X >= 0 && X < WIDTH && Y >= 0 && Y < HEIGHT)
|
||||||
|
surface[X + Y * WIDTH] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pixel_pitch = pitch / sizeof(Uint32);
|
||||||
|
for (int y = 0; y < HEIGHT; ++y) {
|
||||||
|
for (int x = 0; x < WIDTH; ++x) {
|
||||||
|
pixels[x + y * pixel_pitch] =
|
||||||
|
palette[surface[x + y * WIDTH]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_UnlockTexture(texture.get());
|
||||||
|
|
||||||
|
SDL_RenderTexture(renderer.get(), texture.get(), nullptr, nullptr);
|
||||||
|
SDL_RenderPresent(renderer.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
struct SDL_Deleter {
|
||||||
|
void operator()(SDL_Window* p) const { SDL_DestroyWindow(p); }
|
||||||
|
void operator()(SDL_Renderer* p) const { SDL_DestroyRenderer(p); }
|
||||||
|
void operator()(SDL_Texture* p) const { SDL_DestroyTexture(p); }
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unique_ptr<SDL_Window, SDL_Deleter> window;
|
||||||
|
std::unique_ptr<SDL_Renderer, SDL_Deleter> renderer;
|
||||||
|
std::unique_ptr<SDL_Texture, SDL_Deleter> texture;
|
||||||
|
|
||||||
|
std::array<Uint8, SIZE> surface;
|
||||||
|
std::array<Uint32, 2> palette;
|
||||||
|
|
||||||
|
void cleanup() {
|
||||||
|
texture.reset();
|
||||||
|
renderer.reset();
|
||||||
|
window.reset();
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Instancia global controlada por SDL (no por nosotros)
|
||||||
|
static PixelApp app;
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppInit(void**, int, char**) {
|
||||||
|
return app.init() ? SDL_APP_CONTINUE : SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppEvent(void*, SDL_Event* e) {
|
||||||
|
SDL_AppResult result = SDL_APP_CONTINUE;
|
||||||
|
app.handleEvent(*e, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppIterate(void*) {
|
||||||
|
app.update();
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SDL_AppQuit(void*, SDL_AppResult) {
|
||||||
|
// RAII se encarga de todo
|
||||||
|
}
|
||||||
106
source/demo1_sdl_main_callbacks.cpp
Normal file
106
source/demo1_sdl_main_callbacks.cpp
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
#define _USE_MATH_DEFINES
|
||||||
|
#define SDL_MAIN_USE_CALLBACKS
|
||||||
|
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3/SDL_main.h>
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
|
static SDL_Window* window = nullptr;
|
||||||
|
static SDL_Renderer* renderer = nullptr;
|
||||||
|
static SDL_Texture* texture = nullptr;
|
||||||
|
|
||||||
|
constexpr int WIDTH = 160;
|
||||||
|
constexpr int HEIGHT = 160;
|
||||||
|
constexpr int SIZE = WIDTH * HEIGHT;
|
||||||
|
constexpr int ZOOM = 4;
|
||||||
|
|
||||||
|
static Uint8 surface_buffer[SIZE];
|
||||||
|
static Uint32 palette[2] = { 0xFF000000, 0xFFFFFFFF };
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv)
|
||||||
|
{
|
||||||
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
|
return SDL_APP_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
window = SDL_CreateWindow("pixels", WIDTH * ZOOM, HEIGHT * ZOOM, 0);
|
||||||
|
renderer = SDL_CreateRenderer(window, nullptr);
|
||||||
|
SDL_SetRenderLogicalPresentation(renderer, WIDTH, HEIGHT, SDL_LOGICAL_PRESENTATION_INTEGER_SCALE);
|
||||||
|
|
||||||
|
texture = SDL_CreateTexture(renderer,
|
||||||
|
SDL_PIXELFORMAT_ARGB8888,
|
||||||
|
SDL_TEXTUREACCESS_STREAMING,
|
||||||
|
WIDTH, HEIGHT);
|
||||||
|
SDL_SetTextureScaleMode(texture, SDL_SCALEMODE_NEAREST);
|
||||||
|
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||||
|
{
|
||||||
|
if (event->type == SDL_EVENT_QUIT ||
|
||||||
|
(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_ESCAPE))
|
||||||
|
{
|
||||||
|
return SDL_APP_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||||
|
{
|
||||||
|
Uint32* pixels;
|
||||||
|
int pitch;
|
||||||
|
|
||||||
|
SDL_LockTexture(texture, nullptr, (void**)&pixels, &pitch);
|
||||||
|
|
||||||
|
// limpiar surface
|
||||||
|
for (int i = 0; i < SIZE; ++i)
|
||||||
|
surface_buffer[i] = 0;
|
||||||
|
|
||||||
|
// dibujar efecto
|
||||||
|
float time = SDL_GetTicks() / 1000.0f;
|
||||||
|
int rad = 96;
|
||||||
|
int dx = (WIDTH - (rad * 2)) / 2;
|
||||||
|
int dy = (HEIGHT - (rad * 2)) / 2;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
int X = rad + i + dx;
|
||||||
|
int Y = rad + j - z + dy;
|
||||||
|
|
||||||
|
if (X >= 0 && X < WIDTH && Y >= 0 && Y < HEIGHT)
|
||||||
|
surface_buffer[X + Y * WIDTH] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int pixel_pitch = pitch / sizeof(Uint32);
|
||||||
|
for (int y = 0; y < HEIGHT; ++y)
|
||||||
|
{
|
||||||
|
for (int x = 0; x < WIDTH; ++x)
|
||||||
|
{
|
||||||
|
int idx = x + y * WIDTH;
|
||||||
|
pixels[x + y * pixel_pitch] = palette[surface_buffer[idx]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_UnlockTexture(texture);
|
||||||
|
|
||||||
|
SDL_RenderTexture(renderer, texture, nullptr, nullptr);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
|
return SDL_APP_CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||||
|
{
|
||||||
|
SDL_DestroyTexture(texture);
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user