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.
 
 
 
 
 
 

248 lines
6.5 KiB

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