| 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"cxx . . $ `<file>/Descent3/args.h`/ |
| 57 | #include "d3movie.h"cxx . . $ `<file>/Descent3/d3movie.h`/ |
| 58 | #include "manage.h"cxx . . $ `<file>/lib/manage.h`/ |
| 59 | #include "mem.h"cxx . . $ `<file>/mem/mem.h`/ |
| 60 | #include "pserror.h"cxx . . $ `<file>/misc/pserror.h`/ |
| 61 | #include "subtitles.h"cxx . . $ `<file>/Descent3/subtitles.h`/ |
| 62 | |
| 63 | // Subtitle data |
| 64 | struct subtitlecxx . . $ subtitle# { |
| 65 | int first_framecxx . . $ subtitle#first_frame., last_framecxx . . $ subtitle#last_frame.; |
| 66 | char *msgcxx . . $ subtitle#msg.; |
| 67 | }; |
| 68 | |
| 69 | #define MAX_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:69:9`! 500 |
| 70 | #define MOVIE_SUBTITLE_EXTENSIONcxx . . $ `Descent3/subtitles.cpp:70:9`! ".msb" |
| 71 | #define MESSAGE_LENGTHcxx . . $ `Descent3/subtitles.cpp:71:9`! 300 |
| 72 | |
| 73 | static subtitlecxx . . $ subtitle# Subtitlescxx . . $ Subtitles.[MAX_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:69:9`!]; |
| 74 | int Num_subtitlescxx . . $ Num_subtitles.; |
| 75 | int Movie_subtitle_initcxx . . $ Movie_subtitle_init.; |
| 76 | |
| 77 | // Prints out a subtitle on the screen, centered, at y pos y, with color color. |
| 78 | void PrintSubTitlecxx . . $ PrintSubTitle(b0ae78464a8a630a).(int y, ddgr_colorcxx . . $ ddgr_color# color, char *msg); |
| 79 | // Parses a subtitle file |
| 80 | void SubtParseSubtitlescxx . . $ SubtParseSubtitles(3639e799710acb68).(CFILEcxx . . $ CFILE# *file); |
| 81 | // clears the area where subtitle text is display (call once a frame) |
| 82 | void ClearSubTitleAreacxx . . $ ClearSubTitleArea(49f6e7a06ebc5aa8).(void); |
| 83 | // Resets the subtitle information |
| 84 | void SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(void); |
| 85 | |
| 86 | void readlinecxx . . $ readline(9364c45c7ec0f91c).(CFILEcxx . . $ CFILE# *file, char *buf, int maxsize) { |
| 87 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(file); |
| 88 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(buf); |
| 89 | buf[0] = '\0'; |
| 90 | bool done = false; |
| 91 | while (!done) { |
| 92 | if (cfeofcxx . . $ cfeof(413716a422df0036).(file)) { |
| 93 | done = true; |
| 94 | buf[0] = '\0'; |
| 95 | } |
| 96 | if (cf_ReadStringcxx . . $ cf_ReadString(38cd788e4423eff6).(buf, maxsize, file)) |
| 97 | done = true; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | char *parse_whitespacecxx . . $ parse_whitespace(7a9722d75b371431).(char *p) { |
| 102 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(p); |
| 103 | while (isspace(*p)) |
| 104 | p++; |
| 105 | return p; |
| 106 | } |
| 107 | |
| 108 | char *parse_intcxx . . $ parse_int(5874cd7f0254925).(char *buf, int *d) { |
| 109 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(buf); |
| 110 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(d); |
| 111 | char *t; |
| 112 | bool got_digit = 0; |
| 113 | t = buf = parse_whitespacecxx . . $ parse_whitespace(7a9722d75b371431).(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 NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 129 | } |
| 130 | |
| 131 | // Check for valid seperator, and convert |
| 132 | if ((*t == 0) || isspace(*t) || (*t == ',')) { // valid number |
| 133 | *d = atoicxx . . $ atoi(f5ecf3191ed0cd93).(buf); |
| 134 | return t; |
| 135 | } else { |
| 136 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | char *parse_tokencxx . . $ parse_token(b7b3a83273496486).(char *p, const char *token) { |
| 141 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(p); |
| 142 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(token); |
| 143 | |
| 144 | int len = strlen(token); |
| 145 | if (!strnicmpcxx . . $ `linux/linux_fix.h:44:9`!(p, token, len)) { |
| 146 | return p + len; |
| 147 | } |
| 148 | |
| 149 | return NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 150 | } |
| 151 | |
| 152 | #define PARSE_NEXT_LINEcxx . . $ `Descent3/subtitles.cpp:152:9`!() \ |
| 153 | do { \ |
| 154 | readline(file, buf, MESSAGE_LENGTHcxx . . $ `Descent3/subtitles.cpp:71:9`!); \ |
| 155 | p = buf; \ |
| 156 | } while (0) |
| 157 | |
| 158 | // Parses a subtitle file |
| 159 | void SubtParseSubtitlescxx . . $ SubtParseSubtitles(3639e799710acb68).(CFILEcxx . . $ CFILE# *file) { |
| 160 | SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(); |
| 161 | |
| 162 | int first_frame, last_frame; |
| 163 | char buf[MESSAGE_LENGTHcxx . . $ `Descent3/subtitles.cpp:71:9`!]; |
| 164 | char *p = buf; |
| 165 | |
| 166 | PARSE_NEXT_LINEcxx . . $ `Descent3/subtitles.cpp:152:9`!(); |
| 167 | |
| 168 | while (!parse_tokencxx . . $ parse_token(b7b3a83273496486).(buf, "#End")) { |
| 169 | ASSERTcxx . . $ `misc/pserror.h:246:9`!(Num_subtitles < MAX_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:69:9`!); |
| 170 | |
| 171 | // starting frame number |
| 172 | p = parse_intcxx . . $ parse_int(5874cd7f0254925).(p, &first_frame); |
| 173 | if (!p) { |
| 174 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't parse first_frame\n"); |
| 175 | goto subt_parse_error; |
| 176 | } |
| 177 | |
| 178 | p = parse_intcxx . . $ parse_int(5874cd7f0254925).(p, &last_frame); |
| 179 | if (!p) { |
| 180 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Couldn't parse last_frame\n"); |
| 181 | goto subt_parse_error; |
| 182 | } |
| 183 | |
| 184 | p = parse_whitespacecxx . . $ parse_whitespace(7a9722d75b371431).(p); |
| 185 | |
| 186 | Subtitlescxx . . $ Subtitles.[Num_subtitlescxx . . $ Num_subtitles.].first_framecxx . . $ subtitle#first_frame. = first_frame; |
| 187 | Subtitlescxx . . $ Subtitles.[Num_subtitlescxx . . $ Num_subtitles.].last_framecxx . . $ subtitle#last_frame. = last_frame; |
| 188 | Subtitlescxx . . $ Subtitles.[Num_subtitlescxx . . $ Num_subtitles.].msgcxx . . $ subtitle#msg. = (char *)mem_malloccxx . . $ `mem/mem.h:79:9`!(strlen(p) + 1); |
| 189 | if (!Subtitlescxx . . $ Subtitles.[Num_subtitlescxx . . $ Num_subtitles.].msgcxx . . $ subtitle#msg.) |
| 190 | goto subt_parse_error; |
| 191 | strcpy(Subtitlescxx . . $ Subtitles.[Num_subtitlescxx . . $ Num_subtitles.].msgcxx . . $ subtitle#msg., p); // be sure to free this later |
| 192 | |
| 193 | Num_subtitlescxx . . $ Num_subtitles.++; |
| 194 | |
| 195 | PARSE_NEXT_LINEcxx . . $ `Descent3/subtitles.cpp:152:9`!(); |
| 196 | } |
| 197 | return; |
| 198 | subt_parse_error: |
| 199 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Error Parsing SubTitle File!\n"); |
| 200 | SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(); |
| 201 | } |
| 202 | |
| 203 | // Resets the subtitle information |
| 204 | void SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(void) { |
| 205 | for (int i = 0; i < Num_subtitlescxx . . $ Num_subtitles.; i++) { |
| 206 | if (Subtitlescxx . . $ Subtitles.[i].msgcxx . . $ subtitle#msg.) |
| 207 | mem_freecxx . . $ `mem/mem.h:80:9`!(Subtitles[i].msg); |
| 208 | Subtitlescxx . . $ Subtitles.[i].msgcxx . . $ subtitle#msg. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!; |
| 209 | } |
| 210 | Num_subtitlescxx . . $ Num_subtitles. = 0; |
| 211 | Movie_subtitle_initcxx . . $ Movie_subtitle_init. = 1; |
| 212 | } |
| 213 | |
| 214 | // Initializes the subtitles for a given movie file |
| 215 | void SubtInitSubtitlescxx . . $ SubtInitSubtitles(3f42e2c2fac0d5fc).(const stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# &filename) { |
| 216 | // Load the subtitles |
| 217 | Num_subtitlescxx . . $ Num_subtitles. = 0; |
| 218 | Movie_subtitle_initcxx . . $ Movie_subtitle_init. = 0; |
| 219 | |
| 220 | // need a quick out here if no subtitles. |
| 221 | if (!FindArgcxx . . $ FindArg(5c4306d8553b3696).("-subtitles")) |
| 222 | return; |
| 223 | |
| 224 | stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# subtitle_path = stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path#(LocalArtDircxx . . $ LocalArtDir.) /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). "movies"cxx . . $ std/filesystem/__cxx11/path#path(835bc3a16b770d4d). /cxx . . $ std/filesystem/__cxx11/`operator/`(578e0438ad5f0e7e). filename; |
| 225 | subtitle_path.replace_extensioncxx . . $ std/filesystem/__cxx11/path#replace_extension(554473e25f389c7d).(MOVIE_SUBTITLE_EXTENSIONcxx . . $ `Descent3/subtitles.cpp:70:9`!); |
| 226 | |
| 227 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Looking for the subtitle file %s\n", subtitle_path.u8string().c_str()); |
| 228 | |
| 229 | CFILEcxx . . $ CFILE# *ifile = cfopencxx . . $ cfopen(7c7b94e455f1ae56).(subtitle_path, "rt"); |
| 230 | if (!ifile) { |
| 231 | mprintfcxx . . $ `ddebug/mono.h:82:9`!(0, "Movie: Couldn't find subtitle file %s\n", subtitle_path.u8string().c_str()); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(); |
| 236 | SubtParseSubtitlescxx . . $ SubtParseSubtitles(3639e799710acb68).(ifile); |
| 237 | cfclosecxx . . $ cfclose(3639e799710acb68).(ifile); |
| 238 | } |
| 239 | |
| 240 | // Shutsdown the subtitle system |
| 241 | void SubtCloseSubtitlescxx . . $ SubtCloseSubtitles(49f6e7a06ebc5aa8).() { SubtResetSubTitlescxx . . $ SubtResetSubTitles(49f6e7a06ebc5aa8).(); } |
| 242 | |
| 243 | #define MAX_ACTIVE_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:243:9`! 3 |
| 244 | |
| 245 | // draw the subtitles for this frame |
| 246 | void SubtDrawSubtitlescxx . . $ SubtDrawSubtitles(d4f767463ce0a6b3).(int frame_num) { |
| 247 | static int active_subtitles[MAX_ACTIVE_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:243:9`!]; |
| 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_initcxx . . $ Movie_subtitle_init.) { |
| 253 | num_active_subtitles = 0; |
| 254 | next_subtitle = 0; |
| 255 | fh = 12; |
| 256 | line_spacing = fh + (fh >> 2); |
| 257 | Movie_subtitle_initcxx . . $ 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 > Subtitlescxx . . $ Subtitles.[active_subtitles[t]].last_framecxx . . $ subtitle#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_subtitlescxx . . $ Num_subtitles.) && (frame_num >= Subtitlescxx . . $ Subtitles.[next_subtitle].first_framecxx . . $ subtitle#first_frame.)) { |
| 274 | if (num_active_subtitles >= MAX_ACTIVE_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:243:9`!) |
| 275 | Errorcxx . . $ Error(326aeb464d16410c).("Too many active subtitles!"); |
| 276 | active_subtitles[num_active_subtitles++] = next_subtitle; |
| 277 | next_subtitle++; |
| 278 | } |
| 279 | |
| 280 | ClearSubTitleAreacxx . . $ ClearSubTitleArea(49f6e7a06ebc5aa8).(); |
| 281 | |
| 282 | // find y coordinate for first line of subtitles |
| 283 | y = 480 - ((line_spacing + 1) * MAX_ACTIVE_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:243:9`! + 2); |
| 284 | int first, last; |
| 285 | |
| 286 | for (t = 0; t < num_active_subtitles; t++) { |
| 287 | if (active_subtitles[t] != -1) { |
| 288 | first = Subtitlescxx . . $ Subtitles.[active_subtitles[t]].first_framecxx . . $ subtitle#first_frame.; |
| 289 | last = Subtitlescxx . . $ Subtitles.[active_subtitles[t]].last_framecxx . . $ subtitle#last_frame.; |
| 290 | PrintSubTitlecxx . . $ PrintSubTitle(b0ae78464a8a630a).(y, ((first <= frame_num) && (last >= frame_num)) ? GR_RGBcxx . . $ GR_RGB(530956271a9518a5).(255, 255, 255) : GR_RGBcxx . . $ GR_RGB(530956271a9518a5).(128, 128, 128), |
| 291 | Subtitlescxx . . $ Subtitles.[active_subtitles[t]].msgcxx . . $ subtitle#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 PrintSubTitlecxx . . $ PrintSubTitle(b0ae78464a8a630a).(int y, ddgr_colorcxx . . $ ddgr_color# color, char *msg) { mve_Putscxx . . $ mve_Puts(fd7f3a04a1c20bf5).(-1, y, color, msg); } |
| 299 | |
| 300 | // clears the area where subtitle text is display (call once a frame) |
| 301 | void ClearSubTitleAreacxx . . $ ClearSubTitleArea(49f6e7a06ebc5aa8).(void) { |
| 302 | int fh = 12; |
| 303 | int line_spacing = fh + (fh >> 2); |
| 304 | int y = 475 - ((line_spacing + 1) * MAX_ACTIVE_SUBTITLEScxx . . $ `Descent3/subtitles.cpp:243:9`! + 2); |
| 305 | |
| 306 | mve_ClearRectcxx . . $ mve_ClearRect(6f3a90be462c391b).(0, y, 639, 479); |
| 307 | } |