]> git.itanic.dy.fi Git - mandelbrot/blob - mandelbrot.c
Add support for navigation
[mandelbrot] / mandelbrot.c
1 #include <SDL.h>
2 #include <unistd.h>
3 #include <stdio.h>
4 #include <time.h>
5 #include <string.h>
6
7 struct context {
8         struct SDL_Surface *screen;
9         double origo_x, origo_y;
10         int motion_x, motion_y;
11         double zoom;
12         int scroll_enabled;
13 };
14
15 static void putpixel(struct SDL_Surface *screen, const int x, const int y,
16                      const unsigned char r, const unsigned char g,
17                      const unsigned char b)
18 {
19         int offset = y * screen->pitch + x * 4;
20         unsigned char *buf = screen->pixels;
21
22         buf[offset++] = b;
23         buf[offset++] = g;
24         buf[offset]   = r;
25 }
26
27 #define MAX_ITERATION 1000
28
29 int get_mandelbrot_iterations(double x0, double y0)
30 {
31         double x = 0, y = 0, xtemp;
32         int iteration = 0;
33
34         while ((x * x + y * y < 2 * 2) && iteration < MAX_ITERATION) {
35
36                 xtemp = x * x - y * y + x0;
37                 y = 2 * x * y + y0;
38
39                 x = xtemp;
40                 iteration++;
41         }
42
43         return iteration;
44 }
45
46 int draw_mandelbrot(struct SDL_Surface *screen, double x1, double x2,
47                 double y1, double y2)
48 {
49         double x0, y0, xlen, ylen, xstep, ystep;
50         int iteration, xs, ys;
51
52         printf("Drawing area (%f, %f)(%f, %f)\n", x1, y1, x2, y2);
53
54         xlen = x2 - x1;
55         ylen = y2 - y1;
56         xstep = xlen / (double)screen->w;
57         ystep = ylen / (double)screen->h;
58
59         y0 = y1;
60 #pragma omp parallel for private(xs, ys, x0, y0)
61         for (ys = 0; ys < screen->h; ys++) {
62                 y0 = y1 + ystep * ys;
63                 x0 = x1;
64                 for (xs = 0; xs < screen->w; xs++) {
65                         iteration = get_mandelbrot_iterations(x0, y0);
66
67                         if (iteration == MAX_ITERATION)
68                                 putpixel(screen, xs, ys, 255, 255, 255);
69                         else
70                                 putpixel(screen, xs, ys,
71                                         iteration * 8, iteration,
72                                         iteration / 4);
73                         x0 += xstep;
74                 }
75         }
76
77         SDL_Flip(screen);
78         return 0;
79 }
80
81 static int read_events(struct context *ctx)
82 {
83         SDL_Event event;
84         int wait = 1, ret, exit = 0, delayed_exit = 0;
85
86         while (!exit) {
87                 if (wait) {
88                         SDL_WaitEvent(&event);
89                         wait = 0;
90                 } else {
91                         ret = SDL_PollEvent(&event);
92                         if (!ret) {
93                                 wait = 1;
94                                 exit = delayed_exit;
95                         }
96                 }
97
98                 switch (event.type) {
99                 case SDL_KEYDOWN:
100                         switch (event.key.keysym.sym) {
101                         case SDLK_ESCAPE:
102                                 goto quit;
103                         default:
104                                 break;
105                         }
106                         break;
107                 case SDL_KEYUP:
108                         switch (event.key.keysym.sym) {
109                         default:
110                                 break;
111                         }
112                         break;
113                 case SDL_MOUSEBUTTONDOWN:
114                         if (event.button.button == 4) {
115                                 ctx->zoom *= 1.5;
116                                 exit++;
117                         } else if (event.button.button == 5) {
118                                 ctx->zoom /= 1.5;
119                                 delayed_exit++;
120                         } else {
121                                 ctx->scroll_enabled = 1;
122                         }
123                         break;
124                 case SDL_MOUSEBUTTONUP:
125                         ctx->scroll_enabled = 0;
126                         exit++;
127                         break;
128                 case SDL_MOUSEMOTION:
129                         if (!ctx->scroll_enabled)
130                                 break;
131
132                         ctx->motion_x -= event.motion.xrel;
133                         ctx->motion_y -= event.motion.yrel;
134                         delayed_exit++;
135                         break;
136                 case SDL_VIDEORESIZE:
137                         ctx->screen =
138                                 SDL_SetVideoMode(event.resize.w,
139                                                  event.resize.h,
140                                                  32,
141                                                  ctx->screen->flags);
142                         exit++;
143                         break;
144                 case SDL_QUIT:
145                         goto quit;
146                 }
147         }
148
149         return 0;
150 quit:
151         printf("\nExiting. Good bye!\n");
152         return 1;
153 }
154
155 static void loop(struct context *ctx)
156 {
157         double aspect_ratio;
158
159         do {
160                 aspect_ratio = ctx->screen->w / ctx->screen->h;
161                 ctx->origo_x += ctx->motion_x / ctx->zoom / ctx->screen->w;
162                 ctx->origo_y += ctx->motion_y / ctx->zoom / ctx->screen->h;
163                 ctx->motion_x = ctx->motion_y = 0;
164
165                 draw_mandelbrot(ctx->screen,
166                                 ctx->origo_x - aspect_ratio / ctx->zoom,
167                                 ctx->origo_x + aspect_ratio / ctx->zoom,
168                                 ctx->origo_y - 1 / ctx->zoom,
169                                 ctx->origo_y + 1 / ctx->zoom);
170         } while (!read_events(ctx));
171 }
172
173 int main(int argc, char *argv[])
174 {
175         struct context ctx;
176         int flags = SDL_DOUBLEBUF | SDL_HWSURFACE | SDL_RESIZABLE;
177         int xres = 800, yres = 600;
178
179         memset(&ctx, 0, sizeof(ctx));
180         ctx.zoom = 1;
181
182         if (SDL_Init(SDL_INIT_VIDEO) != 0) {
183                 fprintf(stderr, "Unable to initialize SDL: %s\n",
184                         SDL_GetError());
185
186                 return 1;
187         }
188         atexit(SDL_Quit);
189
190         ctx.screen = SDL_SetVideoMode(xres, yres, 32, flags);
191         if (ctx.screen == NULL) {
192                 fprintf(stderr, "Unable to set video mode: %s\n",
193                         SDL_GetError());
194                 return 2;
195         }
196
197         SDL_WM_SetCaption(argv[0], NULL);
198
199         loop(&ctx);
200
201         return 0;
202 }