Rename GP_Context -> GP_Pixmap
[gfxprim.git] / demos / c_simple / linetest.c
blob7e535df94a61317f6dbe5286bf02d48dc6957af8
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-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
21 * *
22 * Copyright (C) 2009-2013 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <math.h>
28 #include <GP.h>
30 /* Values for color pixels in display format. */
31 static GP_Pixel black, white;
33 static double start_angle = 0.0;
35 static int aa_flag = 0;
36 static int pause_flag = 0;
38 static GP_Backend *win;
40 void redraw_screen(void)
42 double angle;
43 int x, y;
44 int w = win->pixmap->w;
45 int h = win->pixmap->h;
46 int xcenter = w/2;
47 int ycenter = h/2;
49 GP_Fill(win->pixmap, black);
51 for (angle = 0.0; angle < 2*M_PI; angle += 0.1) {
52 x = (int) (w/2 * cos(start_angle + angle));
53 y = (int) (h/2 * sin(start_angle + angle));
55 int r = 127.0 + 127.0 * cos(start_angle + angle);
56 int b = 127.0 + 127.0 * sin(start_angle + angle);
58 GP_Pixel pixel;
59 pixel = GP_RGBToPixel(r, 0, b, win->pixmap->pixel_type);
61 if (aa_flag) {
62 GP_LineAA_Raw(win->pixmap, GP_FP_FROM_INT(xcenter), GP_FP_FROM_INT(ycenter),
63 GP_FP_FROM_INT(xcenter + x), GP_FP_FROM_INT(ycenter + y), pixel);
64 } else {
65 GP_Line(win->pixmap, xcenter + x, ycenter + y, xcenter, ycenter, pixel);
66 GP_Line(win->pixmap, xcenter, ycenter, xcenter + x, ycenter + y, pixel);
70 GP_BackendFlip(win);
72 /* axes */
73 // GP_HLineXYW(&pixmap, 0, ycenter, display->w, white);
74 // GP_VLineXYH(&pixmap, xcenter, 0, display->h, white);
77 void event_loop(void)
79 GP_Event ev;
81 while (GP_BackendGetEvent(win, &ev)) {
82 switch (ev.type) {
83 case GP_EV_KEY:
84 if (ev.code != GP_EV_KEY_DOWN)
85 continue;
87 switch (ev.val.key.key) {
88 case GP_KEY_A:
89 aa_flag = !aa_flag;
90 break;
91 case GP_KEY_ESC:
92 GP_BackendExit(win);
93 exit(0);
94 break;
95 case GP_KEY_P:
96 pause_flag = !pause_flag;
97 break;
99 break;
100 case GP_EV_SYS:
101 switch(ev.code) {
102 case GP_EV_SYS_QUIT:
103 GP_BackendExit(win);
104 exit(0);
105 break;
106 case GP_EV_SYS_RESIZE:
107 GP_BackendResizeAck(win);
108 break;
110 break;
115 int main(int argc, char *argv[])
117 const char *backend_opts = "X11";
118 int opt;
120 while ((opt = getopt(argc, argv, "b:")) != -1) {
121 switch (opt) {
122 case 'b':
123 backend_opts = optarg;
124 break;
125 default:
126 fprintf(stderr, "Invalid paramter '%c'\n", opt);
130 win = GP_BackendInit(backend_opts, "Line Test");
132 if (win == NULL) {
133 fprintf(stderr, "Failed to initalize backend '%s'\n",
134 backend_opts);
135 return 1;
138 white = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win->pixmap);
139 black = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win->pixmap);
141 redraw_screen();
143 for (;;) {
144 GP_BackendPoll(win);
145 event_loop();
147 usleep(20000);
149 if (pause_flag)
150 continue;
152 redraw_screen();
153 GP_BackendFlip(win);
155 start_angle += 0.01;
156 if (start_angle > 2*M_PI) {
157 start_angle = 0.0;