ICONSET.CPP 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /***************************************************************************
  15. ** C O N F I D E N T I A L --- W E S T W O O D A S S O C I A T E S **
  16. ***************************************************************************
  17. * *
  18. * Project Name : Library *
  19. * *
  20. * File Name : ICONSET.C *
  21. * *
  22. * Programmer : Joe L. Bostic *
  23. * *
  24. * Start Date : June 9, 1991 *
  25. * *
  26. * Last Update : September 15, 1993 [JLB] *
  27. * *
  28. *-------------------------------------------------------------------------*
  29. * Functions: *
  30. * Load_Icon_Set -- Loads an icons set and initializes it. *
  31. * Free_Icon_Set -- Frees allocations made by Load_Icon_Set(). *
  32. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  33. //#include "function.h"
  34. //#define _WIN32
  35. //#define WIN32_LEAN_AND_MEAN
  36. //#include <dos.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. //#include <mem.h>
  40. #include <wwstd.h>
  41. #include <file.h>
  42. #include "tile.h"
  43. #include <iff.h>
  44. // Misc? ST - 1/3/2019 10:40AM
  45. //extern int Misc;
  46. int Misc;
  47. void * Load_Icon_Set(char const *filename, void *iconsetptr, long buffsize);
  48. void Free_Icon_Set(void const *iconset);
  49. long Get_Icon_Set_Size(void const *iconset);
  50. int Get_Icon_Set_Width(void const *iconset);
  51. int Get_Icon_Set_Height(void const *iconset);
  52. void * Get_Icon_Set_Icondata(void const *iconset);
  53. void * Get_Icon_Set_Trans(void const *iconset);
  54. void * Get_Icon_Set_Remapdata(void const *iconset);
  55. void * Get_Icon_Set_Palettedata(void const *iconset);
  56. int Get_Icon_Set_Count(void const *iconset);
  57. void * Get_Icon_Set_Map(void const *iconset);
  58. //#define ICON_PALETTE_BYTES 16
  59. //#define ICON_MAX 256
  60. /***************************************************************************
  61. ** The terrain is rendered by using icons. These are the buffers that hold
  62. ** the icon data, remap tables, and remap index arrays.
  63. */
  64. //PRIVATE char *IconPalette = NULL; // MCGA only.
  65. //PRIVATE char *IconRemap = NULL; // MCGA only.
  66. #define FORM_RPAL MAKE_ID('R','P','A','L')
  67. #define FORM_RTBL MAKE_ID('R','T','B','L')
  68. #define FORM_SSET MAKE_ID('S','S','E','T')
  69. #define FORM_SINF MAKE_ID('S','I','N','F')
  70. #define FORM_ICON MAKE_ID('I','C','O','N')
  71. #define FORM_TRNS MAKE_ID('T','R','N','S')
  72. #define FORM_MAP MAKE_ID('M','A','P',' ')
  73. /***************************************************************************
  74. * LOAD_ICON_SET -- Loads an icons set and initializes it. *
  75. * *
  76. * This routine will load an IFF icon set from disk. It handles all *
  77. * of the necessary allocations. *
  78. * *
  79. * INPUT: filename -- Name of the icon file. *
  80. * *
  81. * buffer -- Pointer to paragraph aligned buffer to hold data. *
  82. * *
  83. * size -- Size of the buffer (in bytes). *
  84. * *
  85. * OUTPUT: none *
  86. * *
  87. * WARNINGS: In EEGA mode the iconset buffer will be free because the *
  88. * icons will have been transferred to card ram. *
  89. * *
  90. * HISTORY: *
  91. * 06/21/1991 JLB : Created. *
  92. * 07/01/1991 JLB : Determines icon size from file. *
  93. * 07/15/1991 JLB : Load and uncompress onto the same buffer. *
  94. * 09/15/1993 JLB : Added EMS support. *
  95. *=========================================================================*/
  96. void * Load_Icon_Set(char const *filename, void *iconsetptr, long buffsize)
  97. {
  98. int fh; // File handle of iconset.
  99. int bytespericon; // The number of bytes per icon.
  100. unsigned long icons=0; // Number of icons loaded.
  101. unsigned long size; // Size of the icon chunk (raw).
  102. unsigned long transsize;
  103. void *transptr=NULL;
  104. unsigned long mapsize; // Icon map chunk size.
  105. void *mapptr=NULL; // Icon map pointer.
  106. void *returnptr=NULL; // Iconset pointer returned by routine.
  107. BOOL allocated=FALSE; // Was the iconset block allocated?
  108. IControl_Type *idata=NULL; // Icon data loaded.
  109. long id; // ID of file openned.
  110. struct {
  111. char Width; // Width of icon in bytes.
  112. char Height; // Height of icon in bytes.
  113. char Format; // Graphic mode.
  114. //lint -esym(754,Format)
  115. char Bitplanes; // Number of bitplanes per icon.
  116. } sinf;
  117. /*
  118. ** Open the icon set for loading. If it is not a legal icon set
  119. ** data file, then abort.
  120. */
  121. fh = Open_Iff_File(filename);
  122. if (fh != WW_ERROR) {
  123. Read_File(fh, &id, sizeof(long));
  124. if (id == FORM_ICON) {
  125. /*
  126. ** Determine the size of the icons and set up the graphic
  127. ** system accordingly. Also get the sizes of the various
  128. ** data blocks that have to be loaded.
  129. */
  130. Read_Iff_Chunk(fh, FORM_SINF, &sinf, sizeof(sinf));
  131. bytespericon = ((((int)sinf.Width)<<3)*(((int)sinf.Height)<<3)*(int)sinf.Bitplanes)>>3;
  132. size = Get_Iff_Chunk_Size(fh,FORM_SSET);
  133. transsize = Get_Iff_Chunk_Size(fh, FORM_TRNS);
  134. mapsize = Get_Iff_Chunk_Size(fh, FORM_MAP);
  135. /*
  136. ** Allocate the icon buffer if one isn't provided. First try EMS and
  137. ** then try conventional RAM.
  138. */
  139. allocated = FALSE;
  140. if (!iconsetptr) {
  141. buffsize = size + transsize + mapsize + sizeof(IControl_Type);
  142. Misc = buffsize;
  143. iconsetptr = Alloc(buffsize, MEM_NORMAL);
  144. allocated = (iconsetptr != NULL);
  145. }
  146. if (iconsetptr && (size+transsize+mapsize+sizeof(IControl_Type)) <= (unsigned long)buffsize) {
  147. idata = (IControl_Type *)iconsetptr;
  148. memset(idata, 0, sizeof(IControl_Type));
  149. /*
  150. ** Initialize the iconset header structure.
  151. */
  152. idata->Width = (short)(((short)sinf.Width)<<3);
  153. idata->Height = (short)(((short)sinf.Height)<<3);
  154. idata->Allocated = (short)allocated;
  155. idata->Icons = (long)iconsetptr + sizeof(IControl_Type);
  156. idata->Map = idata->Icons + size;
  157. idata->TransFlag = sizeof(IControl_Type) + size + mapsize;
  158. idata->Size = buffsize;
  159. {
  160. long val;
  161. val = Read_Iff_Chunk(fh, FORM_SSET, Add_Long_To_Pointer(iconsetptr, sizeof(IControl_Type)), size);
  162. icons = (int)(val/(long)bytespericon);
  163. idata = (IControl_Type *)iconsetptr;
  164. }
  165. if (mapsize) {
  166. icons = mapsize;
  167. }
  168. idata->Count = (short)icons;
  169. /*
  170. ** Limit buffer to only the size needed. This is done AFTER loading of the
  171. ** raw icon data because it might have been compressed and thus need any
  172. ** extra space to perform an overlapped decompression.
  173. */
  174. if ((unsigned long)buffsize > size + transsize + mapsize + sizeof(IControl_Type)) {
  175. buffsize = size + transsize + mapsize + sizeof(IControl_Type);
  176. }
  177. transptr = Add_Long_To_Pointer(iconsetptr, idata->TransFlag);
  178. Read_Iff_Chunk(fh, FORM_TRNS, transptr, transsize);
  179. idata = (IControl_Type *)iconsetptr;
  180. mapptr = (void*)idata->Map;
  181. Read_Iff_Chunk(fh, FORM_MAP, mapptr, mapsize);
  182. /*
  183. ** Let the graphic overlay know of the icon data. This could involve
  184. ** translation and other data manipulations.
  185. */
  186. //Init_Stamps(iconsetptr);
  187. returnptr = iconsetptr;
  188. }
  189. }
  190. Close_Iff_File(fh);
  191. }
  192. return (returnptr); // Return with icon pointer.
  193. }
  194. /***************************************************************************
  195. * FREE_ICON_SET -- Frees allocations made by Load_Icon_Set(). *
  196. * *
  197. * This routine is used to free up any allocations by Load_Icon_Set(). *
  198. * Use this routine when a new icon set is to be loaded. *
  199. * *
  200. * INPUT: none *
  201. * *
  202. * OUTPUT: none *
  203. * *
  204. * WARNINGS: none *
  205. * *
  206. * HISTORY: *
  207. * 06/21/1991 JLB : Created. *
  208. *=========================================================================*/
  209. void Free_Icon_Set(void const *iconset)
  210. {
  211. IControl_Type *icontrol;
  212. icontrol = (IControl_Type *)iconset;
  213. if (icontrol) {
  214. if (icontrol->Allocated) {
  215. Free((void *)iconset);
  216. }
  217. }
  218. }
  219. long Get_Icon_Set_Size(void const *iconset)
  220. {
  221. IControl_Type *icontrol;
  222. long size=0;
  223. icontrol = (IControl_Type *)iconset;
  224. if (icontrol) {
  225. size = icontrol->Size;
  226. }
  227. return(size);
  228. }
  229. int Get_Icon_Set_Width(void const *iconset)
  230. {
  231. IControl_Type *icontrol;
  232. int width=0;
  233. icontrol = (IControl_Type *)iconset;
  234. if (icontrol) {
  235. width = icontrol->Width;
  236. }
  237. return(width);
  238. }
  239. int Get_Icon_Set_Height(void const *iconset)
  240. {
  241. IControl_Type *icontrol;
  242. int height=0;
  243. icontrol = (IControl_Type *)iconset;
  244. if (icontrol) {
  245. height = icontrol->Height;
  246. }
  247. return(height);
  248. }
  249. void * Get_Icon_Set_Icondata(void const *iconset)
  250. {
  251. IControl_Type *icontrol;
  252. icontrol = (IControl_Type *)iconset;
  253. if (icontrol)
  254. return(Add_Long_To_Pointer(iconset, (LONG)icontrol->Icons));
  255. return(NULL);
  256. }
  257. void * Get_Icon_Set_Trans(void const *iconset)
  258. {
  259. IControl_Type *icontrol;
  260. void *ptr=NULL;
  261. icontrol = (IControl_Type *)iconset;
  262. if (icontrol) {
  263. ptr = Add_Long_To_Pointer((void *)iconset, icontrol->TransFlag);
  264. }
  265. return(ptr);
  266. }
  267. int Get_Icon_Set_Count(void const *iconset)
  268. {
  269. IControl_Type *icontrol;
  270. int count;
  271. icontrol = (IControl_Type *)iconset;
  272. if (icontrol) {
  273. count = icontrol->Count;
  274. }
  275. return(count);
  276. }
  277. void * Get_Icon_Set_Map(void const *iconset)
  278. {
  279. IControl_Type *icontrol;
  280. icontrol = (IControl_Type *)iconset;
  281. if (icontrol)
  282. return(Add_Long_To_Pointer(iconset, (LONG)icontrol->Map));
  283. return(NULL);
  284. }