You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

trifiller.c 6.2 KiB

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