Use std::array for console_buffer::line
This commit is contained in:
parent
23315ebec2
commit
ae5e5c699a
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
@ -36,8 +37,8 @@ constexpr std::integral_constant<std::size_t, 2048> CON_LINE_LENGTH{};
|
||||||
|
|
||||||
struct console_buffer
|
struct console_buffer
|
||||||
{
|
{
|
||||||
char line[CON_LINE_LENGTH];
|
|
||||||
int priority;
|
int priority;
|
||||||
|
std::array<char, CON_LINE_LENGTH> line;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Define to 1 to capture the __FILE__, __LINE__ of callers to
|
/* Define to 1 to capture the __FILE__, __LINE__ of callers to
|
||||||
|
|
|
@ -29,7 +29,7 @@ public:
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
using scratch_buffer = std::false_type;
|
using scratch_buffer = std::false_type;
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
static std::span<char, N> insert_location_leader(char (&buffer)[N])
|
static std::span<char, N> insert_location_leader(std::array<char, N> &buffer)
|
||||||
{
|
{
|
||||||
return buffer;
|
return buffer;
|
||||||
}
|
}
|
||||||
|
@ -63,9 +63,9 @@ public:
|
||||||
* place further text.
|
* place further text.
|
||||||
*/
|
*/
|
||||||
template <std::size_t N>
|
template <std::size_t N>
|
||||||
std::span<char> insert_location_leader(char (&buffer)[N]) const
|
std::span<char> insert_location_leader(std::array<char, N> &buffer) const
|
||||||
{
|
{
|
||||||
const auto written = std::snprintf(buffer, sizeof(buffer), "%s:%u: ", file, line);
|
const auto written = std::snprintf(buffer.data(), buffer.size(), "%s:%u: ", file, line);
|
||||||
return std::span(buffer).subspan(written);
|
return std::span(buffer).subspan(written);
|
||||||
}
|
}
|
||||||
/* Return a span describing the written area that the caller can read
|
/* Return a span describing the written area that the caller can read
|
||||||
|
|
|
@ -85,7 +85,7 @@ static void con_add_buffer_line(const con_priority priority, const char *const b
|
||||||
console_buffer &c = con_buffer.back();
|
console_buffer &c = con_buffer.back();
|
||||||
c.priority=priority;
|
c.priority=priority;
|
||||||
|
|
||||||
size_t copy = std::min(len, CON_LINE_LENGTH - 1);
|
size_t copy = std::min(len, std::size(c.line) - 1);
|
||||||
c.line[copy] = 0;
|
c.line[copy] = 0;
|
||||||
memcpy(&c.line,buffer, copy);
|
memcpy(&c.line,buffer, copy);
|
||||||
}
|
}
|
||||||
|
@ -95,15 +95,15 @@ static void con_add_buffer_line(const con_priority priority, const char *const b
|
||||||
void (con_printf)(const con_priority_wrapper priority, const char *const fmt, ...)
|
void (con_printf)(const con_priority_wrapper priority, const char *const fmt, ...)
|
||||||
{
|
{
|
||||||
va_list arglist;
|
va_list arglist;
|
||||||
char buffer[CON_LINE_LENGTH];
|
|
||||||
|
|
||||||
if (priority <= CGameArg.DbgVerbose)
|
if (priority <= CGameArg.DbgVerbose)
|
||||||
{
|
{
|
||||||
va_start (arglist, fmt);
|
va_start (arglist, fmt);
|
||||||
|
std::array<char, CON_LINE_LENGTH> buffer;
|
||||||
auto &&leader = priority.insert_location_leader(buffer);
|
auto &&leader = priority.insert_location_leader(buffer);
|
||||||
const std::size_t len = std::max(vsnprintf(leader.data(), leader.size(), fmt, arglist), 0);
|
const std::size_t len = std::max(vsnprintf(leader.data(), leader.size(), fmt, arglist), 0);
|
||||||
va_end (arglist);
|
va_end (arglist);
|
||||||
con_force_puts(priority, buffer, len);
|
con_force_puts(priority, buffer.data(), len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,9 +318,9 @@ static void con_draw(grs_canvas &canvas)
|
||||||
{
|
{
|
||||||
auto &b = con_buffer[CON_LINES_MAX - 1 - i];
|
auto &b = con_buffer[CON_LINES_MAX - 1 - i];
|
||||||
gr_set_fontcolor(canvas, get_console_color_by_priority(b.priority), -1);
|
gr_set_fontcolor(canvas, get_console_color_by_priority(b.priority), -1);
|
||||||
const auto &&[w, h] = gr_get_string_size(game_font, b.line);
|
const auto &&[w, h] = gr_get_string_size(game_font, b.line.data());
|
||||||
y -= h + fspacy1;
|
y -= h + fspacy1;
|
||||||
gr_string(canvas, game_font, fspacx1, y, b.line, w, h);
|
gr_string(canvas, game_font, fspacx1, y, b.line.data(), w, h);
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
if (y<=0 || CON_LINES_MAX-1-i <= 0 || i < 0)
|
if (y<=0 || CON_LINES_MAX-1-i <= 0 || i < 0)
|
||||||
|
|
Loading…
Reference in a new issue