Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

151 righe
3.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. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include "ee.h"
  26. struct ee_frame
  27. {
  28. int w, h;
  29. int dx, dy;
  30. char *chars;
  31. int *color;
  32. };
  33. struct ee_sprite
  34. {
  35. int f;
  36. int nf;
  37. struct ee_frame *frames;
  38. };
  39. struct ee_sprite *ee_load_sprite(const char *file)
  40. {
  41. char buf[BUFSIZ];
  42. struct ee_sprite *sprite;
  43. FILE *fd;
  44. fd = fopen(file, "r");
  45. if(fd == NULL)
  46. return NULL;
  47. sprite = malloc(sizeof(struct ee_sprite));
  48. sprite->f = 0;
  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; x++)
  79. frame->chars[w * y + x] = buf[x];
  80. }
  81. for(y = 0; y < h; y++)
  82. {
  83. if(!fgets(buf, BUFSIZ, fd))
  84. goto failed;
  85. for(x = 0; x < w; x++)
  86. frame->color[w * y + x] = buf[x] - 'a';
  87. }
  88. continue;
  89. failed:
  90. free(sprite->frames[sprite->nf - 1].chars);
  91. free(sprite->frames[sprite->nf - 1].color);
  92. sprite->nf--;
  93. break;
  94. }
  95. fclose(fd);
  96. if(sprite->nf == 0)
  97. {
  98. free(sprite);
  99. return NULL;
  100. }
  101. return sprite;
  102. }
  103. void ee_draw_sprite(int x, int y, struct ee_sprite *sprite)
  104. {
  105. int i, j;
  106. struct ee_frame *frame = &sprite->frames[sprite->f];
  107. for(j = 0; j < frame->h; j++)
  108. {
  109. for(i = 0; i < frame->w; i++)
  110. {
  111. int col = frame->color[frame->w * j + i];
  112. if(col >= 0)
  113. {
  114. ee_goto(x + i - frame->dx, y + j - frame->dy);
  115. ee_color(col);
  116. ee_putchar(frame->chars[frame->w * j + i]);
  117. }
  118. }
  119. }
  120. }
  121. void ee_free_sprite(struct ee_sprite *sprite)
  122. {
  123. free(sprite);
  124. }