Skip to content

Commit

Permalink
Modernize: Move some arrays to std::array
Browse files Browse the repository at this point in the history
Also replaces a macro with a template function
  • Loading branch information
GravisZro committed Jun 1, 2024
1 parent 5932087 commit 3332984
Show file tree
Hide file tree
Showing 27 changed files with 113 additions and 86 deletions.
4 changes: 2 additions & 2 deletions Descent3/Game2DLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ static void DUMMYrend_DrawScaledBitmap(int x1, int y1, int x2, int y2, int bm, f

void GetGameAPI(game_api *api) {
api->objs = (int *)Objects;
api->rooms = (int *)Rooms;
api->rooms = (int *)std::data(Rooms);
api->terrain = (int *)Terrain_seg;
api->players = (int *)Players;
api->netgame = (int *)&Netgame;
api->netplayers = (int *)&NetPlayers;
api->ships = (int *)&Ships;
api->weapons = (int *)&Weapons;
api->Current_mission = (int *)&Current_mission;
api->GameTextures = (int *)GameTextures;
api->GameTextures = (int *)std::data(GameTextures);
api->GameVClips = (int *)GameVClips;
// Fill in function pointers here. The order here must match the order on the
// DLL side
Expand Down
8 changes: 4 additions & 4 deletions Descent3/LoadLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2499,7 +2499,7 @@ int ReadRoom(CFILE *ifile, room *rp, int version) {

// Check for bad normal
if (!t) {
mprintf(1, "WARNING: Room %d face %d has bad normal!\n", rp - Rooms, i);
mprintf(1, "WARNING: Room %d face %d has bad normal!\n", rp - std::data(Rooms), i);
}
}

Expand Down Expand Up @@ -5105,7 +5105,7 @@ int SaveLevel(char *filename, bool f_save_room_AABB) {
room *rp;
int nrooms = 0, nverts = 0, nfaces = 0, nfaceverts = 0, nportals = 0;
extern int CountRoomFaceVerts(room * rp); // TEMP: move to room.h
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of faces, verts, etc. in the level
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) // Count the number of faces, verts, etc. in the level
if (rp->used) {
nrooms++;
nverts += rp->num_verts;
Expand All @@ -5131,13 +5131,13 @@ int SaveLevel(char *filename, bool f_save_room_AABB) {
EndChunk(ofile, chunk_start_pos);

// Write room wind, if any rooms have wind
for (i = nrooms = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) // Count the number of rooms with wind
for (i = nrooms = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) // Count the number of rooms with wind
if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0))
nrooms++;
if (nrooms) {
chunk_start_pos = StartChunk(ofile, CHUNK_ROOM_WIND);
cf_WriteInt(ofile, nrooms);
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) { // write the wind values
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) { // write the wind values
if ((rp->wind.x != 0.0) || (rp->wind.y != 0.0) || (rp->wind.z != 0.0)) {
cf_WriteShort(ofile, i);
cf_WriteVector(ofile, &rp->wind);
Expand Down
2 changes: 1 addition & 1 deletion Descent3/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3800,7 +3800,7 @@ void MakeAtuoWaypointList() {
Num_waypoints = 0;

// Clear room waypoint flags
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++)
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++)
rp->flags &= ~RF_WAYPOINT;

// Look through objects for waypoints
Expand Down
2 changes: 1 addition & 1 deletion Descent3/damage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ void BreakGlassFace(room *rp, int facenum, vector *hitpnt, vector *hitvec) {
MultiSendBreakGlass(rp, facenum);

if (Num_broke_glass != MAX_BROKE_GLASS) {
Broke_glass_rooms[Num_broke_glass] = rp - Rooms;
Broke_glass_rooms[Num_broke_glass] = rp - std::data(Rooms);
Broke_glass_faces[Num_broke_glass] = facenum;
Num_broke_glass++;
}
Expand Down
2 changes: 1 addition & 1 deletion Descent3/door.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
// ---------------------------------------------------------------------------
// Globals

door Doors[MAX_DOORS]; // door info.
std::array<door, MAX_DOORS> Doors; // door info.
int Num_doors = 0;

// ---------------------------------------------------------------------------
Expand Down
4 changes: 3 additions & 1 deletion Descent3/door.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
#ifndef DOOR_H
#define DOOR_H

#include <array>

#ifdef NEWEDITOR /* only include tablefile header (manage stuff for NEWEDITOR) */
#include "..\neweditor\ned_TableFile.h"
#include "..\neweditor\ned_Door.h"
Expand Down Expand Up @@ -142,7 +144,7 @@ typedef struct {
// The max number of predefined doors
#define MAX_DOORS 60
extern int Num_doors; // number of actual doors in game.
extern door Doors[];
extern std::array<door, MAX_DOORS> Doors;

// Sets all doors to unused
void InitDoors();
Expand Down
4 changes: 2 additions & 2 deletions Descent3/doorway.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void DoorwayDeactivateAll() {
room *rp;

// Go through all rooms and deactivate doors
for (r = 0, rp = Rooms; r <= Highest_room_index; r++) {
for (r = 0, rp = std::data(Rooms); r <= Highest_room_index; r++) {
if (rp->used && (rp->flags & RF_DOOR)) {
if (rp->doorway_data->state != DOORWAY_STOPPED) {
doorway *dp = rp->doorway_data;
Expand Down Expand Up @@ -629,7 +629,7 @@ void DoorwayRebuildActiveList() {
Num_active_doorways = 0;

// Go through all rooms and look for active doors
for (r = 0, rp = Rooms; r <= Highest_room_index; r++) {
for (r = 0, rp = std::data(Rooms); r <= Highest_room_index; r++) {
if (rp->used && (rp->flags & RF_DOOR)) {
ASSERT(rp->doorway_data != NULL);
if (rp->doorway_data->state != DOORWAY_STOPPED)
Expand Down
2 changes: 1 addition & 1 deletion Descent3/fireball.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ void CreateSplintersFromBody(object *obj, float explosion_mag, float lifetime) {
dest = center + obj->pos;

// Now create splinter with that faces center position
int s_objnum = ObjCreate(OBJ_SPLINTER, pm - Poly_models, obj->roomnum, &dest, NULL);
int s_objnum = ObjCreate(OBJ_SPLINTER, pm - Poly_models.data(), obj->roomnum, &dest, NULL);
if (s_objnum < 0) {
mprintf(0, "Couldn't create splinter object!\n");
return;
Expand Down
31 changes: 16 additions & 15 deletions Descent3/gamesave.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -823,16 +823,17 @@ bool SaveGameState(const char *pathname, const char *description) {

//////////////////////////////////////////////////////////////////////////////

#define SAVE_DATA_TABLE(_nitems, _array) \
do { \
highest_index = -1; \
for (i = 0; i < (_nitems); i++) \
if (_array[i].used) \
highest_index = i; \
gs_WriteShort(fp, highest_index + 1); \
for (i = 0; i <= highest_index; i++) \
cf_WriteString(fp, _array[i].used ? _array[i].name : ""); \
} while (0)
template <typename T, size_t sz>
void save_data_table(CFILE *fp, const std::array<T, sz>& data) {
int16_t highest_index = -1;
for (int16_t i = 0; i < sz; i++)
if (data[i].used)
highest_index = i;

gs_WriteShort(fp, highest_index + 1);
for (int16_t i = 0; i <= highest_index; i++)
cf_WriteString(fp, data[i].used ? data[i].name : "");
}

// writes out translation tables.
void SGSXlateTables(CFILE *fp) {
Expand All @@ -850,19 +851,19 @@ void SGSXlateTables(CFILE *fp) {
cf_WriteString(fp, (Object_info[i].type != OBJ_NONE) ? Object_info[i].name : "");

// write out polymodel list.
SAVE_DATA_TABLE(MAX_POLY_MODELS, Poly_models);
save_data_table(fp, Poly_models);

// save out door list
SAVE_DATA_TABLE(MAX_DOORS, Doors);
save_data_table(fp, Doors);

// save out ship list
SAVE_DATA_TABLE(MAX_SHIPS, Ships);
save_data_table(fp, Ships);

// save out weapons list
SAVE_DATA_TABLE(MAX_WEAPONS, Weapons);
save_data_table(fp, Weapons);

// save out textures list
SAVE_DATA_TABLE(MAX_TEXTURES, GameTextures);
save_data_table(fp, GameTextures);

// write out bitmap handle list. look at all Objects that are fireballs and
// save their handles-names.
Expand Down
2 changes: 1 addition & 1 deletion Descent3/gamesequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1743,7 +1743,7 @@ void StartTextureSounds() {
room *rp;
face *fp;

for (r = 0, rp = Rooms; r <= Highest_room_index; r++, rp++) {
for (r = 0, rp = std::data(Rooms); r <= Highest_room_index; r++, rp++) {
if (rp->used) {
for (f = 0, fp = rp->faces; f < rp->num_faces; f++, fp++) {
int sound = GameTextures[fp->tmap].sound;
Expand Down
2 changes: 1 addition & 1 deletion Descent3/gametexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@

// TODO: MTS: this is only used in this file.
int Num_textures = 0;
texture GameTextures[MAX_TEXTURES];
std::array<texture, MAX_TEXTURES> GameTextures;

extern bool Mem_superlow_memory_mode;

Expand Down
3 changes: 2 additions & 1 deletion Descent3/gametexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@

#ifndef GAMETEXTURE_H
#define GAMETEXTURE_H
#include <array>

#ifdef NEWEDITOR /* only include tablefile header (manage stuff for NEWEDITOR) */
#include "..\neweditor\ned_TableFile.h"
Expand Down Expand Up @@ -308,7 +309,7 @@ typedef struct {

} texture;

extern texture GameTextures[MAX_TEXTURES];
extern std::array<texture, MAX_TEXTURES> GameTextures;
extern int Num_textures;

// Inits the texture system, returning 1 if successful
Expand Down
2 changes: 1 addition & 1 deletion Descent3/multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9132,7 +9132,7 @@ void MultiSendBreakGlass(room *rp, int facenum) {
int size_offset;
size_offset = START_DATA(MP_BREAK_GLASS, data, &count);

MultiAddUshort(rp - Rooms, data, &count);
MultiAddUshort(rp - std::data(Rooms), data, &count);
MultiAddUshort(facenum, data, &count);

END_DATA(count, data, size_offset);
Expand Down
4 changes: 2 additions & 2 deletions Descent3/multi_dll_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,12 @@ void GetMultiAPI(multi_api *api) {
// make the compiler happy
bool (*fp_PlayerIsShipAllowed)(int, int) = PlayerIsShipAllowed;
api->objs = (int *)Objects;
api->rooms = (int *)Rooms;
api->rooms = (int *)std::data(Rooms);
api->terrain = (int *)Terrain_seg;
api->players = (int *)Players;
api->netgame = (int *)&Netgame;
api->netplayers = (int *)&NetPlayers;
api->ships = (int *)Ships;
api->ships = (int *)Ships.data();
// Fill in function pointers here. The order here must match the order on the
// DLL side

Expand Down
34 changes: 17 additions & 17 deletions Descent3/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ void BuildRoomList(int start_room_num) {
if (Render_all_external_rooms) {
int i;
room *rp;
for (i = 0, rp = Rooms; i <= Highest_room_index; i++, rp++) {
for (i = 0, rp = std::data(Rooms); i <= Highest_room_index; i++, rp++) {
if (rp->used && (rp->flags & RF_EXTERNAL)) {
for (int t = 0; t < rp->num_faces; t++)
rp->faces[t].flags |= FF_VISIBLE;
Expand Down Expand Up @@ -1955,7 +1955,7 @@ void RenderFace(room *rp, int facenum) {
if (TSearch_on) {
if (rend_GetPixel(TSearch_x, TSearch_y) != oldcolor) {
TSearch_found_type = TSEARCH_FOUND_MINE;
TSearch_seg = rp - Rooms;
TSearch_seg = rp - std::data(Rooms);
TSearch_face = facenum;
}
}
Expand All @@ -1966,7 +1966,7 @@ void RenderFace(room *rp, int facenum) {
((fp->flags & FF_CORONA) || FastCoronas) && (fp->flags & FF_LIGHTMAP) && UseHardware &&
(GameTextures[fp->tmap].flags & TF_LIGHT)) {
if (Num_glows_this_frame < MAX_LIGHT_GLOWS && Detail_settings.Coronas_enabled) {
LightGlowsThisFrame[Num_glows_this_frame].roomnum = rp - Rooms;
LightGlowsThisFrame[Num_glows_this_frame].roomnum = rp - std::data(Rooms);
LightGlowsThisFrame[Num_glows_this_frame].facenum = facenum;
Num_glows_this_frame++;
}
Expand Down Expand Up @@ -2207,7 +2207,7 @@ void SetupRoomFog(room *rp, vector *eye, matrix *orient, int viewer_room) {
return;
}

if (viewer_room == (rp - Rooms)) {
if (viewer_room == (rp - std::data(Rooms))) {
// viewer is in the room
vector *vec = eye;
Room_fog_plane_check = 1;
Expand All @@ -2219,7 +2219,7 @@ void SetupRoomFog(room *rp, vector *eye, matrix *orient, int viewer_room) {
// find the 'fogroom' number (we should have put it in here if we will render the room)
int found_room = -1;
for (int i = 0; i < Num_fogged_rooms_this_frame && found_room == -1; i++) {
if (Fog_portal_data[i].roomnum == rp - Rooms) {
if (Fog_portal_data[i].roomnum == rp - std::data(Rooms)) {
found_room = i;
break;
}
Expand Down Expand Up @@ -2321,7 +2321,7 @@ void RenderRoomUnsorted(room *rp) {
face_depth[fn] += World_point_buffer[rp->wpb_index + fp->face_verts[vn]].p3_z;
}
Postrender_list[Num_postrenders].type = PRT_WALL;
Postrender_list[Num_postrenders].roomnum = rp - Rooms;
Postrender_list[Num_postrenders].roomnum = rp - std::data(Rooms);
Postrender_list[Num_postrenders].facenum = fn;
Postrender_list[Num_postrenders++].z = face_depth[fn] /= fp->num_verts;
;
Expand Down Expand Up @@ -2388,12 +2388,12 @@ void ComputeRoomPulseLight(room *rp) {
}
if (!In_editor_mode) {
if (rp->flags & RF_STROBE) {
int val = (Gametime * 10) + (rp - Rooms);
int val = (Gametime * 10) + (rp - std::data(Rooms));
if (val % 2)
Room_light_val = 0;
}
if (rp->flags & RF_FLICKER) {
ps_srand((Gametime * 1000) + (rp - Rooms));
ps_srand((Gametime * 1000) + (rp - std::data(Rooms)));
if (ps_rand() % 2)
Room_light_val = 0;
}
Expand Down Expand Up @@ -2575,7 +2575,7 @@ void CheckLightGlowsForRoom(room *rp) {
// shoot a ray from the light position to the current vertex
if (FastCoronas) {
if (rp->flags & RF_EXTERNAL) {
SetGlowStatus(rp - Rooms, LightGlowsThisFrame[i].facenum, &center, size, FastCoronas);
SetGlowStatus(rp - std::data(Rooms), LightGlowsThisFrame[i].facenum, &center, size, FastCoronas);
continue;
}
vector subvec = Viewer_eye - center;
Expand All @@ -2584,7 +2584,7 @@ void CheckLightGlowsForRoom(room *rp) {
subvec += center;
fq.p0 = &center;
fq.p1 = &subvec;
fq.startroom = rp - Rooms;
fq.startroom = rp - std::data(Rooms);
} else {
fq.p0 = &Viewer_eye;
fq.p1 = &center;
Expand All @@ -2598,7 +2598,7 @@ void CheckLightGlowsForRoom(room *rp) {
int fate = fvi_FindIntersection(&fq, &hit_info);
if (fate != HIT_NONE)
continue;
SetGlowStatus(rp - Rooms, LightGlowsThisFrame[i].facenum, &center, size, FastCoronas);
SetGlowStatus(rp - std::data(Rooms), LightGlowsThisFrame[i].facenum, &center, size, FastCoronas);
}
}
// Called before a frame starts to render - sets all of our light glows to decreasing
Expand Down Expand Up @@ -2931,11 +2931,11 @@ void RenderMirroredRoom(room *rp) {
RotateRoomPoints(rp, mirror_dest_vecs);

// Mark facing faces
int save_frame = Facing_visited[rp - Rooms];
Facing_visited[rp - Rooms] = 0;
int save_frame = Facing_visited[rp - std::data(Rooms)];
Facing_visited[rp - std::data(Rooms)] = 0;

MarkFacingFaces(rp - Rooms, mirror_dest_vecs);
Facing_visited[rp - Rooms] = save_frame;
MarkFacingFaces(rp - std::data(Rooms), mirror_dest_vecs);
Facing_visited[rp - std::data(Rooms)] = save_frame;
// Render the mirror room
rend_SetColorModel(CM_MONO);
rend_SetLighting(LS_GOURAUD);
Expand Down Expand Up @@ -2973,7 +2973,7 @@ void RenderRoom(room *rp) {
ComputeRoomPulseLight(rp);

// Mark it visible for automap
AutomapVisMap[rp - Rooms] = 1;
AutomapVisMap[rp - std::data(Rooms)] = 1;

#ifdef EDITOR
if (!UseHardware) {
Expand Down Expand Up @@ -3353,7 +3353,7 @@ void RenderMirrorRooms() {
if (do_mirror_face) // This room has a mirror...render it first
{
Render_mirror_for_room = true;
Mirror_room = rp - Rooms;
Mirror_room = rp - std::data(Rooms);

BuildMirroredRoomList();
for (int t = Num_mirrored_rooms - 1; t >= 0; t--)
Expand Down
Loading

0 comments on commit 3332984

Please sign in to comment.