Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

239 wiersze
6.4 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[4][2] = {
  39. {-SQUARE_SIZE, -SQUARE_SIZE},
  40. { SQUARE_SIZE, -SQUARE_SIZE},
  41. { SQUARE_SIZE, SQUARE_SIZE},
  42. {-SQUARE_SIZE, SQUARE_SIZE},
  43. };
  44. float rotated[4][2];
  45. /* Create displayed canvas */
  46. cv = caca_create_canvas(0, 0);
  47. if(!cv)
  48. {
  49. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  50. return 1;
  51. }
  52. /* Create texture holding canvas */
  53. tex = caca_create_canvas(16, 16);
  54. if(!tex)
  55. {
  56. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  57. return 1;
  58. }
  59. /* Open window */
  60. dp = caca_create_display(cv);
  61. if(!dp)
  62. {
  63. fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
  64. return 1;
  65. }
  66. /* Set the window title */
  67. caca_set_display_title(dp, "trifiller");
  68. /* Frame duration */
  69. caca_set_display_time(dp, 10000);
  70. /* Get displayed canvas size */
  71. ww = caca_get_canvas_width(cv);
  72. wh = caca_get_canvas_height(cv);
  73. /* Texture size */
  74. tw = caca_get_canvas_width(tex);
  75. th = caca_get_canvas_height(tex);
  76. /* Load texture if any */
  77. if(argc == 2)
  78. {
  79. struct image *im = load_image(argv[1]);
  80. if(!im)
  81. {
  82. fprintf(stderr, "%s: unable to load image '%s'\n", argv[0], argv[1]);
  83. return 1;
  84. }
  85. caca_set_dither_algorithm(im->dither,
  86. caca_get_dither_algorithm_list(NULL)[4]);
  87. caca_dither_bitmap(tex,
  88. 0, 0,
  89. tw, th,
  90. im->dither, im->pixels);
  91. unload_image(im);
  92. }
  93. /* or generate one */
  94. else
  95. {
  96. int i;
  97. for(i = 0; i < 16; i ++)
  98. {
  99. caca_set_color_ansi(tex, (i+1)%0xF, i);
  100. caca_put_str(tex, 0, i, "123456789ABCDEF");
  101. }
  102. }
  103. px = 0;
  104. py = 0;
  105. while(!quit)
  106. {
  107. caca_event_t ev;
  108. unsigned int const event_mask = CACA_EVENT_KEY_PRESS
  109. | CACA_EVENT_RESIZE
  110. | 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] = square[p][0] * cos(angle*M_PI/180.0f) - square[p][1] * sin(angle*M_PI/180.0f);
  161. rotated[p][1] = square[p][0] * sin(angle*M_PI/180.0f) + square[p][1] * cos(angle*M_PI/180.0f);
  162. rotated[p][0] += ww/2 + px;
  163. rotated[p][1] += wh/2 + py;
  164. }
  165. angle+=1.0f;
  166. /* Display two triangles */
  167. caca_fill_triangle_textured(cv,
  168. /* triangle screen coordinates */
  169. rotated[0][0], rotated[0][1],
  170. rotated[1][0], rotated[1][1],
  171. rotated[2][0], rotated[2][1],
  172. /* texture coordinates */
  173. 0, 0,
  174. 1, 0,
  175. 1, 1,
  176. tex);
  177. caca_fill_triangle_textured(cv,
  178. /* triangle screen coordinates */
  179. rotated[0][0], rotated[0][1],
  180. rotated[2][0], rotated[2][1],
  181. rotated[3][0], rotated[3][1],
  182. /* texture coordinates */
  183. 0, 0,
  184. 1, 1,
  185. 0, 1,
  186. tex);
  187. /* Refresh display and clear for next frame */
  188. caca_refresh_display(dp);
  189. caca_clear_canvas(cv);
  190. }
  191. caca_free_display(dp);
  192. caca_free_canvas(cv);
  193. caca_free_canvas(tex);
  194. return 0;
  195. }