core: Get rid of GP_Color.
[gfxprim.git] / demos / c_simple / textaligntest.c
blob5e2fbd6914df2f1c93580f18b64554dff8d2dd44
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->context, black_pixel);
46 /* draw axes intersecting in the middle, where text should be shown */
47 GP_HLine(win->context, 0, X, Y/2, darkgray_pixel);
48 GP_VLine(win->context, 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->context, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_BELOW,
72 yellow_pixel, black_pixel, "bottom left");
73 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_BELOW,
74 red_pixel, black_pixel, "bottom right");
75 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_ABOVE,
76 blue_pixel, black_pixel, "top right");
77 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_ABOVE,
78 green_pixel, black_pixel, "top left");
80 GP_HLine(win->context, 0, X, Y/3, darkgray_pixel);
81 GP_Text(win->context, &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->context->x_swap = !win->context->x_swap;
100 break;
101 case GP_KEY_Y:
102 win->context->y_swap = !win->context->y_swap;
103 break;
104 case GP_KEY_R:
105 win->context->axes_swap = !win->context->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;
148 break;
151 redraw_screen();
152 GP_BackendFlip(win);
156 void print_instructions(void)
158 printf("Use the following keys to control the test:\n");
159 printf(" Space ........ toggle font\n");
160 printf(" X ............ mirror X\n");
161 printf(" Y ............ mirror Y\n");
162 printf(" R ............ reverse X and Y\n");
163 printf(" UP/DOWN ...... increase/decrease X and Y space\n");
164 printf(" RIGHT/LEFT ... increase/decrease X and Y mul\n");
167 int main(int argc, char *argv[])
169 const char *backend_opts = "X11";
171 if (argc > 1)
172 font = GP_FontFaceLoad(argv[1], 0, 20);
174 print_instructions();
176 win = GP_BackendInit(backend_opts, "Font Align Test");
178 if (win == NULL) {
179 fprintf(stderr, "Failed to initalize backend '%s'\n",
180 backend_opts);
181 return 1;
184 black_pixel = GP_RGBToContextPixel(0x00, 0x00, 0x00, win->context);
185 red_pixel = GP_RGBToContextPixel(0xff, 0x00, 0x00, win->context);
186 blue_pixel = GP_RGBToContextPixel(0x00, 0x00, 0xff, win->context);
187 green_pixel = GP_RGBToContextPixel(0x00, 0xff, 0x00, win->context);
188 yellow_pixel = GP_RGBToContextPixel(0xff, 0xff, 0x00, win->context);
189 white_pixel = GP_RGBToContextPixel(0xff, 0xff, 0xff, win->context);
190 darkgray_pixel = GP_RGBToContextPixel(0x7f, 0x7f, 0x7f, win->context);
192 redraw_screen();
193 GP_BackendFlip(win);
194 event_loop();
196 return 0;