Skip to content

Instantly share code, notes, and snippets.

@robbrit
Created July 16, 2010 02:26
Show Gist options
  • Save robbrit/477834 to your computer and use it in GitHub Desktop.
Save robbrit/477834 to your computer and use it in GitHub Desktop.
#include <SDL/SDL.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define SIZE 800
#define PRECISION 300000
struct paird{
double x, y;
};
void draw_pixel(SDL_Surface * screen, int x, int y, int r, int g, int b){
*((Uint32*)screen->pixels + y * screen->w + x) = (Uint32)SDL_MapRGB(screen->format, r, g, b);
}
void draw_pixel(SDL_Surface * screen, int x, int y, int c){
draw_pixel(screen, x, y, c, c, c);
}
paird foo1(paird xy){
return {
0.0, 0.16 * xy.y
};
}
paird foo2(paird xy){
return {
0.85 * xy.x + 0.04 * xy.y,
-0.04 * xy.x + 0.85 * xy.y + 1.6
};
}
paird foo3(paird xy){
return {
0.2 * xy.x - 0.26 * xy.y,
0.23 * xy.x + 0.22 * xy.y + 1.6
};
}
paird foo4(paird xy){
return {
-0.15 * xy.x + 0.28 * xy.y,
0.26 * xy.x + 0.24 * xy.y + 0.44
};
}
void render_world(SDL_Surface * screen){
paird xy = {0.0, 0.0};
int which;
int x, y;
for (int i = 0; i < PRECISION; i++){
x = SIZE * (xy.x / 6 + 0.5);
y = SIZE * (1.0 - xy.y / 10);
if (x >= 0 && x < SIZE && y >= 0 && y < SIZE){
draw_pixel(screen, x, y, 0, 192, 0);
}
which = rand() % 100;
if (which < 1){
xy = foo1(xy);
}else if (which < 86){
xy = foo2(xy);
}else if (which < 94){
xy = foo3(xy);
}else{
xy = foo4(xy);
}
}
}
void render(SDL_Surface * screen){
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
render_world(screen);
SDL_UpdateRect(screen, 0, 0, 0, 0);
}
int main(){
SDL_Surface * screen;
SDL_Event evt;
SDL_Init(SDL_INIT_VIDEO);
srand((unsigned)time(NULL));
screen = SDL_SetVideoMode(SIZE, SIZE, 32, SDL_HWSURFACE);
render(screen);
while(1){
if (SDL_PollEvent(&evt)){
if(evt.type == SDL_QUIT) break;
}
SDL_Delay(1);
}
SDL_Quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment