You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.3 KiB
C

12 years ago
#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);
}
12 years ago
screen = SDL_SetVideoMode(1280, 800, 32, SDL_SWSURFACE);
12 years ago
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;
12 years ago
float complex c = ((3.0 * x / screen->w) - 2.0)
+ I * ((2.0 * y / screen->h) - 1.0);
12 years ago
bool diverges = false;
float complex z = 0;
int it;
12 years ago
for (it = 1; it < 256; it++) {
12 years ago
z = cpowf(z, 2) + c;
if (cabs(z) > 100) {
diverges = true;
break;
}
}
12 years ago
Uint32 color;
12 years ago
if (diverges) {
12 years ago
color = 0x00010001 * it;
12 years ago
} else {
color = 0x00000000;
12 years ago
}
pixels[i] = color;
12 years ago
if (y % 10 == 0)
SDL_Flip(screen);
12 years ago
}
12 years ago
/* Save BMP */
char *file = "mandelbrot.bmp";
if (SDL_SaveBMP(screen, file) != 0) {
fprintf(stderr, "Could not write %s!\n", file);
}
12 years ago
if (SDL_MUSTLOCK(screen)) {
SDL_UnlockSurface(screen);
}
12 years ago
SDL_Flip(screen);
12 years ago
SDL_Delay(20000);
SDL_Quit();
}