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.
 
 
 
 
 
 

179 lines
4.0 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. struct ee_frame
  28. {
  29. int w, h;
  30. int dx, dy;
  31. char *chars;
  32. int *color;
  33. };
  34. struct ee_sprite
  35. {
  36. int f;
  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->f = 0;
  50. sprite->nf = 0;
  51. sprite->frames = NULL;
  52. while(!feof(fd))
  53. {
  54. int x, y;
  55. int w = 0, h = 0, dx = 0, dy = 0;
  56. struct ee_frame *frame;
  57. /* Get width and height */
  58. if(!fgets(buf, BUFSIZ, fd))
  59. break;
  60. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  61. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  62. break;
  63. if(sprite->nf++)
  64. sprite->frames = realloc(sprite->frames,
  65. sprite->nf * sizeof(struct ee_frame));
  66. else
  67. sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
  68. frame = &sprite->frames[sprite->nf - 1];
  69. frame->w = w;
  70. frame->h = h;
  71. frame->dx = dx;
  72. frame->dy = dy;
  73. frame->chars = malloc(w * h * sizeof(char));
  74. frame->color = malloc(w * h * sizeof(int));
  75. for(y = 0; y < h; y++)
  76. {
  77. memset(buf, ' ', w);
  78. buf[w] = '\0';
  79. if(!fgets(buf, BUFSIZ, fd))
  80. goto failed;
  81. for(x = 0; x < w; x++)
  82. frame->chars[w * y + x] = buf[x];
  83. }
  84. for(y = 0; y < h; y++)
  85. {
  86. memset(buf, ' ', w);
  87. buf[w] = '\0';
  88. if(!fgets(buf, BUFSIZ, fd))
  89. goto failed;
  90. for(x = 0; x < w; x++)
  91. frame->color[w * y + x] = buf[x] - 'a';
  92. }
  93. continue;
  94. failed:
  95. free(sprite->frames[sprite->nf - 1].chars);
  96. free(sprite->frames[sprite->nf - 1].color);
  97. sprite->nf--;
  98. break;
  99. }
  100. fclose(fd);
  101. if(sprite->nf == 0)
  102. {
  103. free(sprite);
  104. return NULL;
  105. }
  106. return sprite;
  107. }
  108. void ee_set_sprite_frame(struct ee_sprite *sprite, int f)
  109. {
  110. if(f < 0 || f >= sprite->nf)
  111. return;
  112. sprite->f = f;
  113. }
  114. int ee_get_sprite_frame(struct ee_sprite *sprite)
  115. {
  116. return sprite->f;
  117. }
  118. void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
  119. {
  120. int i, j;
  121. struct ee_frame *frame = &sprite->frames[sprite->f];
  122. for(j = 0; j < frame->h; j++)
  123. {
  124. for(i = 0; i < frame->w; i++)
  125. {
  126. int col = frame->color[frame->w * j + i];
  127. if(col >= 0)
  128. {
  129. ee_goto(x + i - frame->dx, y + j - frame->dy);
  130. ee_color(col);
  131. ee_putchar(frame->chars[frame->w * j + i]);
  132. }
  133. }
  134. }
  135. }
  136. void ee_free_sprite(struct ee_sprite *sprite)
  137. {
  138. int i;
  139. for(i = sprite->nf; i--;)
  140. {
  141. struct ee_frame *frame = &sprite->frames[i];
  142. free(frame->chars);
  143. free(frame->color);
  144. }
  145. free(sprite->frames);
  146. free(sprite);
  147. }