Skip to content

Commit

Permalink
console: fix wrapped lines expiring at odd times
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Oct 5, 2024
1 parent c6062d2 commit 1e1ff30
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 102 deletions.
4 changes: 2 additions & 2 deletions docs/tr2/progress.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4416,8 +4416,8 @@ typedef enum {
0x0051BDAC - MCIDEVICEID g_MciDeviceID;
0x0051BDB0 - int32_t g_CD_LoopTrack;
0x0051C820 + TEXTSTRING g_TextstringTable[64]; // MAX_TEXTSTRINGS
0x0051D6A0 - int16_t g_TextstringCount = 0;
0x0051D6C0 - char g_TextstringBuffers[64][64];
0x0051D6A0 + int16_t g_TextstringCount = 0;
0x0051D6C0 + char g_TextstringBuffers[64][64];
0x0051E6C4 - int32_t g_SoundIsActive;
0x0051E9E0 - SAVEGAME_INFO g_SaveGame;
0x005206E0 - LARA_INFO g_Lara;
Expand Down
34 changes: 1 addition & 33 deletions src/libtrx/game/console/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,6 @@
static bool m_IsOpened = false;
static UI_WIDGET *m_Console;

static void M_LogMultiline(const char *text);
static void M_Log(const char *text);

static void M_LogMultiline(const char *const text)
{
assert(text != NULL);
char *wrapped_text = String_WordWrap(text, Console_GetMaxLineLength());

const char *start = wrapped_text;
while (true) {
const char *newline = strchr(start, '\n');
if (newline == NULL) {
break;
}
char temp[newline - start + 1];
strncpy(temp, start, newline - start);
temp[newline - start] = '\0';
M_Log(temp);
start = newline + 1;
}
if (*start != '\0') {
M_Log(start);
}
Memory_FreePointer(&wrapped_text);
}

static void M_Log(const char *text)
{
assert(text != NULL);
UI_Console_HandleLog(m_Console, text);
}

void Console_Init(void)
{
m_Console = UI_Console_Create();
Expand Down Expand Up @@ -113,7 +81,7 @@ void Console_Log(const char *fmt, ...)
va_end(va);

LOG_INFO("%s", text);
M_LogMultiline(text);
UI_Console_HandleLog(m_Console, text);
Memory_FreePointer(&text);
}

Expand Down
10 changes: 7 additions & 3 deletions src/libtrx/game/ui/widgets/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#include "game/ui/widgets/spacer.h"
#include "game/ui/widgets/stack.h"
#include "memory.h"
#include "strings.h"
#include "utils.h"
#include "game/text.h"

#include <string.h>

#define WINDOW_MARGIN 5
#define LOG_HEIGHT 16
#define LOG_MARGIN 10
#define MAX_LOG_LINES 20
#define LOG_SCALE 0.8
Expand Down Expand Up @@ -137,7 +138,7 @@ UI_WIDGET *UI_Console_Create(void)

for (int32_t i = MAX_LOG_LINES - 1; i >= 0; i--) {
self->logs[i].label =
UI_Label_Create("", UI_LABEL_AUTO_SIZE, LOG_HEIGHT * LOG_SCALE);
UI_Label_Create("", UI_LABEL_AUTO_SIZE, UI_LABEL_AUTO_SIZE);
UI_Label_SetScale(self->logs[i].label, LOG_SCALE);
UI_Stack_AddChild(self->container, self->logs[i].label);
}
Expand Down Expand Up @@ -193,7 +194,10 @@ void UI_Console_HandleLog(UI_WIDGET *const widget, const char *const text)

self->logs[0].expire_at =
Clock_GetHighPrecisionCounter() + 1000 * strlen(text) * DELAY_PER_CHAR;
UI_Label_ChangeText(self->logs[0].label, text);

char *wrapped = String_WordWrap(text, Text_GetMaxLineLength());
UI_Label_ChangeText(self->logs[0].label, wrapped);
Memory_FreePointer(&wrapped);

UI_Stack_DoLayout(self->container);
M_UpdateLogCount(self);
Expand Down
1 change: 0 additions & 1 deletion src/libtrx/include/libtrx/game/console/extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

#include <stdint.h>

extern int32_t Console_GetMaxLineLength(void);
extern CONSOLE_COMMAND **Console_GetCommands(void);
5 changes: 5 additions & 0 deletions src/libtrx/include/libtrx/game/text.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include <stdint.h>

extern int32_t Text_GetMaxLineLength(void);
5 changes: 0 additions & 5 deletions src/tr1/game/console/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
#include "game/text.h"
#include "game/viewport.h"

int32_t Console_GetMaxLineLength(void)
{
return TEXT_MAX_STRING_SIZE - 1;
}

extern CONSOLE_COMMAND **Console_GetCommands(void)
{
return g_ConsoleCommands;
Expand Down
1 change: 1 addition & 0 deletions src/tr1/game/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void Shell_Init(const char *gameflow_path)
void Shell_Shutdown(void)
{
GameFlow_Shutdown();
Text_Shutdown();
GameBuf_Shutdown();
Output_Shutdown();
Input_Shutdown();
Expand Down
70 changes: 39 additions & 31 deletions src/tr1/game/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
#include "global/types.h"
#include "global/vars.h"

#include <assert.h>
#include <stdio.h>
#include <string.h>

#include <libtrx/memory.h>

#define TEXT_BOX_OFFSET 2
#define TEXT_MAX_STRINGS 100
#define TRIANGLE_SYM 93
Expand All @@ -29,7 +32,6 @@ static int32_t m_FlashFrames = 0;
static CLOCK_TIMER m_FlashTimer = { 0 };
static int16_t m_TextCount = 0;
static TEXTSTRING m_Table[TEXT_MAX_STRINGS] = { 0 };
static char m_TextstringBuffers[TEXT_MAX_STRINGS][TEXT_MAX_STRING_SIZE] = { 0 };

static int8_t m_TextSpacing[110] = {
14 /*A*/, 11 /*B*/, 11 /*C*/, 11 /*D*/, 11 /*E*/, 11 /*F*/, 11 /*G*/,
Expand Down Expand Up @@ -210,8 +212,15 @@ void Text_Init(void)
m_TextCount = 0;
}

TEXTSTRING *Text_Create(
const int16_t x, const int16_t y, const char *const content)
void Text_Shutdown(void)
{
for (int32_t i = 0; i < TEXT_MAX_STRINGS; i++) {
TEXTSTRING *const text = &m_Table[i];
Memory_FreePointer(&text->content);
}
}

TEXTSTRING *Text_Create(int16_t x, int16_t y, const char *const content)
{
if (m_TextCount == TEXT_MAX_STRINGS) {
return NULL;
Expand All @@ -233,7 +242,7 @@ TEXTSTRING *Text_Create(
return NULL;
}

text->content = m_TextstringBuffers[n];
text->content = Memory_DupStr(content);
text->pos.x = x;
text->pos.y = y;
text->letter_spacing = 1;
Expand All @@ -249,8 +258,6 @@ TEXTSTRING *Text_Create(
text->bgnd_off.x = 0;
text->bgnd_off.y = 0;

Text_ChangeText(text, content);

m_TextCount++;

return text;
Expand All @@ -261,17 +268,14 @@ void Text_ChangeText(TEXTSTRING *const text, const char *const content)
if (text == NULL) {
return;
}
if (!text->flags.active) {
return;
}
size_t length = strlen(content) + 1;
strncpy(text->content, content, TEXT_MAX_STRING_SIZE - 1);
if (length >= TEXT_MAX_STRING_SIZE) {
text->content[TEXT_MAX_STRING_SIZE - 1] = '\0';
assert(content != NULL);
if (text->flags.active) {
Memory_FreePointer(&text->content);
text->content = Memory_DupStr(content);
}
}

void Text_SetPos(TEXTSTRING *const text, int16_t x, int16_t y)
void Text_SetPos(TEXTSTRING *const text, const int16_t x, const int16_t y)
{
if (text == NULL) {
return;
Expand Down Expand Up @@ -422,14 +426,14 @@ int32_t Text_GetHeight(const TEXTSTRING *const text)
if (text == NULL) {
return 0;
}
int32_t height = TEXT_HEIGHT;
int32_t height = TEXT_HEIGHT_FIXED * text->scale.v / PHD_ONE;
char *content = text->content;
for (char letter = *content; letter != '\0'; letter = *content++) {
if (text->flags.multiline && letter == '\n') {
height += TEXT_HEIGHT + TEXT_Y_SPACING;
height += TEXT_HEIGHT_FIXED * text->scale.v / PHD_ONE;
}
}
return height * text->scale.v / PHD_ONE;
return height;
}

int32_t Text_GetWidth(const TEXTSTRING *const text)
Expand All @@ -439,8 +443,8 @@ int32_t Text_GetWidth(const TEXTSTRING *const text)
}

int32_t width = 0;
for (const char *content = text->content; *content != '\0'; *content++) {
const char letter = *content;
for (const char *ptr = text->content; *ptr != '\0'; *ptr++) {
const char letter = *ptr;
if (letter == 0x7F || (letter > 10 && letter < 32)) {
continue;
}
Expand Down Expand Up @@ -473,14 +477,14 @@ void Text_Draw(void)
{
m_FlashFrames = Clock_GetFrameAdvance();
for (int i = 0; i < TEXT_MAX_STRINGS; i++) {
TEXTSTRING *const text = &m_Table[i];
TEXTSTRING *text = &m_Table[i];
if (text->flags.active && !text->flags.manual_draw) {
Text_DrawText(text);
}
}
}

void Text_DrawText(TEXTSTRING *const text)
void Text_DrawText(TEXTSTRING *text)
{
int sx, sy, sh, sv;

Expand All @@ -498,18 +502,18 @@ void Text_DrawText(TEXTSTRING *const text)
}

char *content = text->content;
int32_t x = text->pos.x;
int32_t y = text->pos.y;
double x = text->pos.x;
double y = text->pos.y;
int32_t text_width = Text_GetWidth(text);

if (text->flags.centre_h) {
x += (Screen_GetResWidthDownscaled(RSR_TEXT) - text_width) / 2;
} else if (text->flags.right) {
x += Screen_GetResWidthDownscaled(RSR_TEXT) - text_width;
x += (Screen_GetResWidthDownscaled(RSR_TEXT) - text_width);
}

if (text->flags.centre_v) {
y += Screen_GetResHeightDownscaled(RSR_TEXT) / 2;
y += Screen_GetResHeightDownscaled(RSR_TEXT) / 2.0;
} else if (text->flags.bottom) {
y += Screen_GetResHeightDownscaled(RSR_TEXT);
}
Expand All @@ -527,16 +531,16 @@ void Text_DrawText(TEXTSTRING *const text)
}

if (text->flags.multiline && letter == '\n') {
y += (TEXT_HEIGHT + TEXT_Y_SPACING) * text->scale.h / PHD_ONE;
y += TEXT_HEIGHT_FIXED * text->scale.v / PHD_ONE;
x = start_x;
continue;
}
if (letter == ' ') {
x += (text->word_spacing * text->scale.h) / PHD_ONE;
x += text->word_spacing * text->scale.h / PHD_ONE;
continue;
}

uint8_t sprite_num = M_MapLetterToSpriteNum(letter);
const uint8_t sprite_num = M_MapLetterToSpriteNum(letter);
sx = Screen_GetRenderScale(x, RSR_TEXT);
sy = Screen_GetRenderScale(y, RSR_TEXT);
sh = Screen_GetRenderScale(text->scale.h, RSR_TEXT);
Expand All @@ -550,9 +554,8 @@ void Text_DrawText(TEXTSTRING *const text)
continue;
}

x += (((int32_t)text->letter_spacing + m_TextSpacing[sprite_num])
* text->scale.h)
/ PHD_ONE;
x += ((int32_t)text->letter_spacing + m_TextSpacing[sprite_num])
* text->scale.h / PHD_ONE;
}

int32_t bwidth = 0;
Expand Down Expand Up @@ -596,3 +599,8 @@ void Text_DrawText(TEXTSTRING *const text)
g_Config.ui.menu_style, sx, sy, sh, sv, text->outline.style);
}
}

int32_t Text_GetMaxLineLength(void)
{
return Screen_GetResWidthDownscaled(RSR_TEXT) / (TEXT_HEIGHT * 0.75);
}
5 changes: 3 additions & 2 deletions src/tr1/game/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
#include <stdbool.h>
#include <stdint.h>

#define TEXT_HEIGHT 11
#define TEXT_Y_SPACING 3
#define TEXT_HEIGHT 11 // TODO: Get rid of this
#define TEXT_HEIGHT_FIXED 15
#define TEXT_OUTLINE_THICKNESS 2
#define TEXT_MAX_STRING_SIZE 100

RGBA_8888 Text_GetMenuColor(MENU_COLOR color);
void Text_Init(void);
void Text_Shutdown(void);
TEXTSTRING *Text_Create(int16_t x, int16_t y, const char *content);
void Text_ChangeText(TEXTSTRING *text, const char *content);
void Text_SetPos(TEXTSTRING *text, int16_t x, int16_t y);
Expand Down
5 changes: 3 additions & 2 deletions src/tr1/game/ui/widgets/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ static int32_t M_GetHeight(const UI_LABEL *const self)
if (self->height != UI_LABEL_AUTO_SIZE) {
return self->height;
}
return Text_GetHeight(self->text) * PHD_ONE / self->text->scale.v;
return Text_GetHeight(self->text);
}

static void M_SetPosition(
UI_LABEL *const self, const int32_t x, const int32_t y)
{
Text_SetPos(self->text, x, y + TEXT_HEIGHT);
Text_SetPos(self->text, x, y + TEXT_HEIGHT_FIXED);
}

static void M_Draw(UI_LABEL *const self)
Expand Down Expand Up @@ -68,6 +68,7 @@ UI_WIDGET *UI_Label_Create(
self->has_frame = false;

self->text = Text_Create(0, 0, text);
Text_SetMultiline(self->text, true);
self->text->flags.manual_draw = 1;

return (UI_WIDGET *)self;
Expand Down
5 changes: 0 additions & 5 deletions src/tr2/game/console/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
#include "game/console/setup.h"
#include "game/text.h"

int32_t Console_GetMaxLineLength(void)
{
return TEXT_MAX_STRING_SIZE - 1;
}

CONSOLE_COMMAND **Console_GetCommands(void)
{
return g_ConsoleCommands;
Expand Down
Loading

0 comments on commit 1e1ff30

Please sign in to comment.