Browse Source

Use string_view in DrawString and friends

pull/2499/head
Vladimir Olteanu 5 years ago committed by Anders Jenbo
parent
commit
b17ff04ee2
  1. 4
      Source/control.cpp
  2. 11
      Source/engine/render/text_render.cpp
  3. 7
      Source/engine/render/text_render.hpp
  4. 2
      Source/qol/itemlabels.cpp

4
Source/control.cpp

@ -308,9 +308,9 @@ void PrintSBookHotkey(const Surface &out, Point position, const std::string &tex
position += Displacement { SPLICONLENGTH - (GetLineWidth(text.c_str()) + 5), 17 - SPLICONLENGTH };
// Draw a drop shadow below and to the left of the text
DrawString(out, text.c_str(), position + Displacement { -1, 1 }, UiFlags::ColorBlack);
DrawString(out, text, position + Displacement { -1, 1 }, UiFlags::ColorBlack);
// Then draw the text over the top
DrawString(out, text.c_str(), position, UiFlags::ColorSilver);
DrawString(out, text, position, UiFlags::ColorSilver);
}
/**

11
Source/engine/render/text_render.cpp

@ -197,13 +197,12 @@ void InitText()
}
}
int GetLineWidth(const char *text, GameFontTables size, int spacing, int *charactersInLine)
int GetLineWidth(string_view text, GameFontTables size, int spacing, int *charactersInLine)
{
int lineWidth = 0;
size_t textLength = strlen(text);
size_t i = 0;
for (; i < textLength; i++) {
for (; i < text.length(); i++) {
if (text[i] == '\n')
break;
@ -271,7 +270,7 @@ void WordWrapGameString(char *text, size_t width, GameFontTables size, int spaci
/**
* @todo replace Rectangle with cropped Surface
*/
uint16_t DrawString(const Surface &out, const char *text, const Rectangle &rect, UiFlags flags, int spacing, int lineHeight, bool drawTextCursor)
uint16_t DrawString(const Surface &out, string_view text, const Rectangle &rect, UiFlags flags, int spacing, int lineHeight, bool drawTextCursor)
{
GameFontTables size = GameFontSmall;
if (HasAnyOf(flags, UiFlags::FontMedium))
@ -289,8 +288,6 @@ uint16_t DrawString(const Surface &out, const char *text, const Rectangle &rect,
else if (HasAnyOf(flags, UiFlags::ColorBlack))
color = ColorBlack;
const size_t textLength = strlen(text);
int charactersInLine = 0;
int lineWidth = 0;
if (HasAnyOf(flags, (UiFlags::AlignCenter | UiFlags::AlignRight | UiFlags::KerningFitSpacing)))
@ -313,7 +310,7 @@ uint16_t DrawString(const Surface &out, const char *text, const Rectangle &rect,
lineHeight = LineHeights[size];
uint16_t i = 0;
for (; i < textLength; i++) {
for (; i < text.length(); i++) {
uint8_t frame = FontFrame[size][FontIndex[static_cast<uint8_t>(text[i])]];
int symbolWidth = FontKern[size][frame];
if (text[i] == '\n' || characterPosition.x + symbolWidth > rightMargin) {

7
Source/engine/render/text_render.hpp

@ -14,6 +14,7 @@
#include "engine/cel_sprite.hpp"
#include "engine/rectangle.hpp"
#include "utils/stdcompat/optional.hpp"
#include "utils/stdcompat/string_view.hpp"
namespace devilution {
@ -35,7 +36,7 @@ void InitText();
* @param charactersInLine Receives characters read until newline or terminator
* @return Line width in pixels
*/
int GetLineWidth(const char *text, GameFontTables size = GameFontSmall, int spacing = 1, int *charactersInLine = nullptr);
int GetLineWidth(string_view text, GameFontTables size = GameFontSmall, int spacing = 1, int *charactersInLine = nullptr);
void WordWrapGameString(char *text, size_t width, GameFontTables size = GameFontSmall, int spacing = 1);
/**
@ -58,7 +59,7 @@ void WordWrapGameString(char *text, size_t width, GameFontTables size = GameFont
* @param drawTextCursor Whether to draw an animated cursor sprite at the end of the text (default is to display nothing).
* @return The number of characters rendered, including characters "drawn" outside the buffer.
*/
uint16_t DrawString(const Surface &out, const char *text, const Rectangle &rect, UiFlags flags = UiFlags::None, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false);
uint16_t DrawString(const Surface &out, string_view text, const Rectangle &rect, UiFlags flags = UiFlags::None, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false);
/**
* @brief Draws a line of text at the given position relative to the origin of the output buffer.
@ -78,7 +79,7 @@ uint16_t DrawString(const Surface &out, const char *text, const Rectangle &rect,
* @param drawTextCursor Whether to draw an animated cursor sprite at the end of the text (default is to display nothing).
* @return The number of characters rendered (could be less than the string length if it wrapped past the bottom of the buffer).
*/
inline uint16_t DrawString(const Surface &out, const char *text, const Point &position, UiFlags flags = UiFlags::None, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false)
inline uint16_t DrawString(const Surface &out, string_view text, const Point &position, UiFlags flags = UiFlags::None, int spacing = 1, int lineHeight = -1, bool drawTextCursor = false)
{
return DrawString(out, text, { position, { out.w() - position.x, 0 } }, flags, spacing, lineHeight, drawTextCursor);
}

2
Source/qol/itemlabels.cpp

@ -160,7 +160,7 @@ void DrawItemNameLabels(const Surface &out)
FillRect(out, label.pos.x, label.pos.y - Height + MarginY, label.width, Height, PAL8_BLUE + 6);
else
DrawHalfTransparentRectTo(out, label.pos.x, label.pos.y - Height + MarginY, label.width, Height);
DrawString(out, label.text.c_str(), { { label.pos.x + MarginX, label.pos.y }, { label.width, Height } }, itm.getTextColor());
DrawString(out, label.text, { { label.pos.x + MarginX, label.pos.y }, { label.width, Height } }, itm.getTextColor());
}
labelQueue.clear();
}

Loading…
Cancel
Save