spry.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // 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. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // spry.cpp
  19. //
  20. // This module impliments the RSpry class, which is a simple array of RSprites.
  21. //
  22. // History:
  23. // 12/25/96 MJR Started.
  24. //
  25. // 05/13/97 JMI Casted instances of warning C4018 (signed/unsigned mismatch)
  26. // to make MSVC 4.1(Alpha) happy (these seem to fall under
  27. // Warning Level 3 in 4.1(Alpha) but not in 4.2(Intel)).
  28. //
  29. // 06/29/97 MJR Replaced STL vector with an RSP list. STL is an evil
  30. // entity that should be banished from the face of the earth.
  31. // Whoever suggested we use it should be shot. (Good thing
  32. // I'm the president -- it's against the rules to shoot me.)
  33. //
  34. ////////////////////////////////////////////////////////////////////////////////
  35. #include "RSPiX.h"
  36. #include "spry.h"
  37. ////////////////////////////////////////////////////////////////////////////////
  38. // Macros/types/etc.
  39. ////////////////////////////////////////////////////////////////////////////////
  40. ////////////////////////////////////////////////////////////////////////////////
  41. // Variables/data
  42. ////////////////////////////////////////////////////////////////////////////////
  43. ////////////////////////////////////////////////////////////////////////////////
  44. // Function prototypes
  45. ////////////////////////////////////////////////////////////////////////////////
  46. ////////////////////////////////////////////////////////////////////////////////
  47. // Default (and only) constructor
  48. ////////////////////////////////////////////////////////////////////////////////
  49. RSpry::RSpry()
  50. {
  51. // On construction, the member vector is automatically initialized by its
  52. // own constructor.
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////
  55. // Destructor
  56. ////////////////////////////////////////////////////////////////////////////////
  57. RSpry::~RSpry()
  58. {
  59. Clear();
  60. }
  61. ////////////////////////////////////////////////////////////////////////////////
  62. // Clear
  63. ////////////////////////////////////////////////////////////////////////////////
  64. short RSpry::Clear(void)
  65. {
  66. short sResult = 0;
  67. // Destroy any existing sprites and get rid of all list nodes
  68. while (m_listSprites.GetHead())
  69. {
  70. delete m_listSprites.GetHeadData();
  71. m_listSprites.RemoveHead();
  72. }
  73. return sResult;
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // Load from specified file
  77. ////////////////////////////////////////////////////////////////////////////////
  78. short RSpry::Load(
  79. char* pszFile)
  80. {
  81. short sResult = 0;
  82. // Clear any existing sprites
  83. Clear();
  84. // Open file
  85. RFile file;
  86. sResult = file.Open(pszFile, "rb", RFile::LittleEndian);
  87. if (sResult == 0)
  88. {
  89. // Load everything from file
  90. sResult = Load(&file);
  91. file.Close();
  92. }
  93. else
  94. {
  95. sResult = -1;
  96. TRACE("RSpry::Load(): Couldn't open file: %s !\n", pszFile);
  97. }
  98. return sResult;
  99. }
  100. ////////////////////////////////////////////////////////////////////////////////
  101. // Load from already-open file
  102. ////////////////////////////////////////////////////////////////////////////////
  103. short RSpry::Load(
  104. RFile* pFile)
  105. {
  106. short sResult = 0;
  107. // Clear any existing sprites
  108. Clear();
  109. // Read & validate file ID
  110. ULONG ulFileID;
  111. if (pFile->Read(&ulFileID) == 1)
  112. {
  113. if (ulFileID == RSpry::FileID)
  114. {
  115. // Read & validate file version
  116. ULONG ulFileVersion;
  117. if (pFile->Read(&ulFileVersion) == 1)
  118. {
  119. if (ulFileVersion == RSpry::FileVersion)
  120. {
  121. // Read count of sprites
  122. short sCount;
  123. pFile->Read(&sCount);
  124. // Construct and load indicated number of RSprite's and add them to list
  125. for (short i = 0; (i < sCount) && !sResult; i++)
  126. {
  127. RSprite* pSprite = new RSprite;
  128. if (pSprite != 0)
  129. {
  130. sResult = pSprite->Load(pFile);
  131. if (sResult == 0)
  132. m_listSprites.InsertTail(pSprite);
  133. }
  134. else
  135. {
  136. sResult = -1;
  137. TRACE("RSpry::Load(): Error creating new RSprite!\n");
  138. }
  139. }
  140. }
  141. else
  142. {
  143. sResult = -1;
  144. TRACE("RSpry::Load(): Incorrect file version (should be 0x%lx, was 0x%lx)!\n", RSpry::FileVersion, ulFileVersion);
  145. }
  146. }
  147. else
  148. {
  149. sResult = -1;
  150. TRACE("RSpry::Load(): Error reading file version!\n");
  151. }
  152. }
  153. else
  154. {
  155. sResult = -1;
  156. TRACE("RSpry::Load(): Incorrect file ID (should be 0x%lx, was 0x%lx)!\n", RSpry::FileID, ulFileID);
  157. }
  158. }
  159. else
  160. {
  161. sResult = -1;
  162. TRACE("RSpry::Load(): Error reading file ID!\n");
  163. }
  164. return sResult;
  165. }
  166. ////////////////////////////////////////////////////////////////////////////////
  167. // Save to specified file
  168. ////////////////////////////////////////////////////////////////////////////////
  169. short RSpry::Save(
  170. char* pszFile)
  171. {
  172. short sResult = 0;
  173. // Open file
  174. RFile file;
  175. sResult = file.Open(pszFile, "wb", RFile::LittleEndian);
  176. if (sResult == 0)
  177. {
  178. // Save everything to file
  179. sResult = Save(&file);
  180. file.Close();
  181. }
  182. else
  183. {
  184. sResult = -1;
  185. TRACE("RSpry::Save(): Couldn't open file: %s !\n", pszFile);
  186. }
  187. return sResult;
  188. }
  189. ////////////////////////////////////////////////////////////////////////////////
  190. // Save to already-open file
  191. ////////////////////////////////////////////////////////////////////////////////
  192. short RSpry::Save(
  193. RFile* pFile)
  194. {
  195. short sResult = 0;
  196. // Write out file ID and version
  197. pFile->Write((unsigned long)RSpry::FileID);
  198. pFile->Write((unsigned long)RSpry::FileVersion);
  199. // Write out number of sprites
  200. pFile->Write((short)m_listSprites.GetCount());
  201. // Write out each sprite
  202. ListOfSprites::Pointer p = m_listSprites.GetHead();
  203. while (p && !sResult)
  204. {
  205. sResult = m_listSprites.GetData(p)->Save(pFile);
  206. p = m_listSprites.GetNext(p);
  207. }
  208. // If no errors were reported, double-check for I/O errors
  209. if (!sResult && pFile->Error())
  210. {
  211. sResult = -1;
  212. TRACE("RSpry::Save(): Error writing file!\n");
  213. }
  214. return sResult;
  215. }
  216. ////////////////////////////////////////////////////////////////////////////////
  217. // Convert to specified RImage type
  218. ////////////////////////////////////////////////////////////////////////////////
  219. short RSpry::Convert(
  220. RImage::Type type)
  221. {
  222. short sResult = 0;
  223. // Convert all sprites to specified type
  224. ListOfSprites::Pointer p = m_listSprites.GetHead();
  225. while (p && !sResult)
  226. {
  227. if (m_listSprites.GetData(p)->m_pImage->m_type != type)
  228. {
  229. if (m_listSprites.GetData(p)->m_pImage->Convert(type) != type)
  230. {
  231. sResult = -1;
  232. TRACE("RSpry::Convert(): Couldn't convert sprite!\n");
  233. }
  234. }
  235. p = m_listSprites.GetNext(p);
  236. }
  237. return sResult;
  238. }
  239. ////////////////////////////////////////////////////////////////////////////////
  240. // EOF
  241. ////////////////////////////////////////////////////////////////////////////////