Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

276 lignes
7.8 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the main functions used by \e libcucul applications
  15. * to initialise a drawing context.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. /** \brief Initialise a \e libcucul canvas.
  26. *
  27. * This function initialises internal \e libcucul structures and the backend
  28. * that will be used for subsequent graphical operations. It must be the
  29. * first \e libcucul function to be called in a function. cucul_free_canvas()
  30. * should be called at the end of the program to free all allocated resources.
  31. *
  32. * If one of the desired canvas coordinates is zero, a default canvas size
  33. * of 80x32 is used instead.
  34. *
  35. * \param width The desired canvas width
  36. * \param height The desired canvas height
  37. * \return A libcucul canvas handle upon success, NULL if an error occurred.
  38. */
  39. cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
  40. {
  41. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  42. cv->refcount = 0;
  43. cv->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  44. cv->bgcolor = CUCUL_COLOR_BLACK;
  45. cv->width = cv->height = 0;
  46. cv->chars = NULL;
  47. cv->attr = NULL;
  48. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  49. * default X11 window. When a graphic driver attaches to us, it can set
  50. * a different size. */
  51. if(width && height)
  52. _cucul_set_canvas_size(cv, width, height);
  53. else
  54. _cucul_set_canvas_size(cv, 80, 32);
  55. if(_cucul_init_dither())
  56. {
  57. free(cv);
  58. return NULL;
  59. }
  60. return cv;
  61. }
  62. /** \brief Resize a canvas.
  63. *
  64. * This function sets the canvas width and height, in character cells.
  65. *
  66. * The contents of the canvas are preserved to the extent of the new
  67. * canvas size. Newly allocated character cells at the right and/or at
  68. * the bottom of the canvas are filled with spaces.
  69. *
  70. * It is an error to try to resize the canvas if an output driver has
  71. * been attached to the canvas using caca_create_display(). You need to
  72. * remove the output driver using caca_free_display() before you can change
  73. * the canvas size again. However, the caca output driver can cause a
  74. * canvas resize through user interaction. See the caca_event() documentation
  75. * for more about this.
  76. *
  77. * \param cv A libcucul canvas
  78. * \param width The desired canvas width
  79. * \param height The desired canvas height
  80. */
  81. void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  82. unsigned int height)
  83. {
  84. if(cv->refcount)
  85. return;
  86. _cucul_set_canvas_size(cv, width, height);
  87. }
  88. /** \brief Get the canvas width.
  89. *
  90. * This function returns the current canvas width, in character cells.
  91. *
  92. * \param cv A libcucul canvas
  93. * \return The canvas width.
  94. */
  95. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  96. {
  97. return cv->width;
  98. }
  99. /** \brief Get the canvas height.
  100. *
  101. * This function returns the current canvas height, in character cells.
  102. *
  103. * \param cv A libcucul canvas
  104. * \return The canvas height.
  105. */
  106. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  107. {
  108. return cv->height;
  109. }
  110. /** \brief Translate a colour index into the colour's name.
  111. *
  112. * This function translates a cucul_color enum into a human-readable
  113. * description string of the associated colour.
  114. *
  115. * \param color The colour value.
  116. * \return A static string containing the colour's name.
  117. */
  118. char const *cucul_get_color_name(unsigned int color)
  119. {
  120. static char const *color_names[] =
  121. {
  122. "black",
  123. "blue",
  124. "green",
  125. "cyan",
  126. "red",
  127. "magenta",
  128. "brown",
  129. "light gray",
  130. "dark gray",
  131. "light blue",
  132. "light green",
  133. "light cyan",
  134. "light red",
  135. "light magenta",
  136. "yellow",
  137. "white",
  138. };
  139. if(color < 0 || color > 15)
  140. return "unknown";
  141. return color_names[color];
  142. }
  143. /** \brief Uninitialise \e libcucul.
  144. *
  145. * This function frees all resources allocated by cucul_create_canvas(). After
  146. * cucul_free_canvas() has been called, no other \e libcucul functions may be
  147. * used unless a new call to cucul_create_canvas() is done.
  148. *
  149. * \param cv A libcucul canvas
  150. */
  151. void cucul_free_canvas(cucul_canvas_t *cv)
  152. {
  153. _cucul_end_dither();
  154. free(cv->chars);
  155. free(cv->attr);
  156. free(cv);
  157. }
  158. /** \brief Generate a random integer within a range.
  159. *
  160. * \param min The lower bound of the integer range.
  161. * \param max The upper bound of the integer range.
  162. * \return A random integer comprised between \p min and \p max - 1
  163. * (inclusive).
  164. */
  165. int cucul_rand(int min, int max)
  166. {
  167. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  168. }
  169. /*
  170. * XXX: The following functions are local.
  171. */
  172. void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  173. unsigned int height)
  174. {
  175. unsigned int x, y, old_width, old_height, new_size, old_size;
  176. old_width = cv->width;
  177. old_height = cv->height;
  178. old_size = old_width * old_height;
  179. cv->width = width;
  180. cv->height = height;
  181. new_size = width * height;
  182. /* Step 1: if new area is bigger, resize the memory area now. */
  183. if(new_size > old_size)
  184. {
  185. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  186. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  187. }
  188. /* Step 2: move line data if necessary. */
  189. if(width == old_width)
  190. {
  191. /* Width did not change, which means we do not need to move data. */
  192. ;
  193. }
  194. else if(width > old_width)
  195. {
  196. /* New width is bigger than old width, which means we need to
  197. * copy lines starting from the bottom of the screen otherwise
  198. * we will overwrite information. */
  199. for(y = height < old_height ? height : old_height; y--; )
  200. {
  201. for(x = old_width; x--; )
  202. {
  203. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  204. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  205. }
  206. /* Zero the end of the line */
  207. for(x = width - old_width; x--; )
  208. cv->chars[y * width + old_width + x] = (uint32_t)' ';
  209. memset(cv->attr + y * width + old_width, 0,
  210. (width - old_width) * 4);
  211. }
  212. }
  213. else
  214. {
  215. /* New width is smaller. Copy as many lines as possible. Ignore
  216. * the first line, it is already in place. */
  217. unsigned int lines = height < old_height ? height : old_height;
  218. for(y = 1; y < lines; y++)
  219. {
  220. for(x = 0; x < width; x++)
  221. {
  222. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  223. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  224. }
  225. }
  226. }
  227. /* Step 3: fill the bottom of the new screen if necessary. */
  228. if(height > old_height)
  229. {
  230. /* Zero the bottom of the screen */
  231. for(x = (height - old_height) * width; x--; )
  232. cv->chars[old_height * width + x] = (uint32_t)' ';
  233. memset(cv->attr + old_height * width, 0,
  234. (height - old_height) * width * 4);
  235. }
  236. /* Step 4: if new area is smaller, resize memory area now. */
  237. if(new_size <= old_size)
  238. {
  239. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  240. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  241. }
  242. }