Skip to content
Open
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: 0 additions & 2 deletions code/globalincs/memory/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <cstddef>
#include <cstdlib>

#include "globalincs/pstypes.h"

namespace memory
{
struct quiet_alloc_t { quiet_alloc_t(){} };
Expand Down
17 changes: 9 additions & 8 deletions code/globalincs/memory/utils.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
#pragma once
#ifndef MEMORY_UTILS_H
#define MEMORY_UTILS_H

#include <cstring>

#include "globalincs/pstypes.h"

inline char *vm_strndup(const char *ptr, size_t size)
inline char *vm_strndup(const char *ptr, size_t len)
{
char *dst = (char *) vm_malloc(size + 1);
char *dst = static_cast<char *>(vm_malloc(len + 1));

if (!dst)
return NULL;
return nullptr;

std::strncpy(dst, ptr, size);
std::strncpy(dst, ptr, len);
// make sure it has a NULL terminiator
dst[size] = '\0';
dst[len] = '\0';

return dst;
}
Expand All @@ -24,3 +23,5 @@ inline char *vm_strdup(const char *ptr)

return vm_strndup(ptr, len);
}

#endif // MEMORY_UTILS_H
1 change: 1 addition & 0 deletions code/globalincs/toolchain/clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#if defined(__clang__)

#define SCP_FORMAT_STRING
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))

#define __UNUSED __attribute__((__unused__))
Expand Down
1 change: 1 addition & 0 deletions code/globalincs/toolchain/gcc.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#if defined(__GNUC__) && !defined(__clang__)

#define SCP_FORMAT_STRING
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(printf, x, y)))

#define __UNUSED __attribute__((__unused__))
Expand Down
1 change: 1 addition & 0 deletions code/globalincs/toolchain/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <stdio.h>

#define SCP_FORMAT_STRING
// from gcc: Since non-static C++ methods have an implicit this argument, the arguments of such methods should be counted from two, not one, when giving values for string-index and first-to-check.
#define SCP_FORMAT_STRING_ARGS(x,y) __attribute__((format(__MINGW_PRINTF_FORMAT, x, y)))

