Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

201 rader
4.4 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. #ifdef USE_SLANG
  24. # include <slang.h>
  25. #elif USE_NCURSES
  26. # include <curses.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "ee.h"
  32. struct ee_frame
  33. {
  34. int w, h;
  35. int dx, dy;
  36. char *chars;
  37. int *color;
  38. };
  39. struct ee_sprite
  40. {
  41. int f;
  42. int nf;
  43. struct ee_frame *frames;
  44. };
  45. struct ee_sprite *ee_load_sprite(const char *file)
  46. {
  47. char buf[BUFSIZ];
  48. struct ee_sprite *sprite;
  49. FILE *fd;
  50. fd = fopen(file, "r");
  51. if(fd == NULL)
  52. return NULL;
  53. sprite = malloc(sizeof(struct ee_sprite));
  54. sprite->f = 0;
  55. sprite->nf = 0;
  56. sprite->frames = NULL;
  57. while(!feof(fd))
  58. {
  59. int x, y;
  60. int w = 0, h = 0, dx = 0, dy = 0;
  61. struct ee_frame *frame;
  62. /* Get width and height */
  63. if(!fgets(buf, BUFSIZ, fd))
  64. break;
  65. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  66. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  67. break;
  68. if(sprite->nf++)
  69. sprite->frames = realloc(sprite->frames,
  70. sprite->nf * sizeof(struct ee_frame));
  71. else
  72. sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
  73. frame = &sprite->frames[sprite->nf - 1];
  74. frame->w = w;
  75. frame->h = h;
  76. frame->dx = dx;
  77. frame->dy = dy;
  78. frame->chars = malloc(w * h * sizeof(char));
  79. frame->color = malloc(w * h * sizeof(int));
  80. for(y = 0; y < h; y++)
  81. {
  82. if(!fgets(buf, BUFSIZ, fd))
  83. goto failed;
  84. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  85. frame->chars[w * y + x] = buf[x];
  86. for(; x < w; x++)
  87. frame->chars[w * y + x] = ' ';
  88. }
  89. for(y = 0; y < h; y++)
  90. {
  91. if(!fgets(buf, BUFSIZ, fd))
  92. goto failed;
  93. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  94. frame->color[w * y + x] = buf[x] - 'a';
  95. for(; x < w; x++)
  96. frame->color[w * y + x] = ' ' - 'a';
  97. }
  98. continue;
  99. failed:
  100. free(sprite->frames[sprite->nf - 1].chars);
  101. free(sprite->frames[sprite->nf - 1].color);
  102. sprite->nf--;
  103. break;
  104. }
  105. fclose(fd);
  106. if(sprite->nf == 0)
  107. {
  108. free(sprite);
  109. return NULL;
  110. }
  111. return sprite;
  112. }
  113. void ee_set_sprite_frame(struct ee_sprite *sprite, int f)
  114. {
  115. if(sprite == NULL)
  116. return;
  117. if(f < 0 || f >= sprite->nf)
  118. return;
  119. sprite->f = f;
  120. }
  121. int ee_get_sprite_frame(struct ee_sprite *sprite)
  122. {
  123. if(sprite == NULL)
  124. return -1;
  125. return sprite->f;
  126. }
  127. void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
  128. {
  129. int i, j;
  130. struct ee_frame *frame;
  131. if(sprite == NULL)
  132. return;
  133. frame = &sprite->frames[sprite->f];
  134. for(j = 0; j < frame->h; j++)
  135. {
  136. for(i = 0; i < frame->w; i++)
  137. {
  138. int col = frame->color[frame->w * j + i];
  139. if(col >= 0)
  140. {
  141. ee_color(col);
  142. ee_putchar(x + i - frame->dx, y + j - frame->dy,
  143. frame->chars[frame->w * j + i]);
  144. }
  145. }
  146. }
  147. }
  148. void ee_free_sprite(struct ee_sprite *sprite)
  149. {
  150. int i;
  151. if(sprite == NULL)
  152. return;
  153. for(i = sprite->nf; i--;)
  154. {
  155. struct ee_frame *frame = &sprite->frames[i];
  156. free(frame->chars);
  157. free(frame->color);
  158. }
  159. free(sprite->frames);
  160. free(sprite);
  161. }