| 1 |
/* |
| 2 |
* Portions of this file are copyright Rebirth contributors and licensed as |
| 3 |
* described in COPYING.txt. |
| 4 |
* Portions of this file are copyright Parallax Software and licensed |
| 5 |
* according to the Parallax license below. |
| 6 |
* See COPYING.txt for license details. |
| 7 |
|
| 8 |
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX |
| 9 |
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO |
| 10 |
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A |
| 11 |
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS |
| 12 |
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS |
| 13 |
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE |
| 14 |
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE |
| 15 |
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS |
| 16 |
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE. |
| 17 |
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* |
| 21 |
* |
| 22 |
* Routines to display the credits. |
| 23 |
* |
| 24 |
*/ |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <stdlib.h> |
| 28 |
#include <string.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
#include "dxxerror.h" |
| 34 |
#include "pstypes.h" |
| 35 |
#include "gr.h" |
| 36 |
#include "window.h" |
| 37 |
#include "key.h" |
| 38 |
#include "mouse.h" |
| 39 |
#include "palette.h" |
| 40 |
#include "timer.h" |
| 41 |
#include "gamefont.h" |
| 42 |
#include "pcx.h" |
| 43 |
#include "credits.h" |
| 44 |
#include "u_mem.h" |
| 45 |
#include "screens.h" |
| 46 |
#include "text.h" |
| 47 |
#include "songs.h" |
| 48 |
#include "menu.h" |
| 49 |
#include "config.h" |
| 50 |
#include "physfsx.h" |
| 51 |
#if DXX_BUILD_DESCENT == 2 |
| 52 |
#include "mission.h" |
| 53 |
#include "gamepal.h" |
| 54 |
#include "args.h" |
| 55 |
#endif |
| 56 |
#include "piggy.h" |
| 57 |
#include <memory> |
| 58 |
|
| 59 |
#define ROW_SPACING (SHEIGHT / 17) |
| 60 |
#define NUM_LINES 20 //14 |
| 61 |
#if DXX_BUILD_DESCENT == 1 |
| 62 |
#define MAKE_CREDITS_PAIR(F) std::span<const char, sizeof(F) + 1>(F ".tex", sizeof(F) + 1) |
| 63 |
#define CREDITS_FILE MAKE_CREDITS_PAIR("credits") |
| 64 |
#elif DXX_BUILD_DESCENT == 2 |
| 65 |
#define MAKE_CREDITS_PAIR(F) std::span<const char>(F ".tex", sizeof(F) + 1) |
| 66 |
#define CREDITS_FILE ( \ |
| 67 |
PHYSFS_exists("mcredits.tex") \ |
| 68 |
? MAKE_CREDITS_PAIR("mcredits") \ |
| 69 |
: PHYSFS_exists("ocredits.tex") \ |
| 70 |
? MAKE_CREDITS_PAIR("ocredits") \ |
| 71 |
: MAKE_CREDITS_PAIR("credits") \ |
| 72 |
) |
| 73 |
#define ALLOWED_CHAR (!Current_mission ? 'R' : (is_SHAREWARE ? 'S' : 'R')) |
| 74 |
#endif |
| 75 |
|
| 76 |
namespace dcx { |
| 77 |
|
| 78 |
namespace { |
| 79 |
|
| 80 |
struct credits_window : window |
| 81 |
{ |
| 82 |
RAIIPHYSFS_File file; |
| 83 |
const uint8_t have_bin_file; |
| 84 |
std::array<PHYSFSX_gets_line_t<80>, NUM_LINES> buffer{}; |
| 85 |
int buffer_line{0}; |
| 86 |
int first_line_offset{0}; |
| 87 |
int extra_inc{0}; |
| 88 |
int done{0}; |
| 89 |
int row{0}; |
| 90 |
grs_main_bitmap backdrop{}; |
| 91 |
credits_window(grs_canvas &src, int x, int y, int w, int h, RAIIPHYSFS_File f, const uint8_t bin_file) : |
| 92 |
window(src, x, y, w, h), file(std::move(f)), have_bin_file(bin_file) |
| 93 |
{ |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
namespace dsx { |
| 102 |
|
| 103 |
namespace { |
| 104 |
|
| 105 |
struct credits_window : ::dcx::credits_window |
| 106 |
{ |
| 107 |
using ::dcx::credits_window::credits_window; |
| 108 |
virtual window_event_result event_handler(const d_event &) override; |
| 109 |
}; |
| 110 |
|
| 111 |
window_event_result credits_window::event_handler(const d_event &event) |
| 112 |
{ |
| 113 |
int l, y; |
| 114 |
window_event_result result; |
| 115 |
|
| 116 |
switch (event.type) |
| 117 |
{ |
| 118 |
case event_type::key_command: |
| 119 |
if ((result = call_default_handler(event)) == window_event_result::ignored) // if not print screen, debug etc |
| 120 |
{ |
| 121 |
return window_event_result::close; |
| 122 |
} |
| 123 |
return result; |
| 124 |
|
| 125 |
case event_type::mouse_button_down: |
| 126 |
case event_type::mouse_button_up: |
| 127 |
if (event_mouse_get_button(event) == mbtn::left || event_mouse_get_button(event) == mbtn::right) |
| 128 |
{ |
| 129 |
return window_event_result::close; |
| 130 |
} |
| 131 |
break; |
| 132 |
|
| 133 |
#if DXX_MAX_BUTTONS_PER_JOYSTICK |
| 134 |
case event_type::joystick_button_down: |
| 135 |
return window_event_result::close; |
| 136 |
#endif |
| 137 |
|
| 138 |
case event_type::idle: |
| 139 |
if (done > NUM_LINES) |
| 140 |
{ |
| 141 |
return window_event_result::close; |
| 142 |
} |
| 143 |
break; |
| 144 |
case event_type::window_draw: |
| 145 |
{ |
| 146 |
#if DXX_BUILD_DESCENT == 1 |
| 147 |
timer_delay(F1_0/17); |
| 148 |
#elif DXX_BUILD_DESCENT == 2 |
| 149 |
timer_delay(F1_0/28); |
| 150 |
#endif |
| 151 |
|
| 152 |
if (row == 0) |
| 153 |
{ |
| 154 |
do { |
| 155 |
buffer_line = (buffer_line + 1) % NUM_LINES; |
| 156 |
#if DXX_BUILD_DESCENT == 2 |
| 157 |
get_line:; |
| 158 |
#endif |
| 159 |
if (PHYSFSX_fgets(buffer[buffer_line], file)) |
| 160 |
{ |
| 161 |
char *p; |
| 162 |
if (have_bin_file) // is this a binary tbl file |
| 163 |
decode_text_line (buffer[buffer_line]); |
| 164 |
#if DXX_BUILD_DESCENT == 1 |
| 165 |
p = strchr(&buffer[buffer_line][0u],'\n'); |
| 166 |
if (p) *p = '\0'; |
| 167 |
#elif DXX_BUILD_DESCENT == 2 |
| 168 |
p = buffer[buffer_line]; |
| 169 |
if (p[0] == ';') |
| 170 |
goto get_line; |
| 171 |
|
| 172 |
if (p[0] == '%') |
| 173 |
{ |
| 174 |
if (p[1] == ALLOWED_CHAR) |
| 175 |
{ |
| 176 |
for (int i = 0; p[i]; i++) |
| 177 |
p[i] = p[i+2]; |
| 178 |
} |
| 179 |
else |
| 180 |
goto get_line; |
| 181 |
} |
| 182 |
#endif |
| 183 |
} else { |
| 184 |
//fseek( file, 0, SEEK_SET); |
| 185 |
buffer[buffer_line][0u] = 0; |
| 186 |
done++; |
| 187 |
} |
| 188 |
} while (extra_inc--); |
| 189 |
extra_inc = 0; |
| 190 |
} |
| 191 |
|
| 192 |
// cheap but effective: towards end of credits sequence, fade out the music volume |
| 193 |
if (done >= NUM_LINES-16) |
| 194 |
{ |
| 195 |
static int curvol = -10; |
| 196 |
if (curvol == -10) |
| 197 |
curvol = CGameCfg.MusicVolume; |
| 198 |
const auto limitvol = (NUM_LINES - done) / 2; |
| 199 |
if (curvol > limitvol) |
| 200 |
{ |
| 201 |
curvol = limitvol; |
| 202 |
songs_set_volume(curvol); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
y = first_line_offset - row; |
| 207 |
auto &canvas = w_canv; |
| 208 |
show_fullscr(canvas, backdrop); |
| 209 |
for (uint_fast32_t j=0; j != NUM_LINES; ++j, y += ROW_SPACING) |
| 210 |
{ |
| 211 |
l = (buffer_line + j + 1 ) % NUM_LINES; |
| 212 |
const char *s = buffer[l]; |
| 213 |
if (!s) |
| 214 |
continue; |
| 215 |
|
| 216 |
const grs_font *cv_font; |
| 217 |
if ( s[0] == '!' ) { |
| 218 |
s++; |
| 219 |
cv_font = canvas.cv_font; |
| 220 |
} else |
| 221 |
{ |
| 222 |
const auto c = s[0]; |
| 223 |
cv_font = (c == '$' |
| 224 |
? (++s, HUGE_FONT) |
| 225 |
: (c == '*') |
| 226 |
? (++s, MEDIUM3_FONT) |
| 227 |
: MEDIUM2_FONT |
| 228 |
).get(); |
| 229 |
} |
| 230 |
|
| 231 |
const auto tempp = strchr( s, '\t' ); |
| 232 |
if ( !tempp ) { |
| 233 |
// Wacky Fast Credits thing |
| 234 |
gr_string(canvas, *cv_font, 0x8000, y, s); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
row += SHEIGHT / 200; |
| 239 |
if (row >= ROW_SPACING) |
| 240 |
row = 0; |
| 241 |
break; |
| 242 |
} |
| 243 |
|
| 244 |
case event_type::window_close: |
| 245 |
songs_set_volume(CGameCfg.MusicVolume); |
| 246 |
songs_play_song(song_number::title, 1); |
| 247 |
break; |
| 248 |
default: |
| 249 |
break; |
| 250 |
} |
| 251 |
return window_event_result::ignored; |
| 252 |
} |
| 253 |
|
| 254 |
static void credits_show_common(RAIIPHYSFS_File file, const int have_bin_file) |
| 255 |
{ |
| 256 |
palette_array_t backdrop_palette; |
| 257 |
auto cr = window_create<credits_window>(grd_curscreen->sc_canvas, 0, 0, SWIDTH, SHEIGHT, std::move(file), have_bin_file); |
| 258 |
|
| 259 |
set_screen_mode(SCREEN_MENU); |
| 260 |
#if DXX_BUILD_DESCENT == 2 |
| 261 |
gr_use_palette_table( "credits.256" ); |
| 262 |
#endif |
| 263 |
|
| 264 |
pcx_read_bitmap_or_default(STARS_BACKGROUND, cr->backdrop, backdrop_palette); |
| 265 |
songs_play_song(song_number::credits, 1); |
| 266 |
|
| 267 |
gr_remap_bitmap_good(cr->backdrop,backdrop_palette, -1, -1); |
| 268 |
|
| 269 |
show_fullscr(cr->w_canv, cr->backdrop); |
| 270 |
gr_palette_load( gr_palette ); |
| 271 |
|
| 272 |
key_flush(); |
| 273 |
event_process_all(); |
| 274 |
} |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
void credits_show(const char *const filename) |
| 279 |
{ |
| 280 |
if (auto &&file = PHYSFSX_openReadBuffered(filename).first) |
| 281 |
credits_show_common(std::move(file), 1); |
| 282 |
} |
| 283 |
|
| 284 |
void credits_show() |
| 285 |
{ |
| 286 |
const auto &&credits_file = CREDITS_FILE; |
| 287 |
int have_bin_file{0}; |
| 288 |
auto &&[file, physfserr] = PHYSFSX_openReadBuffered(credits_file.data()); |
| 289 |
if (!file) |
| 290 |
{ |
| 291 |
char nfile[32]; |
| 292 |
snprintf(nfile, sizeof(nfile), "%.*sxb", static_cast<int>(credits_file.size()), credits_file.data()); |
| 293 |
auto &&[file2, physfserr2] = PHYSFSX_openReadBuffered_updateCase(nfile); |
| 294 |
if (!file2) |
| 295 |
Error("Failed to open CREDITS.TEX and CREDITS.TXB file: \"%s\", \"%s\"\n", PHYSFS_getErrorByCode(physfserr), PHYSFS_getErrorByCode(physfserr2)); |
| 296 |
file = std::move(file2); |
| 297 |
have_bin_file = 1; |
| 298 |
} |
| 299 |
credits_show_common(std::move(file), have_bin_file); |
| 300 |
} |
| 301 |
|
| 302 |
} |