Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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