Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include <SDL/SDL.h>
#include <SDL/SDL_thread.h>
#include <cmath>
#include <iostream>
using namespace std;
double scale = 1.0;
double x_offset = 0.0, y_offset = 0.0;
const double radius_sq = 4.0;
const int iterations = 500;
const double STEP_AMOUNT = 0.1;
int hue_offset = 180;
int value_offset = 0;
double saturation = 1.0;
double phi = 0.0; // golden ratio - calculated in main()
void plot(SDL_Surface * surface, int x, int y, int i){
double h = ((i+hue_offset) % 360);
double s = saturation;
double v = i == iterations ? 0.0 : 1.0;
int hi = (int)(h / 60.0) % 6;
double f = h / 60.0 - (int)(h / 60);
double p = v * (1 - s);
double q = v * (1 - f * s);
double t = v * (1 - (1 - f) * s);
double r, g, b;
switch((int)hi){
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
default:
r = v;
g = p;
b = q;
break;
}
*((Uint32*)surface->pixels + y * surface->w + x) =
SDL_MapRGB(surface->format, (int)(r * 255), (int)(g * 255), (int)(b * 255));
}
void render(SDL_Surface * surface){
int x, y;
x = 0;
int i;
double x0, y0;
double cr0, ci0;
double rtemp;
while (x < surface->w){
y = 0;
while (y < surface->h){
x0 = ((double)x / (double)surface->w - 0.5) * scale * 2.0 + x_offset;
y0 = ((double)y / (double)surface->h - 0.5) * scale * 2.0 + y_offset;
i = 0;
cr0 = -0.835;
ci0 = -0.2321;
while (x0*x0 + y0*y0 <= radius_sq && i < iterations){
rtemp = x0*x0 - y0*y0 + cr0;
y0 = 2*x0*y0 + ci0;
x0 = rtemp;
i++;
}
plot(surface, x, surface->h - y - 1, i);
y++;
}
x++;
}
SDL_UpdateRect(surface, 0, 0, 0, 0);
}
int main(){
phi = (1.0 + sqrt(5.0)) / 2.0;
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface * wnd = SDL_SetVideoMode(800, 800, 32, SDL_HWSURFACE);
bool loop = true;
SDL_Event evt;
render(wnd);
while (loop){
if (SDL_PollEvent(&evt)){
switch(evt.type){
case SDL_QUIT:
loop = false;
break;
case SDL_KEYUP:
switch(evt.key.keysym.sym){
case SDLK_z:
if ((evt.key.keysym.mod & KMOD_SHIFT) > 0){
if (scale < 16.0)
scale *= 2.0;
}else{
if (scale > 0.000030518)
scale /= 2.0;
}
break;
case SDLK_s:
if ((evt.key.keysym.mod & KMOD_SHIFT) > 0){
if (saturation < 1.0)
saturation += 0.1;
}else{
if (saturation > 0.0)
saturation -= 0.1;
}
break;
case SDLK_h:
if ((evt.key.keysym.mod & KMOD_SHIFT) > 0){
hue_offset = (hue_offset + 10) % 360;
}else{
hue_offset = (hue_offset + 350) % 360;
}
break;
case SDLK_LEFT:
x_offset -= STEP_AMOUNT * scale;
break;
case SDLK_RIGHT:
x_offset += STEP_AMOUNT * scale;
break;
case SDLK_DOWN:
y_offset -= STEP_AMOUNT * scale;
break;
case SDLK_UP:
y_offset += STEP_AMOUNT * scale;
break;
default:
break;
}
default:
break;
}
}
hue_offset = (hue_offset + 1) % 360;
render(wnd);
SDL_Delay(1);
}
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment