Skip to content

Commit

Permalink
rooms: move room draw tracking to TRX
Browse files Browse the repository at this point in the history
This moves the room draw count and array to TRX.
  • Loading branch information
lahm86 committed Feb 6, 2025
1 parent 33658a1 commit 2c5e864
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 68 deletions.
34 changes: 34 additions & 0 deletions src/libtrx/game/rooms/draw.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "game/rooms.h"

static int32_t m_DrawCount = 0;
static int16_t m_RoomsToDraw[MAX_ROOMS_TO_DRAW] = {};

void Room_DrawReset(void)
{
m_DrawCount = 0;
}

void Room_MarkToBeDrawn(const int16_t room_num)
{
if (m_DrawCount + 1 == MAX_ROOMS_TO_DRAW) {
return;
}

for (int32_t i = 0; i < m_DrawCount; i++) {
if (m_RoomsToDraw[i] == room_num) {
return;
}
}

m_RoomsToDraw[m_DrawCount++] = room_num;
}

int32_t Room_DrawGetCount(void)
{
return m_DrawCount;
}

int16_t Room_DrawGetRoom(const int16_t idx)
{
return m_RoomsToDraw[idx];
}
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/rooms.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "rooms/common.h"
#include "rooms/const.h"
#include "rooms/draw.h"
#include "rooms/enum.h"
1 change: 1 addition & 0 deletions src/libtrx/include/libtrx/game/rooms/const.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#define MAX_ROOMS 1024
#define MAX_ROOMS_TO_DRAW 100

#define NO_ROOM_NEG (-1)
#define NO_ROOM 255 // TODO: merge this with NO_ROOM_NEG
Expand Down
8 changes: 8 additions & 0 deletions src/libtrx/include/libtrx/game/rooms/draw.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#pragma once

#include <stdint.h>