#define __UNUSED __attribute__((__unused__))
Expand Down
12 changes: 8 additions & 4 deletions code/graphics/2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,13 +1043,17 @@ bool gr_resize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL,
// Does formatted printing. This calls gr_string after formatting,
// so if you don't need to format the string, then call gr_string
// directly.
extern void gr_printf( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
// same as gr_printf but positions text correctly in menus
extern void gr_printf_menu( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_menu(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_menu(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
// same as gr_printf_menu but accounts for menu zooming
extern void gr_printf_menu_zoomed( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_menu_zoomed(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_menu_zoomed(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
// same as gr_printf but doesn't resize for non-standard resolutions
extern void gr_printf_no_resize( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_no_resize(int x, int y, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4);
extern void gr_printf_no_resize(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);

// Returns the size of the string in pixels in w and h
extern void gr_get_string_size( int *w, int *h, const char * text, float scaleMultiplier = 1.0f, size_t len = std::string::npos);
Expand Down
77 changes: 49 additions & 28 deletions code/graphics/software/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,60 +801,81 @@ void gr_string_win(int x, int y, char *s)

#endif // ifdef _WIN32

char grx_printf_text[2048];
static char grx_printf_text[2048];

void gr_printf(int x, int y, const char * format, ...)
void gr_printf_args(int resize_mode, int x, int y, size_t len, SCP_FORMAT_STRING const char *format, va_list args)
{
va_list args;
if (!FontManager::isReady())
return;

if (!FontManager::isReady()) return;
len = std::min(len, sizeof(grx_printf_text) - 1);

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

gr_string(x, y, grx_printf_text);
gr_string(x, y, grx_printf_text, resize_mode, 1.0f, len);
}

void gr_printf_menu(int x, int y, const char * format, ...)
void gr_printf(int x, int y, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;

if (!FontManager::isReady()) return;

va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
gr_printf_args(GR_RESIZE_FULL, x, y, std::string::npos, format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';

gr_string(x, y, grx_printf_text, GR_RESIZE_MENU);
}

void gr_printf_menu_zoomed(int x, int y, const char * format, ...)
void gr_printf(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
gr_printf_args(GR_RESIZE_FULL, x, y, len, format, args);
va_end(args);
}

if (!FontManager::isReady()) return;

void gr_printf_menu(int x, int y, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
gr_printf_args(GR_RESIZE_MENU, x, y, std::string::npos, format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
}

gr_string(x, y, grx_printf_text, GR_RESIZE_MENU_ZOOMED);
void gr_printf_menu(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
gr_printf_args(GR_RESIZE_MENU, x, y, len, format, args);
va_end(args);
}

void gr_printf_no_resize(int x, int y, const char * format, ...)
void gr_printf_menu_zoomed(int x, int y, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
gr_printf_args(GR_RESIZE_MENU_ZOOMED, x, y, std::string::npos, format, args);
va_end(args);
}

if (!FontManager::isReady()) return;
void gr_printf_menu_zoomed(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
gr_printf_args(GR_RESIZE_MENU_ZOOMED, x, y, len, format, args);
va_end(args);
}

void gr_printf_no_resize(int x, int y, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
gr_printf_args(GR_RESIZE_NONE, x, y, std::string::npos, format, args);
va_end(args);
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
}

gr_string(x, y, grx_printf_text, GR_RESIZE_NONE);
void gr_printf_no_resize(int x, int y, size_t len, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
va_start(args, format);
gr_printf_args(GR_RESIZE_NONE, x, y, len, format, args);
va_end(args);
}
41 changes: 28 additions & 13 deletions code/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,12 @@ void HudGauge::render(float /*frametime*/, bool config)
}
}

void HudGauge::renderString(int x, int y, const char *str, float scale, bool config)
void HudGauge::renderString(int x, int y, const char *str, float scale, bool config) const
{
renderString(x, y, str, std::string::npos, scale, config);
}

void HudGauge::renderString(int x, int y, const char *str, size_t len, float scale, bool config) const
{
int nx = 0, ny = 0;
int resize = GR_RESIZE_FULL;
Expand All @@ -923,15 +928,20 @@ void HudGauge::renderString(int x, int y, const char *str, float scale, bool con
if (HUD_shadows) {
color cur = gr_screen.current_color;
gr_set_color_fast(&Color_black);
gr_string(x + nx + 1, y + ny + 1, str, resize, scale);
gr_string(x + nx + 1, y + ny + 1, str, resize, scale, len);
gr_set_color_fast(&cur);
}

gr_string(x + nx, y + ny, str, resize, scale);
gr_string(x + nx, y + ny, str, resize, scale, len);
gr_reset_screen_scale();
}

void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float scale, bool config)
void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float scale, bool config) const
{
renderString(x, y, gauge_id, str, std::string::npos, scale, config);
}

void HudGauge::renderString(int x, int y, int gauge_id, const char *str, size_t len, float scale, bool config) const
{
int nx = 0, ny = 0;
int resize = GR_RESIZE_FULL;
Expand Down Expand Up @@ -960,32 +970,37 @@ void HudGauge::renderString(int x, int y, int gauge_id, const char *str, float s
if (HUD_shadows) {
color cur = gr_screen.current_color;
gr_set_color_fast(&Color_black);
emp_hud_string(x + nx + 1, y + ny + 1, gauge_id, str, resize, scale);
emp_hud_string(x + nx + 1, y + ny + 1, gauge_id, str, len, resize, scale);
gr_set_color_fast(&cur);
}
emp_hud_string(x + nx, y + ny, gauge_id, str, resize, scale);
emp_hud_string(x + nx, y + ny, gauge_id, str, len, resize, scale);
} else {
if (HUD_shadows) {
color cur = gr_screen.current_color;
gr_set_color_fast(&Color_black);
gr_string(x + nx + 1, y + ny + 1, str, resize, scale);
gr_string(x + nx + 1, y + ny + 1, str, resize, scale, len);
gr_set_color_fast(&cur);
}
gr_string(x + nx, y + ny, str, resize, scale);
gr_string(x + nx, y + ny, str, resize, scale, len);
}

gr_reset_screen_scale();
}

void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale, bool config)
void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale, bool config) const
{
renderStringAlignCenter(x, y, area_width, s, std::string::npos, scale, config);
}

void HudGauge::renderStringAlignCenter(int x, int y, int area_width, const char *s, size_t len, float scale, bool config) const
{
int w, h;

gr_get_string_size(&w, &h, s, scale);
renderString(x + ((area_width - w) / 2), y, s, scale, config);
gr_get_string_size(&w, &h, s, scale, len);
renderString(x + ((area_width - w) / 2), y, s, len, scale, config);
}

void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char* format, ...)
void HudGauge::renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const
{
SCP_string tmp;
va_list args;
Expand All @@ -998,7 +1013,7 @@ void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char*
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, ...)
void HudGauge::renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const
{
SCP_string tmp;
va_list args;
Expand Down
13 changes: 8 additions & 5 deletions code/hud/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,11 +366,14 @@ class HudGauge
void renderBitmap(int frame, int x, int y, float scale = 1.0f, bool config = false) const;
void renderBitmapColor(int frame, int x, int y, float scale = 1.0f, bool config = false) const;
void renderBitmapEx(int frame, int x, int y, int w, int h, int sx, int sy, float scale = 1.0f, bool config = false) const;
void renderString(int x, int y, const char *str, float scale = 1.0f, bool config = false);
void renderString(int x, int y, int gauge_id, const char *str, float scale = 1.0f, bool config = false);
void renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale = 1.0f, bool config = false);
void renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(6, 7);
void renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(7, 8);
void renderString(int x, int y, const char *str, float scale = 1.0f, bool config = false) const;
void renderString(int x, int y, const char *str, size_t len, float scale = 1.0f, bool config = false) const;
void renderString(int x, int y, int gauge_id, const char *str, float scale = 1.0f, bool config = false) const;
void renderString(int x, int y, int gauge_id, const char *str, size_t len, float scale = 1.0f, bool config = false) const;
void renderStringAlignCenter(int x, int y, int area_width, const char *s, float scale = 1.0f, bool config = false) const;
void renderStringAlignCenter(int x, int y, int area_width, const char *s, size_t len, float scale = 1.0f, bool config = false) const;
void renderPrintf(int x, int y, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const SCP_FORMAT_STRING_ARGS(6, 7);
void renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bool config, SCP_FORMAT_STRING const char* format, ...) const SCP_FORMAT_STRING_ARGS(7, 8);
void renderLine(int x1, int y1, int x2, int y2, bool config = false) const;
void renderGradientLine(int x1, int y1, int x2, int y2, bool config = false) const;
void renderRect(int x, int y, int w, int h, bool config = false) const;
Expand Down
38 changes: 8 additions & 30 deletions code/hud/hudmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,9 @@ void HudGaugeMessages::processMessageBuffer()

void HudGaugeMessages::addPending(const char *text, int source, int x)
{
Assert(text != NULL);
Assert(text != nullptr);

HUD_message_data new_message;

new_message.text = text;
new_message.source = source;
new_message.x = x;

pending_messages.push(new_message);
pending_messages.emplace(text, source, x);
}

void HudGaugeMessages::scrollMessages()
Expand Down Expand Up @@ -484,7 +478,7 @@ void HudGaugeMessages::render(float /*frametime*/, bool config)
}

// Similar to HUD printf, but shows only one message at a time, at a fixed location.
void HUD_fixed_printf(float duration, color col, const char *format, ...)
void HUD_fixed_printf(float duration, color col, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;

Expand Down Expand Up @@ -525,8 +519,7 @@ int HUD_source_get_team(int source)
return source - HUD_SOURCE_TEAM_OFFSET;
}


void HUD_printf(const char *format, ...)
void HUD_printf(SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
SCP_string tmp;
Expand All @@ -551,7 +544,7 @@ void HUD_printf(const char *format, ...)
// message on the HUD. Text is split into multiple lines if width exceeds msg display area
// width. 'source' is used to indicate who send the message, and is used to color code text.
//
void HUD_sourced_printf(int source, const char *format, ...)
void HUD_sourced_printf(int source, SCP_FORMAT_STRING const char *format, ...)
{
va_list args;
SCP_string tmp;
Expand Down Expand Up @@ -579,13 +572,7 @@ void hud_sourced_print(int source, const SCP_string &msg)
// 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);
HUD_msg_buffer.emplace_back(msg, source, 0);

// Invoke the scripting hook
if (OnHudMessageReceivedHook->isActive()) {
Expand All @@ -604,13 +591,7 @@ void hud_sourced_print(int source, const char *msg)
// add message to the scrollback log first
hud_add_msg_to_scrollback(msg, source, Missiontime);

HUD_message_data new_msg;

new_msg.text = SCP_string(msg);
new_msg.source = source;
new_msg.x = 0;

HUD_msg_buffer.push_back(new_msg);
HUD_msg_buffer.emplace_back(msg, source, 0);

// Invoke the scripting hook
if (OnHudMessageReceivedHook->isActive()) {
Expand Down Expand Up @@ -663,10 +644,7 @@ void hud_add_msg_to_scrollback(const char *text, int source, int t)
}

// create the new node for the vector
line_node newLine = {t, The_mission.HUD_timer_padding, source, 0, 1, w, ""};
newLine.text = text;

Msg_scrollback_vec.push_back(newLine);
Msg_scrollback_vec.emplace_back(t, The_mission.HUD_timer_padding, source, 0, 1, w, text);
}

// how many lines to skip
Expand Down
Loading