Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion code/cfile/cfilesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2480,7 +2480,7 @@ void cfile_spew_pack_file_crcs()
my_time = time(NULL);

memset( datetime, 0, sizeof(datetime) );
snprintf(datetime, sizeof(datetime)-1, "%s", ctime(&my_time));
snprintf(datetime, sizeof(datetime), "%s", ctime(&my_time));
// ctime() adds a newline char, so we have to strip it off
datetime[strlen(datetime)-1] = '\0';

Expand Down
18 changes: 9 additions & 9 deletions code/fireball/fireballs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,45 +107,45 @@ void fireball_play_warphole_close_sound(fireball *fb)
snd_play_3d(gamesnd_get_game_sound(sound_index), &fireball_objp->pos, &Eye_position, fireball_objp->radius, NULL, 0, 1.0F, SND_PRIORITY_SINGLE_INSTANCE, NULL, fb->warp_sound_range_multiplier); // play warp sound effect
}

static void fireball_generate_unique_id(char *unique_id, int buffer_len, int fireball_index)
static void fireball_generate_unique_id(char *unique_id, size_t buffer_size, int fireball_index)
{
Assertion(SCP_vector_inbounds(Fireball_info, fireball_index), "fireball_index is out of bounds!");

switch (fireball_index)
{
// use sensible names for the fireball.tbl default entries
case FIREBALL_EXPLOSION_MEDIUM:
strncpy(unique_id, "Medium Explosion", buffer_len);
strncpy(unique_id, "Medium Explosion", buffer_size-1);
break;

case FIREBALL_WARP:
strncpy(unique_id, "Warp Effect", buffer_len);
strncpy(unique_id, "Warp Effect", buffer_size-1);
break;

case FIREBALL_KNOSSOS:
strncpy(unique_id, "Knossos Effect", buffer_len);
strncpy(unique_id, "Knossos Effect", buffer_size-1);
break;

case FIREBALL_ASTEROID:
strncpy(unique_id, "Asteroid Explosion", buffer_len);
strncpy(unique_id, "Asteroid Explosion", buffer_size-1);
break;

case FIREBALL_EXPLOSION_LARGE1:
strncpy(unique_id, "Large Explosion 1", buffer_len);
strncpy(unique_id, "Large Explosion 1", buffer_size-1);
break;

case FIREBALL_EXPLOSION_LARGE2:
strncpy(unique_id, "Large Explosion 2", buffer_len);
strncpy(unique_id, "Large Explosion 2", buffer_size-1);
break;

// base the id on the index
default:
snprintf(unique_id, buffer_len, "Custom Fireball %d", fireball_index - NUM_DEFAULT_FIREBALLS + 1);
snprintf(unique_id, buffer_size, "Custom Fireball %d", fireball_index - NUM_DEFAULT_FIREBALLS + 1);
break;
}

// null-terminate
unique_id[buffer_len - 1] = '\0';
unique_id[buffer_size-1] = '\0';
}

