25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

frame.c 7.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library 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. /*
  15. * This file contains a small framework for canvas frame management.
  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 "caca.h"
  24. #include "caca_internals.h"
  25. /** \brief Get the number of frames in a canvas.
  26. *
  27. * Return the current canvas' frame count.
  28. *
  29. * This function never fails.
  30. *
  31. * \param cv A libcaca canvas
  32. * \return The frame count
  33. */
  34. int caca_get_frame_count(caca_canvas_t const *cv)
  35. {
  36. return cv->framecount;
  37. }
  38. /** \brief Activate a given canvas frame.
  39. *
  40. * Set the active canvas frame. All subsequent drawing operations will
  41. * be performed on that frame. The current painting context set by
  42. * caca_set_attr() is inherited.
  43. *
  44. * If the frame index is outside the canvas' frame range, nothing happens.
  45. *
  46. * If an error occurs, -1 is returned and \b errno is set accordingly:
  47. * - \c EINVAL Requested frame is out of range.
  48. *
  49. * \param cv A libcaca canvas
  50. * \param id The canvas frame to activate
  51. * \return 0 in case of success, -1 if an error occurred.
  52. */
  53. int caca_set_frame(caca_canvas_t *cv, int id)
  54. {
  55. if(id < 0 || id >= cv->framecount)
  56. {
  57. seterrno(EINVAL);
  58. return -1;
  59. }
  60. /* Bail out if no operation is required */
  61. if(id == cv->frame)
  62. return 0;
  63. _caca_save_frame_info(cv);
  64. cv->frame = id;
  65. _caca_load_frame_info(cv);
  66. if(!cv->dirty_disabled)
  67. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  68. return 0;
  69. }
  70. /** \brief Get the current frame's name.
  71. *
  72. * Return the current frame's name. The returned string is valid until
  73. * the frame is deleted or caca_set_frame_name() is called to change
  74. * the frame name again.
  75. *
  76. * This function never fails.
  77. *
  78. * \param cv A libcaca canvas.
  79. * \return The current frame's name.
  80. */
  81. char const *caca_get_frame_name(caca_canvas_t const *cv)
  82. {
  83. return cv->frames[cv->frame].name;
  84. }
  85. /** \brief Set the current frame's name.
  86. *
  87. * Set the current frame's name. Upon creation, a frame has a default name
  88. * of \c "frame#xxxxxxxx" where \c xxxxxxxx is a self-incrementing
  89. * hexadecimal number.
  90. *
  91. * If an error occurs, -1 is returned and \b errno is set accordingly:
  92. * - \c ENOMEM Not enough memory to allocate new frame.
  93. *
  94. * \param cv A libcaca canvas.
  95. * \param name The name to give to the current frame.
  96. * \return 0 in case of success, -1 if an error occurred.
  97. */
  98. int caca_set_frame_name(caca_canvas_t *cv, char const *name)
  99. {
  100. char *newname = strdup(name);
  101. if(!newname)
  102. {
  103. seterrno(ENOMEM);
  104. return -1;
  105. }
  106. free(cv->frames[cv->frame].name);
  107. cv->frames[cv->frame].name = newname;
  108. return 0;
  109. }
  110. /** \brief Add a frame to a canvas.
  111. *
  112. * Create a new frame within the given canvas. Its contents and attributes
  113. * are copied from the currently active frame.
  114. *
  115. * The frame index indicates where the frame should be inserted. Valid
  116. * values range from 0 to the current canvas frame count. If the frame
  117. * index is greater than or equals the current canvas frame count, the new
  118. * frame is appended at the end of the canvas. If the frame index is less
  119. * than zero, the new frame is inserted at index 0.
  120. *
  121. * The active frame does not change, but its index may be renumbered due
  122. * to the insertion.
  123. *
  124. * If an error occurs, -1 is returned and \b errno is set accordingly:
  125. * - \c ENOMEM Not enough memory to allocate new frame.
  126. *
  127. * \param cv A libcaca canvas
  128. * \param id The index where to insert the new frame
  129. * \return 0 in case of success, -1 if an error occurred.
  130. */
  131. int caca_create_frame(caca_canvas_t *cv, int id)
  132. {
  133. int size = cv->width * cv->height;
  134. int f;
  135. if(id < 0)
  136. id = 0;
  137. else if(id > cv->framecount)
  138. id = cv->framecount;
  139. cv->framecount++;
  140. cv->frames = realloc(cv->frames,
  141. sizeof(struct caca_frame) * cv->framecount);
  142. for(f = cv->framecount - 1; f > id; f--)
  143. cv->frames[f] = cv->frames[f - 1];
  144. if(cv->frame >= id)
  145. cv->frame++;
  146. cv->frames[id].width = cv->width;
  147. cv->frames[id].height = cv->height;
  148. cv->frames[id].chars = malloc(size * sizeof(uint32_t));
  149. memcpy(cv->frames[id].chars, cv->chars, size * sizeof(uint32_t));
  150. cv->frames[id].attrs = malloc(size * sizeof(uint32_t));
  151. memcpy(cv->frames[id].attrs, cv->attrs, size * sizeof(uint32_t));
  152. cv->frames[id].curattr = cv->curattr;
  153. cv->frames[id].x = cv->frames[cv->frame].x;
  154. cv->frames[id].y = cv->frames[cv->frame].y;
  155. cv->frames[id].handlex = cv->frames[cv->frame].handlex;
  156. cv->frames[id].handley = cv->frames[cv->frame].handley;
  157. cv->frames[id].name = strdup("frame#--------");
  158. sprintf(cv->frames[id].name + 6, "%.08x", ++cv->autoinc);
  159. return 0;
  160. }
  161. /** \brief Remove a frame from a canvas.
  162. *
  163. * Delete a frame from a given canvas.
  164. *
  165. * The frame index indicates the frame to delete. Valid values range from
  166. * 0 to the current canvas frame count minus 1. If the frame index is
  167. * greater than or equals the current canvas frame count, the last frame
  168. * is deleted.
  169. *
  170. * If the active frame is deleted, frame 0 becomes the new active frame.
  171. * Otherwise, the active frame does not change, but its index may be
  172. * renumbered due to the deletion.
  173. *
  174. * If an error occurs, -1 is returned and \b errno is set accordingly:
  175. * - \c EINVAL Requested frame is out of range, or attempt to delete the
  176. * last frame of the canvas.
  177. *
  178. * \param cv A libcaca canvas
  179. * \param id The index of the frame to delete
  180. * \return 0 in case of success, -1 if an error occurred.
  181. */
  182. int caca_free_frame(caca_canvas_t *cv, int id)
  183. {
  184. int f;
  185. if(id < 0 || id >= cv->framecount)
  186. {
  187. seterrno(EINVAL);
  188. return -1;
  189. }
  190. if(cv->framecount == 1)
  191. {
  192. seterrno(EINVAL);
  193. return -1;
  194. }
  195. free(cv->frames[id].chars);
  196. free(cv->frames[id].attrs);
  197. free(cv->frames[id].name);
  198. for(f = id + 1; f < cv->framecount; f++)
  199. cv->frames[f - 1] = cv->frames[f];
  200. cv->framecount--;
  201. cv->frames = realloc(cv->frames,
  202. sizeof(struct caca_frame) * cv->framecount);
  203. if(cv->frame > id)
  204. cv->frame--;
  205. else if(cv->frame == id)
  206. {
  207. cv->frame = 0;
  208. _caca_load_frame_info(cv);
  209. if(!cv->dirty_disabled)
  210. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  211. }
  212. return 0;
  213. }
  214. /*
  215. * XXX: the following functions are local.
  216. */
  217. void _caca_save_frame_info(caca_canvas_t *cv)
  218. {
  219. cv->frames[cv->frame].width = cv->width;
  220. cv->frames[cv->frame].height = cv->height;
  221. cv->frames[cv->frame].curattr = cv->curattr;
  222. }
  223. void _caca_load_frame_info(caca_canvas_t *cv)
  224. {
  225. cv->width = cv->frames[cv->frame].width;
  226. cv->height = cv->frames[cv->frame].height;
  227. cv->chars = cv->frames[cv->frame].chars;
  228. cv->attrs = cv->frames[cv->frame].attrs;
  229. cv->curattr = cv->frames[cv->frame].curattr;
  230. }
  231. /*
  232. * XXX: The following functions are aliases.
  233. */
  234. int cucul_get_frame_count(cucul_canvas_t const *)
  235. CACA_ALIAS(caca_get_frame_count);
  236. int cucul_set_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_set_frame);
  237. char const *cucul_get_frame_name(cucul_canvas_t const *)
  238. CACA_ALIAS(caca_get_frame_name);
  239. int cucul_set_frame_name(cucul_canvas_t *, char const *)
  240. CACA_ALIAS(caca_set_frame_name);
  241. int cucul_create_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_create_frame);
  242. int cucul_free_frame(cucul_canvas_t *, int) CACA_ALIAS(caca_free_frame);