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.
 
 
 
 
 
 

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