/**
Expand Down
10 changes: 10 additions & 0 deletions code/globalincs/vmallocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ class SCP_vector : public std::vector<T, std::allocator<T>>
return std::find(this->begin(), this->end(), item) != this->end();
}

void concat(SCP_vector<T>&& other)
{
insert(this->end(), std::make_move_iterator(other.begin()), std::make_move_iterator(other.end()));
}

void concat(const SCP_vector<T>& other)
{
insert(this->end(), other.begin(), other.end());
}

bool in_bounds(int idx) const
{
return (idx >= 0) && (static_cast<size_t>(idx) < this->size());
Expand Down
4 changes: 2 additions & 2 deletions code/graphics/opengl/gropengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ void gr_opengl_print_screen(const char *filename)
GLuint pbo = 0;

// save to a "screenshots" directory and tack on the filename
snprintf(tmp, MAX_PATH_LEN-1, "screenshots/%s.png", filename);
snprintf(tmp, MAX_PATH_LEN, "screenshots/%s.png", filename);

_mkdir(os_get_config_path("screenshots").c_str());

Expand Down Expand Up @@ -436,7 +436,7 @@ void gr_opengl_dump_envmap(const char* filename)
glBindTexture(sphere_tex->texture_target, sphere_tex->texture_id);
pixels = (GLubyte*)vm_malloc(sphere_width * sphere_height * 4, memory::quiet_alloc);
glGetTexImage(sphere_tex->texture_target, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
snprintf(tmp, MAX_PATH_LEN - 1, "envmaps/%s.png", filename);
snprintf(tmp, MAX_PATH_LEN, "envmaps/%s.png", filename);
if (!png_write_bitmap(os_get_config_path(tmp).c_str(), 4 * width, 2 * height, true, pixels)) {
ReleaseWarning(LOCATION, "Failed to write envmap to \"%s\".", os_get_config_path(tmp).c_str());
}
Expand Down
8 changes: 4 additions & 4 deletions code/graphics/software/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ void gr_printf(int x, int y, const char * format, ...)
if (!FontManager::isReady()) return;

va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';

Expand All @@ -791,7 +791,7 @@ void gr_printf_menu(int x, int y, const char * format, ...)
if (!FontManager::isReady()) return;

va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';

Expand All @@ -805,7 +805,7 @@ void gr_printf_menu_zoomed(int x, int y, const char * format, ...)
if (!FontManager::isReady()) return;

va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';

Expand All @@ -819,7 +819,7 @@ void gr_printf_no_resize(int x, int y, const char * format, ...)
if (!FontManager::isReady()) return;

va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';

Expand Down
14 changes: 6 additions & 8 deletions code/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,30 +987,28 @@ void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char

void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char* format, ...)
{
char tmp[256] = "";
SCP_string tmp;
va_list args;

// format the text
va_start(args, format);
vsnprintf(tmp, sizeof(tmp)-1, format, args);
vsprintf(tmp, format, args);
va_end(args);
tmp[sizeof(tmp)-1] = '\0';

renderString(x, y, tmp, scale, config);
renderString(x, y, tmp.c_str(), scale, config);
}

void HudGauge::renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, const char* format, ...)
{
char tmp[256] = "";
SCP_string tmp;
va_list args;

// format the text
va_start(args, format);
vsnprintf(tmp, sizeof(tmp)-1, format, args);
vsprintf(tmp, format, args);
va_end(args);
tmp[sizeof(tmp)-1] = '\0';

renderString(x, y, gauge_id, tmp, scale, config);
renderString(x, y, gauge_id, tmp.c_str(), scale, config);
}

void HudGauge::renderBitmapColor(int frame, int x, int y, float scale, bool config) const
Expand Down
70 changes: 36 additions & 34 deletions code/hud/hudmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ static SCP_vector<line_node> Msg_scrollback_lines;

typedef struct HUD_ft {
int end_time; // Timestamp at which this message will go away.
char text[MAX_HUD_LINE_LEN]; // Text to display.
char text[MAX_HUD_LINE_BUF]; // Text to display.
int color; // 0rgb color, 8 bit fields.
} HUD_ft;

Expand Down Expand Up @@ -487,8 +487,6 @@ void HudGaugeMessages::render(float /*frametime*/, bool config)
void HUD_fixed_printf(float duration, color col, const char *format, ...)
{
va_list args;
char tmp[HUD_MSG_LENGTH_MAX];
size_t msg_length;

// make sure we only print these messages if we're in the correct state
if((Game_mode & GM_MULTIPLAYER) && (Netgame.game_state != NETGAME_STATE_IN_MISSION)){
Expand All @@ -497,22 +495,9 @@ void HUD_fixed_printf(float duration, color col, const char *format, ...)
}

va_start(args, format);
vsnprintf(tmp, sizeof(tmp)-1, format, args);
vsnprintf(HUD_fixed_text[0].text, MAX_HUD_LINE_BUF, format, args);
va_end(args);
tmp[sizeof(tmp)-1] = '\0';

msg_length = strlen(tmp);

if ( !msg_length ) {
nprintf(("Warning", "HUD_fixed_printf ==> attempt to print a 0 length string in msg window\n"));
return;

} else if (msg_length > MAX_HUD_LINE_LEN - 1){
nprintf(("Warning", "HUD_fixed_printf ==> Following string truncated to %d chars: %s\n", MAX_HUD_LINE_LEN - 1, tmp));
tmp[MAX_HUD_LINE_LEN-1] = '\0';
}

strcpy_s(HUD_fixed_text[0].text, tmp);
HUD_fixed_text[0].text[MAX_HUD_LINE_BUF-1] = '\0';

if (duration == 0.0f){
HUD_fixed_text[0].end_time = timestamp(-1);
Expand Down Expand Up @@ -544,7 +529,7 @@ int HUD_source_get_team(int source)
void HUD_printf(const char *format, ...)
{
va_list args;
char tmp[HUD_MSG_LENGTH_MAX];
SCP_string tmp;

// make sure we only print these messages if we're in the correct state
if((Game_mode & GM_MULTIPLAYER) && (Net_player->state != NETPLAYER_STATE_IN_MISSION)){
Expand All @@ -553,9 +538,8 @@ void HUD_printf(const char *format, ...)
}

va_start(args, format);
vsnprintf(tmp, sizeof(tmp)-1, format, args);
vsprintf(tmp, format, args);
va_end(args);
tmp[sizeof(tmp)-1] = '\0';

hud_sourced_print(HUD_SOURCE_COMPUTER, tmp);
}
Expand All @@ -570,7 +554,7 @@ void HUD_printf(const char *format, ...)
void HUD_sourced_printf(int source, const char *format, ...)
{
va_list args;
char tmp[HUD_MSG_LENGTH_MAX];
SCP_string tmp;

// make sure we only print these messages if we're in the correct state
if((Game_mode & GM_MULTIPLAYER) && (Net_player->state != NETPLAYER_STATE_IN_MISSION)){
Expand All @@ -579,16 +563,40 @@ void HUD_sourced_printf(int source, const char *format, ...)
}

va_start(args, format);
vsnprintf(tmp, sizeof(tmp)-1, format, args);
vsprintf(tmp, format, args);
va_end(args);
tmp[sizeof(tmp)-1] = '\0';

hud_sourced_print(source, tmp);
}

void hud_sourced_print(int source, const SCP_string &msg)
{
if ( msg.empty() ) {
nprintf(("Warning", "HUD ==> attempt to print a 0 length string in msg window\n"));
return;
}

// add message to the scrollback log first
hud_add_msg_to_scrollback(msg.c_str(), source, Missiontime);

HUD_message_data new_msg;

new_msg.text = msg;
new_msg.source = source;
new_msg.x = 0;

HUD_msg_buffer.push_back(new_msg);

// Invoke the scripting hook
if (OnHudMessageReceivedHook->isActive()) {
OnHudMessageReceivedHook->run(scripting::hook_param_list(scripting::hook_param("Text", 's', msg.c_str()),
scripting::hook_param("SourceType", 'i', source)));
}
}

void hud_sourced_print(int source, const char *msg)
{
if ( !strlen(msg) ) {
if ( !*msg ) {
nprintf(("Warning", "HUD ==> attempt to print a 0 length string in msg window\n"));
return;
}
Expand Down Expand Up @@ -643,21 +651,15 @@ void HUD_add_to_scrollback(const char *text, int source)

void hud_add_msg_to_scrollback(const char *text, int source, int t)
{
size_t msg_len = strlen(text);
if (msg_len == 0)
if (!*text)
return;

Assert(msg_len < HUD_MSG_LENGTH_MAX);

char buf[HUD_MSG_LENGTH_MAX], *ptr;
strcpy_s(buf, text);
ptr = strstr(buf, NOX(": "));

int w = 0;

// determine the length of the sender's name for underlining
auto ptr = strstr(text, NOX(": "));
if (ptr) {
gr_get_string_size(&w, nullptr, buf, 1.0f, (ptr - buf));
gr_get_string_size(&w, nullptr, text, 1.0f, (ptr - text));
}

// create the new node for the vector
Expand Down
4 changes: 2 additions & 2 deletions code/hud/hudmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "graphics/generic.h"
#include "hud/hud.h"

#define MAX_HUD_LINE_LEN 256 // maximum number of characters for a HUD message
#define MAX_HUD_LINE_BUF 256 // maximum size for a HUD message

// If these are changed, the lua 'addMessageToScrollback' method in mission.cpp should be updated.
#define HUD_SOURCE_COMPUTER 0
Expand Down Expand Up @@ -67,12 +67,12 @@ int HUD_team_get_source(int team);
int HUD_source_get_team(int team);
void HUD_printf(SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(1, 2);
void hud_sourced_print(int source, const char *msg);
void hud_sourced_print(int source, const SCP_string &msg);
void HUD_sourced_printf(int source, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3); // send hud message from specified source
void HUD_fixed_printf(float duration, color col, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4); // Display a single message for duration seconds.
void HUD_init_fixed_text(); // Clear all pending fixed text.

void HUD_add_to_scrollback(const char *text, int source);
void hud_add_line_to_scrollback(const char *text, int source, int t, int x, int y, int w);
void hud_add_msg_to_scrollback(const char *text, int source, int t);

class HudGaugeMessages: public HudGauge // HUD_MESSAGE_LINES
Expand Down
6 changes: 3 additions & 3 deletions code/lab/dialogs/lab_ui_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ SCP_string get_ship_table_text(ship_info* sip)
if (!stricmp(line2 + i, sip->name)) {
memset(file_text, 0, sizeof(file_text));
snprintf(file_text,
sizeof(file_text) - 1,
sizeof(file_text),
"-- %s -------------------------------\r\n",
tbl_file_names[n].c_str());
result += file_text;
Expand Down Expand Up @@ -269,7 +269,7 @@ SCP_string get_weapon_table_text(weapon_info* wip)
if (!stricmp(line2 + i, wip->name)) {
memset(file_text, 0, sizeof(file_text));
snprintf(file_text,
sizeof(file_text) - 1,
sizeof(file_text),
"-- %s -------------------------------\r\n",
tbl_file_names[n].c_str());
result += file_text;
Expand Down Expand Up @@ -391,7 +391,7 @@ SCP_string get_asteroid_table_text(const asteroid_info* aip)
if (!stricmp(line2 + i, aip->name)) {
memset(file_text, 0, sizeof(file_text));
snprintf(file_text,
sizeof(file_text) - 1,
sizeof(file_text),
"-- %s -------------------------------\r\n",
tbl_file_names[n].c_str());
result += file_text;
Expand Down
4 changes: 2 additions & 2 deletions code/network/multiui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4457,7 +4457,7 @@ void multi_create_list_load_missions()
Multi_create_mission_list.clear();

memset( wild_card, 0, sizeof(wild_card) );
snprintf(wild_card, sizeof(wild_card) - 1, "*%s", FS_MISSION_FILE_EXT);
snprintf(wild_card, sizeof(wild_card), "*%s", FS_MISSION_FILE_EXT);

file_list = (char**) vm_malloc( sizeof(char*) * 1024 );

Expand Down Expand Up @@ -4550,7 +4550,7 @@ void multi_create_list_load_campaigns()
Multi_create_campaign_list.clear();

memset( wild_card, 0, sizeof(wild_card) );
snprintf(wild_card, sizeof(wild_card) - 1, "*%s", FS_CAMPAIGN_FILE_EXT);
snprintf(wild_card, sizeof(wild_card), "*%s", FS_CAMPAIGN_FILE_EXT);

file_list = (char**) vm_malloc( sizeof(char*) * 1024 );

Expand Down
Loading
Loading