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