gfx: Implement GP_FillCircleSeg()
[gfxprim.git] / demos / c_simple / textaligntest.c
blob9827ee6b96a0a96670dff117f7794ee65ef58f5f
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-2014 Cyril Hrubis <metan@ucw.cz> *
23 * *
24 *****************************************************************************/
26 #include <stdio.h>
27 #include <GP.h>
29 static GP_Pixel black_pixel, red_pixel, yellow_pixel, green_pixel, blue_pixel,
30 darkgray_pixel, white_pixel;
32 static int font_flag = 0;
34 static int X = 640;
35 static int Y = 480;
37 static GP_FontFace *font = NULL;
38 static GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
40 static GP_Backend *win;
42 void redraw_screen(void)
44 GP_Fill(win->pixmap, black_pixel);
46 /* draw axes intersecting in the middle, where text should be shown */
47 GP_HLine(win->pixmap, 0, X, Y/2, darkgray_pixel);
48 GP_VLine(win->pixmap, X/2, 0, Y, darkgray_pixel);
50 switch (font_flag) {
51 case 0:
52 style.font = &GP_DefaultProportionalFont;
53 break;
54 case 1:
55 style.font = &GP_DefaultConsoleFont;
56 break;
57 case 2:
58 style.font = GP_FontTinyMono;
59 break;
60 case 3:
61 style.font = GP_FontTiny;
62 break;
63 case 4:
64 style.font = GP_FontC64;
65 break;
66 case 5:
67 style.font = font;
68 break;
71 GP_Text(win->pixmap, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_BELOW,
72 yellow_pixel, black_pixel, "bottom left");
73 GP_Text(win->pixmap, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_BELOW,
74 red_pixel, black_pixel, "bottom right");
75 GP_Text(win->pixmap, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_ABOVE,
76 blue_pixel, black_pixel, "top right");
77 GP_Text(win->pixmap, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_ABOVE,
78 green_pixel, black_pixel, "top left");
80 GP_HLine(win->pixmap, 0, X, Y/3, darkgray_pixel);
81 GP_Text(win->pixmap, &style, X/2, Y/3, GP_ALIGN_CENTER|GP_VALIGN_BASELINE,
82 white_pixel, black_pixel, "x center y baseline");
85 static void event_loop(void)
87 GP_Event ev;
89 for (;;) {
90 GP_BackendWaitEvent(win, &ev);
92 switch (ev.type) {
93 case GP_EV_KEY:
94 if (ev.code != GP_EV_KEY_DOWN)
95 continue;
97 switch (ev.val.key.key) {
98 case GP_KEY_X:
99 win->pixmap->x_swap = !win->pixmap->x_swap;
100 break;
101 case GP_KEY_Y:
102 win->pixmap->y_swap = !win->pixmap->y_swap;
103 break;
104 case GP_KEY_R:
105 win->pixmap->axes_swap = !win->pixmap->axes_swap;
106 GP_SWAP(X, Y);
107 break;
108 case GP_KEY_SPACE:
109 font_flag++;
111 if (font) {
112 if (font_flag > 5)
113 font_flag = 0;
114 } else {
115 if (font_flag > 4)
116 font_flag = 0;
118 break;
119 case GP_KEY_UP:
120 style.pixel_xspace++;
121 style.pixel_yspace++;
122 break;
123 case GP_KEY_DOWN:
124 style.pixel_xspace--;
125 style.pixel_yspace--;
126 break;
127 case GP_KEY_RIGHT:
128 style.pixel_xmul++;
129 style.pixel_ymul++;
130 break;
131 case GP_KEY_LEFT:
132 style.pixel_xmul--;
133 style.pixel_ymul--;
134 break;
135 case GP_KEY_ESC:
136 GP_BackendExit(win);
137 exit(0);
138 break;
140 break;
141 case GP_EV_SYS:
142 switch(ev.code) {
143 case GP_EV_SYS_QUIT:
144 GP_BackendExit(win);
145 exit(0);
146 break;
147 case GP_EV_SYS_RESIZE:
148 GP_BackendResizeAck(win);
149 X = win->pixmap->w;
150 Y = win->pixmap->h;
151 break;
153 break;
156 redraw_screen();
157 GP_BackendFlip(win);
161 void print_instructions(void)
163 printf("Use the following keys to control the test:\n");
164 printf(" Space ........ toggle font\n");
165 printf(" X ............ mirror X\n");
166 printf(" Y ............ mirror Y\n");
167 printf(" R ............ reverse X and Y\n");
168 printf(" UP/DOWN ...... increase/decrease X and Y space\n");
169 printf(" RIGHT/LEFT ... increase/decrease X and Y mul\n");
172 int main(int argc, char *argv[])
174 const char *backend_opts = "X11";
176 if (argc > 1)
177 font = GP_FontFaceLoad(argv[1], 0, 20);
179 print_instructions();
181 win = GP_BackendInit(backend_opts, "Font Align Test");
183 if (win == NULL) {
184 fprintf(stderr, "Failed to initalize backend '%s'\n",
185 backend_opts);
186 return 1;
189 black_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0x00, win->pixmap);
190 red_pixel = GP_RGBToPixmapPixel(0xff, 0x00, 0x00, win->pixmap);
191 blue_pixel = GP_RGBToPixmapPixel(0x00, 0x00, 0xff, win->pixmap);
192 green_pixel = GP_RGBToPixmapPixel(0x00, 0xff, 0x00, win->pixmap);
193 yellow_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0x00, win->pixmap);
194 white_pixel = GP_RGBToPixmapPixel(0xff, 0xff, 0xff, win->pixmap);
195 darkgray_pixel = GP_RGBToPixmapPixel(0x7f, 0x7f, 0x7f, win->pixmap);
197 redraw_screen();
198 GP_BackendFlip(win);
199 event_loop();
201 return 0;