add mandelbrot

master
neingeist 12 years ago
parent e525a5e557
commit 1e6f8726a6

@ -0,0 +1,54 @@
#include "SDL.h"
#include <complex.h>
#include <stdbool.h>
int main(int argc, char *argv[]) {
SDL_Surface *screen = NULL;
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
printf("Error: Could not initialize SDL: %s.\n", SDL_GetError());
exit(1);
}
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if (SDL_MUSTLOCK(screen)) {
SDL_LockSurface(screen);
}
Uint32 *pixels = (Uint32 *) screen->pixels;
for (int i = 0; i < screen->w * screen->h; i++) {
int y = i / screen->w;
int x = i % screen->w;
if (x == 0) {
printf("y = %d\n", y);
}
float complex c = (3.0 * x / 640.0) - 2.0
+ I * ((2.0 * y / 480.0) - 1.0);
bool diverges = false;
float complex z = 0;
int it;
for (it=1; it<256; it++) {
z = cpowf(z, 2) + c;
if (cabs(z) > 100) {
diverges = true;
break;
}
}
Uint32 color = 0x00000000;
if (diverges) {
color = 0x01010101 * it;
}
pixels[i] = color;
}
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
SDL_Flip(screen); /* XXX */
SDL_Delay(20000);
SDL_Quit();
}
Loading…
Cancel
Save