mandelbrot

view src/main.c @ 0:4d85805eb875

mandelbrot initial import
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 19 Jun 2012 06:48:38 +0300
parents
children
line source
1 /*
2 A simple interactive mandelbrot fractal explorer
3 Copyright (C) John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <SDL.h>
21 #include "mbrot.h"
22 #include "palette.h"
24 static struct {
25 int width, height;
26 int num_threads;
27 int num_iter;
28 const char *palfile;
29 } opt = { 800, 600, 1, 100, "default.pal" };
31 void init_area(void);
32 void redraw(void);
33 int parse_args(int argc, char **argv);
34 int handle_event(SDL_Event *ev);
35 int handle_keypress(int key);
37 static int must_redraw;
39 static SDL_Surface *framebuffer_surf;
41 static struct area area;
42 static double aspect;
45 int main(int argc, char **argv)
46 {
47 if(parse_args(argc, argv) == -1) {
48 return 1;
49 }
51 /* load palette file */
52 if(load_palette(opt.palfile) == -1) {
53 return 1;
54 }
56 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
57 if(!(framebuffer_surf = SDL_SetVideoMode(opt.width, opt.height, 32, SDL_SWSURFACE))) {
58 fprintf(stderr, "failed to initialize graphics\n");
59 return 1;
60 }
62 init_area();
63 redraw();
65 for(;;) {
66 SDL_Event ev;
67 SDL_WaitEvent(&ev);
69 do {
70 if(!handle_event(&ev)) {
71 goto done;
72 }
73 } while(SDL_PollEvent(&ev));
75 if(must_redraw) {
76 redraw();
77 }
78 }
80 done:
81 SDL_Quit();
82 return 0;
83 }
85 void init_area(void)
86 {
87 aspect = (double)opt.width / (double)opt.height;
89 area.x = -2.0;
90 area.width = 3.0;
91 area.height = area.width / aspect;
92 area.y = -area.height / 2.0;
93 }
95 void redraw(void)
96 {
97 void *fb;
99 if(SDL_MUSTLOCK(framebuffer_surf)) {
100 SDL_LockSurface(framebuffer_surf);
101 }
103 fb = framebuffer_surf->pixels;
104 draw_mandelbrot(fb, opt.width, opt.height, &area, opt.num_iter);
106 if(SDL_MUSTLOCK(framebuffer_surf)) {
107 SDL_UnlockSurface(framebuffer_surf);
108 }
110 SDL_UpdateRect(framebuffer_surf, 0, 0, opt.width, opt.height);
112 must_redraw = 0;
113 }
115 int parse_args(int argc, char **argv)
116 {
117 int i;
118 char *endp;
120 for(i=1; i<argc; i++) {
121 if(argv[i][0] == '-' && argv[i][2] == 0) {
122 switch(argv[i][1]) {
123 case 's':
124 if(sscanf(argv[++i], "%dx%d", &opt.width, &opt.height) != 2) {
125 fprintf(stderr, "-s must be followed by WIDTHxHEIGHT\n");
126 return -1;
127 }
128 break;
130 case 'i':
131 opt.num_iter = strtol(argv[++i], &endp, 10);
132 if(endp == argv[i]) {
133 fprintf(stderr, "-i must be followed by the number of iterations\n");
134 return -1;
135 }
136 break;
138 case 't':
139 opt.num_threads = strtol(argv[++i], &endp, 10);
140 if(endp == argv[i]) {
141 fprintf(stderr, "-t must be followed by the number of threads\n");
142 return -1;
143 }
144 break;
146 case 'p':
147 opt.palfile = argv[++i];
148 break;
150 case 'h':
151 printf("usage: %s [-s <WxH>] [-i <iterations>] [-t <threads>] [-p <palette>]\n", argv[0]);
152 printf(" -s <WxH> set the render size (default: %dx%d)\n", opt.width, opt.height);
153 printf(" -i <iterations> set the maximum number of mandelbrot iterations (default: %d)\n", opt.num_iter);
154 printf(" -t <threads> number of threads for mandelbrot calculations (default: %d)\n", opt.num_threads);
155 printf(" -p <palette> use this palette file (see README) (default: %s)\n", opt.palfile);
156 exit(0);
158 default:
159 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
160 return -1;
161 }
162 } else {
163 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
164 return -1;
165 }
166 }
167 return 0;
168 }
170 int handle_event(SDL_Event *ev)
171 {
172 static int prev_x, prev_y;
174 switch(ev->type) {
175 case SDL_KEYDOWN:
176 if(!handle_keypress(ev->key.keysym.sym)) {
177 return 0;
178 }
179 break;
181 case SDL_MOUSEBUTTONDOWN:
182 prev_x = ev->button.x;
183 prev_y = ev->button.y;
184 break;
186 case SDL_MOUSEMOTION:
187 {
188 int dx = ev->motion.x - prev_x;
189 int dy = ev->motion.y - prev_y;
190 prev_x = ev->motion.x;
191 prev_y = ev->motion.y;
193 if(ev->motion.state & 1) {
194 /* pan */
195 area.x -= area.width * (double)dx / (double)opt.width;
196 area.y -= area.height * (double)dy / (double)opt.height;
197 must_redraw = 1;
198 } else if(ev->motion.state & 4) {
199 /* zoom */
200 double zoom = 1.0 + (double)dy / (double)opt.height;
201 double new_width = area.width * zoom;
202 double new_height = area.height * zoom;
203 area.x += (area.width - new_width) / 2.0;
204 area.y += (area.height - new_height) / 2.0;
205 area.width = new_width;
206 area.height = new_height;
207 must_redraw = 1;
208 }
209 }
210 break;
212 case SDL_VIDEOEXPOSE:
213 must_redraw = 1;
214 break;
216 case SDL_QUIT:
217 return 0;
218 }
219 return 1;
220 }
222 int handle_keypress(int key)
223 {
224 switch(key) {
225 case SDLK_ESCAPE:
226 case 'q':
227 case 'Q':
228 return 0;
230 case '-':
231 if(opt.num_iter > 10) {
232 opt.num_iter -= 10;
233 printf("iterations: %d\n", opt.num_iter);
234 must_redraw = 1;
235 }
236 break;
238 case '=':
239 opt.num_iter += 10;
240 printf("iterations: %d\n", opt.num_iter);
241 must_redraw = 1;
242 break;
244 case '?':
245 case '/':
246 printf("current coordinates: (%f, %f) -> (%f, %f)\n", area.x, area.y,
247 area.x + area.width, area.y + area.height);
248 break;
250 case '\b':
251 init_area();
252 must_redraw = 1;
253 break;
255 default:
256 break;
257 }
258 return 1;
259 }