void Room_DrawReset(void);
void Room_MarkToBeDrawn(int16_t room_num);
int32_t Room_DrawGetCount(void);
int16_t Room_DrawGetRoom(int16_t idx);
1 change: 1 addition & 0 deletions src/libtrx/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ sources = [
'game/phase/phase_stats.c',
'game/random.c',
'game/rooms/common.c',
'game/rooms/draw.c',
'game/savegame.c',
'game/shell/common.c',
'game/sound.c',
Expand Down
5 changes: 2 additions & 3 deletions src/tr1/game/collide.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,8 @@ bool Collide_CollideStaticObjects(

Room_GetNearByRooms(x, y, z, coll->radius + 50, height + 50, room_num);

for (int i = 0; i < g_RoomsToDrawCount; i++) {
int16_t room_num = g_RoomsToDraw[i];
const ROOM *const room = Room_Get(room_num);
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
const ROOM *const room = Room_Get(Room_DrawGetRoom(i));

for (int32_t j = 0; j < room->num_static_meshes; j++) {
const STATIC_MESH *const mesh = &room->static_meshes[j];
Expand Down
6 changes: 2 additions & 4 deletions src/tr1/game/cutscene.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,10 @@ bool Cutscene_Start(const int32_t level_num)
}
}

g_RoomsToDrawCount = 0;
Room_DrawReset();
for (int16_t room_num = 0; room_num < room_count; room_num++) {
if (!Room_Get(room_num)->bound_active) {
if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = room_num;
}
Room_MarkToBeDrawn(room_num);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/tr1/game/game/game_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ void Game_Draw(bool draw_overlay)

} else {
// cinematic scene
for (int i = 0; i < g_RoomsToDrawCount; i++) {
int16_t room_num = g_RoomsToDraw[i];
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
const int16_t room_num = Room_DrawGetRoom(i);
ROOM *const room = Room_Get(room_num);
room->bound_top = 0;
room->bound_left = 0;
Expand Down
18 changes: 3 additions & 15 deletions src/tr1/game/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,8 @@ int32_t Room_FindGridShift(int32_t src, int32_t dst)
void Room_GetNearByRooms(
int32_t x, int32_t y, int32_t z, int32_t r, int32_t h, int16_t room_num)
{
g_RoomsToDrawCount = 0;
if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = room_num;
}
Room_DrawReset();
Room_MarkToBeDrawn(room_num);
Room_GetNewRoom(x + r, y, z + r, room_num);
Room_GetNewRoom(x - r, y, z + r, room_num);
Room_GetNewRoom(x + r, y, z - r, room_num);
Expand All @@ -212,17 +210,7 @@ void Room_GetNearByRooms(
void Room_GetNewRoom(int32_t x, int32_t y, int32_t z, int16_t room_num)
{
Room_GetSector(x, y, z, &room_num);

for (int i = 0; i < g_RoomsToDrawCount; i++) {
int16_t drawn_room = g_RoomsToDraw[i];
if (drawn_room == room_num) {
return;
}
}

if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = room_num;
}
Room_MarkToBeDrawn(room_num);
}

SECTOR *Room_GetPitSector(
Expand Down
14 changes: 5 additions & 9 deletions src/tr1/game/room_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ static bool M_SetBounds(const PORTAL *portal, const ROOM *parent)
}

if (!room->bound_active) {
if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = portal->room_num;
}
Room_MarkToBeDrawn(portal->room_num);
room->bound_active = 1;
}
return true;
Expand Down Expand Up @@ -200,14 +198,14 @@ void Room_DrawAllRooms(int16_t base_room, int16_t target_room)
g_PhdRight = Viewport_GetMaxX();
g_PhdBottom = Viewport_GetMaxY();

g_RoomsToDrawCount = 0;
Room_DrawReset();

M_PrepareToDraw(base_room);
M_PrepareToDraw(target_room);
M_DrawSkybox();

for (int i = 0; i < g_RoomsToDrawCount; i++) {
Room_DrawSingleRoom(g_RoomsToDraw[i]);
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
Room_DrawSingleRoom(Room_DrawGetRoom(i));
}
Output_SetupAboveWater(false);
}
Expand All @@ -225,9 +223,7 @@ static void M_PrepareToDraw(int16_t room_num)
room->bound_bottom = g_PhdBottom;
room->bound_active = 1;

if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = room_num;
}
Room_MarkToBeDrawn(room_num);

Matrix_Push();
Matrix_TranslateAbs32(room->pos);
Expand Down
1 change: 0 additions & 1 deletion src/tr1/global/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#define MAX_FRAMES 10
#define MAX_CD_TRACKS 64
#define MAX_FLIP_MAPS 10
#define MAX_ROOMS_TO_DRAW 100
#define MAX_ITEMS 10240
#define MAX_SECRETS 16
#define LARA_MAX_HITPOINTS 1000
Expand Down
2 changes: 0 additions & 2 deletions src/tr1/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ int16_t *g_GroundZone2[2] = { nullptr };
int16_t *g_FlyZone[2] = { nullptr };
int32_t g_NumberSoundEffects = 0;
OBJECT_VECTOR *g_SoundEffectsTable = nullptr;
int16_t g_RoomsToDraw[MAX_ROOMS_TO_DRAW] = { -1 };
int16_t g_RoomsToDrawCount = 0;

INVENTORY_MODE g_InvMode;

Expand Down
2 changes: 0 additions & 2 deletions src/tr1/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ extern int16_t *g_GroundZone2[2];
extern int16_t *g_FlyZone[2];
extern int32_t g_NumberSoundEffects;
extern OBJECT_VECTOR *g_SoundEffectsTable;
extern int16_t g_RoomsToDraw[MAX_ROOMS_TO_DRAW];
extern int16_t g_RoomsToDrawCount;

extern REQUEST_INFO g_SavegameRequester;

Expand Down
4 changes: 2 additions & 2 deletions src/tr2/game/collide.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ int32_t Collide_CollideStaticObjects(

Room_GetNearbyRooms(x, y, z, coll->radius + 50, height + 50, room_num);

for (int32_t i = 0; i < g_RoomsToDrawCount; i++) {
const ROOM *const room = Room_Get(g_RoomsToDraw[i]);
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
const ROOM *const room = Room_Get(Room_DrawGetRoom(i));

for (int32_t j = 0; j < room->num_static_meshes; j++) {
const STATIC_MESH *const mesh = &room->static_meshes[j];
Expand Down
4 changes: 2 additions & 2 deletions src/tr2/game/room.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ void Room_GetNearbyRooms(
const int32_t x, const int32_t y, const int32_t z, const int32_t r,
const int32_t h, const int16_t room_num)
{
g_RoomsToDrawCount = 0;
Room_DrawReset();
Room_MarkToBeDrawn(room_num);

Room_GetNewRoom(r + x, y, r + z, room_num);
Expand Down Expand Up @@ -843,7 +843,7 @@ void Room_InitCinematic(void)
room->flags |= RF_OUTSIDE;
}

g_RoomsToDrawCount = 0;
Room_DrawReset();
for (int32_t i = 0; i < room_count; i++) {
if (!Room_Get(i)->bound_active) {
Room_MarkToBeDrawn(i);
Expand Down
23 changes: 5 additions & 18 deletions src/tr2/game/room_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,6 @@ static int32_t m_BoxLines[12][2] = {
{ 6, 7 }, { 7, 4 }, { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 },
};

void Room_MarkToBeDrawn(const int16_t room_num)
{
for (int32_t i = 0; i < g_RoomsToDrawCount; i++) {
if (g_RoomsToDraw[i] == room_num) {
return;
}
}

if (g_RoomsToDrawCount + 1 < MAX_ROOMS_TO_DRAW) {
g_RoomsToDraw[g_RoomsToDrawCount++] = room_num;
}
}

void Room_GetBounds(void)
{
while (m_BoundStart != m_BoundEnd) {
Expand Down Expand Up @@ -486,7 +473,7 @@ void Room_DrawAllRooms(const int16_t current_room)
m_BoundStart = 0;
m_BoundEnd = 1;

g_RoomsToDrawCount = 0;
Room_DrawReset();
m_Outside = room->flags & RF_OUTSIDE;

if (m_Outside) {
Expand Down Expand Up @@ -540,13 +527,13 @@ void Room_DrawAllRooms(const int16_t current_room)
Lara_Draw(g_LaraItem);
}

for (int32_t i = 0; i < g_RoomsToDrawCount; i++) {
const int16_t room_num = g_RoomsToDraw[i];
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
const int16_t room_num = Room_DrawGetRoom(i);
Room_DrawSingleRoomGeometry(room_num);
}

for (int32_t i = 0; i < g_RoomsToDrawCount; i++) {
const int16_t room_num = g_RoomsToDraw[i];
for (int32_t i = 0; i < Room_DrawGetCount(); i++) {
const int16_t room_num = Room_DrawGetRoom(i);
Room_DrawSingleRoomObjects(room_num);
}

Expand Down
1 change: 0 additions & 1 deletion src/tr2/game/room_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "global/types.h"

void Room_MarkToBeDrawn(int16_t room_num);
void Room_GetBounds(void);
void Room_SetBounds(
const int16_t *obj_ptr, int32_t room_num, const ROOM *parent);
Expand Down
1 change: 0 additions & 1 deletion src/tr2/global/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#define MAX_AUDIO_SAMPLE_TRACKS 32
#define MAX_CD_TRACKS 64
#define MAX_PALETTES 16
#define MAX_ROOMS_TO_DRAW 100
#define MAX_FLIP_MAPS 10
#define MAX_VERTICES 0x2000
#define MAX_BOUND_ROOMS 128
Expand Down
3 changes: 0 additions & 3 deletions src/tr2/global/vars.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
const char *g_TRXVersion = "TR2X (non-Docker build)";
#endif

int16_t g_RoomsToDraw[MAX_ROOMS_TO_DRAW] = {};
int16_t g_RoomsToDrawCount = 0;

const float g_RhwFactor = 0x14000000.p0;

SDL_Window *g_SDLWindow = nullptr;
Expand Down
3 changes: 0 additions & 3 deletions src/tr2/global/vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#include <SDL2/SDL.h>

extern int16_t g_RoomsToDraw[MAX_ROOMS_TO_DRAW];
extern int16_t g_RoomsToDrawCount;

extern const float g_RhwFactor;

extern SDL_Window *g_SDLWindow;
Expand Down

0 comments on commit 2c5e864

Please sign in to comment.