game: convert RoomsToDraw to a dynamic array

Closes #173
This commit is contained in:
rr- 2021-11-03 20:32:54 +01:00 committed by Marcin Kurczewski
parent f74f33ca3c
commit 59f45e6623
10 changed files with 122 additions and 31 deletions

73
src/dynarray.c Normal file
View file

@ -0,0 +1,73 @@
#include "dynarray.h"
#include "specific/init.h"
#include <stdlib.h>
#include <string.h>
struct DYNARRAY {
char *data;
size_t elem_size;
size_t used;
size_t size;
};
DYNARRAY *DynArray_Create(size_t elem_size)
{
DYNARRAY *arr = malloc(sizeof(DYNARRAY));
arr->data = NULL;
arr->size = 0;
arr->used = 0;
arr->elem_size = elem_size;
return arr;
}
size_t DynArray_Size(DYNARRAY *arr)
{
return arr->used;
}
const void *DynArray_Get(DYNARRAY *arr, size_t idx)
{
if (idx < 0 || idx >= arr->used) {
return NULL;
}
return arr->data + idx * arr->elem_size;
}
void DynArray_Reset(DYNARRAY *arr)
{
if (arr->data) {
free(arr->data);
}
arr->data = NULL;
arr->used = 0;
arr->size = 0;
}
void DynArray_Append(DYNARRAY *arr, const void *element)
{
if (arr->used == arr->size) {
if (!arr->size) {
arr->size += 1;
} else {
arr->size = arr->size * 3 / 2;
}
arr->data = realloc(arr->data, arr->size * arr->elem_size);
if (!arr->data) {
S_ExitSystem("Failed to allocate memory");
}
}
memcpy(arr->data + arr->used * arr->elem_size, element, arr->elem_size);
arr->used++;
}
void DynArray_Free(DYNARRAY *arr)
{
if (arr->data) {
free(arr->data);
}
arr->data = NULL;
arr->used = 0;
arr->size = 0;
}

15
src/dynarray.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef T1M_DYNARRAY_H
#define T1M_DYNARRAY_H
#include <stdint.h>
typedef struct DYNARRAY DYNARRAY;
DYNARRAY *DynArray_Create(size_t elem_size);
size_t DynArray_Size(DYNARRAY *arr);
const void *DynArray_Get(DYNARRAY *arr, size_t idx);
void DynArray_Reset(DYNARRAY *arr);
void DynArray_Append(DYNARRAY *arr, const void *element);
void DynArray_Free(DYNARRAY *arr);
#endif

View file

