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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
* Copyright (C) 2000-2022 The Exult Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "txtscroll.h"
#include "common_types.h"
#include "files/U7file.h"
#include "font.h"
#include "game.h"
#include "gamewin.h"
#include "shapeid.h"
#include <cstdlib>
#include <cstring>
#ifdef __GNUC__
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wold-style-cast"
# pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif // __GNUC__
#include <SDL.h>
#ifdef __GNUC__
# pragma GCC diagnostic pop
#endif // __GNUC__
using std::atoi;
using std::make_unique;
using std::size_t;
using std::strchr;
using std::string;
using std::strncmp;
using std::unique_ptr;
using std::vector;
TextScroller::TextScroller(
const char* archive, int index, std::shared_ptr<Font> fnt, Shape* shp) {
font = fnt;
shapes = shp;
auto txtobj = [&]() {
// Hack to patch MAINSHP_FLX.
if (!strncmp(archive, MAINSHP_FLX, sizeof(MAINSHP_FLX) - 1)) {
return U7multiobject(archive, PATCH_MAINSHP, index);
}
return U7multiobject(archive, index);
}();
size_t len;
const char CR = '\r';
const char LF = '\n';
const unique_ptr<unsigned char[]> txt = txtobj.retrieve(len);
if (!txt || len <= 0) {
text = new vector<string>();
return;
}
char* ptr = reinterpret_cast<char*>(txt.get());
char* end = ptr + len;
text = new vector<string>();
while (ptr < end) {
char* start = ptr;
ptr = strchr(ptr, LF);
if (ptr) {
if (*(ptr - 1) == CR) { // It's CR/LF
*(ptr - 1) = 0;
} else {
*ptr = 0;
}
text->emplace_back(start);
ptr += 1;
} else {
break;
}
}
}
TextScroller::~TextScroller() {
delete text;
}
int TextScroller::show_line(
Game_window* gwin, int left, int right, int y, int index) {
Shape_manager* sman = Shape_manager::get_instance();
// The texts used in the main menu contains backslashed sequences that
// indicates the output format of the lines:
// \Px include picture number x (frame nr. of shape passed to constructor)
// \C center line
// \L left aligned to right center line
// \R right aligned to left center line
// | carriage return (stay on same line)
// #xxx display character with number xxx
const string str = (*text)[index];
const char* ptr = str.c_str();
char* txt = new char[strlen(ptr) + 1];
char* txtptr = txt;
int ypos = y;
const int vspace = 2; // 2 extra pixels between lines
// Align text to the left by default
int align = -1;
int xpos = left;
const int center = (right + left) / 2;
bool add_line = true;
while (*ptr) {
if (!strncmp(ptr, "\\P", 2)) {
const int pix = *(ptr + 2) - '0';
ptr += 3;
Shape_frame* frame = shapes->get_frame(pix);
if (frame) {
sman->paint_shape(center - frame->get_width() / 2, ypos, frame);
ypos += frame->get_height() + vspace;
}
} else if (!strncmp(ptr, "\\C", 2)) {
ptr += 2;
align = 0;
} else if (!strncmp(ptr, "\\L", 2)) {
ptr += 2;
align = 1;
} else if (!strncmp(ptr, "\\R", 2)) {
ptr += 2;
align = -1;
} else if (*ptr == '|' || *(ptr + 1) == 0) {
if (*(ptr + 1) == 0 && *ptr != '|') {
*txtptr++ = *ptr;
add_line = false;
}
*txtptr = 0;
if (align < 0) {
xpos = center - font->get_text_width(txt);
} else if (align == 0) {
xpos = center - font->get_text_width(txt) / 2;
} else {
xpos = center;
}
font->draw_text(gwin->get_win()->get_ib8(), xpos, ypos, txt);
if (*ptr != '|') {
ypos += font->get_text_height() + vspace;
}
txtptr = txt; // Go to beginning of string
++ptr;
} else if (*ptr == '#') {
ptr++;
if (*ptr == '#') { // Double hash
*txtptr++ = *ptr++;
continue;
}
char numerical[4] = {0, 0, 0, 0};
char* num = numerical;
while (std::isdigit(static_cast<unsigned char>(*ptr))) {
*num++ = *ptr++;
}
*txtptr++ = atoi(numerical);
} else {
*txtptr++ = *ptr++;
}
}
delete[] txt;
if (add_line) {
ypos += font->get_text_height();
}
return ypos;
}
bool TextScroller::run(Game_window* gwin) {
gwin->clear_screen();
gwin->show(true);
const int topx = (gwin->get_width() - 320) / 2;
const int topy = (gwin->get_height() - 200) / 2;
const int endy = topy + 200;
uint32 starty = endy;
uint32 startline = 0;
const unsigned int maxlines = text->size();
if (!maxlines) {
gwin->clear_screen();
gwin->show(true);
return false;
}
bool looping = true;
bool complete = false;
SDL_Event event;
uint32 next_time = SDL_GetTicks() + 200;
uint32 incr = 120;
// pal.apply();
gwin->get_pal()->apply();
while (looping) {
int ypos = starty;
uint32 curline = startline;
gwin->clear_screen();
do {
if (curline == maxlines) {
break;
}
ypos = show_line(gwin, topx, topx + 320, ypos, curline++);
if (ypos < topy) { // If this line doesn't appear, don't show it
// next time
++startline;
starty = ypos;
if (startline >= maxlines) {
looping = false;
complete = true;
break;
}
}
} while (ypos < endy);
// pal.apply();
gwin->show(true);
do {
// this could be a problem when too many events are produced
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_RSHIFT
|| event.key.keysym.sym == SDLK_LSHIFT) {
incr = 0;
} else {
looping = false;
}
break;
case SDL_KEYUP:
incr = 120;
next_time = SDL_GetTicks();
break;
case SDL_MOUSEBUTTONUP:
looping = false;
break;
default:
break;
}
}
} while (next_time > SDL_GetTicks());
next_time = SDL_GetTicks() + incr;
if (!looping) {
gwin->get_pal()->fade_out(c_fade_out_time);
}
starty--;
}
gwin->clear_screen();
gwin->show(true);
return complete;
}
int TextScroller::get_count() {
return text->size();
}
|