| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/main/subtitles.cpp $ |
| 21 | * $Revision: 7 $ |
| 22 | * $Date: 5/06/99 1:20a $ |
| 23 | * $Author: Samir $ |
| 24 | * |
| 25 | * Subtitle system for cinematics |
| 26 | * |
| 27 | * $Log: /DescentIII/main/subtitles.cpp $ |
| 28 | * |
| 29 | * 7 5/06/99 1:20a Samir |
| 30 | * use mve_ calls for subtitles. |
| 31 | * |
| 32 | * 6 4/20/99 12:48a Matt |
| 33 | * Made subtitles use briefing font instead of HUD font, even though I |
| 34 | * know the subtitles don't actually work. |
| 35 | * |
| 36 | * 5 4/14/99 3:57a Jeff |
| 37 | * fixed case mismatch in #includes |
| 38 | * |
| 39 | * 4 10/08/98 4:23p Kevin |
| 40 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 41 | * , mem_free and mem_strdup |
| 42 | * |
| 43 | * 3 8/29/98 6:53p Jeff |
| 44 | * only do subtitles if -subtitles is given on command line |
| 45 | * |
| 46 | * 2 8/28/98 4:20p Jeff |
| 47 | * got subtitles completly working |
| 48 | * |
| 49 | * $NoKeywords: $ |
| 50 | */ |
| 51 | |
| 52 | #include <cctype> |
| 53 | #include <cstdlib> |
| 54 | #include <cstring> |
| 55 | |
| 56 | #include "args.h" |
| 57 | #include "d3movie.h" |
| 58 | #include "manage.h" |
| 59 | #include "mem.h" |
| 60 | #include "pserror.h" |
| 61 | #include "subtitles.h" |
| 62 | |
| 63 | // Subtitle data |
| 64 | struct subtitle { |
| 65 | int first_frame, last_frame; |
| 66 | char *msg; |
| 67 | }; |
| 68 | |
| 69 | #define MAX_SUBTITLES 500 |
| 70 | #define MOVIE_SUBTITLE_EXTENSION ".msb" |
| 71 | #define MESSAGE_LENGTH 300 |
| 72 | |
| 73 | static subtitle Subtitles[MAX_SUBTITLES]; |
| 74 | int Num_subtitles; |
| 75 | int Movie_subtitle_init; |
| 76 | |
| 77 | // Prints out a subtitle on the screen, centered, at y pos y, with color color. |
| 78 | void PrintSubTitle(int y, ddgr_color color, char *msg); |
| 79 | // Parses a subtitle file |
| 80 | void SubtParseSubtitles(CFILE *file); |
| 81 | // clears the area where subtitle text is display (call once a frame) |
| 82 | void ClearSubTitleArea(void); |
| 83 | // Resets the subtitle information |
| 84 | void SubtResetSubTitles(void); |
| 85 | |
| 86 | void readline(CFILE *file, char *buf, int maxsize) { |
| 87 | ASSERT(file); |
| 88 | ASSERT(buf); |
| 89 | buf[0] = '\0'; |
| 90 | bool done = false; |
| 91 | while (!done) { |
| 92 | if (cfeof(file)) { |
| 93 | done = true; |
| 94 | buf[0] = '\0'; |
| 95 | } |
| 96 | if (cf_ReadString(buf, maxsize, file)) |
| 97 | done = true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | char *parse_whitespace(char *p) { |
| 102 | ASSERT(p); |
| 103 | while (isspace(*p)) |
| 104 | p++; |
| 105 | return p; |
| 106 | } |
| 107 | |
| 108 | char *parse_int(char *buf, int *d) { |
| 109 | ASSERT(buf); |
| 110 | ASSERT(d); |
| 111 | char *t; |
| 112 | bool got_digit = 0; |
| 113 | t = buf = parse_whitespace(buf); |
| 114 | |
| 115 | // Check for negative sign |
| 116 | if (*t == '-') |
| 117 | t++; |
| 118 | |
| 119 | // Check for valid characters in number |
| 120 | while (isdigit(*t)) { |
| 121 | t++; |
| 122 | got_digit = 1; |
| 123 | } |
| 124 | |
| 125 | // Make sure at least one digit |
| 126 | if (!got_digit) { |
| 127 | *d = 0; |
| 128 | return NULL; |
| 129 | } |
| 130 | |
| 131 | // Check for valid seperator, and convert |
| 132 | if ((*t == 0) || isspace(*t) || (*t == ',')) { // valid number |
| 133 | *d = atoi(buf); |
| 134 | return t; |
| 135 | } else { |
| 136 | return NULL; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | char *parse_token(char *p, const char *token) { |
| 141 | ASSERT(p); |
| 142 | ASSERT(token); |
| 143 | |
| 144 | int len = strlen(token); |
| 145 | if (!strnicmp(p, token, len)) { |
| 146 | return p + len; |
| 147 | } |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | |
| 152 | #define PARSE_NEXT_LINE() \ |
| 153 | do { \ |
| 154 | readline(file, buf, MESSAGE_LENGTH); \ |
| 155 | p = buf; \ |
| 156 | } while (0) |
| 157 | |
| 158 | // Parses a subtitle file |
| 159 | void SubtParseSubtitles(CFILE *file) { |
| 160 | SubtResetSubTitles(); |
| 161 | |
| 162 | int first_frame, last_frame; |
| 163 | char buf[MESSAGE_LENGTH]; |
| 164 | char *p = buf; |
| 165 | |
| 166 | PARSE_NEXT_LINE(); |
| 167 | |
| 168 | while (!parse_token(buf, "#End")) { |
| 169 | ASSERT(Num_subtitles < MAX_SUBTITLES); |
| 170 | |
| 171 | // starting frame number |
| 172 | p = parse_int(p, &first_frame); |
| 173 | if (!p) { |
| 174 | mprintf(0, "Couldn't parse first_frame\n"); |
| 175 | goto subt_parse_error; |
| 176 | } |
| 177 | |
| 178 | p = parse_int(p, &last_frame); |
| 179 | if (!p) { |
| 180 | mprintf(0, "Couldn't parse last_frame\n"); |
| 181 | goto subt_parse_error; |
| 182 | } |
| 183 | |
| 184 | p = parse_whitespace(p); |
| 185 | |
| 186 | Subtitles[Num_subtitles].first_frame = first_frame; |
| 187 | Subtitles[Num_subtitles].last_frame = last_frame; |
| 188 | Subtitles[Num_subtitles].msg = (char *)mem_malloc(strlen(p) + 1); |
| 189 | if (!Subtitles[Num_subtitles].msg) |
| 190 | goto subt_parse_error; |
| 191 | strcpy(Subtitles[Num_subtitles].msg, p); // be sure to free this later |
| 192 | |
| 193 | Num_subtitles++; |
| 194 | |
| 195 | PARSE_NEXT_LINE(); |
| 196 | } |
| 197 | return; |
| 198 | subt_parse_error: |
| 199 | mprintf(0, "Error Parsing SubTitle File!\n"); |
| 200 | SubtResetSubTitles(); |
| 201 | } |
| 202 | |
| 203 | // Resets the subtitle information |
| 204 | void SubtResetSubTitles(void) { |
| 205 | for (int i = 0; i < Num_subtitles; i++) { |
| 206 | if (Subtitles[i].msg) |
| 207 | mem_free(Subtitles[i].msg); |
| 208 | Subtitles[i].msg = NULL; |
| 209 | } |
| 210 | Num_subtitles = 0; |
| 211 | Movie_subtitle_init = 1; |
| 212 | } |
| 213 | |
| 214 | // Initializes the subtitles for a given movie file |
| 215 | void SubtInitSubtitles(const std::filesystem::path &filename) { |
| 216 | // Load the subtitles |
| 217 | Num_subtitles = 0; |
| 218 | Movie_subtitle_init = 0; |
| 219 | |
| 220 | // need a quick out here if no subtitles. |
| 221 | if (!FindArg("-subtitles")) |
| 222 | return; |
| 223 | |
| 224 | std::filesystem::path subtitle_path = std::filesystem::path(LocalArtDir) / "movies" / filename; |
| 225 | subtitle_path.replace_extension(MOVIE_SUBTITLE_EXTENSION); |
| 226 | |
| 227 | mprintf(0, "Looking for the subtitle file %s\n", subtitle_path.u8string().c_str()); |
| 228 | |
| 229 | CFILE *ifile = cfopen(subtitle_path, "rt"); |
| 230 | if (!ifile) { |
| 231 | mprintf(0, "Movie: Couldn't find subtitle file %s\n", subtitle_path.u8string().c_str()); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | SubtResetSubTitles(); |
| 236 | SubtParseSubtitles(ifile); |
| 237 | cfclose(ifile); |
| 238 | } |
| 239 | |
| 240 | // Shutsdown the subtitle system |
| 241 | void SubtCloseSubtitles() { SubtResetSubTitles(); } |
| 242 | |
| 243 | #define MAX_ACTIVE_SUBTITLES 3 |
| 244 | |
| 245 | // draw the subtitles for this frame |
| 246 | void SubtDrawSubtitles(int frame_num) { |
| 247 | static int active_subtitles[MAX_ACTIVE_SUBTITLES]; |
| 248 | static int num_active_subtitles, next_subtitle, line_spacing; |
| 249 | int t, y, fh; |
| 250 | int must_erase = 0; |
| 251 | |
| 252 | if (Movie_subtitle_init) { |
| 253 | num_active_subtitles = 0; |
| 254 | next_subtitle = 0; |
| 255 | fh = 12; |
| 256 | line_spacing = fh + (fh >> 2); |
| 257 | Movie_subtitle_init = 0; |
| 258 | } |
| 259 | |
| 260 | // get rid of any subtitles that have expired |
| 261 | for (t = 0; t < num_active_subtitles;) { |
| 262 | if (frame_num > Subtitles[active_subtitles[t]].last_frame) { |
| 263 | int t2; |
| 264 | for (t2 = t; t2 < num_active_subtitles - 1; t2++) |
| 265 | active_subtitles[t2] = active_subtitles[t2 + 1]; |
| 266 | num_active_subtitles--; |
| 267 | must_erase = 1; |
| 268 | } else |
| 269 | t++; |
| 270 | } |
| 271 | |
| 272 | // get any subtitles new for this frame |
| 273 | while ((next_subtitle < Num_subtitles) && (frame_num >= Subtitles[next_subtitle].first_frame)) { |
| 274 | if (num_active_subtitles >= MAX_ACTIVE_SUBTITLES) |
| 275 | Error("Too many active subtitles!"); |
| 276 | active_subtitles[num_active_subtitles++] = next_subtitle; |
| 277 | next_subtitle++; |
| 278 | } |
| 279 | |
| 280 | ClearSubTitleArea(); |
| 281 | |
| 282 | // find y coordinate for first line of subtitles |
| 283 | y = 480 - ((line_spacing + 1) * MAX_ACTIVE_SUBTITLES + 2); |
| 284 | int first, last; |
| 285 | |
| 286 | for (t = 0; t < num_active_subtitles; t++) { |
| 287 | if (active_subtitles[t] != -1) { |
| 288 | first = Subtitles[active_subtitles[t]].first_frame; |
| 289 | last = Subtitles[active_subtitles[t]].last_frame; |
| 290 | PrintSubTitle(y, ((first <= frame_num) && (last >= frame_num)) ? GR_RGB(255, 255, 255) : GR_RGB(128, 128, 128), |
| 291 | Subtitles[active_subtitles[t]].msg); |
| 292 | y += line_spacing + 1; |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Prints out a subtitle on the screen, centered, at y pos y, with color color. |
| 298 | void PrintSubTitle(int y, ddgr_color color, char *msg) { mve_Puts(-1, y, color, msg); } |
| 299 | |
| 300 | // clears the area where subtitle text is display (call once a frame) |
| 301 | void ClearSubTitleArea(void) { |
| 302 | int fh = 12; |
| 303 | int line_spacing = fh + (fh >> 2); |
| 304 | int y = 475 - ((line_spacing + 1) * MAX_ACTIVE_SUBTITLES + 2); |
| 305 | |
| 306 | mve_ClearRect(0, y, 639, 479); |
| 307 | } |