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