ICONSET.CPP 12 KB

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