| 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 | #ifndef PSBITMAP_H |
| 20 | #define PSBITMAP_H |
| 21 | |
| 22 | #include <filesystem> |
| 23 | |
| 24 | #include "cfile.h" |
| 25 | |
| 26 | #if defined(POSIX) |
| 27 | #include "linux_fix.h" //needed for stricmp's throughout bitmap lib |
| 28 | #endif |
| 29 | |
| 30 | #define MAX_BITMAPS 5000 |
| 31 | #define NUM_MIP_LEVELS 5 |
| 32 | |
| 33 | // It really doesn't matter what these are, as long as its above 10 |
| 34 | #define OUTRAGE_4444_COMPRESSED_MIPPED 121 |
| 35 | #define OUTRAGE_1555_COMPRESSED_MIPPED 122 |
| 36 | #define OUTRAGE_NEW_COMPRESSED_MIPPED 123 |
| 37 | #define OUTRAGE_COMPRESSED_MIPPED 124 |
| 38 | #define OUTRAGE_COMPRESSED_OGF_8BIT 125 |
| 39 | #define OUTRAGE_TGA_TYPE 126 |
| 40 | #define OUTRAGE_COMPRESSED_OGF 127 |
| 41 | #define BITMAP_NAME_LEN 35 |
| 42 | #define BAD_BITMAP_HANDLE 0 |
| 43 | |
| 44 | // Bitmap flags |
| 45 | #define BF_TRANSPARENT 1 |
| 46 | #define BF_CHANGED 2 // this bitmap has changed since last frame (useful for hardware cacheing) |
| 47 | #define BF_MIPMAPPED 4 // This bitmap has mip levels |
| 48 | #define BF_NOT_RESIDENT 8 // This bitmap is not paged in |
| 49 | #define BF_WANTS_MIP 16 // Calculate mip levels when this bitmap is paged in |
| 50 | #define BF_WANTS_4444 32 // Read data as 4444 when this bitmap is paged in |
| 51 | #define BF_BRAND_NEW 64 // This bitmap was just allocated and hasn't been to the video card |
| 52 | #define BF_COMPRESSABLE 128 // This bitmap is compressable for 3dhardware that supports it |
| 53 | |
| 54 | // Bitmap priorities |
| 55 | #define BITMAP_FORMAT_STANDARD 0 |
| 56 | #define BITMAP_FORMAT_1555 0 |
| 57 | #define BITMAP_FORMAT_4444 1 |
| 58 | |
| 59 | struct bms_bitmap { |
| 60 | uint16_t *data16; // 16bit data |
| 61 | uint16_t width, height; // Width and height in pixels |
| 62 | uint16_t used; // Is this bitmap free to be allocated? |
| 63 | |
| 64 | int16_t cache_slot; // For use by the rendering lib |
| 65 | uint8_t mip_levels; |
| 66 | uint8_t flags; |
| 67 | |
| 68 | uint8_t format; // See bitmap format types above |
| 69 | char name[BITMAP_NAME_LEN]; // Whats the name of this bitmap? (ie SteelWall) |
| 70 | }; |
| 71 | |
| 72 | struct chunked_bitmap { |
| 73 | int pw, ph; // pixel width and height |
| 74 | int w, h; // width and height in square bitmaps. |
| 75 | int *bm_array; // array of bitmap handles. |
| 76 | }; |
| 77 | extern bms_bitmap GameBitmaps[MAX_BITMAPS]; |
| 78 | extern uint32_t Bitmap_memory_used; |
| 79 | |
| 80 | // Sets all the bitmaps to unused |
| 81 | void bm_InitBitmaps(); |
| 82 | // Frees up all memory used by bitmaps |
| 83 | void bm_ShutdownBitmaps(void); |
| 84 | // Allocs a bitmap of w x h size |
| 85 | // If add_mem is nonzero, adds that to the amount alloced |
| 86 | // (added due to the way the tmapper works) |
| 87 | // Returns bitmap handle if successful, -1 if otherwise |
| 88 | int bm_AllocBitmap(int w, int h, int add_mem); |
| 89 | // Given a handle, frees the bitmap memory and flags this bitmap as unused |
| 90 | void bm_FreeBitmap(int handle); |
| 91 | // Allocs and loads a bitmap |
| 92 | // Loads either regular TGAs or OUTRAGE_TGA_TYPEs |
| 93 | // Returns the handle of the loaded bitmap |
| 94 | // Returns -1 if something is wrong |
| 95 | int bm_AllocLoadFileBitmap(const char *filename, int mipped, int format = BITMAP_FORMAT_1555); |
| 96 | // Allocs and loads a bitmap from an open file |
| 97 | // Returns the handle of the loaded bitmap |
| 98 | // Returns -1 if something is wrong |
| 99 | // If mipped is non-zero, allocs extra space for mips and computes them |
| 100 | int bm_AllocLoadBitmap(CFILE *infile, int mipped, int format = BITMAP_FORMAT_1555); |
| 101 | // Given a handle, makes a big random shape to let you know you are screwed. |
| 102 | void bm_MakeBad(int handle); |
| 103 | // Searches thru all bitmaps for a specific name, returns -1 if not found |
| 104 | // or index of bitmap with name |
| 105 | int bm_FindBitmapName(const char *name); |
| 106 | // Saves a bitmap as an OUTRAGE_TGA_TYPE to an open file |
| 107 | // Returns -1 if something is wrong. |
| 108 | int bm_SaveBitmap(CFILE *fp, int handle); |
| 109 | // Saves a bitmap to a file. Saves the bitmap as an OUTRAGE_TGA_TYPE. |
| 110 | // Returns -1 if something is wrong. |
| 111 | int bm_SaveFileBitmap(const std::filesystem::path& filename, int handle); |
| 112 | // given a handle to a bitmap, returns its width, or -1 if handle is invalid |
| 113 | int bm_w(int handle, int miplevel); |
| 114 | // given a handle to a bitmap, returns its height, or -1 if handle is invalid |
| 115 | int bm_h(int handle, int miplevel); |
| 116 | // given a handle to a bitmap, returns mipped status, or -1 if handle is invalid |
| 117 | int bm_mipped(int handle); |
| 118 | // a function to determine if a pixel in a bitmap is transparent |
| 119 | bool bm_pixel_transparent(int bm_handle, int x, int y); |
| 120 | // a function to determine if a pixel in a bitmap is transparent |
| 121 | uint16_t bm_pixel(int bm_handle, int x, int y); |
| 122 | // given a handle to a bitmap, returns a pointer to its data, or NULL if handle is invalid |
| 123 | uint16_t *bm_data(int handle, int miplevel); |
| 124 | // Gets bits per pixel for a particular bitmap |
| 125 | // As of 12/30/96 always returns 16 |
| 126 | int bm_bpp(int handle); |
| 127 | // Given a source bitmap, generates mipmaps for it |
| 128 | void bm_GenerateMipMaps(int handle); |
| 129 | // Given two bitmaps, scales the data from src to the size of dest |
| 130 | void bm_ScaleBitmapToBitmap(int dest, int src); |
| 131 | // given a handle and a miplevel, returns the bytes per bitmap row |
| 132 | int bm_rowsize(int handle, int miplevel); |
| 133 | // Goes through the bitmap and sees if there is any transparency...if so, flag it! |
| 134 | int bm_SetBitmapIfTransparent(int handle); |
| 135 | // Allocs and loads a bitmap but doesn't actually load texel data! |
| 136 | // Returns the handle of the loaded bitmap |
| 137 | // Returns -1 if something is wrong |
| 138 | int bm_AllocLoadFileNoMemBitmap(const char *filename, int mipped, int format = BITMAP_FORMAT_1555); |
| 139 | // Just like bm_AllocBitmap but doesn't actually allocate memory. Useful for paging! |
| 140 | int bm_AllocNoMemBitmap(int w, int h); |
| 141 | // clears bitmap |
| 142 | void bm_ClearBitmap(int handle); |
| 143 | // Changes the size of a bitmap to a new size |
| 144 | void bm_ChangeSize(int handle, int new_w, int new_h); |
| 145 | void bm_ChangeEndName(const char *src, char *dest); |
| 146 | // takes a large static bitmap and breaks it into smaller managable bitmaps |
| 147 | bool bm_CreateChunkedBitmap(int bm_handle, chunked_bitmap *chunk); |
| 148 | // destroys a chunked bitmap. |
| 149 | void bm_DestroyChunkedBitmap(chunked_bitmap *chunk); |
| 150 | // simply frees up a bitmap |
| 151 | void bm_FreeBitmapData(int handle); |
| 152 | // Returns the format of this bitmap |
| 153 | int bm_format(int handle); |
| 154 | // Returns the number of mipmap levels |
| 155 | int bm_miplevels(int handle); |
| 156 | |
| 157 | #endif |