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.
 
 
 
 
 
 

290 lignes
8.2 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. cv->empty_line = cv->scratch_line = NULL;
  49. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  50. * default X11 window. When a graphic driver attaches to us, it can set
  51. * a different size. */
  52. if(width && height)
  53. _cucul_set_canvas_size(cv, width, height);
  54. else
  55. _cucul_set_canvas_size(cv, 80, 32);
  56. if(_cucul_init_dither())
  57. {
  58. free(cv);
  59. return NULL;
  60. }
  61. return cv;
  62. }
  63. /** \brief Resize a canvas.
  64. *
  65. * This function sets the canvas width and height, in character cells.
  66. *
  67. * The contents of the canvas are preserved to the extent of the new
  68. * canvas size. Newly allocated character cells at the right and/or at
  69. * the bottom of the canvas are filled with spaces.
  70. *
  71. * It is an error to try to resize the canvas if an output driver has
  72. * been attached to the canvas using caca_create_display(). You need to
  73. * remove the output driver using caca_free_display() before you can change
  74. * the canvas size again. However, the caca output driver can cause a
  75. * canvas resize through user interaction. See the caca_event() documentation
  76. * for more about this.
  77. *
  78. * \param cv A libcucul canvas
  79. * \param width The desired canvas width
  80. * \param height The desired canvas height
  81. */
  82. void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  83. unsigned int height)
  84. {
  85. if(cv->refcount)
  86. return;
  87. _cucul_set_canvas_size(cv, width, height);
  88. }
  89. /** \brief Get the canvas width.
  90. *
  91. * This function returns the current canvas width, in character cells.
  92. *
  93. * \param cv A libcucul canvas
  94. * \return The canvas width.
  95. */
  96. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  97. {
  98. return cv->width;
  99. }
  100. /** \brief Get the canvas height.
  101. *
  102. * This function returns the current canvas height, in character cells.
  103. *
  104. * \param cv A libcucul canvas
  105. * \return The canvas height.
  106. */
  107. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  108. {
  109. return cv->height;
  110. }
  111. /** \brief Translate a colour index into the colour's name.
  112. *
  113. * This function translates a cucul_color enum into a human-readable
  114. * description string of the associated colour.
  115. *
  116. * \param color The colour value.
  117. * \return A static string containing the colour's name.
  118. */
  119. char const *cucul_get_color_name(unsigned int color)
  120. {
  121. static char const *color_names[] =
  122. {
  123. "black",
  124. "blue",
  125. "green",
  126. "cyan",
  127. "red",
  128. "magenta",
  129. "brown",
  130. "light gray",
  131. "dark gray",
  132. "light blue",
  133. "light green",
  134. "light cyan",
  135. "light red",
  136. "light magenta",
  137. "yellow",
  138. "white",
  139. };
  140. if(color < 0 || color > 15)
  141. return "unknown";
  142. return color_names[color];
  143. }
  144. /** \brief Uninitialise \e libcucul.
  145. *
  146. * This function frees all resources allocated by cucul_create_canvas(). After
  147. * cucul_free_canvas() has been called, no other \e libcucul functions may be
  148. * used unless a new call to cucul_create_canvas() is done.
  149. *
  150. * \param cv A libcucul canvas
  151. */
  152. void cucul_free_canvas(cucul_canvas_t *cv)
  153. {
  154. _cucul_end_dither();
  155. free(cv->empty_line);
  156. free(cv->scratch_line);
  157. free(cv->chars);
  158. free(cv->attr);
  159. free(cv);
  160. }
  161. /** \brief Generate a random integer within a range.
  162. *
  163. * \param min The lower bound of the integer range.
  164. * \param max The upper bound of the integer range.
  165. * \return A random integer comprised between \p min and \p max - 1
  166. * (inclusive).
  167. */
  168. int cucul_rand(int min, int max)
  169. {
  170. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  171. }
  172. /*
  173. * XXX: The following functions are local.
  174. */
  175. void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  176. unsigned int height)
  177. {
  178. unsigned int x, y, old_width, old_height, new_size, old_size;
  179. old_width = cv->width;
  180. old_height = cv->height;
  181. old_size = old_width * old_height;
  182. cv->width = width;
  183. cv->height = height;
  184. new_size = width * height;
  185. /* Step 1: if new area is bigger, resize the memory area now. */
  186. if(new_size > old_size)
  187. {
  188. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  189. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  190. }
  191. /* Step 2: move line data if necessary. */
  192. if(width == old_width)
  193. {
  194. /* Width did not change, which means we do not need to move data. */
  195. ;
  196. }
  197. else if(width > old_width)
  198. {
  199. /* New width is bigger than old width, which means we need to
  200. * copy lines starting from the bottom of the screen otherwise
  201. * we will overwrite information. */
  202. for(y = height < old_height ? height : old_height; y--; )
  203. {
  204. for(x = old_width; x--; )
  205. {
  206. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  207. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  208. }
  209. /* Zero the end of the line */
  210. for(x = width - old_width; x--; )
  211. cv->chars[y * width + old_width + x] = (uint32_t)' ';
  212. memset(cv->attr + y * width + old_width, 0,
  213. (width - old_width) * 4);
  214. }
  215. }
  216. else
  217. {
  218. /* New width is smaller. Copy as many lines as possible. Ignore
  219. * the first line, it is already in place. */
  220. unsigned int lines = height < old_height ? height : old_height;
  221. for(y = 1; y < lines; y++)
  222. {
  223. for(x = 0; x < width; x++)
  224. {
  225. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  226. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  227. }
  228. }
  229. }
  230. /* Step 3: fill the bottom of the new screen if necessary. */
  231. if(height > old_height)
  232. {
  233. /* Zero the bottom of the screen */
  234. for(x = (height - old_height) * width; x--; )
  235. cv->chars[old_height * width + x] = (uint32_t)' ';
  236. memset(cv->attr + old_height * width, 0,
  237. (height - old_height) * width * 4);
  238. }
  239. /* Step 4: if new area is smaller, resize memory area now. */
  240. if(new_size <= old_size)
  241. {
  242. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  243. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  244. }
  245. /* Recompute the scratch line and the empty line */
  246. if(width != old_width)
  247. {
  248. cv->empty_line = realloc(cv->empty_line, width + 1);
  249. memset(cv->empty_line, ' ', width);
  250. cv->empty_line[width] = '\0';
  251. cv->scratch_line = realloc(cv->scratch_line, width + 1);
  252. }
  253. }