demos: Fix and cleanup GP_BackendInit() + docs.
[gfxprim/pasky.git] / demos / c_simple / textaligntest.c
blob1e8a77dbe40518c2606bc6f44176c4655437de22
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 <GP.h>
28 static GP_Pixel black_pixel, red_pixel, yellow_pixel, green_pixel, blue_pixel,
29 darkgray_pixel;
31 static int font_flag = 0;
33 static int X = 640;
34 static int Y = 480;
36 static GP_FontFace *font = NULL;
38 static GP_Backend *win;
40 void redraw_screen(void)
42 GP_Fill(win->context, black_pixel);
44 /* draw axes intersecting in the middle, where text should be shown */
45 GP_HLine(win->context, 0, X, Y/2, darkgray_pixel);
46 GP_VLine(win->context, X/2, 0, Y, darkgray_pixel);
48 GP_TextStyle style = GP_DEFAULT_TEXT_STYLE;
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 = font;
59 break;
62 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_BELOW,
63 yellow_pixel, black_pixel, "bottom left");
64 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_BELOW,
65 red_pixel, black_pixel, "bottom right");
66 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_RIGHT|GP_VALIGN_ABOVE,
67 blue_pixel, black_pixel, "top right");
68 GP_Text(win->context, &style, X/2, Y/2, GP_ALIGN_LEFT|GP_VALIGN_ABOVE,
69 green_pixel, black_pixel, "top left");
72 static void event_loop(void)
74 GP_Event ev;
76 for (;;) {
77 GP_BackendWaitEvent(win, &ev);
79 switch (ev.type) {
80 case GP_EV_KEY:
81 if (ev.code != GP_EV_KEY_DOWN)
82 continue;
84 switch (ev.val.key.key) {
85 case GP_KEY_X:
86 win->context->x_swap = !win->context->x_swap;
87 break;
88 case GP_KEY_Y:
89 win->context->y_swap = !win->context->y_swap;
90 break;
91 case GP_KEY_R:
92 win->context->axes_swap = !win->context->axes_swap;
93 GP_SWAP(X, Y);
94 break;
95 case GP_KEY_SPACE:
96 font_flag++;
98 if (font) {
99 if (font_flag >= 3)
100 font_flag = 0;
101 } else {
102 if (font_flag >= 2)
103 font_flag = 0;
105 break;
106 case GP_KEY_ESC:
107 GP_BackendExit(win);
108 exit(0);
109 break;
111 break;
112 case GP_EV_SYS:
113 switch(ev.code) {
114 case GP_EV_SYS_QUIT:
115 GP_BackendExit(win);
116 exit(0);
117 break;
119 break;
122 redraw_screen();
123 GP_BackendFlip(win);
127 void print_instructions(void)
129 printf("Use the following keys to control the test:\n");
130 printf(" Space ........ toggle proportional/nonproportional font\n");
131 printf(" X ............ mirror X\n");
132 printf(" Y ............ mirror Y\n");
133 printf(" R ............ reverse X and Y\n");
136 int main(int argc, char *argv[])
138 const char *backend_opts = "X11";
140 if (argc > 1)
141 font = GP_FontFaceLoad(argv[1], 0, 16);
143 print_instructions();
145 win = GP_BackendInit(backend_opts, "Font Align Test");
147 if (win == NULL) {
148 fprintf(stderr, "Failed to initalize backend '%s'\n",
149 backend_opts);
150 return 1;
153 black_pixel = GP_ColorToContextPixel(GP_COL_BLACK, win->context);
154 red_pixel = GP_ColorToContextPixel(GP_COL_RED, win->context);
155 blue_pixel = GP_ColorToContextPixel(GP_COL_BLUE, win->context);
156 green_pixel = GP_ColorToContextPixel(GP_COL_GREEN, win->context);
157 yellow_pixel = GP_ColorToContextPixel(GP_COL_YELLOW, win->context);
158 darkgray_pixel = GP_ColorToContextPixel(GP_COL_GRAY_DARK, win->context);
160 redraw_screen();
161 GP_BackendFlip(win);
162 event_loop();
164 return 0;