Skip to content

Commit

Permalink
output: read vertex buffer size from level (LostArtefacts#1400)
Browse files Browse the repository at this point in the history
This replaces the fixed vertex buffer size of 1500 with a maximum value
read from the level - so the "biggest" room, object or static mesh
determines the array size.

Resolves LostArtefacts#1398.
  • Loading branch information
lahm86 authored Jul 3, 2024
1 parent f44cbcf commit d0ba0e5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- fixed lightning rendering z-buffer issues (#1385, regression from 1.4)
- fixed possible game crashes if more than 16 savegame slots are set (#1374)
- fixed savegame slots higher than 64 not working (#1395)
- fixed a crash in custom levels if a room had more than 1500 vertices (#1398)

## [4.1.2](https://github.com/LostArtefacts/TR1X/compare/4.1.1...4.1.2) - 2024-04-28
- fixed pictures display time (#1349, regression from 4.1)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ Not all options are turned on by default. Refer to `TR1X_ConfigTool.exe` for det
- expanded moveable limit from 256 to 10240
- expanded maximum textures from 2048 to 8192
- expanded maximum texture pages from 32 to 128
- expanded maximum vertices of a single drawable object from 1500 to unlimited
- expanded the number of visible enemies from 8 to 32
- ported audio decoding library to ffmpeg
- ported video decoding library to ffmpeg
Expand Down
1 change: 1 addition & 0 deletions src/game/gamebuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ static const char *GameBuf_GetBufferName(GAME_BUFFER buffer)
case GBUF_SAMPLES: return "Samples";
case GBUF_TRAP_DATA: return "Trap data";
case GBUF_CREATURE_DATA: return "Creature data";
case GBUF_VERTEX_BUFFER: return "Vertex buffer";
}
// clang-format on
return "Unknown";
Expand Down
1 change: 1 addition & 0 deletions src/game/gamebuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ typedef enum GAME_BUFFER {
GBUF_SAMPLES,
GBUF_TRAP_DATA,
GBUF_CREATURE_DATA,
GBUF_VERTEX_BUFFER,
} GAME_BUFFER;

void GameBuf_Init(void);
Expand Down
37 changes: 37 additions & 0 deletions src/game/level.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ static bool Level_LoadTexturePages(MYFILE *fp);
static bool Level_LoadFromFile(
const char *filename, int32_t level_num, bool is_demo);
static void Level_CompleteSetup(int32_t level_num);
static size_t Level_CalculateMaxVertices(void);

static bool Level_LoadFromFile(
const char *filename, int32_t level_num, bool is_demo)
Expand Down Expand Up @@ -890,6 +891,10 @@ static void Level_CompleteSetup(int32_t level_num)
// Configure enemies who carry and drop items
Carrier_InitialiseLevel(level_num);

const size_t max_vertices = Level_CalculateMaxVertices();
LOG_INFO("Maximum vertices: %d", max_vertices);
Output_ReserveVertexBuffer(max_vertices);

// Move the prepared texture pages into g_TexturePagePtrs.
uint8_t *base = GameBuf_Alloc(
m_LevelInfo.texture_page_count * PAGE_SIZE, GBUF_TEXTURE_PAGES);
Expand Down Expand Up @@ -925,6 +930,38 @@ static void Level_CompleteSetup(int32_t level_num)
Memory_FreePointer(&sample_sizes);
}

static size_t Level_CalculateMaxVertices(void)
{
size_t max_vertices = 0;
for (int32_t i = 0; i < O_NUMBER_OF; i++) {
const OBJECT_INFO *object_info = &g_Objects[i];
if (!object_info->loaded) {
continue;
}

for (int32_t j = 0; j < object_info->nmeshes; j++) {
max_vertices =
MAX(max_vertices, *(g_Meshes[object_info->mesh_index + j] + 5));
}
}

for (int32_t i = 0; i < STATIC_NUMBER_OF; i++) {
const STATIC_INFO *static_info = &g_StaticObjects[i];
if (!static_info->loaded || static_info->nmeshes < 0) {
continue;
}

max_vertices =
MAX(max_vertices, *(g_Meshes[static_info->mesh_number] + 5));
}

for (int32_t i = 0; i < g_RoomCount; i++) {
max_vertices = MAX(max_vertices, *g_RoomInfo[i].data);
}

return max_vertices;
}

bool Level_Load(int level_num)
{
LOG_INFO("%d (%s)", level_num, g_GameFlow.levels[level_num].level_file);
Expand Down
8 changes: 7 additions & 1 deletion src/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "config.h"
#include "game/clock.h"
#include "game/console.h"
#include "game/gamebuf.h"
#include "game/overlay.h"
#include "game/phase/phase.h"
#include "game/picture.h"
Expand Down Expand Up @@ -48,7 +49,7 @@ static int32_t m_WibbleTable[WIBBLE_SIZE] = { 0 };
static int32_t m_ShadeTable[WIBBLE_SIZE] = { 0 };
static int32_t m_RandTable[WIBBLE_SIZE] = { 0 };

static PHD_VBUF m_VBuf[1500] = { 0 };
static PHD_VBUF *m_VBuf = NULL;
static int32_t m_DrawDistFade = 0;
static int32_t m_DrawDistMax = 0;
static RGB_F m_WaterColor = { 0 };
Expand Down Expand Up @@ -402,6 +403,11 @@ void Output_Shutdown(void)
Memory_FreePointer(&m_BackdropImagePath);
}

void Output_ReserveVertexBuffer(const size_t size)
{
m_VBuf = GameBuf_Alloc(size * sizeof(PHD_VBUF), GBUF_VERTEX_BUFFER);
}

void Output_SetWindowSize(int width, int height)
{
S_Output_SetWindowSize(width, height);
Expand Down
1 change: 1 addition & 0 deletions src/game/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

bool Output_Init(void);
void Output_Shutdown(void);
void Output_ReserveVertexBuffer(size_t size);

void Output_SetWindowSize(int width, int height);
void Output_ApplyRenderSettings(void);
Expand Down

0 comments on commit d0ba0e5

Please sign in to comment.