reinventant la roda

This commit is contained in:
2026-03-24 11:00:30 +01:00
parent 82afafa3ef
commit 2a04f5161f
5 changed files with 239 additions and 28 deletions

View 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();
}