11 static int init(void);
12 static void destroy(void);
13 static void draw(void);
14 static int julia(long x, long y, long cx, long cy, int max_iter);
15 static int calc_walk(struct vec2x *path, long x, long y, int max_steps);
17 static struct screen scr = {
25 /*static long aspect_24x8 = (long)(1.3333333 * 256.0);*/
26 static long xscale_24x8 = (long)(1.3333333 * 1.2 * 256.0);
27 static long yscale_24x8 = (long)(1.2 * 256.0);
29 static int max_iter = 50;
33 struct screen *fract_screen(void)
43 static void destroy(void)
47 #define PACK_RGB16(r, g, b) \
48 (((((r) >> 3) & 0x1f) << 11) | ((((g) >> 2) & 0x3f) << 5) | (((b) >> 3) & 0x1f))
50 static void draw(void)
53 unsigned short *pixels = fb_pixels;
54 struct vec2x walkpos[WALK_SIZE];
59 for(i=0; i<fb_height; i++) {
60 for(j=0; j<fb_width; j++) {
61 unsigned char pidx = julia(j, i, cx, cy, max_iter) & 0xff;
62 *pixels++ = (pidx >> 3) | ((pidx >> 2) << 5) | ((pidx >> 3) << 11);
68 if((len = calc_walk(walkpos, mouse_x, mouse_y, WALK_SIZE))) {
69 x = walkpos[0].x >> 16;
70 y = walkpos[0].y >> 16;
72 for(i=1; i<len; i++) {
75 int x1 = walkpos[i].x >> 16;
76 int y1 = walkpos[i].y >> 16;
78 if(clip_line(&x0, &y0, &x1, &y1, 0, 0, fb_width - 1, fb_height - 1)) {
79 draw_line(x0, y0, x1, y1, PACK_RGB16(32, 128, 255));
86 pixels[mouse_y * fb_width + mouse_x] = 0xffe;
88 swap_buffers(fb_pixels);
91 static long normalize_coord(long x, long range)
93 /* 2 * x / range - 1*/
94 return (x << 17) / range - 65536;
97 static long device_coord(long x, long range)
99 /* (x + 1) / 2 * (range - 1) */
100 return ((x + 65536) >> 1) * (range - 1);
103 static int julia(long x, long y, long cx, long cy, int max_iter)
107 /* convert to fixed point roughly [-1, 1] */
108 x = (normalize_coord(x, fb_width) >> 8) * xscale_24x8;
109 y = (normalize_coord(y, fb_height) >> 8) * yscale_24x8;
110 cx = (normalize_coord(cx, fb_width) >> 8) * xscale_24x8;
111 cy = (normalize_coord(cy, fb_height) >> 8) * yscale_24x8;
113 for(i=0; i<max_iter; i++) {
114 /* z_n = z_{n-1}**2 + c */
118 if(px * px + py * py > (4 << 16)) {
121 x = px * px - py * py + cx;
122 y = (px * py << 1) + cy;
125 return i < max_iter ? (256 * i / max_iter) : 0;
128 static int calc_walk(struct vec2x *path, long x, long y, int max_steps)
133 /* convert to fixed point roughly [-1, 1] */
134 x = cx = (normalize_coord(x, fb_width) >> 8) * xscale_24x8;
135 y = cy = (normalize_coord(y, fb_height) >> 8) * yscale_24x8;
137 for(i=0; i<max_steps; i++) {
138 /* z_n = z_{n-1}**2 + c */
142 path[i].x = device_coord((x << 8) / xscale_24x8, fb_width);
143 path[i].y = device_coord((y << 8) / yscale_24x8, fb_height);
145 if(px * px + py * py > (4 << 16)) {
148 x = px * px - py * py + cx;
149 y = (px * py << 1) + cy;