@ -55,16 +55,16 @@ int32_t StopCinematic(int32_t level_num)
void InitCinematicRooms()
{
for (int i = 0; i < RoomCount; i++) {
if (RoomInfo[i].flipped_room >= 0) {
RoomInfo[RoomInfo[i].flipped_room].bound_active = 1;
for (int16_t room_num = 0; room_num < RoomCount; room_num++) {
if (RoomInfo[room_num].flipped_room >= 0) {
RoomInfo[RoomInfo[room_num].flipped_room].bound_active = 1;
}
}
RoomsToDrawNum = 0;
for (int i = 0; i < RoomCount; i++) {
if (!RoomInfo[i].bound_active) {
RoomsToDraw[RoomsToDrawNum++] = i;
DynArray_Reset(RoomsToDraw);
for (int16_t room_num = 0; room_num < RoomCount; room_num++) {
if (!RoomInfo[room_num].bound_active) {
DynArray_Append(RoomsToDraw, &room_num);
}
}
}

View file

@ -311,8 +311,9 @@ int32_t CollideStaticObjects(
GetNearByRooms(x, y, z, coll->radius + 50, hite + 50, room_number);
for (int i = 0; i < RoomsToDrawNum; i++) {
ROOM_INFO *r = &RoomInfo[RoomsToDraw[i]];
for (int i = 0; i < DynArray_Size(RoomsToDraw); i++) {
int16_t room_num = *(int16_t *)DynArray_Get(RoomsToDraw, i);
ROOM_INFO *r = &RoomInfo[room_num];
MESH_INFO *mesh = r->mesh;
for (int j = 0; j < r->num_meshes; j++, mesh++) {
@ -455,8 +456,8 @@ int32_t CollideStaticObjects(
void GetNearByRooms(
int32_t x, int32_t y, int32_t z, int32_t r, int32_t h, int16_t room_num)
{
RoomsToDraw[0] = room_num;
RoomsToDrawNum = 1;
DynArray_Reset(RoomsToDraw);
DynArray_Append(RoomsToDraw, &room_num);
GetNewRoom(x + r, y, z + r, room_num);
GetNewRoom(x - r, y, z + r, room_num);
GetNewRoom(x + r, y, z - r, room_num);
@ -472,18 +473,15 @@ void GetNewRoom(int32_t x, int32_t y, int32_t z, int16_t room_num)
GetFloor(x, y, z, &room_num);
int i;
for (i = 0; i < RoomsToDrawNum; i++) {
if (RoomsToDraw[i] == room_num) {
for (i = 0; i < DynArray_Size(RoomsToDraw); i++) {
int16_t drawn_room = *(int16_t *)DynArray_Get(RoomsToDraw, i);
if (drawn_room == room_num) {
break;
}
}
if (i >= MAX_ROOMS_TO_DRAW) {
return;
}
if (i == RoomsToDrawNum) {
RoomsToDraw[RoomsToDrawNum++] = room_num;
if (i == DynArray_Size(RoomsToDraw)) {
DynArray_Append(RoomsToDraw, &room_num);
}
}

View file

@ -21,8 +21,8 @@ int32_t DrawPhaseCinematic()
S_InitialisePolyList();
S_ClearScreen();
CameraUnderwater = 0;
for (int i = 0; i < RoomsToDrawNum; i++) {
int32_t room_num = RoomsToDraw[i];
for (int i = 0; i < DynArray_Size(RoomsToDraw); i++) {
int16_t room_num = *(int16_t *)DynArray_Get(RoomsToDraw, i);
ROOM_INFO *r = &RoomInfo[room_num];
r->top = 0;
r->left = 0;
@ -61,8 +61,8 @@ void DrawRooms(int16_t current_room)
r->bottom = PhdBottom;
r->bound_active = 1;
RoomsToDrawNum = 0;
RoomsToDraw[RoomsToDrawNum++] = current_room;
DynArray_Reset(RoomsToDraw);
DynArray_Append(RoomsToDraw, &current_room);
CameraUnderwater = r->flags & RF_UNDERWATER;
@ -79,8 +79,8 @@ void DrawRooms(int16_t current_room)
phd_PopMatrix();
S_ClearScreen();
for (int i = 0; i < RoomsToDrawNum; i++) {
PrintRooms(RoomsToDraw[i]);
for (int i = 0; i < DynArray_Size(RoomsToDraw); i++) {
PrintRooms(*(int16_t *)DynArray_Get(RoomsToDraw, i));
}
if (Objects[O_LARA].loaded) {
@ -241,7 +241,7 @@ int32_t SetRoomBounds(int16_t *objptr, int16_t room_num, ROOM_INFO *parent)
}
if (!r->bound_active) {
RoomsToDraw[RoomsToDrawNum++] = room_num;
DynArray_Append(RoomsToDraw, &room_num);
r->bound_active = 1;
}
return 1;

View file

@ -31,7 +31,6 @@
#define MAX_LEVEL_NAME_LENGTH 48
#define NUM_SLOTS 8
#define MAX_ROOMS 1024
#define MAX_ROOMS_TO_DRAW 100
#define MAX_FRAMES 10
#define MAX_CD_TRACKS 64
#define MAX_TEXTURES 2048

View file

@ -95,11 +95,10 @@ int16_t *Cine;
int16_t NumCineFrames;
int16_t CineFrame;
PHD_3DPOS CinePosition;
int16_t RoomsToDraw[MAX_ROOMS_TO_DRAW];
int32_t RoomsToDrawNum;
int32_t NumberCameras;
int32_t NumberSoundEffects;
OBJECT_VECTOR *SoundEffectsTable;
DYNARRAY *RoomsToDraw = NULL; // array of int16_t
int16_t *TriggerIndex;
int32_t FlipTimer = 0;

View file

@ -1,6 +1,7 @@
#ifndef T1M_GLOBAL_VARS_H
#define T1M_GLOBAL_VARS_H
#include "dynarray.h"
#include "global/const.h"
#include "global/types.h"
#include "inject_util.h"
@ -181,11 +182,10 @@ extern int16_t *Cine;
extern int16_t NumCineFrames;
extern int16_t CineFrame;
extern PHD_3DPOS CinePosition;
extern int16_t RoomsToDraw[MAX_ROOMS_TO_DRAW];
extern int32_t RoomsToDrawNum;
extern int32_t NumberCameras;
extern int32_t NumberSoundEffects;
extern OBJECT_VECTOR *SoundEffectsTable;
extern DYNARRAY *RoomsToDraw; // array of int16_t
extern int16_t *TriggerIndex;
extern int32_t FlipTimer;

View file

@ -145,6 +145,11 @@ static int32_t LoadRooms(MYFILE *fp)
uint16_t count2;
uint32_t count4;
if (RoomsToDraw) {
DynArray_Free(RoomsToDraw);
}
RoomsToDraw = DynArray_Create(sizeof(int16_t));
FileRead(&RoomCount, sizeof(uint16_t), 1, fp);
LOG_INFO("%d rooms", RoomCount);
if (RoomCount > MAX_ROOMS) {

View file

@ -72,6 +72,8 @@ void S_InitialiseSystem()
SoundIsActive = 0;
}
RoomsToDraw = DynArray_Create(sizeof(int16_t));
CalculateWibbleTable();
GameMemorySize = MALLOC_SIZE;