Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / gfx_koch.c
blob615f2345ce9e63e0adb5a8985cb62aa455799632
1 /*****************************************************************************
2 * This file is part of gfxprim library. *
3 * *
4 * Gfxprim is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Lesser General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2.1 of the License, or (at your option) any later version. *
8 * *
9 * Gfxprim is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Lesser General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
18 * *
19 * Copyright (C) 2009-2012 Cyril Hrubis <metan@ucw.cz> *
20 * *
21 *****************************************************************************/
25 Simple GFX Example.
29 #include <stdio.h>
30 #include <string.h>
31 #include <errno.h>
33 #include <GP.h>
35 /* Set to 1 to use Anti Aliased drawing */
36 static int aa_flag = 0;
39 * Generate color depending on distance from center
41 * We could do this only and only because the pixmap
42 * pixel type is fixed to GP_PIXEL_RGB888.
44 static GP_Pixel do_color(int xc, int yc, float x, float y)
46 float dx = GP_ABS(1.00 * xc - x)/(2*xc);
47 float dy = GP_ABS(1.00 * yc - y)/(2*yc);
49 float dc = sqrt(dx*dx + dy*dy);
51 int rmask = 0;
52 int gmask = 0xff * sqrt(dc);
53 int bmask = 0xff * (1 - dc);
55 if (dc < 0.1) {
56 rmask = 0xff * (1 - dc);
57 bmask = 0x00;
60 if (dc > 0.55) {
61 rmask = 0xff * dc;
62 gmask /= 6;
63 bmask = 0x00;
66 return bmask | (gmask<<8) | (rmask << 16);
69 static void draw(GP_Pixmap *img, int level, float x0, float y0, float x1, float y1)
71 if (level == 0) {
72 GP_Pixel pixel;
74 pixel = do_color(img->w/2, img->h/2, 1.00 * (x0+x1)/2, 1.00 * (y0 + y1)/2);
76 if (aa_flag)
77 GP_LineAA(img, x0 * 256, y0 * 256, x1 * 256, y1 * 256, pixel);
78 else
79 GP_Line(img, x0, y0, x1, y1, pixel);
81 return;
84 /* Compute varation of Koch curve */
85 float x34 = (x0 + 3 * x1) / 4;
86 float y34 = (y0 + 3 * y1) / 4;
88 float x14 = (3 * x0 + x1) / 4;
89 float y14 = (3 * y0 + y1) / 4;
91 float x12 = (x0 + x1)/2;
92 float y12 = (y0 + y1)/2;
94 float dx = (x1 - x0)/4;
95 float dy = (y1 - y0)/4;
97 draw(img, level - 1, x0, y0, x14, y14);
98 draw(img, level - 1, x14, y14, x14 - dy, y14 + dx);
99 draw(img, level - 1, x14 - dy, y14 + dx, x12 - dy, y12 + dx);
100 draw(img, level - 1, x12 - dy, y12 + dx, x12, y12);
101 draw(img, level - 1, x12, y12, x12 + dy, y12 - dx);
102 draw(img, level - 1, x12 + dy, y12 - dx, x34 + dy, y34 - dx);
103 draw(img, level - 1, x34 + dy, y34 - dx, x34, y34);
104 draw(img, level - 1, x1, y1, x34, y34);
107 int main(void)
109 GP_Pixmap *img;
111 /* Create RGB 24 bit image */
112 img = GP_PixmapAlloc(600, 600, GP_PIXEL_RGB888);
114 if (img == NULL) {
115 fprintf(stderr, "Failed to allocate pixmap");
116 return 1;
119 /* Clean up the bitmap */
120 GP_Fill(img, 0);
122 /* Draw a fractal */
123 draw(img, 4, 0, 0, img->w - 1, img->h - 1);
124 draw(img, 4, 0, img->h - 1, img->w - 1, 0);
126 if (GP_SavePNG(img, "out.png", NULL)) {
127 fprintf(stderr, "Failed to save image %s", strerror(errno));
128 return 1;
131 /* Cleanup */
132 GP_PixmapFree(img);
134 return 0;