-
Notifications
You must be signed in to change notification settings - Fork 804
/
minitext.cpp
188 lines (157 loc) · 4.67 KB
/
minitext.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* @file minitext.cpp
*
* Implementation of scrolling dialog text.
*/
#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
#include "DiabloUI/ui_flags.hpp"
#include "control.h"
#include "engine.h"
#include "engine/clx_sprite.hpp"
#include "engine/dx.h"
#include "engine/load_cel.hpp"
#include "engine/render/clx_render.hpp"
#include "engine/render/text_render.hpp"
#include "playerdat.hpp"
#include "textdat.h"
#include "utils/language.h"
#include "utils/timer.hpp"
namespace devilution {
bool qtextflag;
namespace {
/** Vertical speed of the scrolling text in ms/px */
int qtextSpd;
/** Start time of scrolling */
uint32_t ScrollStart;
/** Graphics for the window border */
OptionalOwnedClxSpriteList pTextBoxCels;
/** Pixels for a line of text and the empty space under it. */
const int LineHeight = 38;
std::vector<std::string> TextLines;
void LoadText(std::string_view text)
{
TextLines.clear();
const std::string paragraphs = WordWrapString(text, 543, GameFont30);
size_t previous = 0;
while (true) {
size_t next = paragraphs.find('\n', previous);
TextLines.emplace_back(paragraphs.substr(previous, next - previous));
if (next == std::string::npos)
break;
previous = next + 1;
}
}
/**
* @brief Calculate the speed the current text should scroll to match the given audio
* @param nSFX The index of the sound in the sgSFX table
* @return ms/px
*/
uint32_t CalculateTextSpeed(SfxID nSFX)
{
const auto numLines = static_cast<uint32_t>(TextLines.size());
#ifndef NOSOUND
uint32_t sfxFrames = GetSFXLength(nSFX);
#else
// Sound is disabled -- estimate length from the number of lines.
uint32_t sfxFrames = numLines * 3000;
#endif
assert(sfxFrames != 0);
uint32_t textHeight = LineHeight * numLines;
textHeight += LineHeight * 5; // adjust so when speaker is done two line are left
assert(textHeight != 0);
return sfxFrames / textHeight;
}
int CalculateTextPosition()
{
const uint32_t currTime = GetMillisecondsSinceStartup();
const int y = (currTime - ScrollStart) / qtextSpd - 260;
const auto textHeight = static_cast<int>(LineHeight * TextLines.size());
if (y >= textHeight)
qtextflag = false;
return y;
}
/**
* @brief Draw the current text in the quest dialog window
*/
void DrawQTextContent(const Surface &out)
{
int y = CalculateTextPosition();
const int sx = GetUIRectangle().position.x + 48;
const int sy = 0 - (y % LineHeight);
const unsigned int skipLines = y / LineHeight;
for (int i = 0; i < 8; i++) {
const unsigned int lineNumber = skipLines + i;
if (lineNumber >= TextLines.size()) {
continue;
}
const std::string &line = TextLines[lineNumber];
if (line.empty()) {
continue;
}
DrawString(out, line, { { sx, sy + i * LineHeight }, { 543, LineHeight } },
{ .flags = UiFlags::FontSize30 | UiFlags::ColorGold });
}
}
} // namespace
void FreeQuestText()
{
pTextBoxCels = std::nullopt;
}
void InitQuestText()
{
pTextBoxCels = LoadCel("data\\textbox", 591);
}
void InitQTextMsg(_speech_id m)
{
SfxID sfxnr = Speeches[m].sfxnr;
const SfxID *classSounds = herosounds[static_cast<size_t>(MyPlayer->_pClass)];
switch (sfxnr) {
case SfxID::Warrior1:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::ChamberOfBoneLore)];
break;
case SfxID::Warrior10:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::ValorLore)];
break;
case SfxID::Warrior11:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::HallsOfTheBlindLore)];
break;
case SfxID::Warrior12:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::WarlordOfBloodLore)];
break;
case SfxID::Warrior54:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::InSpirituSanctum)];
break;
case SfxID::Warrior55:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::PraedictumOtium)];
break;
case SfxID::Warrior56:
sfxnr = classSounds[static_cast<size_t>(HeroSpeech::EfficioObitusUtInimicus)];
break;
default:
break;
}
if (Speeches[m].scrlltxt) {
QuestLogIsOpen = false;
LoadText(_(Speeches[m].txtstr));
qtextflag = true;
qtextSpd = CalculateTextSpeed(sfxnr);
ScrollStart = GetMillisecondsSinceStartup();
}
PlaySFX(sfxnr);
}
void DrawQTextBack(const Surface &out)
{
const Point uiPosition = GetUIRectangle().position;
ClxDraw(out, uiPosition + Displacement { 24, 327 }, (*pTextBoxCels)[0]);
DrawHalfTransparentRectTo(out, uiPosition.x + 27, uiPosition.y + 28, 585, 297);
}
void DrawQText(const Surface &out)
{
DrawQTextBack(out);
DrawQTextContent(out.subregionY(GetUIRectangle().position.y + 49, 260));
}
} // namespace devilution