Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

241 řádky
6.1 KiB

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