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
62 lines
1.3 KiB
C
#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(1280, 800, 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;
|
|
|
|
float complex c = ((3.0 * x / screen->w) - 2.0)
|
|
+ I * ((2.0 * y / screen->h) - 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;
|
|
if (diverges) {
|
|
color = 0x00010001 * it;
|
|
} else {
|
|
color = 0x00000000;
|
|
}
|
|
|
|
pixels[i] = color;
|
|
if (y % 10 == 0)
|
|
SDL_Flip(screen);
|
|
}
|
|
|
|
/* Save BMP */
|
|
char *file = "mandelbrot.bmp";
|
|
if (SDL_SaveBMP(screen, file) != 0) {
|
|
fprintf(stderr, "Could not write %s!\n", file);
|
|
}
|
|
|
|
if (SDL_MUSTLOCK(screen)) {
|
|
SDL_UnlockSurface(screen);
|
|
}
|
|
SDL_Flip(screen);
|
|
|
|
SDL_Delay(20000);
|
|
|
|
SDL_Quit();
|
|
}
|