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
4 changes: 4 additions & 0 deletions code/graphics/2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -1044,12 +1044,16 @@ bool gr_resize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL,
// 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, size_t len, const char * format, SCP_FORMAT_STRING ... ) 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, size_t len, const char * format, SCP_FORMAT_STRING ... ) 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, size_t len, const char * format, SCP_FORMAT_STRING ... ) 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, size_t len, const char * format, SCP_FORMAT_STRING ... ) 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
64 changes: 60 additions & 4 deletions code/graphics/software/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ void gr_string_win(int x, int y, char *s)

char grx_printf_text[2048];

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

Expand All @@ -817,7 +817,21 @@ void gr_printf(int x, int y, const char * format, ...)
gr_string(x, y, grx_printf_text);
}

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

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

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

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

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

Expand All @@ -831,7 +845,21 @@ void gr_printf_menu(int x, int y, const char * format, ...)
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_menu(int x, int y, size_t len, const char * format, SCP_FORMAT_STRING ... )
{
va_list args;

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

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

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

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

Expand All @@ -845,7 +873,21 @@ void gr_printf_menu_zoomed(int x, int y, const char * format, ...)
gr_string(x, y, grx_printf_text, GR_RESIZE_MENU_ZOOMED);
}

void gr_printf_no_resize(int x, int y, const char * format, ...)
void gr_printf_menu_zoomed(int x, int y, size_t len, const char * format, SCP_FORMAT_STRING ... )
{
va_list args;

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

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

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

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

Expand All @@ -858,3 +900,17 @@ void gr_printf_no_resize(int x, int y, const char * format, ...)

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

void gr_printf_no_resize(int x, int y, size_t len, const char * format, SCP_FORMAT_STRING ... )
{
va_list args;

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

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

gr_string(x, y, grx_printf_text, GR_RESIZE_NONE, 1.0f, len);
}
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
19 changes: 15 additions & 4 deletions code/hud/hudmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,32 @@

#define HUD_SOURCE_TEAM_OFFSET 8 // must be higher than any previous hud source

typedef struct HUD_message_data {
struct HUD_message_data
{
SCP_string text;
int source; // where this message came from so we can color code it
int x;
} HUD_message_data;

typedef struct line_node {
HUD_message_data() = default;
HUD_message_data(SCP_string _text, int _source, int _x)
: text(std::move(_text)), source(_source), x(_x)
{}
};

struct line_node
{
fix time; // timestamp when message was added
int timer_padding; // the mission timer padding, in seconds, at the time the message was added
int source; // who/what the source of the message was (for color coding)
int x;
int y;
int underline_width;
SCP_string text;
} line_node;

line_node(fix _time, int _timer_padding, int _source, int _x, int _y, int _underline_width, SCP_string _text)
: time(_time), timer_padding(_timer_padding), source(_source), x(_x), y(_y), underline_width(_underline_width), text(std::move(_text))
{}
};

extern SCP_vector<line_node> Msg_scrollback_vec;

Expand Down
Loading