選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

252 行
6.2 KiB

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