| 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 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include "bumpmap.h" |
| 22 |
#include "pstypes.h" |
| 23 |
#include "pserror.h" |
| 24 |
#include "mono.h" |
| 25 |
#include "mem.h" |
| 26 |
|
| 27 |
static int Num_of_bumpmaps = 0; |
| 28 |
static uint16_t Free_bumpmap_list[MAX_BUMPMAPS]; |
| 29 |
|
| 30 |
bms_bumpmap GameBumpmaps[MAX_BUMPMAPS]; |
| 31 |
static int Bumpmap_mem_used = 0; |
| 32 |
|
| 33 |
// Sets all the bumpmaps to unused |
| 34 |
void bump_InitBumpmaps() { |
| 35 |
int i; |
| 36 |
|
| 37 |
for (i = 0; i < MAX_BUMPMAPS; i++) { |
| 38 |
GameBumpmaps[i].flags = 0; |
| 39 |
GameBumpmaps[i].data = NULL; |
| 40 |
GameBumpmaps[i].cache_slot = -1; |
| 41 |
Free_bumpmap_list[i] = i; |
| 42 |
} |
| 43 |
|
| 44 |
atexit(bump_ShutdownBumpmaps); |
| 45 |
} |
| 46 |
void bump_ShutdownBumpmaps(void) { |
| 47 |
int i; |
| 48 |
|
| 49 |
mprintf(0, "Freeing all bumpmap memory.\n"); |
| 50 |
|
| 51 |
for (i = 0; i < MAX_BUMPMAPS; i++) { |
| 52 |
while (GameBumpmaps[i].flags & BUMPF_USED) |
| 53 |
bump_FreeBumpmap(i); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
// Allocs a bumpmap of w*h size |
| 58 |
// Returns bumpmap handle if successful, -1 if otherwise |
| 59 |
|
| 60 |
int bump_AllocBumpmap(int w, int h) { |
| 61 |
int n; //,i; |
| 62 |
|
| 63 |
if (Num_of_bumpmaps == MAX_BUMPMAPS) |
| 64 |
Int3(); // Ran out of bumpmaps! |
| 65 |
|
| 66 |
n = Free_bumpmap_list[Num_of_bumpmaps++]; |
| 67 |
ASSERT(!(GameBumpmaps[n].flags & BUMPF_USED)); |
| 68 |
|
| 69 |
// If no go on the malloc, bail out with -1 |
| 70 |
|
| 71 |
memset(&GameBumpmaps[n], 0, sizeof(bms_bumpmap)); |
| 72 |
|
| 73 |
GameBumpmaps[n].data = (uint16_t *)mem_malloc(w * h * 2); |
| 74 |
if (!GameBumpmaps[n].data) { |
| 75 |
mprintf(0, "NOT ENOUGHT MEMORY FOR BUMPMAP!\n"); |
| 76 |
Int3(); |
| 77 |
return BAD_BUMP_INDEX; |
| 78 |
} |
| 79 |
|
| 80 |
GameBumpmaps[n].cache_slot = -1; |
| 81 |
GameBumpmaps[n].flags = BUMPF_USED | BUMPF_CHANGED; |
| 82 |
GameBumpmaps[n].width = w; |
| 83 |
GameBumpmaps[n].height = h; |
| 84 |
|
| 85 |
Bumpmap_mem_used += (w * h * 2); |
| 86 |
|
| 87 |
return n; |
| 88 |
} |
| 89 |
|
| 90 |
// Given a handle, frees the bumpmap memory and flags this bumpmap as unused |
| 91 |
void bump_FreeBumpmap(int handle) { |
| 92 |
ASSERT(GameBumpmaps[handle].flags & BUMPF_USED); |
| 93 |
|
| 94 |
if (GameBumpmaps[handle].data != NULL) |
| 95 |
mem_free(GameBumpmaps[handle].data); |
| 96 |
|
| 97 |
Bumpmap_mem_used -= (GameBumpmaps[handle].width * GameBumpmaps[handle].height * 2); |
| 98 |
|
| 99 |
GameBumpmaps[handle].data = NULL; |
| 100 |
GameBumpmaps[handle].flags &= ~BUMPF_USED; |
| 101 |
|
| 102 |
Free_bumpmap_list[--Num_of_bumpmaps] = handle; |
| 103 |
} |
| 104 |
|
| 105 |
// returns a bumpmaps data else NULL if something is wrong |
| 106 |
uint16_t *bump_data(int handle) { |
| 107 |
uint16_t *d; |
| 108 |
|
| 109 |
if (!(GameBumpmaps[handle].flags & BUMPF_USED)) { |
| 110 |
Int3(); |
| 111 |
return NULL; |
| 112 |
} |
| 113 |
|
| 114 |
d = GameBumpmaps[handle].data; |
| 115 |
|
| 116 |
return (d); |
| 117 |
} |
| 118 |
|
| 119 |
// returns width of bumpmap |
| 120 |
uint8_t bump_w(int handle) { |
| 121 |
ASSERT(GameBumpmaps[handle].flags & BUMPF_USED); |
| 122 |
return GameBumpmaps[handle].width; |
| 123 |
} |
| 124 |
|
| 125 |
// returns height of bumpmap |
| 126 |
uint8_t bump_h(int handle) { |
| 127 |
ASSERT(GameBumpmaps[handle].flags & BUMPF_USED); |
| 128 |
return GameBumpmaps[handle].height; |
| 129 |
} |