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 77ffd76
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Descent3/Game2DLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void GetGameAPI(game_api *api) {
api->ships = (int *)&Ships;
api->weapons = (int *)&Weapons;
api->Current_mission = (int *)&Current_mission;
api->GameTextures = (int *)GameTextures;
api->GameTextures = (int *)GameTextures.data();
api->GameVClips = (int *)GameVClips;
// Fill in function pointers here. The order here must match the order on the
// DLL side
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
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/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_dll_mgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ void GetMultiAPI(multi_api *api) {
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
2 changes: 1 addition & 1 deletion Descent3/ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@

#define DEFAULT_SHIP_SIZE 4.0

ship Ships[MAX_SHIPS];
std::array<ship, MAX_SHIPS> Ships;
int Num_ships = 0;

// There are no static ships
Expand Down
4 changes: 3 additions & 1 deletion Descent3/ship.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
#ifndef SHIP_H
#define SHIP_H

#include <array>

#include "pstypes.h"
#include "manage.h"
#include "object.h"
Expand Down Expand Up @@ -174,7 +176,7 @@ typedef struct {
} ship;

extern int Num_ships;
extern ship Ships[MAX_SHIPS];
extern std::array<ship, MAX_SHIPS> Ships;

extern const char *AllowedShips[];

Expand Down
2 changes: 1 addition & 1 deletion Descent3/weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
// #include "samirlog.h"
#define LOGFILE(_s)

weapon Weapons[MAX_WEAPONS];
std::array<weapon, MAX_WEAPONS> Weapons;
int Num_weapons = 0;

const char *Static_weapon_names[] = {
Expand Down
4 changes: 3 additions & 1 deletion Descent3/weapon.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@
#ifndef WEAPON_H
#define WEAPON_H

#include <array>

#include "pstypes.h"
#include "manage.h"
#include "object.h"
Expand Down Expand Up @@ -325,7 +327,7 @@ typedef struct {
extern float Primary_ramp_time, Secondary_ramp_time;

extern int Num_weapons;
extern weapon Weapons[MAX_WEAPONS];
extern std::array<weapon, MAX_WEAPONS> Weapons;
extern const char *Static_weapon_names[];
extern int Static_weapon_names_msg[];
extern int Static_weapon_ckpt_names[][2];
Expand Down
6 changes: 4 additions & 2 deletions lib/polymodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@
#ifndef POLYMODEL_H
#define POLYMODEL_H

#include <array>

#include "manage.h"
#include "pstypes.h"
#include "vecmat.h"
Expand All @@ -311,7 +313,7 @@
#define SOF_MONITOR_MASK 0x0ff0 // mask for monitors

extern int Num_poly_models;
extern poly_model Poly_models[];
extern std::array<poly_model, MAX_POLY_MODELS> Poly_models;

extern int Polymodel_use_effect;
extern polymodel_effect Polymodel_effect;
Expand All @@ -326,7 +328,7 @@ extern lightmap_object *Polylighting_lightmap_object;

extern vector Model_eye_position;
extern vector Interp_pos_instance_vec;
extern g3Point Robot_points[];
extern std::array<g3Point, MAX_POLYGON_VECS> Robot_points;

// Flag to draw an outline around the faces
extern bool Polymodel_outline_mode;
Expand Down
4 changes: 2 additions & 2 deletions model/newstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ inline void RenderSubmodelFace(poly_model *pm, bsp_info *sm, int facenum) {

// If there is a texture, set it up
if (texp) {
bm_handle = GetTextureBitmap(texp - GameTextures, 0);
bm_handle = GetTextureBitmap(texp - GameTextures.data(), 0);

rend_SetTextureType(TT_LINEAR);
if (Polymodel_light_type == POLYMODEL_LIGHTING_GOURAUD)
Expand All @@ -323,7 +323,7 @@ inline void RenderSubmodelFace(poly_model *pm, bsp_info *sm, int facenum) {

// Setup custom color if there is one
if (Polymodel_use_effect && (Polymodel_effect.type & PEF_CUSTOM_COLOR) &&
(texp - GameTextures) == Multicolor_texture) {
(texp - GameTextures.data()) == Multicolor_texture) {
int r, g, b;

rend_SetLighting(LS_FLAT_GOURAUD);
Expand Down
9 changes: 5 additions & 4 deletions model/polymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@
* $NoKeywords: $
*/


#include "objinfo.h"
#include "polymodel.h"
#include "pserror.h"
Expand All @@ -619,12 +620,12 @@
#include <algorithm>

int Num_poly_models = 0;
poly_model Poly_models[MAX_POLY_MODELS];
std::array<poly_model, MAX_POLY_MODELS> Poly_models;

g3Point Robot_points[MAX_POLYGON_VECS];
std::array<g3Point, MAX_POLYGON_VECS> Robot_points;

vector Interp_pos_instance_vec = {0, 0, 0};
vector Instance_vec_stack[MAX_SUBOBJECTS];
std::array<vector, MAX_SUBOBJECTS> Instance_vec_stack;
int Instance_vec_cnt = 0;

#ifdef _DEBUG
Expand Down Expand Up @@ -2055,7 +2056,7 @@ int ReadNewModelFile(int polynum, CFILE *infile) {
if (pm->n_models > MAX_SUBOBJECTS) {
mprintf(0, "This model has more than the max number of subobjects! (%d)\n", MAX_SUBOBJECTS);
Int3();
FreePolyModel(pm - Poly_models);
FreePolyModel(pm - Poly_models.data());
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion physics/Collide.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ void DoWallEffects(object *weapon, int surface_tmap) {
} else if (texp->flags & TF_RUBBLE) {
if ((ps_rand() % 4) == 0) {
int num_rubble = (ps_rand() % 3) + 1;
int bm_handle = GetTextureBitmap(texp - GameTextures, 0);
int bm_handle = GetTextureBitmap(texp - GameTextures.data(), 0);
uint16_t *data = bm_data(bm_handle, 0);

uint16_t color = data[(bm_w(bm_handle, 0) * (bm_h(bm_handle, 0) / 2)) + (bm_w(bm_handle, 0) / 2)];
Expand Down

0 comments on commit 77ffd76

Please sign in to comment.