altos/draw: Add transforms to polygon code
[fw/altos] / src / draw / draw-test.c
1 /*
2  * Copyright © 2023 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define WIDTH   128
20 #define HEIGHT  64
21
22 #define DEFAULT_WIDTH   WIDTH
23 #define DEFAULT_HEIGHT  HEIGHT
24
25 #define PASS_KEYS
26 #include "frame.c"
27 #include "ao_draw.h"
28
29 #define STRIDE  ((WIDTH + 31) / 32)
30
31 static uint32_t bits[STRIDE * HEIGHT];
32
33 static struct ao_bitmap fb = {
34         .base = bits,
35         .stride = STRIDE,
36         .width = WIDTH,
37         .height = HEIGHT
38 };
39
40 #define BIG_FONT FrutigerLT_Roman_64_font
41 #define SMALL_FONT FrutigerLT_Roman_12_font
42 #define LOGO_FONT BenguiatGothicStd_Bold_26_font
43
44 #define VALUE_Y         BIG_FONT.ascent
45 #define LABEL_Y         (int16_t) (BIG_FONT.ascent + SMALL_FONT.ascent + 2)
46 #define BOX_X           2
47 #define PAD_X           90
48 #define BOX_LABEL_X     30
49 #define PAD_LABEL_X     95
50 #define SEP_X           (PAD_X - 8)
51
52 static int      box_number = 1;
53 static int      pad_number = 1;
54 static int      do_polys = 0;
55
56 static const struct ao_coord trek[] = {
57         { .x = 90, .y = 0 },
58         { .x = 60, .y = 40 },
59         { .x = 90, .y = 20 },
60         { .x = 120, .y = 40 },
61 };
62
63 #define NCOORD_TREK (sizeof(trek)/sizeof(trek[0]))
64
65 static const struct ao_coord donut[] = {
66         { .x = 30, .y = 0 },
67         { .x = 0, .y = 30 },
68         { .x = 30, .y = 60 },
69         { .x = 60, .y = 30 },
70         { .x = 30, .y = 0 },
71         { .x = 30, .y = 10 },
72         { .x = 50, .y = 30 },
73         { .x = 30, .y = 50 },
74         { .x = 10, .y = 30 },
75         { .x = 30, .y = 10 },
76 };
77
78 #define NCOORD_DONUT (sizeof(donut)/sizeof(donut[0]))
79
80 static const struct ao_coord bowtie[] = {
81         { .x = 0, .y = 0 },
82         { .x = 32, .y = 32 },
83         { .x = 0, .y = 32 },
84         { .x = 32, .y = 0 },
85 };
86
87 #define NCOORD_BOWTIE (sizeof(bowtie)/sizeof(bowtie[0]))
88
89 static const struct ao_transform logo_transform = {
90         .x_scale = 48, .x_off = 0,
91         .y_scale = 48, .y_off = 10,
92 };
93
94 void HandleExpose(Display *dpy, Window win, GC gc)
95 {
96         char    str[64];
97
98         ao_rect(&fb, 0, 0, WIDTH, HEIGHT, 0xffffffff, AO_COPY);
99
100         switch (do_polys) {
101         case 1:
102                 ao_logo(&fb, &logo_transform, &LOGO_FONT, 0x00000000, AO_COPY);
103                 break;
104         case 2:
105                 ao_poly(&fb, trek, NCOORD_TREK, NULL, 0x00000000, AO_COPY);
106                 ao_poly(&fb, donut, NCOORD_DONUT, NULL, 0x00000000, AO_COPY);
107                 break;
108         case 3:
109                 ao_poly(&fb, bowtie, NCOORD_BOWTIE, NULL, 0x00000000, AO_COPY);
110                 break;
111         default:
112         case 0:
113
114                 sprintf(str, "%02d", box_number);
115                 ao_text(&fb, &BIG_FONT, BOX_X, VALUE_Y, str, 0x00000000, AO_COPY);
116                 ao_text(&fb, &SMALL_FONT, BOX_LABEL_X, LABEL_Y, "box", 0x00000000, AO_COPY);
117
118                 sprintf(str, "%d", pad_number);
119                 ao_text(&fb, &BIG_FONT, PAD_X, VALUE_Y, str, 0x00000000, AO_COPY);
120                 ao_text(&fb, &SMALL_FONT, PAD_LABEL_X, LABEL_Y, "pad", 0x00000000, AO_COPY);
121
122                 ao_line(&fb, SEP_X, 0, SEP_X, HEIGHT, 0x00000000, AO_COPY);
123                 break;
124         }
125
126         XImage *image = XCreateImage(dpy, visual, 1, XYBitmap, 0, (char *) bits, WIDTH, HEIGHT, 32, STRIDE*4);
127         XSetForeground(dpy, gc, WhitePixel(dpy, screen));
128         XSetBackground(dpy, gc, BlackPixel(dpy, screen));
129         XPutImage(dpy, win, gc, image, 0, 0, 0, 0, WIDTH, HEIGHT);
130         free(image);
131 }
132
133 void
134 HandleKeyPress(Display *dpy, Window win, GC gc, XEvent *ev)
135 {
136         char    string[10];
137         if (XLookupString ((XKeyEvent *) ev, string, sizeof (string), 0, 0) >= 1) {
138                 switch (string[0]) {
139                 case 'q':
140                         exit (0);
141                 case 'p':
142                         pad_number++;
143                         if (pad_number > 8)
144                                 pad_number = 1;
145                         break;
146                 case 'P':
147                         pad_number--;
148                         if (pad_number < 1)
149                                 pad_number = 8;
150                         break;
151                 case 'b':
152                         box_number++;
153                         if (box_number > 99)
154                                 box_number = 1;
155                         break;
156                 case 'B':
157                         box_number--;
158                         if (box_number < 1)
159                                 box_number = 99;
160                         break;
161                 case 's':
162                         do_polys++;
163                         if (do_polys == 4)
164                                 do_polys = 0;
165                         break;
166                 case 'c':
167                         break;
168                 }
169                 HandleExpose(dpy, win, gc);
170         }
171 }
172
173 void
174 HandleKeyRelease(Display *dpy, Window win, GC gc, XEvent *ev)
175 {
176         (void) dpy;
177         (void) win;
178         (void) gc;
179         (void) ev;
180 }