Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

234 рядки
4.9 KiB

  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "ee.h"
  27. #include "ee_internals.h"
  28. struct ee_frame
  29. {
  30. int w, h;
  31. int dx, dy;
  32. char *chars;
  33. int *color;
  34. };
  35. struct ee_sprite
  36. {
  37. int nf;
  38. struct ee_frame *frames;
  39. };
  40. struct ee_sprite *ee_load_sprite(const char *file)
  41. {
  42. char buf[BUFSIZ];
  43. struct ee_sprite *sprite;
  44. FILE *fd;
  45. fd = fopen(file, "r");
  46. if(fd == NULL)
  47. return NULL;
  48. sprite = malloc(sizeof(struct ee_sprite));
  49. sprite->nf = 0;
  50. sprite->frames = NULL;
  51. while(!feof(fd))
  52. {
  53. int x, y;
  54. int w = 0, h = 0, dx = 0, dy = 0;
  55. struct ee_frame *frame;
  56. /* Get width and height */
  57. if(!fgets(buf, BUFSIZ, fd))
  58. break;
  59. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  60. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  61. break;
  62. if(sprite->nf++)
  63. sprite->frames = realloc(sprite->frames,
  64. sprite->nf * sizeof(struct ee_frame));
  65. else
  66. sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
  67. frame = &sprite->frames[sprite->nf - 1];
  68. frame->w = w;
  69. frame->h = h;
  70. frame->dx = dx;
  71. frame->dy = dy;
  72. frame->chars = malloc(w * h * sizeof(char));
  73. frame->color = malloc(w * h * sizeof(int));
  74. for(y = 0; y < h; y++)
  75. {
  76. if(!fgets(buf, BUFSIZ, fd))
  77. goto failed;
  78. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  79. frame->chars[w * y + x] = buf[x];
  80. for(; x < w; x++)
  81. frame->chars[w * y + x] = ' ';
  82. }
  83. for(y = 0; y < h; y++)
  84. {
  85. if(!fgets(buf, BUFSIZ, fd))
  86. goto failed;
  87. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  88. frame->color[w * y + x] = buf[x] - 'a';
  89. for(; x < w; x++)
  90. frame->color[w * y + x] = ' ' - 'a';
  91. }
  92. continue;
  93. failed:
  94. free(sprite->frames[sprite->nf - 1].chars);
  95. free(sprite->frames[sprite->nf - 1].color);
  96. sprite->nf--;
  97. break;
  98. }
  99. fclose(fd);
  100. if(sprite->nf == 0)
  101. {
  102. free(sprite);
  103. return NULL;
  104. }
  105. return sprite;
  106. }
  107. int ee_get_sprite_frames(struct ee_sprite *sprite)
  108. {
  109. if(sprite == NULL)
  110. return 0;
  111. return sprite->nf;
  112. }
  113. int ee_get_sprite_width(struct ee_sprite *sprite, int f)
  114. {
  115. if(sprite == NULL)
  116. return 0;
  117. if(f < 0 || f >= sprite->nf)
  118. return 0;
  119. return sprite->frames[f].w;
  120. }
  121. int ee_get_sprite_height(struct ee_sprite *sprite, int f)
  122. {
  123. if(sprite == NULL)
  124. return 0;
  125. if(f < 0 || f >= sprite->nf)
  126. return 0;
  127. return sprite->frames[f].h;
  128. }
  129. int ee_get_sprite_dx(struct ee_sprite *sprite, int f)
  130. {
  131. if(sprite == NULL)
  132. return 0;
  133. if(f < 0 || f >= sprite->nf)
  134. return 0;
  135. return sprite->frames[f].dx;
  136. }
  137. int ee_get_sprite_dy(struct ee_sprite *sprite, int f)
  138. {
  139. if(sprite == NULL)
  140. return 0;
  141. if(f < 0 || f >= sprite->nf)
  142. return 0;
  143. return sprite->frames[f].dy;
  144. }
  145. void ee_draw_sprite(int x, int y, struct ee_sprite *sprite, int f)
  146. {
  147. int i, j, oldcol;
  148. struct ee_frame *frame;
  149. if(sprite == NULL)
  150. return;
  151. if(f < 0 || f >= sprite->nf)
  152. return;
  153. frame = &sprite->frames[f];
  154. oldcol = ee_get_color();
  155. for(j = 0; j < frame->h; j++)
  156. {
  157. for(i = 0; i < frame->w; i++)
  158. {
  159. int col = frame->color[frame->w * j + i];
  160. if(col >= 0)
  161. {
  162. ee_set_color(col);
  163. ee_putchar(x + i - frame->dx, y + j - frame->dy,
  164. frame->chars[frame->w * j + i]);
  165. }
  166. }
  167. }
  168. ee_set_color(oldcol);
  169. }
  170. void ee_free_sprite(struct ee_sprite *sprite)
  171. {
  172. int i;
  173. if(sprite == NULL)
  174. return;
  175. for(i = sprite->nf; i--;)
  176. {
  177. struct ee_frame *frame = &sprite->frames[i];
  178. free(frame->chars);
  179. free(frame->color);
  180. }
  181. free(sprite->frames);
  182. free(sprite);
  183. }