#include <SDL2/SDL.h>
#include <stdio.h>
/*
Copyright (c) 2020 Devine Lu Linvega
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
typedef unsigned char Uint8;
typedef signed char Sint8;
typedef unsigned short Uint16;
typedef signed short Sint16;
#define NORM_BITS 8
#define NORM_FACT ((Sint16)1 << NORM_BITS)
Uint16 WIDTH = 600;
Uint16 HEIGHT = 400;
Uint16 FPS = 30, ZOOM = 1;
Uint32 theme[] = {
0xFFFFFF,
0x000000,
0x72DEC2,
0x666666,
0x222222};
SDL_Window *gWindow;
SDL_Renderer *gRenderer;
SDL_Texture *gTexture;
Uint32 *pixels;
#pragma mark - DRAW
void
clear(Uint32 *dst)
{
int v, h;
for(v = 0; v < HEIGHT; v++)
for(h = 0; h < WIDTH; h++)
dst[v * WIDTH + h] = theme[0];
}
void
putpixel(Uint32 *dst, int x, int y, Uint32 color)
{
if(x >= 0 && x < WIDTH - 8 && y >= 0 && y < HEIGHT - 8)
dst[y * WIDTH + x] = color + (color << 8) + (color << 16);
}
int
iterate(Uint16 real0, Uint16 imag0)
{
Uint8 i;
Sint16 realq, imagq, real = real0, imag = imag0;
for(i = 0; i < 255; i++) {
realq = (real * real) >> NORM_BITS;
imagq = (imag * imag) >> NORM_BITS;
if((realq + imagq) > (Sint16)4 * NORM_FACT)
break;
imag = ((real * imag) >> (NORM_BITS - 1)) + imag0;
real = realq - imagq + real0;
}
return i;
}
void
mandel(Sint16 realmin, Sint16 imagmin, Sint16 realmax, Sint16 imagmax)
{
Uint16 x, y,
deltareal = (realmax - realmin) / WIDTH,
deltaimag = (imagmax - imagmin) / HEIGHT,
real0 = realmin,
imag0;
for(x = 0; x < WIDTH; x++) {
imag0 = imagmax;
for(y = 0; y < HEIGHT; y++) {
putpixel(pixels, x, y, iterate(real0, imag0));
imag0 -= deltaimag;
}
real0 += deltareal;
}
}
void
redraw(Uint32 *dst)
{
mandel(-2.0 * NORM_FACT, -1.2 * NORM_FACT, 0.7 * NORM_FACT, 1.2 * NORM_FACT);
SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
}
#pragma mark - OPTIONS
int
error(char *msg, const char *err)
{
printf("Error %s: %s\n", msg, err);
return 0;
}
void
quit(void)
{
free(pixels);
SDL_DestroyTexture(gTexture);
gTexture = NULL;
SDL_DestroyRenderer(gRenderer);
gRenderer = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
SDL_Quit();
exit(0);
}
#pragma mark - TRIGGERS
int
init(void)
{
if(SDL_Init(SDL_INIT_VIDEO) < 0)
return error("Init", SDL_GetError());
gWindow = SDL_CreateWindow("Nasu",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
WIDTH * ZOOM,
HEIGHT * ZOOM,
SDL_WINDOW_SHOWN);
if(gWindow == NULL)
return error("Window", SDL_GetError());
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
if(gRenderer == NULL)
return error("Renderer", SDL_GetError());
gTexture = SDL_CreateTexture(gRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC,
WIDTH,
HEIGHT);
if(gTexture == NULL)
return error("Texture", SDL_GetError());
pixels = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32));
if(pixels == NULL)
return error("Pixels", "Failed to allocate memory");
clear(pixels);
return 1;
}
int
main(int argc, char **argv)
{
int ticknext = 0;
if(!init())
return error("Init", "Failure");
redraw(pixels);
while(1) {
int tick = SDL_GetTicks();
SDL_Event event;
if(tick < ticknext)
SDL_Delay(ticknext - tick);
ticknext = tick + (1000 / FPS);
while(SDL_PollEvent(&event) != 0) {
switch(event.type) {
case SDL_QUIT: quit(); break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_EXPOSED)
redraw(pixels);
break;
}
}
}
quit();
return 0;
}