| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 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 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/gametexture.cpp $ |
| 21 | * $Revision: 57 $ |
| 22 | * $Date: 4/19/00 5:07p $ |
| 23 | * $Author: Matt $ |
| 24 | * |
| 25 | * <GameTexture--Texture management at the game level> |
| 26 | * |
| 27 | * $Log: /DescentIII/Main/gametexture.cpp $ |
| 28 | * |
| 29 | * 57 4/19/00 5:07p Matt |
| 30 | * From Duane for 1.4 |
| 31 | * Added checks, asserts, and fixes for bad return values |
| 32 | * |
| 33 | * 56 10/21/99 6:34p Matt |
| 34 | * Mac merge |
| 35 | * |
| 36 | * 55 5/23/99 7:43p Jason |
| 37 | * fixed briefing problem with lowmem textures |
| 38 | * |
| 39 | * 54 5/07/99 7:22p Jason |
| 40 | * changes for cards with low videomemory |
| 41 | * |
| 42 | * 53 4/30/99 5:06p Kevin |
| 43 | * misc dedicated server, networking and low memory enhancements |
| 44 | * |
| 45 | * 52 4/22/99 3:23p Jason |
| 46 | * |
| 47 | * 51 4/17/99 6:15p Samir |
| 48 | * replaced gr.h with grdefs.h and fixed resulting compile bugs. |
| 49 | * |
| 50 | * 50 4/13/99 12:03p Jason |
| 51 | * more memory savings for dedicated server |
| 52 | * |
| 53 | * 49 4/12/99 3:04p Jason |
| 54 | * changes for low memory |
| 55 | * |
| 56 | * 48 4/09/99 7:04p Jason |
| 57 | * changed some texture defines |
| 58 | * |
| 59 | * 47 4/01/99 1:21p Jason |
| 60 | * don't resize huge textures |
| 61 | * |
| 62 | * 46 3/30/99 6:01p Jason |
| 63 | * added new procedural effect |
| 64 | * |
| 65 | * 45 2/12/99 3:37p Jason |
| 66 | * added client-side interpolation...its not fully debugged though. |
| 67 | * |
| 68 | * 44 2/10/99 3:38p Jason |
| 69 | * table file filter fixups |
| 70 | * |
| 71 | * 43 1/29/99 6:29p Jason |
| 72 | * first pass at adding bumpmaps |
| 73 | * |
| 74 | * 42 1/22/99 3:59p Jason |
| 75 | * added 256x256 textures to help with terrain skies |
| 76 | * |
| 77 | * 41 1/21/99 11:15p Jeff |
| 78 | * pulled out some structs and defines from header files and moved them |
| 79 | * into seperate header files so that multiplayer dlls don't require major |
| 80 | * game headers, just those new headers. Side effect is a shorter build |
| 81 | * time. Also cleaned up some header file #includes that weren't needed. |
| 82 | * This affected polymodel.h, object.h, player.h, vecmat.h, room.h, |
| 83 | * manage.h and multi.h |
| 84 | * |
| 85 | * 40 1/18/99 11:11a Jason |
| 86 | * made vclips be specular |
| 87 | * |
| 88 | * 39 11/30/98 5:50p Jason |
| 89 | * added 4444 bitmap support |
| 90 | * |
| 91 | * 38 10/21/98 9:47a Kevin |
| 92 | * |
| 93 | * 37 10/20/98 6:22p Jason |
| 94 | * added superlowmem mode |
| 95 | * |
| 96 | * 36 10/14/98 5:50p Jason |
| 97 | * added lowmem modes for quartering textures |
| 98 | * |
| 99 | * 35 10/13/98 1:26p Jason |
| 100 | * made new detail level settings work |
| 101 | * |
| 102 | * 34 10/08/98 2:27p Jason |
| 103 | * sped up table file loading |
| 104 | * |
| 105 | * 33 10/07/98 5:03p Jason |
| 106 | * more options for textures |
| 107 | * |
| 108 | * 32 10/01/98 2:47p Jason |
| 109 | * added timed procedurals |
| 110 | * |
| 111 | * 31 8/13/98 11:57a Jason |
| 112 | * added water procedurals |
| 113 | * |
| 114 | * 30 7/31/98 10:50a Jason |
| 115 | * fixed memory leak |
| 116 | * |
| 117 | * 29 7/16/98 12:41p Jason |
| 118 | * fixed dumb bug |
| 119 | * |
| 120 | * 28 7/14/98 6:50p Jason |
| 121 | * made procedurals save out to the table file |
| 122 | * |
| 123 | * 27 7/09/98 7:30p Jason |
| 124 | * more procedural stuff |
| 125 | * |
| 126 | * 26 7/07/98 4:12p Jason |
| 127 | * checkin for procedurals |
| 128 | * |
| 129 | * 25 7/06/98 12:55p Jason |
| 130 | * added first pass at procedurals |
| 131 | * |
| 132 | * 24 6/10/98 12:21p Matt |
| 133 | * Revamped system that controls what textures are displayed on the |
| 134 | * texture tab. Should work correctly now, and will now save the settings |
| 135 | * to the registry. Also, textures now default to no type set, not all |
| 136 | * types set. |
| 137 | * |
| 138 | * 23 5/20/98 5:44p Jason |
| 139 | * incremental checkin for bumpmapping |
| 140 | * |
| 141 | * 22 5/20/98 1:08p Jason |
| 142 | * more stuff for adding bumpmaps |
| 143 | * |
| 144 | * 21 5/14/98 2:38p Jason |
| 145 | * made temp solution for vclip residency problem |
| 146 | * |
| 147 | * 20 5/14/98 2:17p Jason |
| 148 | * more changes to be memory friendly |
| 149 | * |
| 150 | * 19 3/17/98 4:52p Jason |
| 151 | * fixed compiler warning |
| 152 | * |
| 153 | * 17 3/17/98 4:33p Jason |
| 154 | * Added size changing to bitmaps/textures |
| 155 | * |
| 156 | * 16 2/13/98 12:40p Jason |
| 157 | * added functions to touch all data in a level when the level is loaded |
| 158 | * |
| 159 | * 15 2/11/98 4:58p Jason |
| 160 | * fixed some memory problems with resizing and mipping |
| 161 | * |
| 162 | * 14 2/10/98 11:38a Matt |
| 163 | * Ripped out the obsolete merge stuff |
| 164 | * |
| 165 | * 13 12/19/97 6:07p Jason |
| 166 | * sped up bitmap loading |
| 167 | * |
| 168 | * 12 12/19/97 2:46p Jason |
| 169 | * implemented bitmap paging routines |
| 170 | * |
| 171 | * 11 10/30/97 12:02p Jason |
| 172 | * fixed an error checking bug |
| 173 | * |
| 174 | * 10 10/21/97 12:06p Jason |
| 175 | * added mprintf warning to resizing of textures |
| 176 | * |
| 177 | * 9 10/20/97 6:20p Mark |
| 178 | * FROM JASON: Fixed dumb no-name bug |
| 179 | * |
| 180 | * 8 10/15/97 5:20p Jason |
| 181 | * did a HUGE overhaul of the bitmap system |
| 182 | * |
| 183 | * 7 10/13/97 3:56p Jason |
| 184 | * made a better 3d bitmap system |
| 185 | * |
| 186 | * 6 9/12/97 4:00p Matt |
| 187 | * If a game texture is not used, return the handle for the error bitmap |
| 188 | * |
| 189 | * 5 8/19/97 1:20p Jason |
| 190 | * set reflectivity to .5 by default |
| 191 | * |
| 192 | * 4 8/08/97 12:58p Jason |
| 193 | * got texture sliding and pingponging working |
| 194 | * |
| 195 | * 3 8/04/97 3:28p Jason |
| 196 | * added alpha blending per texture |
| 197 | * |
| 198 | * 21 6/17/97 2:53p Matt |
| 199 | * Allow 128x128 tmap2s over 64x64 tmap1s by scaling up the bottom |
| 200 | * texture. This is only temporary, because I think we'll be totally |
| 201 | * revising the tmap2 system. |
| 202 | * |
| 203 | * 20 6/06/97 3:57p Jason |
| 204 | * implemented changes for small textures and automatic tmap2 recognition |
| 205 | * |
| 206 | * 19 6/03/97 3:28p Jason |
| 207 | * fixed texturing problems |
| 208 | * |
| 209 | * 18 5/27/97 4:39p Jason |
| 210 | * changes to support OpenGL |
| 211 | * |
| 212 | * 17 5/23/97 7:07p Matt |
| 213 | * Added code, data, & dialogs to keep track of what a texture is used for |
| 214 | * (mine, terrain, object) so we can display in the texture dialog only |
| 215 | * the ones we're interested in. |
| 216 | * |
| 217 | * 16 5/19/97 5:10p Jason |
| 218 | * changes for our new abstracted renderer code |
| 219 | * |
| 220 | * 15 5/13/97 2:24p Jason |
| 221 | * fixed a couple of animating texture bugs |
| 222 | * |
| 223 | * 14 5/13/97 12:45p Matt |
| 224 | * Fixed bug in rotated cases of 16-bit texmerge |
| 225 | * |
| 226 | * 13 5/12/97 11:41a Jason |
| 227 | * made game work (default) to 16bit no mip maps mode |
| 228 | * Saves us alot of memory |
| 229 | * |
| 230 | * 12 5/12/97 11:19a Matt |
| 231 | * Worked on 16-bit merge code, which is still broken |
| 232 | * |
| 233 | * 11 5/02/97 7:28p Matt |
| 234 | * Got tmap2 system working, though I think the merge for 16-bit textures |
| 235 | * probably doesn't work |
| 236 | * |
| 237 | * 10 4/28/97 6:36p Jason |
| 238 | * fixed a bug with vclips |
| 239 | * |
| 240 | * 9 4/25/97 3:59p Jason |
| 241 | * fixed a bug that my last checkin caused |
| 242 | * |
| 243 | * 8 4/25/97 3:31p Jason |
| 244 | * implemented better memory management for vclips and bitmaps |
| 245 | * |
| 246 | * 7 4/21/97 5:29p Jason |
| 247 | * got animating textures to work on polygonal objects |
| 248 | * |
| 249 | * 6 2/19/97 3:23p Jason |
| 250 | * added hooks to import animated iffs |
| 251 | * |
| 252 | * 5 2/19/97 11:37a Jason |
| 253 | * take gametime into consideration when animating a vclip |
| 254 | * |
| 255 | * 4 2/11/97 3:21p Jason |
| 256 | * added MakeTexture8Bit function |
| 257 | * |
| 258 | * 3 1/25/97 5:55p Jason |
| 259 | * changes to support new 8bit palettized textures and vclips |
| 260 | * |
| 261 | * 2 1/16/97 3:01p Jason |
| 262 | * moved all Gametexture management stuff to the game level instead of in |
| 263 | * the texmap libarry. |
| 264 | * |
| 265 | * $NoKeywords: $ |
| 266 | */ |
| 267 | |
| 268 | #include "grdefs.h" |
| 269 | #include "pstypes.h" |
| 270 | #include "gametexture.h" |
| 271 | #include "mono.h" |
| 272 | #include "bitmap.h" |
| 273 | #include "pserror.h" |
| 274 | #include "vclip.h" |
| 275 | #include "game.h" |
| 276 | #include "bumpmap.h" |
| 277 | #include <stdlib.h> |
| 278 | #include <string.h> |
| 279 | #include "procedurals.h" |
| 280 | #include "ddio.h" |
| 281 | #include "config.h" |
| 282 | #include "args.h" |
| 283 | #include "mem.h" |
| 284 | |
| 285 | // TODO: MTS: this is only used in this file. |
| 286 | int Num_textures = 0; |
| 287 | texture GameTextures[MAX_TEXTURES]; |
| 288 | |
| 289 | extern bool Mem_superlow_memory_mode; |
| 290 | |
| 291 | static int Total_memory_saved = 0; |
| 292 | |
| 293 | void FreeAllTextures() { |
| 294 | for (int i = 0; i < MAX_TEXTURES; i++) { |
| 295 | if (GameTextures[i].used) |
| 296 | FreeTexture(i); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | int InitTextures() { |
| 301 | // Initalizes the texture system |
| 302 | |
| 303 | int i, tex; |
| 304 | |
| 305 | mprintf(0, "Initializing texture system.\n"); |
| 306 | for (i = 0; i < MAX_TEXTURES; i++) |
| 307 | GameTextures[i].used = 0; |
| 308 | |
| 309 | tex = AllocTexture(); |
| 310 | GameTextures[tex].bm_handle = BAD_BITMAP_HANDLE; |
| 311 | strcpy(GameTextures[tex].name, "SAMPLE TEXTURE"); |
| 312 | |
| 313 | // Initialize procedural tables and such |
| 314 | InitProcedurals(); |
| 315 | |
| 316 | atexit(FreeAllTextures); |
| 317 | |
| 318 | return 1; |
| 319 | } |
| 320 | |
| 321 | // Set aside a texture for use |
| 322 | // This function returns a valid texture handle |
| 323 | int AllocTexture(void) { |
| 324 | int i; |
| 325 | |
| 326 | for (i = 0; i < MAX_TEXTURES; i++) |
| 327 | if (GameTextures[i].used == 0) |
| 328 | break; |
| 329 | |
| 330 | ASSERT(i != MAX_TEXTURES); // we've run out of texture slots |
| 331 | |
| 332 | Num_textures++; |
| 333 | |
| 334 | memset(&GameTextures[i], 0, sizeof(texture)); |
| 335 | |
| 336 | GameTextures[i].used = 1; |
| 337 | |
| 338 | GameTextures[i].corona_type = 0; |
| 339 | GameTextures[i].bumpmap = -1; |
| 340 | GameTextures[i].procedural = NULL; |
| 341 | GameTextures[i].name[0] = 0; |
| 342 | GameTextures[i].flags = 0; // default to no type |
| 343 | GameTextures[i].alpha = 1.0; |
| 344 | GameTextures[i].speed = 1.0; |
| 345 | GameTextures[i].reflectivity = .6f; |
| 346 | GameTextures[i].bm_handle = -1; |
| 347 | GameTextures[i].destroy_handle = -1; |
| 348 | |
| 349 | return i; |
| 350 | } |
| 351 | |
| 352 | // Searches thru all textures for a specific name, returns -1 if not found |
| 353 | // or index of texture with name |
| 354 | int FindTextureName(const char *name) { |
| 355 | int i, num_counted = 0; |
| 356 | ; |
| 357 | |
| 358 | ASSERT(name != NULL); |
| 359 | |
| 360 | for (i = 0; i < MAX_TEXTURES && num_counted < Num_textures; i++) { |
| 361 | if (GameTextures[i].used) { |
| 362 | num_counted++; |
| 363 | if (!stricmp(name, GameTextures[i].name)) |
| 364 | return i; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | // Searches thru all textures for a bitmap of a specific name, returns -1 if not found |
| 372 | // or index of texture with name |
| 373 | int FindTextureBitmapName(const char *name) { |
| 374 | int i; |
| 375 | |
| 376 | ASSERT(name != NULL); |
| 377 | |
| 378 | for (i = 0; i < MAX_TEXTURES; i++) { |
| 379 | if (GameTextures[i].used) { |
| 380 | if (GameTextures[i].flags & TF_ANIMATED) { |
| 381 | int not_res = 0; |
| 382 | if (GameVClips[GameTextures[i].bm_handle].flags & VCF_NOT_RESIDENT) |
| 383 | not_res = 1; |
| 384 | |
| 385 | PageInVClip(GameTextures[i].bm_handle); |
| 386 | vclip *vc = &GameVClips[GameTextures[i].bm_handle]; |
| 387 | ASSERT(vc->used); |
| 388 | |
| 389 | int t; |
| 390 | int retval = -1; |
| 391 | |
| 392 | for (t = 0; t < vc->num_frames && retval == -1; t++) |
| 393 | if ((!stricmp(GameBitmaps[vc->frames[t]].name, name))) |
| 394 | retval = i; |
| 395 | |
| 396 | if (retval != -1) |
| 397 | return retval; |
| 398 | } else { |
| 399 | if ((!stricmp(GameBitmaps[GameTextures[i].bm_handle].name, name))) |
| 400 | return i; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | return -1; |
| 406 | } |
| 407 | |
| 408 | // Allocates memory for a procedural that is attached to a texture |
| 409 | int AllocateProceduralForTexture(int handle) { |
| 410 | int w, h; |
| 411 | |
| 412 | if (GameTextures[handle].flags & TF_TEXTURE_64) { |
| 413 | w = h = 64; |
| 414 | } else if (GameTextures[handle].flags & TF_TEXTURE_32) { |
| 415 | w = h = 32; |
| 416 | } else { |
| 417 | w = h = 128; |
| 418 | } |
| 419 | |
| 420 | GameTextures[handle].procedural = (proc_struct *)mem_malloc(sizeof(proc_struct)); |
| 421 | ASSERT(GameTextures[handle].procedural); |
| 422 | |
| 423 | GameTextures[handle].procedural->proc1 = NULL; |
| 424 | GameTextures[handle].procedural->proc2 = NULL; |
| 425 | |
| 426 | GameTextures[handle].procedural->last_evaluation_time = 0; |
| 427 | GameTextures[handle].procedural->osc_time = 0; |
| 428 | GameTextures[handle].procedural->osc_value = 8; |
| 429 | |
| 430 | GameTextures[handle].procedural->procedural_bitmap = bm_AllocBitmap(w, h, 0); |
| 431 | ASSERT(GameTextures[handle].procedural->procedural_bitmap >= 0); |
| 432 | |
| 433 | GameTextures[handle].procedural->memory_type = PROC_MEMORY_TYPE_NONE; |
| 434 | |
| 435 | GameTextures[handle].procedural->palette = (uint16_t *)mem_malloc(256 * 2); |
| 436 | |
| 437 | GameTextures[handle].procedural->last_procedural_frame = -1; |
| 438 | GameTextures[handle].procedural->heat = 128; |
| 439 | |
| 440 | GameTextures[handle].procedural->dynamic_proc_elements = -1; |
| 441 | GameTextures[handle].procedural->static_proc_elements = NULL; |
| 442 | |
| 443 | GameTextures[handle].procedural->num_static_elements = 0; |
| 444 | |
| 445 | GameTextures[handle].procedural->light = 1; |
| 446 | GameTextures[handle].procedural->thickness = 4; |
| 447 | |
| 448 | memcpy(GameTextures[handle].procedural->palette, DefaultProcPalette, 256 * 2); |
| 449 | |
| 450 | return 1; |
| 451 | } |
| 452 | |
| 453 | // Allocates the memory needed for static elements for a procedural texture |
| 454 | void AllocateStaticProceduralsForTexture(int handle, int num_elements) { |
| 455 | GameTextures[handle].procedural->static_proc_elements = |
| 456 | (static_proc_element *)mem_malloc(sizeof(static_proc_element) * num_elements); |
| 457 | ASSERT(GameTextures[handle].procedural->static_proc_elements); |
| 458 | } |
| 459 | |
| 460 | // Frees the memory used by static procedural elements |
| 461 | void FreeStaticProceduralsForTexture(int handle) { |
| 462 | if (GameTextures[handle].procedural->static_proc_elements) |
| 463 | mem_free(GameTextures[handle].procedural->static_proc_elements); |
| 464 | GameTextures[handle].procedural->static_proc_elements = NULL; |
| 465 | } |
| 466 | |
| 467 | // Given a texture handle, returns that textures bitmap |
| 468 | // If the texture is animated, returns framenum mod num_of_frames in the animation |
| 469 | // Force is to force the evaluation of a procedural |
| 470 | // Also figures in gametime |
| 471 | int GetTextureBitmap(int handle, int framenum, bool force) { |
| 472 | int src_bitmap; |
| 473 | |
| 474 | if (!GameTextures[handle].used) |
| 475 | return 0; |
| 476 | |
| 477 | if (GameTextures[handle].flags & TF_ANIMATED) { |
| 478 | float cur_frametime; |
| 479 | int int_frame; |
| 480 | texture *tex = &GameTextures[handle]; |
| 481 | PageInVClip(GameTextures[handle].bm_handle); |
| 482 | |
| 483 | vclip *vc = &GameVClips[GameTextures[handle].bm_handle]; |
| 484 | ASSERT(vc->used >= 1); |
| 485 | |
| 486 | if (GameTextures[handle].flags & TF_PING_PONG) { |
| 487 | // Ping pong this texture |
| 488 | |
| 489 | float frametime = tex->speed / vc->num_frames; |
| 490 | cur_frametime = Gametime / frametime; |
| 491 | int_frame = cur_frametime; |
| 492 | int_frame += framenum; |
| 493 | |
| 494 | int_frame %= (vc->num_frames * 2); |
| 495 | if (int_frame >= vc->num_frames) |
| 496 | int_frame = (vc->num_frames - 1) - (int_frame % vc->num_frames); |
| 497 | else |
| 498 | int_frame %= vc->num_frames; |
| 499 | src_bitmap = vc->frames[int_frame]; |
| 500 | } else { |
| 501 | float frametime = tex->speed / vc->num_frames; |
| 502 | cur_frametime = Gametime / frametime; |
| 503 | int_frame = cur_frametime; |
| 504 | int_frame += framenum; |
| 505 | src_bitmap = vc->frames[int_frame % vc->num_frames]; |
| 506 | } |
| 507 | } else { |
| 508 | src_bitmap = GameTextures[handle].bm_handle; |
| 509 | } |
| 510 | |
| 511 | if (GameTextures[handle].flags & TF_PROCEDURAL) // Do a procedural |
| 512 | { |
| 513 | int do_eval = 1; |
| 514 | |
| 515 | if (GameTextures[handle].procedural == NULL) |
| 516 | AllocateProceduralForTexture(handle); |
| 517 | |
| 518 | if (GameTextures[handle].procedural->last_procedural_frame == FrameCount) |
| 519 | do_eval = 0; |
| 520 | if (timer_GetTime() < |
| 521 | GameTextures[handle].procedural->last_evaluation_time + GameTextures[handle].procedural->evaluation_time) |
| 522 | do_eval = 0; |
| 523 | |
| 524 | if (!force && !Detail_settings.Procedurals_enabled) { |
| 525 | if (timer_GetTime() < GameTextures[handle].procedural->last_evaluation_time + 10.0) |
| 526 | do_eval = 0; |
| 527 | } |
| 528 | |
| 529 | if (do_eval) { |
| 530 | EvaluateProcedural(handle); |
| 531 | GameTextures[handle].procedural->last_procedural_frame = FrameCount; |
| 532 | GameTextures[handle].procedural->last_evaluation_time = timer_GetTime(); |
| 533 | src_bitmap = GameTextures[handle].procedural->procedural_bitmap; |
| 534 | GameBitmaps[src_bitmap].flags |= BF_CHANGED; |
| 535 | } else |
| 536 | src_bitmap = GameTextures[handle].procedural->procedural_bitmap; |
| 537 | } |
| 538 | |
| 539 | return src_bitmap; |
| 540 | } |
| 541 | |
| 542 | // Given a filename, loads either the bitmap or vclip found in that file. If type |
| 543 | // is not NULL, sets it to 1 if file is animation, otherwise sets it to zero |
| 544 | int LoadTextureImage(const char *filename, int *type, int texture_size, int mipped, int pageable, int format) { |
| 545 | int anim = 0, bm_handle; |
| 546 | char extension[10]; |
| 547 | int w, h; |
| 548 | |
| 549 | int len = strlen(filename); |
| 550 | |
| 551 | if (len < 4) |
| 552 | return -1; |
| 553 | |
| 554 | strncpy(extension, &filename[len - 3], 5); |
| 555 | |
| 556 | if ((!strnicmp("oaf", extension, 3)) || (!strnicmp("ifl", extension, 3)) || (!strnicmp("abm", extension, 3))) |
| 557 | anim = 1; |
| 558 | |
| 559 | if (type != NULL) |
| 560 | *type = anim; |
| 561 | |
| 562 | if (anim) |
| 563 | bm_handle = AllocLoadVClip(IGNORE_TABLE(filename), texture_size, mipped, pageable, format); |
| 564 | else { |
| 565 | if (pageable) |
| 566 | bm_handle = bm_AllocLoadFileNoMemBitmap(IGNORE_TABLE(filename), mipped, format); |
| 567 | else |
| 568 | bm_handle = bm_AllocLoadFileBitmap(IGNORE_TABLE(filename), mipped, format); |
| 569 | |
| 570 | if (bm_handle < 1) |
| 571 | return -1; |
| 572 | |
| 573 | if (texture_size == NORMAL_TEXTURE) { |
| 574 | w = TEXTURE_WIDTH; |
| 575 | h = TEXTURE_HEIGHT; |
| 576 | } else if (texture_size == SMALL_TEXTURE) { |
| 577 | // Make small textures a quarter of the size of normal textures |
| 578 | w = TEXTURE_WIDTH / 2; |
| 579 | h = TEXTURE_HEIGHT / 2; |
| 580 | } else if (texture_size == TINY_TEXTURE) { |
| 581 | // Make these tinys an eigth of the size of normal textures |
| 582 | w = TEXTURE_WIDTH / 4; |
| 583 | h = TEXTURE_HEIGHT / 4; |
| 584 | } else if (texture_size == HUGE_TEXTURE) { |
| 585 | // Make these tinys an eigth of the size of normal textures |
| 586 | w = TEXTURE_WIDTH * 2; |
| 587 | h = TEXTURE_HEIGHT * 2; |
| 588 | } else |
| 589 | return bm_handle; |
| 590 | |
| 591 | // If differing size, resize! |
| 592 | if (!pageable && (w != bm_w(bm_handle, 0) || h != bm_h(bm_handle, 0))) { |
| 593 | int dest_bm; |
| 594 | |
| 595 | mprintf(0, "WARNING: Resizing bitmap %s from %d x %d to %d x %d!\n", GameBitmaps[bm_handle].name, |
| 596 | bm_w(bm_handle, 0), bm_h(bm_handle, 0), w, h); |
| 597 | |
| 598 | dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h * 2) / 3)); |
| 599 | ASSERT(dest_bm >= 0); |
| 600 | |
| 601 | if (mipped) |
| 602 | GameBitmaps[dest_bm].flags |= BF_MIPMAPPED; |
| 603 | GameBitmaps[dest_bm].format = format; |
| 604 | |
| 605 | bm_ScaleBitmapToBitmap(dest_bm, bm_handle); |
| 606 | strcpy(GameBitmaps[dest_bm].name, GameBitmaps[bm_handle].name); |
| 607 | bm_FreeBitmap(bm_handle); |
| 608 | |
| 609 | bm_handle = dest_bm; |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | return bm_handle; |
| 614 | } |
| 615 | |
| 616 | // Goes through and marks a texture as a tmap2 if its bitmap(s) have transparency |
| 617 | bool CheckIfTextureIsTmap2(int texnum) { |
| 618 | if (GameTextures[texnum].flags & TF_ANIMATED) { |
| 619 | vclip *vc = &GameVClips[GameTextures[texnum].bm_handle]; |
| 620 | PageInVClip(GameTextures[texnum].bm_handle); |
| 621 | |
| 622 | ASSERT(vc->used); |
| 623 | for (int i = 0; i < vc->num_frames; i++) { |
| 624 | if (bm_SetBitmapIfTransparent(vc->frames[i])) { |
| 625 | GameTextures[texnum].flags |= TF_TMAP2; |
| 626 | return 1; |
| 627 | } |
| 628 | } |
| 629 | } else { |
| 630 | if (bm_SetBitmapIfTransparent(GameTextures[texnum].bm_handle)) { |
| 631 | GameTextures[texnum].flags |= TF_TMAP2; |
| 632 | return 1; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | return 0; |
| 637 | } |
| 638 | |
| 639 | extern int Low_vidmem; |
| 640 | // Pages a texture into memory, optionally resizing it if in low mem mode |
| 641 | // Doesn't resize if resize is false |
| 642 | void PageInTexture(int n, bool resize) { |
| 643 | static int super_low_mem = -1; |
| 644 | |
| 645 | if (super_low_mem == -1) { |
| 646 | if (Mem_superlow_memory_mode || FindArg("-dedicated")) |
| 647 | super_low_mem = 1; |
| 648 | else |
| 649 | super_low_mem = 0; |
| 650 | } |
| 651 | if (GameTextures[n].used == 0) |
| 652 | return; |
| 653 | |
| 654 | texture *texp = &GameTextures[n]; |
| 655 | |
| 656 | bm_data(texp->bm_handle, 0); |
| 657 | |
| 658 | #ifndef EDITOR |
| 659 | if (resize == true && (Mem_low_memory_mode || Low_vidmem)) |
| 660 | { |
| 661 | if (!(texp->flags & (TF_TEXTURE_32 | TF_TEXTURE_256)) && !(texp->flags & TF_PROCEDURAL)) { |
| 662 | int bm_handle = texp->bm_handle; |
| 663 | int w = TEXTURE_WIDTH, h = TEXTURE_HEIGHT; |
| 664 | |
| 665 | if (texp->flags & TF_TEXTURE_64) { |
| 666 | w /= 2; |
| 667 | h /= 2; |
| 668 | } |
| 669 | |
| 670 | w /= 2; |
| 671 | h /= 2; |
| 672 | |
| 673 | if (super_low_mem) |
| 674 | { |
| 675 | if (w != 32 || h != 32) { |
| 676 | w = 32; |
| 677 | h = 32; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if ((w != bm_w(bm_handle, 0) || h != bm_h(bm_handle, 0))) { |
| 682 | int dest_bm; |
| 683 | int mipped = 0; |
| 684 | int saved = 0; |
| 685 | |
| 686 | if (texp->flags & TF_TEXTURE_64) |
| 687 | saved = 6144; |
| 688 | else |
| 689 | saved = 24576; |
| 690 | |
| 691 | if (bm_mipped(bm_handle)) |
| 692 | mipped = 1; |
| 693 | |
| 694 | if (mipped) |
| 695 | saved += (saved / 3); |
| 696 | |
| 697 | Total_memory_saved += saved; |
| 698 | |
| 699 | mprintf(0, "Low mem: Resizing bitmap %s to %d x %d!\n", GameBitmaps[bm_handle].name, w, h); |
| 700 | mprintf(0, "Total memory saved=%d\n", Total_memory_saved); |
| 701 | |
| 702 | dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h * 2) / 3)); |
| 703 | ASSERT(dest_bm >= 0); |
| 704 | if (mipped) |
| 705 | GameBitmaps[dest_bm].flags |= BF_MIPMAPPED; |
| 706 | |
| 707 | GameBitmaps[dest_bm].format = GameBitmaps[bm_handle].format; |
| 708 | |
| 709 | bm_ScaleBitmapToBitmap(dest_bm, bm_handle); |
| 710 | strcpy(GameBitmaps[dest_bm].name, GameBitmaps[bm_handle].name); |
| 711 | bm_FreeBitmap(bm_handle); |
| 712 | |
| 713 | texp->bm_handle = dest_bm; |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | #endif |
| 718 | } |
| 719 | |
| 720 | // Pages in this particular texture |
| 721 | void TouchTexture(int n) { |
| 722 | if (GameTextures[n].flags & TF_ANIMATED) { |
| 723 | PageInVClip(GameTextures[n].bm_handle); |
| 724 | } else |
| 725 | PageInTexture(n, true); |
| 726 | } |
| 727 | |
| 728 | // Frees all the bumpmaps associated with a texture |
| 729 | void FreeTextureBumpmaps(int tex_handle) { |
| 730 | ASSERT(GameTextures[tex_handle].used > 0); |
| 731 | |
| 732 | if (GameTextures[tex_handle].bumpmap != -1) { |
| 733 | bump_FreeBumpmap(GameTextures[tex_handle].bumpmap); |
| 734 | GameTextures[tex_handle].bumpmap = -1; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | // Builds the bumpmaps for the past in texture |
| 739 | void BuildTextureBumpmaps(int texhandle) { |
| 740 | int i, t; |
| 741 | int8_t *buffer; |
| 742 | |
| 743 | if (GameTextures[texhandle].flags & TF_ANIMATED) |
| 744 | return; // No bumps for animated textures |
| 745 | |
| 746 | mprintf(0, "Calculating bumpmap for texture named %s.\n", GameTextures[texhandle].name); |
| 747 | |
| 748 | // Make sure there is no bump already |
| 749 | ASSERT(GameTextures[texhandle].bumpmap == -1); |
| 750 | |
| 751 | // Get width and height |
| 752 | int w = bm_w(GameTextures[texhandle].bm_handle, 0); |
| 753 | int h = bm_h(GameTextures[texhandle].bm_handle, 0); |
| 754 | |
| 755 | // Allocate a bumpmap |
| 756 | int bump = bump_AllocBumpmap(w, h); |
| 757 | ASSERT(bump != -1); |
| 758 | GameTextures[texhandle].bumpmap = bump; |
| 759 | |
| 760 | // allocate memory for our buffer |
| 761 | buffer = (int8_t *)mem_malloc(w * h); |
| 762 | ASSERT(buffer); |
| 763 | |
| 764 | uint16_t *src_data = (uint16_t *)bm_data(GameTextures[texhandle].bm_handle, 0); |
| 765 | |
| 766 | // create the grayscale |
| 767 | for (i = 0; i < h; i++) { |
| 768 | for (t = 0; t < w; t++) { |
| 769 | uint16_t color = src_data[i * w + t]; |
| 770 | |
| 771 | int red = ((color >> 10) & 0x1f) << 3; |
| 772 | int green = ((color >> 5) & 0x1f) << 3; |
| 773 | int blue = ((color) & 0x1f) << 3; |
| 774 | |
| 775 | buffer[i * w + t] = (int8_t)(0.29 * red + 0.60 * green + 0.11 * blue); |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | uint16_t *dest_data = (uint16_t *)bump_data(bump); |
| 780 | |
| 781 | for (i = 0; i < h; i++) { |
| 782 | int8_t *pDst = (int8_t *)(dest_data + i * w); |
| 783 | for (t = 0; t < w; t++) { |
| 784 | int v00, v01, v10; |
| 785 | |
| 786 | v00 = buffer[(i * w + t)]; // Get current pixel. *3 for 24 bits src |
| 787 | |
| 788 | if (t == w - 1) // Special case for last column |
| 789 | v01 = buffer[(i * w + t)]; // Get pixel to the right |
| 790 | else |
| 791 | v01 = buffer[(i * h + t + 1)]; // Get pixel to the right |
| 792 | |
| 793 | if (i == h - 1) // Special case for last row |
| 794 | v10 = buffer[i * w + t]; // Get pixel one line below |
| 795 | else |
| 796 | v10 = buffer[((i + 1) * w) + t]; // Get pixel one line below |
| 797 | |
| 798 | int iDu = v00 - v01; // The delta-u bump value |
| 799 | int iDv = v00 - v10; // The delta-v bump value |
| 800 | *pDst++ = (int8_t)iDu; |
| 801 | *pDst++ = (int8_t)iDv; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | mem_free(buffer); |
| 806 | } |
| 807 | |
| 808 | // Frees all the procedural memory for a texture |
| 809 | void FreeProceduralForTexture(int n) { |
| 810 | if (GameTextures[n].procedural != NULL) { |
| 811 | FreeStaticProceduralsForTexture(n); |
| 812 | mem_free(GameTextures[n].procedural->proc1); |
| 813 | mem_free(GameTextures[n].procedural->proc2); |
| 814 | bm_FreeBitmap(GameTextures[n].procedural->procedural_bitmap); |
| 815 | mem_free(GameTextures[n].procedural->palette); |
| 816 | mem_free(GameTextures[n].procedural); |
| 817 | |
| 818 | GameTextures[n].procedural = NULL; |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // Frees a texture that is being used and |
| 823 | // Decrements number of textures in use |
| 824 | void FreeTexture(int n) { |
| 825 | ASSERT(GameTextures[n].used); |
| 826 | |
| 827 | FreeTextureBumpmaps(n); |
| 828 | |
| 829 | GameTextures[n].used = 0; |
| 830 | GameTextures[n].name[0] = 0; |
| 831 | Num_textures--; |
| 832 | |
| 833 | FreeProceduralForTexture(n); |
| 834 | } |
| 835 | |
| 836 | // Given current index, gets index of next texture in use |
| 837 | int GetNextTexture(int n) { |
| 838 | int i; |
| 839 | |
| 840 | if (Num_textures == 0) |
| 841 | return -1; |
| 842 | |
| 843 | for (i = n + 1; i < MAX_TEXTURES; i++) { |
| 844 | if (GameTextures[i].used) |
| 845 | return i; |
| 846 | } |
| 847 | for (i = 0; i < n; i++) { |
| 848 | if (GameTextures[i].used) |
| 849 | return i; |
| 850 | } |
| 851 | |
| 852 | return n; |
| 853 | } |
| 854 | // Given current index, gets index of prev texture in use |
| 855 | int GetPreviousTexture(int n) { |
| 856 | int i; |
| 857 | |
| 858 | if (Num_textures == 0) |
| 859 | return -1; |
| 860 | |
| 861 | for (i = n - 1; i >= 0; i--) { |
| 862 | if (GameTextures[i].used) |
| 863 | return i; |
| 864 | } |
| 865 | for (i = MAX_TEXTURES - 1; i > n; i--) { |
| 866 | if (GameTextures[i].used) |
| 867 | return i; |
| 868 | } |
| 869 | |
| 870 | return n; |
| 871 | } |