| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <ctype.h> |
| 7 |
#include <stdarg.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
#include <strings.h> |
| 12 |
|
| 13 |
#include "glk.h" |
| 14 |
#ifdef SPATTERLIGHT |
| 15 |
#include "glkimp.h" |
| 16 |
#endif |
| 17 |
#include "glkstart.h" |
| 18 |
|
| 19 |
#include "animations.h" |
| 20 |
#include "layouttext.h" |
| 21 |
#include "sagadraw.h" |
| 22 |
#include "taylor.h" |
| 23 |
#include "utility.h" |
| 24 |
|
| 25 |
#define GLK_BUFFER_ROCK 1 |
| 26 |
#define GLK_STATUS_ROCK 1010 |
| 27 |
#define GLK_GRAPHICS_ROCK 1020 |
| 28 |
|
| 29 |
winid_t Bottom, Top, Graphics; |
| 30 |
winid_t CurrentWindow; |
| 31 |
static int OutC; |
| 32 |
static char OutWord[128]; |
| 33 |
|
| 34 |
glui32 TopWidth; |
| 35 |
glui32 TopHeight = 1; |
| 36 |
|
| 37 |
int Options; |
| 38 |
int LineEvent = 0; |
| 39 |
|
| 40 |
winid_t FindGlkWindowWithRock(glui32 rock) |
| 41 |
{ |
| 42 |
winid_t win; |
| 43 |
glui32 rockptr; |
| 44 |
for (win = glk_window_iterate(NULL, &rockptr); win; |
| 45 |
win = glk_window_iterate(win, &rockptr)) { |
| 46 |
if (rockptr == rock) |
| 47 |
return win; |
| 48 |
} |
| 49 |
return 0; |
| 50 |
} |
| 51 |
|
| 52 |
void Display(winid_t w, const char *fmt, ...) |
| 53 |
{ |
| 54 |
va_list ap; |
| 55 |
char msg[2048]; |
| 56 |
|
| 57 |
int size = sizeof msg; |
| 58 |
|
| 59 |
va_start(ap, fmt); |
| 60 |
vsnprintf(msg, size, fmt, ap); |
| 61 |
va_end(ap); |
| 62 |
|
| 63 |
glk_put_string_stream(glk_window_get_stream(w), msg); |
| 64 |
} |
| 65 |
|
| 66 |
void HitEnter(void) |
| 67 |
{ |
| 68 |
glk_request_char_event(Bottom); |
| 69 |
|
| 70 |
event_t ev; |
| 71 |
int result = 0; |
| 72 |
do { |
| 73 |
glk_select(&ev); |
| 74 |
if (ev.type == evtype_CharInput) { |
| 75 |
if (ev.val1 == keycode_Return) { |
| 76 |
result = 1; |
| 77 |
} else { |
| 78 |
glk_request_char_event(Bottom); |
| 79 |
} |
| 80 |
} |
| 81 |
} while (result == 0); |
| 82 |
|
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
static void WordFlush(winid_t win); |
| 87 |
|
| 88 |
static int JustWroteNewline = 0; |
| 89 |
|
| 90 |
void PrintCharacter(unsigned char c) |
| 91 |
{ |
| 92 |
if (OutC == 0 && c == '\0') |
| 93 |
return; |
| 94 |
|
| 95 |
if (c == '.' || c == '!') { |
| 96 |
if (JustWrotePeriod) |
| 97 |
return; |
| 98 |
JustWrotePeriod = 1; |
| 99 |
} else if (JustWrotePeriod && !isspace(c)) { |
| 100 |
JustWrotePeriod = 0; |
| 101 |
} |
| 102 |
|
| 103 |
if (CurrentWindow == Bottom) { |
| 104 |
if (isspace(c)) { |
| 105 |
WordFlush(Bottom); |
| 106 |
if ((c == 10 || c == 13) && !JustWroteNewline) { |
| 107 |
Display(Bottom, "\n"); |
| 108 |
JustWroteNewline = 1; |
| 109 |
} else if (!JustWroteNewline) { |
| 110 |
Display(Bottom, " "); |
| 111 |
} |
| 112 |
return; |
| 113 |
} |
| 114 |
JustWroteNewline = 0; |
| 115 |
OutWord[OutC] = c; |
| 116 |
OutC++; |
| 117 |
if (OutC > 127) |
| 118 |
WordFlush(Bottom); |
| 119 |
return; |
| 120 |
} else { |
| 121 |
if (isspace(c)) { |
| 122 |
WordFlush(Top); |
| 123 |
WriteToRoomDescriptionStream(" "); |
| 124 |
if (c == '\n') { |
| 125 |
WriteToRoomDescriptionStream("\n"); |
| 126 |
} |
| 127 |
return; |
| 128 |
} |
| 129 |
OutWord[OutC] = c; |
| 130 |
OutC++; |
| 131 |
if (OutC == TopWidth) |
| 132 |
WordFlush(Top); |
| 133 |
} |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
unsigned char WaitCharacter(void) |
| 138 |
{ |
| 139 |
WordFlush(Bottom); |
| 140 |
glk_request_char_event(Bottom); |
| 141 |
|
| 142 |
event_t ev; |
| 143 |
do { |
| 144 |
glk_select(&ev); |
| 145 |
Updates(ev); |
| 146 |
} while (ev.type != evtype_CharInput); |
| 147 |
return ev.val1; |
| 148 |
} |
| 149 |
|
| 150 |
static void WordFlush(winid_t win) |
| 151 |
{ |
| 152 |
int i; |
| 153 |
for (i = 0; i < OutC; i++) { |
| 154 |
if (win == Top) |
| 155 |
WriteToRoomDescriptionStream("%c", OutWord[i]); |
| 156 |
else |
| 157 |
Display(win, "%c", OutWord[i]); |
| 158 |
} |
| 159 |
OutC = 0; |
| 160 |
} |
| 161 |
|
| 162 |
extern strid_t room_description_stream; |
| 163 |
char *roomdescbuf = NULL; |
| 164 |
|
| 165 |
void TopWindow(void) |
| 166 |
{ |
| 167 |
WordFlush(Bottom); |
| 168 |
if (roomdescbuf != NULL) { |
| 169 |
fprintf(stderr, "roomdescbuf was not Null, so freeing it\n"); |
| 170 |
free(roomdescbuf); |
| 171 |
} |
| 172 |
roomdescbuf = MemAlloc(1000); |
| 173 |
roomdescbuf = memset(roomdescbuf, 0, 1000); |
| 174 |
room_description_stream = glk_stream_open_memory(roomdescbuf, 1000, filemode_Write, 0); |
| 175 |
|
| 176 |
CurrentWindow = Top; |
| 177 |
glk_window_clear(Top); |
| 178 |
} |
| 179 |
|
| 180 |
static void PrintWindowDelimiter(void) |
| 181 |
{ |
| 182 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 183 |
glk_window_move_cursor(Top, 0, TopHeight - 1); |
| 184 |
glk_stream_set_current(glk_window_get_stream(Top)); |
| 185 |
for (int i = 0; i < TopWidth; i++) |
| 186 |
glk_put_char(DelimiterChar); |
| 187 |
} |
| 188 |
|
| 189 |
static void FlushRoomDescription(void) |
| 190 |
{ |
| 191 |
if (!room_description_stream) |
| 192 |
return; |
| 193 |
|
| 194 |
glk_stream_close(room_description_stream, 0); |
| 195 |
|
| 196 |
if (Transcript) { |
| 197 |
if (LastChar != '\n') |
| 198 |
glk_put_string_stream(Transcript, "\n"); |
| 199 |
glk_put_string_stream(Transcript, roomdescbuf); |
| 200 |
} |
| 201 |
|
| 202 |
int print_delimiter = 1; |
| 203 |
|
| 204 |
glk_window_clear(Top); |
| 205 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 206 |
int rows, length; |
| 207 |
char *text_with_breaks = LineBreakText(roomdescbuf, TopWidth, &rows, &length); |
| 208 |
|
| 209 |
glui32 bottomheight; |
| 210 |
glk_window_get_size(Bottom, NULL, &bottomheight); |
| 211 |
winid_t o2 = glk_window_get_parent(Top); |
| 212 |
if (!(bottomheight < 3 && TopHeight < rows)) { |
| 213 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 214 |
glk_window_set_arrangement(o2, winmethod_Above | winmethod_Fixed, rows, |
| 215 |
Top); |
| 216 |
} else { |
| 217 |
print_delimiter = 0; |
| 218 |
} |
| 219 |
|
| 220 |
int line = 0; |
| 221 |
int index = 0; |
| 222 |
int i; |
| 223 |
int empty_lines = 0; |
| 224 |
char string[2048]; |
| 225 |
if (TopWidth > 2047) |
| 226 |
TopWidth = 2047; |
| 227 |
for (line = 0; line < rows && index < length; line++) { |
| 228 |
for (i = 0; i < TopWidth; i++) { |
| 229 |
string[i] = text_with_breaks[index++]; |
| 230 |
if (string[i] == 10 || string[i] == 13 || index >= length) |
| 231 |
break; |
| 232 |
} |
| 233 |
if (i < TopWidth + 1) { |
| 234 |
string[i++] = '\n'; |
| 235 |
if (i < 2) |
| 236 |
empty_lines++; |
| 237 |
else |
| 238 |
empty_lines = 0; |
| 239 |
} |
| 240 |
string[i] = 0; |
| 241 |
if (strlen(string) == 0) |
| 242 |
break; |
| 243 |
glk_window_move_cursor(Top, 0, line); |
| 244 |
Display(Top, "%s", string); |
| 245 |
} |
| 246 |
|
| 247 |
line -= empty_lines; |
| 248 |
|
| 249 |
if (line < rows - 1) { |
| 250 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 251 |
glk_window_set_arrangement(o2, winmethod_Above | winmethod_Fixed, |
| 252 |
MIN(rows - 1, TopHeight - 1), Top); |
| 253 |
} |
| 254 |
|
| 255 |
free(text_with_breaks); |
| 256 |
|
| 257 |
if (print_delimiter) { |
| 258 |
PrintWindowDelimiter(); |
| 259 |
} |
| 260 |
|
| 261 |
if (roomdescbuf != NULL) { |
| 262 |
free(roomdescbuf); |
| 263 |
roomdescbuf = NULL; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void BottomWindow(void) |
| 268 |
{ |
| 269 |
WordFlush(Top); |
| 270 |
FlushRoomDescription(); |
| 271 |
WordFlush(Top); |
| 272 |
PendSpace = 0; |
| 273 |
CurrentWindow = Bottom; |
| 274 |
} |
| 275 |
|
| 276 |
void Look(void); |
| 277 |
|
| 278 |
void OpenGraphicsWindow(void); |
| 279 |
void CloseGraphicsWindow(void); |
| 280 |
|
| 281 |
void UpdateSettings(void) |
| 282 |
{ |
| 283 |
#ifdef SPATTERLIGHT |
| 284 |
if (gli_sa_delays) |
| 285 |
Options &= ~NO_DELAYS; |
| 286 |
else |
| 287 |
Options |= NO_DELAYS; |
| 288 |
|
| 289 |
switch (gli_sa_inventory) { |
| 290 |
case 0: |
| 291 |
Options &= ~(FORCE_INVENTORY | FORCE_INVENTORY_OFF); |
| 292 |
break; |
| 293 |
case 1: |
| 294 |
Options = (Options | FORCE_INVENTORY) & ~FORCE_INVENTORY_OFF; |
| 295 |
break; |
| 296 |
case 2: |
| 297 |
Options = (Options | FORCE_INVENTORY_OFF) & ~FORCE_INVENTORY; |
| 298 |
break; |
| 299 |
} |
| 300 |
|
| 301 |
switch (gli_sa_palette) { |
| 302 |
case 0: |
| 303 |
Options &= ~(FORCE_PALETTE_ZX | FORCE_PALETTE_C64); |
| 304 |
break; |
| 305 |
case 1: |
| 306 |
Options = (Options | FORCE_PALETTE_ZX) & ~FORCE_PALETTE_C64; |
| 307 |
break; |
| 308 |
case 2: |
| 309 |
Options = (Options | FORCE_PALETTE_C64) & ~FORCE_PALETTE_ZX; |
| 310 |
break; |
| 311 |
} |
| 312 |
#endif |
| 313 |
palette_type previous_pal = palchosen; |
| 314 |
if (Options & FORCE_PALETTE_ZX) |
| 315 |
palchosen = ZXOPT; |
| 316 |
else if (Options & FORCE_PALETTE_C64) { |
| 317 |
if (BaseGame == QUESTPROBE3 || BaseGame == BLIZZARD_PASS) |
| 318 |
palchosen = C64A; |
| 319 |
else |
| 320 |
palchosen = C64B; |
| 321 |
} else |
| 322 |
palchosen = Game->palette; |
| 323 |
if (palchosen != previous_pal) { |
| 324 |
DefinePalette(); |
| 325 |
Resizing = 0; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
int Resizing = 0; |
| 330 |
|
| 331 |
void Updates(event_t ev) |
| 332 |
{ |
| 333 |
if (ev.type == evtype_Arrange) { |
| 334 |
Resizing = 1; |
| 335 |
UpdateSettings(); |
| 336 |
CloseGraphicsWindow(); |
| 337 |
Look(); |
| 338 |
Resizing = 0; |
| 339 |
} else if (ev.type == evtype_Timer && TAYLOR_GRAPHICS_ENABLED) { |
| 340 |
switch (BaseGame) { |
| 341 |
case REBEL_PLANET: |
| 342 |
UpdateRebelAnimations(); |
| 343 |
break; |
| 344 |
case KAYLETH: |
| 345 |
UpdateKaylethAnimations(); |
| 346 |
break; |
| 347 |
default: |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
const glui32 OptimalPictureSize(glui32 *width, glui32 *height) |
| 354 |
{ |
| 355 |
*width = 255; |
| 356 |
*height = 96; |
| 357 |
int multiplier = 1; |
| 358 |
glui32 graphwidth, graphheight; |
| 359 |
glk_window_get_size(Graphics, &graphwidth, &graphheight); |
| 360 |
multiplier = graphheight / 96; |
| 361 |
if (255 * multiplier > graphwidth) |
| 362 |
multiplier = graphwidth / 255; |
| 363 |
|
| 364 |
if (multiplier == 0) |
| 365 |
multiplier = 1; |
| 366 |
|
| 367 |
*width = 255 * multiplier; |
| 368 |
*height = 96 * multiplier; |
| 369 |
|
| 370 |
return multiplier; |
| 371 |
} |
| 372 |
|
| 373 |
void CloseGraphicsWindow(void) |
| 374 |
{ |
| 375 |
if (Graphics == NULL) |
| 376 |
Graphics = FindGlkWindowWithRock(GLK_GRAPHICS_ROCK); |
| 377 |
if (Graphics) { |
| 378 |
glk_window_close(Graphics, NULL); |
| 379 |
Graphics = NULL; |
| 380 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
void OpenGraphicsWindow(void) |
| 385 |
{ |
| 386 |
#ifdef SPATTERLIGHT |
| 387 |
if (!gli_enable_graphics) |
| 388 |
return; |
| 389 |
#endif |
| 390 |
glui32 graphwidth, graphheight, optimal_width, optimal_height; |
| 391 |
|
| 392 |
if (Top == NULL) |
| 393 |
Top = FindGlkWindowWithRock(GLK_STATUS_ROCK); |
| 394 |
if (Graphics == NULL) |
| 395 |
Graphics = FindGlkWindowWithRock(GLK_GRAPHICS_ROCK); |
| 396 |
if (Graphics == NULL && Top != NULL) { |
| 397 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 398 |
glk_window_close(Top, NULL); |
| 399 |
Graphics = glk_window_open(Bottom, winmethod_Above | winmethod_Proportional, |
| 400 |
60, wintype_Graphics, GLK_GRAPHICS_ROCK); |
| 401 |
glk_window_get_size(Graphics, &graphwidth, &graphheight); |
| 402 |
pixel_size = OptimalPictureSize(&optimal_width, &optimal_height); |
| 403 |
x_offset = ((int)graphwidth - (int)optimal_width) / 2; |
| 404 |
|
| 405 |
if (graphheight > optimal_height) { |
| 406 |
winid_t parent = glk_window_get_parent(Graphics); |
| 407 |
glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed, |
| 408 |
optimal_height, NULL); |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
glui32 background_color; |
| 416 |
if (glk_style_measure(Bottom, style_Normal, stylehint_BackColor, |
| 417 |
&background_color)) { |
| 418 |
glk_window_set_background_color(Graphics, background_color); |
| 419 |
glk_window_clear(Graphics); |
| 420 |
} |
| 421 |
|
| 422 |
Top = glk_window_open(Bottom, winmethod_Above | winmethod_Fixed, TopHeight, |
| 423 |
wintype_TextGrid, GLK_STATUS_ROCK); |
| 424 |
glk_window_get_size(Top, &TopWidth, &TopHeight); |
| 425 |
} else { |
| 426 |
if (!Graphics) |
| 427 |
Graphics = glk_window_open(Bottom, winmethod_Above | winmethod_Proportional, 60, |
| 428 |
wintype_Graphics, GLK_GRAPHICS_ROCK); |
| 429 |
glk_window_get_size(Graphics, &graphwidth, &graphheight); |
| 430 |
pixel_size = OptimalPictureSize(&optimal_width, &optimal_height); |
| 431 |
x_offset = (graphwidth - optimal_width) / 2; |
| 432 |
winid_t parent = glk_window_get_parent(Graphics); |
| 433 |
glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed, |
| 434 |
optimal_height, NULL); |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
void OpenTopWindow(void) |
| 439 |
{ |
| 440 |
Top = FindGlkWindowWithRock(GLK_STATUS_ROCK); |
| 441 |
if (Top == NULL) { |
| 442 |
Top = glk_window_open(Bottom, winmethod_Above | winmethod_Fixed, |
| 443 |
TopHeight, wintype_TextGrid, GLK_STATUS_ROCK); |
| 444 |
if (Top == NULL) { |
| 445 |
Top = Bottom; |
| 446 |
} else { |
| 447 |
glk_window_get_size(Top, &TopWidth, NULL); |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
void DrawBlack(void) |
| 453 |
{ |
| 454 |
glk_window_fill_rect(Graphics, 0, x_offset, 0, 32 * 8 * pixel_size, |
| 455 |
12 * 8 * pixel_size); |
| 456 |
} |
| 457 |
|
| 458 |
void DrawRoomImage(void) |
| 459 |
{ |
| 460 |
if (MyLoc == 0 || (BaseGame == KAYLETH && MyLoc == 91) || NoGraphics) { |
| 461 |
return; |
| 462 |
} |
| 463 |
ClearGraphMem(); |
| 464 |
DrawTaylor(MyLoc); |
| 465 |
StartAnimations(); |
| 466 |
DrawSagaPictureFromBuffer(); |
| 467 |
} |
| 468 |
|
| 469 |
void OpenBottomWindow(void) |
| 470 |
{ |
| 471 |
Bottom = glk_window_open(0, 0, 0, wintype_TextBuffer, GLK_BUFFER_ROCK); |
| 472 |
} |
| 473 |
|
| 474 |
void DisplayInit(void) |
| 475 |
{ |
| 476 |
if (!Bottom) |
| 477 |
OpenBottomWindow(); |
| 478 |
OpenTopWindow(); |
| 479 |
UpdateSettings(); |
| 480 |
} |