restructure

master
Tanjita 12 years ago
parent 2d836e1022
commit 9f204ddb1b

@ -2,31 +2,35 @@
#include <complex.h> #include <complex.h>
#include <stdbool.h> #include <stdbool.h>
int main(int argc, char *argv[]) { /* Compute the out-coloring based on the iteration counter. */
SDL_Surface *screen = NULL; Uint32 outcolor(int it) {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { return 0x00010001 * it;
printf("Error: Could not initialize SDL: %s.\n", SDL_GetError()); }
exit(1);
/* Compute the in-coloring. */
Uint32 incolor() {
return 0x00000000; /* black */
} }
screen = SDL_SetVideoMode(1280, 800, 32, SDL_SWSURFACE);
if (SDL_MUSTLOCK(screen)) { void drawmandelbrot(SDL_Surface *surface) {
SDL_LockSurface(screen); if (SDL_MUSTLOCK(surface)) {
SDL_LockSurface(surface);
} }
Uint32 *pixels = (Uint32 *) screen->pixels; Uint32 *pixels = (Uint32 *) surface->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) for (int i = 0; i < surface->w * surface->h; i++) {
+ I * ((2.0 * y / screen->h) - 1.0); int y = i / surface->w;
int x = i % surface->w;
float complex c = ((3.0 * x / surface->w) - 2.0)
+ I * ((2.0 * y / surface->h) - 1.0);
bool diverges = false; bool diverges = false;
float complex z = 0; float complex z = 0;
int it; int it;
for (it = 1; it < 256; it++) { for (it = 1; it < 256; it++) {
z = cpowf(z, 2) + c; z = cpowf(z, 2) + c;
if (cabs(z) > 100) { if (cabs(z) > 2) {
diverges = true; diverges = true;
break; break;
} }
@ -34,15 +38,35 @@ int main(int argc, char *argv[]) {
Uint32 color; Uint32 color;
if (diverges) { if (diverges) {
color = 0x00010001 * it; color = outcolor(it);
} else { } else {
color = 0x00000000; color = incolor();
} }
pixels[i] = color; pixels[i] = color;
if (y % 10 == 0)
SDL_Flip(screen); /* Update the screen every 10 lines. */
if (y % 10 == 0) {
SDL_Flip(surface);
}
}
if (SDL_MUSTLOCK(surface)) {
SDL_UnlockSurface(surface);
} }
SDL_Flip(surface);
}
int main(int argc, char *argv[]) {
/* Set up SDL. */
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
fprintf(stderr, "Error: Could not initialize SDL: %s.\n", SDL_GetError());
exit(1);
}
SDL_Surface *screen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE);
/* Do the mandelbrot. */
drawmandelbrot(screen);
/* Save BMP */ /* Save BMP */
char *file = "mandelbrot.bmp"; char *file = "mandelbrot.bmp";
@ -50,12 +74,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Could not write %s!\n", file); fprintf(stderr, "Could not write %s!\n", file);
} }
if (SDL_MUSTLOCK(screen)) { /* Quit. */
SDL_UnlockSurface(screen);
}
SDL_Flip(screen);
SDL_Delay(20000); SDL_Delay(20000);
SDL_Quit(); SDL_Quit();
} }

Loading…
Cancel
Save