sprites.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1997-2006 Id Software, Inc.
  4. This file is part of Quake 2 Tools source code.
  5. Quake 2 Tools source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake 2 Tools source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Quake 2 Tools source code; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include "qdata.h"
  19. #define MAX_SPRFRAMES MAX_MD2SKINS
  20. dsprite_t sprite;
  21. dsprframe_t frames[MAX_SPRFRAMES];
  22. byte *byteimage, *lbmpalette;
  23. int byteimagewidth, byteimageheight;
  24. char spritename[1024];
  25. void FinishSprite (void);
  26. void Cmd_Spritename (void);
  27. /*
  28. ==============
  29. FinishSprite
  30. ==============
  31. */
  32. void FinishSprite (void)
  33. {
  34. FILE *spriteouthandle;
  35. int i, curframe;
  36. dsprite_t spritetemp;
  37. char savename[1024];
  38. if (sprite.numframes == 0)
  39. return;
  40. if (!strlen(spritename))
  41. Error ("Didn't name sprite file");
  42. sprintf (savename, "%s%s.sp2", gamedir, spritename);
  43. if (g_release)
  44. {
  45. char name[1024];
  46. sprintf (name, "%s.sp2", spritename);
  47. ReleaseFile (name);
  48. spritename[0] = 0; // clear for a new sprite
  49. sprite.numframes = 0;
  50. return;
  51. }
  52. printf ("saving in %s\n", savename);
  53. CreatePath (savename);
  54. spriteouthandle = SafeOpenWrite (savename);
  55. //
  56. // write out the sprite header
  57. //
  58. spritetemp.ident = LittleLong (IDSPRITEHEADER);
  59. spritetemp.version = LittleLong (SPRITE_VERSION);
  60. spritetemp.numframes = LittleLong (sprite.numframes);
  61. SafeWrite (spriteouthandle, &spritetemp, 12);
  62. //
  63. // write out the frames
  64. //
  65. curframe = 0;
  66. for (i=0 ; i<sprite.numframes ; i++)
  67. {
  68. frames[i].width = LittleLong(frames[i].width);
  69. frames[i].height = LittleLong(frames[i].height);
  70. frames[i].origin_x = LittleLong(frames[i].origin_x);
  71. frames[i].origin_y = LittleLong(frames[i].origin_y);
  72. }
  73. SafeWrite (spriteouthandle, frames, sizeof(frames[0])*sprite.numframes);
  74. fclose (spriteouthandle);
  75. spritename[0] = 0; // clear for a new sprite
  76. sprite.numframes = 0;
  77. }
  78. /*
  79. ===============
  80. Cmd_Load
  81. ===============
  82. */
  83. void Cmd_Load (void)
  84. {
  85. char *name;
  86. GetToken (false);
  87. if (g_release)
  88. return;
  89. name = ExpandPathAndArchive(token);
  90. // load the image
  91. printf ("loading %s\n", name);
  92. Load256Image (name, &byteimage, &lbmpalette,
  93. &byteimagewidth, &byteimageheight);
  94. RemapZero (byteimage, lbmpalette,
  95. byteimagewidth, byteimageheight);
  96. }
  97. /*
  98. ===============
  99. Cmd_SpriteFrame
  100. ===============
  101. */
  102. void Cmd_SpriteFrame (void)
  103. {
  104. int y,xl,yl,xh,yh,w,h;
  105. dsprframe_t *pframe;
  106. int ox, oy;
  107. byte *cropped;
  108. char savename[1024];
  109. GetToken (false);
  110. xl = atoi (token);
  111. GetToken (false);
  112. yl = atoi (token);
  113. GetToken (false);
  114. w = atoi (token);
  115. GetToken (false);
  116. h = atoi (token);
  117. // origin offset is optional
  118. if (TokenAvailable ())
  119. {
  120. GetToken (false);
  121. ox = atoi (token);
  122. GetToken (false);
  123. oy = atoi (token);
  124. }
  125. else
  126. {
  127. ox = w/2;
  128. oy = h/2;
  129. }
  130. if ((xl & 0x07) || (yl & 0x07) || (w & 0x07) || (h & 0x07))
  131. Error ("Sprite dimensions not multiples of 8\n");
  132. if ((w > 256) || (h > 256))
  133. Error ("Sprite has a dimension longer than 256");
  134. xh = xl+w;
  135. yh = yl+h;
  136. if (sprite.numframes >= MAX_SPRFRAMES)
  137. Error ("Too many frames; increase MAX_SPRFRAMES\n");
  138. pframe = &frames[sprite.numframes];
  139. pframe->width = w;
  140. pframe->height = h;
  141. pframe->origin_x = ox;
  142. pframe->origin_y = oy;
  143. sprintf (pframe->name, "%s_%i.pcx", spritename, sprite.numframes);
  144. sprintf (savename, "%s%s_%i.pcx", gamedir, spritename, sprite.numframes);
  145. sprite.numframes++;
  146. if (g_release)
  147. {
  148. ReleaseFile (pframe->name);
  149. return;
  150. }
  151. // crop it to the proper size
  152. cropped = malloc (w*h);
  153. for (y=0 ; y<h ; y++)
  154. {
  155. memcpy (cropped+y*w, byteimage+(y+yl)*byteimagewidth+xl, w);
  156. }
  157. // save off the new image
  158. printf ("saving %s\n", savename);
  159. CreatePath (savename);
  160. WritePCXfile (savename, cropped, w, h, lbmpalette);
  161. free (cropped);
  162. }
  163. /*
  164. ==============
  165. Cmd_SpriteName
  166. ==============
  167. */
  168. void Cmd_SpriteName (void)
  169. {
  170. if (sprite.numframes)
  171. FinishSprite ();
  172. GetToken (false);
  173. strcpy (spritename, token);
  174. memset (&sprite, 0, sizeof(sprite));
  175. memset (&frames, 0, sizeof(frames));
  176. }