131 lines
3.7 KiB
C++
131 lines
3.7 KiB
C++
#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
|
|
}
|