Debian Codegraph @

examples/trifiller.c

raw
1 /*
2 * trifiller texture mapping features
3 * Copyright (c) 2009-2015 Jean-Yves Lamoureux <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 /* This will include config.h and everything else */
14 #include "../src/common-image.c"
15
16 #include <stdio.h> /* fprintf */
17 #include <math.h> /* M_PI / cos / sin */
18
19
20 #define SQUARE_SIZE 20
21
22 int main(int argc, char *argv[])
23 {
24
25 /* libcaca/libcaca contexts */
26 caca_canvas_t *cv;
27 caca_display_t *dp;
28 caca_canvas_t *tex;
29
30 /* cached canvas size */
31 int ww, wh, tw, th;
32
33 /* logic */
34 int quit = 0;
35 int update = 1;
36 int px, py;
37 float angle = 0;
38
39
40 float square[6][2] = {
41 {-SQUARE_SIZE, -SQUARE_SIZE},
42 {SQUARE_SIZE, -SQUARE_SIZE},
43 {SQUARE_SIZE, SQUARE_SIZE},
44 {-SQUARE_SIZE, SQUARE_SIZE},
45 };
46 float uv1[6] = {
47 0, 0,
48 1, 0,
49 1, 1
50 };
51 float uv2[6] = {
52 0, 0,
53 1, 1,
54 0, 1
55 };
56
57
58 float rotated[4][2];
59 int coords1[6], coords2[6];
60
61 /* Create displayed canvas */
62 cv = caca_create_canvas(0, 0);
63 if (!cv)
64 {
65 fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
66 return 1;
67 }
68
69 /* Create texture holding canvas */
70 tex = caca_create_canvas(16, 16);
71 if (!tex)
72 {
73 fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
74 return 1;
75 }
76
77 /* Open window */
78 dp = caca_create_display(cv);
79 if (!dp)
80 {
81 fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
82 return 1;
83 }
84
85
86
87 /* Set the window title */
88 caca_set_display_title(dp, "trifiller");
89
90 /* Frame duration */
91 caca_set_display_time(dp, 10000);
92
93 /* Get displayed canvas size */
94 ww = caca_get_canvas_width(cv);
95 wh = caca_get_canvas_height(cv);
96
97 /* Texture size */
98 tw = caca_get_canvas_width(tex);
99 th = caca_get_canvas_height(tex);
100
101 /* Load texture if any */
102 if (argc == 2)
103 {
104 struct image *im = load_image(argv[1]);
105 if (!im)
106 {
107 fprintf(stderr, "%s: unable to load image '%s'\n", argv[0],
108 argv[1]);
109 return 1;
110 }
111
112 caca_set_dither_algorithm(im->dither,
113 caca_get_dither_algorithm_list(NULL)[4]);
114 caca_dither_bitmap(tex, 0, 0, tw, th, im->dither, im->pixels);
115 unload_image(im);
116 }
117 /* or generate one */
118 else
119 {
120
121 int i;
122 for (i = 0; i < 16; i++)
123 {
124 caca_set_color_ansi(tex, (i + 1) % 0xF, i % 0xF);
125 caca_put_str(tex, 0, i, "0123456789ABCDEF");
126 }
127 }
128
129
130 px = 0;
131 py = 0;
132
133 while (!quit)
134 {
135 caca_event_t ev;
136 unsigned int const event_mask = CACA_EVENT_KEY_PRESS
137 | CACA_EVENT_RESIZE | CACA_EVENT_QUIT;
138 int event;
139
140 if (update)
141 event = caca_get_event(dp, event_mask, &ev, 0);
142 else
143 event = caca_get_event(dp, event_mask, &ev, -1);
144
145 while (event)
146 {
147 if (caca_get_event_type(&ev) & CACA_EVENT_KEY_PRESS)
148 switch (caca_get_event_key_ch(&ev))
149 {
150 case 'q':
151 case 'Q':
152 case CACA_KEY_ESCAPE:
153 quit = 1;
154 break;
155 case CACA_KEY_UP:
156 py--;
157 break;
158 case CACA_KEY_DOWN:
159 py++;
160 break;
161 case CACA_KEY_LEFT:
162 px--;
163 break;
164 case CACA_KEY_RIGHT:
165 px++;
166 break;
167 case 'a':
168 angle += 1.0f;
169 break;
170 case 's':
171 angle -= 1.0f;
172 break;
173 }
174 else if (caca_get_event_type(&ev) == CACA_EVENT_RESIZE)
175 {
176 caca_refresh_display(dp);
177 ww = caca_get_event_resize_width(&ev);
178 wh = caca_get_event_resize_height(&ev);
179 update = 1;
180 }
181 else if (caca_get_event_type(&ev) & CACA_EVENT_QUIT)
182 quit = 1;
183
184 event = caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0);
185 }
186
187
188
189 /* 2D Rotation around screen center */
190 int p;
191 for (p = 0; p < 4; p++)
192 {
193 rotated[p][0] =
194 square[p][0] * cos(angle * M_PI / 180.0f) -
195 square[p][1] * sin(angle * M_PI / 180.0f);
196 rotated[p][1] =
197 square[p][0] * sin(angle * M_PI / 180.0f) +
198 square[p][1] * cos(angle * M_PI / 180.0f);
199
200 rotated[p][0] += ww / 2 + px;
201 rotated[p][1] += wh / 2 + py;
202 }
203
204 angle += 1.0f;
205
206
207 /* Reaarange coordinates to fit libcaca's format */
208 coords1[0] = rotated[0][0];
209 coords1[1] = rotated[0][1];
210 coords1[2] = rotated[1][0];
211 coords1[3] = rotated[1][1];
212 coords1[4] = rotated[2][0];
213 coords1[5] = rotated[2][1];
214
215 coords2[0] = rotated[0][0];
216 coords2[1] = rotated[0][1];
217 coords2[2] = rotated[2][0];
218 coords2[3] = rotated[2][1];
219 coords2[4] = rotated[3][0];
220 coords2[5] = rotated[3][1];
221
222 /* Display two triangles */
223 caca_fill_triangle_textured(cv, /* canvas */
224 coords1, /* triangle coordinates */
225 tex, /* texture canvas */
226 uv1); /* texture coordinates */
227 caca_fill_triangle_textured(cv, coords2, tex, uv2);
228
229 /* Refresh display and clear for next frame */
230 caca_refresh_display(dp);
231 caca_clear_canvas(cv);
232
233 }
234
235 caca_free_display(dp);
236 caca_free_canvas(cv);
237 caca_free_canvas(tex);
238
239 return 0;
240 }