93 lines
2.7 KiB
C++
93 lines
2.7 KiB
C++
#define _USE_MATH_DEFINES
|
|
#include <SDL3/SDL.h>
|
|
#include <cmath>
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
SDL_Init(SDL_INIT_VIDEO);
|
|
|
|
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, 0);
|
|
SDL_Renderer *renderer = SDL_CreateRenderer(window, nullptr);
|
|
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);
|
|
|
|
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 event;
|
|
bool exit = false;
|
|
while (!exit)
|
|
{
|
|
while (SDL_PollEvent(&event))
|
|
{
|
|
if ((event.type == SDL_EVENT_QUIT) || (event.type == SDL_EVENT_KEY_DOWN && event.key.repeat == 0 && event.key.key == SDLK_ESCAPE))
|
|
{
|
|
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 en memoria
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
int pixel_pitch = pitch / sizeof(Uint32);
|
|
for (int y = 0; y < HEIGHT; ++y)
|
|
{
|
|
for (int x = 0; x < WIDTH; ++x)
|
|
{
|
|
const int INDEX_SURFACE = x + y * WIDTH;
|
|
const int INDEX_TEX = x + y * pixel_pitch;
|
|
pixels[INDEX_TEX] = paleta[surface[INDEX_SURFACE]];
|
|
}
|
|
}
|
|
|
|
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;
|
|
} |