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