1 /******************************************************************************
3 * Copyright (C) 2006-2009 by Tor Andersson. *
4 * Copyright (C) 2010 by Ben Cressey, Chris Spiegel. *
6 * This file is part of Gargoyle. *
8 * Gargoyle is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU General Public License as published by *
10 * the Free Software Foundation; either version 2 of the License, or *
11 * (at your option) any later version. *
13 * Gargoyle is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU General Public License for more details. *
18 * You should have received a copy of the GNU General Public License *
19 * along with Gargoyle; if not, write to the Free Software *
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
22 *****************************************************************************/
24 /* screen.c - Generic screen manipulation
26 * Portions copyright (c) 1995-1997 Stefan Jokisch.
31 static zchar statusline[256];
32 static int oldstyle = 0;
33 static int curstyle = 0;
36 static int fixforced = 0;
38 static int curr_fg = -2;
39 static int curr_bg = -2;
40 static int curr_font = 1;
41 static int prev_font = 1;
42 static int temp_font = 0;
44 /* To make the common code happy */
46 int os_char_width (zchar z)
51 int os_string_width (const zchar *s)
55 while ((c = *s++) != 0)
56 if (c == ZC_NEW_STYLE || c == ZC_NEW_FONT)
59 width += os_char_width(c);
63 int os_string_length (zchar *s)
66 while (*s++) length++;
70 void os_prepare_sample (int a)
72 glk_sound_load_hint(a, 1);
75 void os_finish_with_sample (int a)
77 glk_sound_load_hint(a, 0);
83 * Play the given sample at the given volume (ranging from 1 to 8 and
84 * 255 meaning a default volume). The sound is played once or several
85 * times in the background (255 meaning forever). In Z-code 3 the
86 * repeats value is always 0 and the number of repeats is taken from
87 * the sound file itself. The end_of_sound function is called as soon
88 * as the sound finishes.
92 void os_start_sample (int number, int volume, int repeats, zword eos)
98 gos_channel = glk_schannel_create(0);
105 case 1: vol = 0x02000; break;
106 case 2: vol = 0x04000; break;
107 case 3: vol = 0x06000; break;
108 case 4: vol = 0x08000; break;
109 case 5: vol = 0x0a000; break;
110 case 6: vol = 0x0c000; break;
111 case 7: vol = 0x0e000; break;
112 case 8: vol = 0x10000; break;
113 default: vol = 0x20000; break;
116 /* we dont do repeating or eos-callback for now... */
117 glk_schannel_play_ext(gos_channel, number, 1, 0);
118 glk_schannel_set_volume(gos_channel, vol);
121 void os_stop_sample (int a)
125 glk_schannel_stop(gos_channel);
128 void os_beep (int volume)
132 void gos_update_width(void)
137 glk_window_get_size(gos_upper, &width, NULL);
138 h_screen_cols = width;
139 SET_BYTE(H_SCREEN_COLS, width);
142 glk_window_move_cursor(gos_upper, 0, cury-1);
148 void gos_update_height(void)
154 glk_window_get_size(gos_upper, NULL, &height_upper);
155 glk_window_get_size(gos_lower, NULL, &height_lower);
156 h_screen_rows = height_upper + height_lower + 1;
157 SET_BYTE(H_SCREEN_ROWS, h_screen_rows);
161 void reset_status_ht(void)
166 glk_window_get_size(gos_upper, NULL, &height);
167 if (mach_status_ht != height)
169 glk_window_set_arrangement(
170 glk_window_get_parent(gos_upper),
171 winmethod_Above | winmethod_Fixed,
172 mach_status_ht, NULL);
177 void erase_window (zword w)
180 glk_window_clear(gos_lower);
184 garglk_set_reversevideo_stream(
185 glk_window_get_stream(gos_upper),
188 memset(statusline, ' ', sizeof statusline);
189 glk_window_clear(gos_upper);
195 void split_window (zword lines)
199 /* The top line is always set for V1 to V3 games */
203 if (!lines || lines > curr_status_ht)
207 glk_window_get_size(gos_upper, NULL, &height);
209 glk_window_set_arrangement(
210 glk_window_get_parent(gos_upper),
211 winmethod_Above | winmethod_Fixed,
213 curr_status_ht = lines;
215 mach_status_ht = lines;
218 glk_window_move_cursor(gos_upper, 0, 0);
224 glk_window_clear(gos_upper);
227 void restart_screen (void)
235 * statusline overflowed the window size ... bad game!
236 * so ... split status text into regions, reformat and print anew.
239 void packspaces(zchar *src, zchar *dst)
244 if (*src == 0x20202020)
258 void smartstatusline (void)
262 zchar *a, *b, *c, *d;
263 int roomlen, scorelen, scoreofs;
266 statusline[curx - 1] = 0; /* terminate! */
268 packspaces(statusline, packed);
269 //strcpy(packed, statusline);
270 len = os_string_length(packed);
277 while (b[0] != 0 && !(b[0] == ' ' && b[1] == ' '))
284 d = packed + len - 1;
285 while (d[0] == ' ' && d > c)
287 if (d[0] != ' ' && d[0] != 0)
292 //printf("smart '%s'\n", packed);
293 //printf("smart %d %d %d %d\n",a-packed,b-packed,c-packed,d-packed);
297 scoreofs = h_screen_cols - scorelen - 2;
298 if (scoreofs <= roomlen)
299 scoreofs = roomlen + 2;
301 for (tmp = 0; tmp < h_screen_cols; tmp++)
304 memcpy(buf + 1 + scoreofs, c, scorelen * sizeof(zchar));
305 memcpy(buf + 1, a, roomlen * sizeof(zchar));
306 //if (roomlen >= scoreofs)
307 // buf[roomlen + 1] = '|';
309 glk_window_move_cursor(gos_upper, 0, 0);
310 glk_put_buffer_uni(buf, h_screen_cols);
311 glk_window_move_cursor(gos_upper, cury - 1, curx - 1);
314 void screen_char (zchar c)
316 if (gos_linepending && (gos_curwin == gos_linewin))
318 gos_cancel_pending_line();
319 if (gos_curwin == gos_upper)
328 /* check fixed flag in header, game can change it at whim */
329 int forcefix = ((h_flags & FIXED_FONT_FLAG) != 0);
330 int curfix = ((curstyle & FIXED_WIDTH_STYLE) != 0);
331 if (forcefix && !curfix)
333 zargs[0] = 0xf000; /* tickle tickle! */
337 else if (!forcefix && fixforced)
339 zargs[0] = 0xf000; /* tickle tickle! */
344 if (gos_upper && gos_curwin == gos_upper)
346 if (c == '\n' || c == ZC_RETURN) {
354 if (curx < sizeof statusline)
355 statusline[curx - 1] = c;
357 if (curx <= h_screen_cols)
366 if (curx > h_screen_cols) {
373 else if (gos_curwin == gos_lower)
377 else glk_put_char_uni(c);
381 void screen_new_line (void)
386 void screen_word (const zchar *s)
389 while ((c = *s++) != 0)
390 if (c == ZC_NEW_FONT)
392 else if (c == ZC_NEW_STYLE)
398 void screen_mssg_on (void)
400 if (gos_curwin == gos_lower)
403 glk_set_style(style_Preformatted);
404 glk_put_string("\n ");
408 void screen_mssg_off (void)
410 if (gos_curwin == gos_lower)
421 * z_buffer_mode, turn text buffering on/off.
423 * zargs[0] = new text buffering flag (0 or 1)
427 void z_buffer_mode (void)
432 * z_buffer_screen, set the screen buffering mode.
438 void z_buffer_screen (void)
444 * z_erase_line, erase the line starting at the cursor position.
446 * zargs[0] = 1 + #units to erase (1 clears to the end of the line)
450 void z_erase_line (void)
454 if (gos_upper && gos_curwin == gos_upper)
456 for (i = 0; i < h_screen_cols + 1 - curx; i++)
458 glk_window_move_cursor(gos_curwin, curx - 1, cury - 1);
463 * z_erase_window, erase a window or the screen to background colour.
465 * zargs[0] = window (-3 current, -2 screen, -1 screen & unsplit)
469 void z_erase_window (void)
475 glk_set_window(gos_upper);
477 garglk_set_zcolors(curr_fg, curr_bg);
479 glk_window_clear(gos_upper);
480 glk_set_window(gos_curwin);
482 glk_window_clear(gos_lower);
487 glk_set_window(gos_upper);
489 garglk_set_zcolors(curr_fg, curr_bg);
491 glk_window_clear(gos_upper);
493 glk_window_clear(gos_lower);
495 glk_set_window(gos_lower);
496 gos_curwin = gos_lower;
499 glk_window_clear(gos_lower);
500 if (w == 1 && gos_upper)
501 glk_window_clear(gos_upper);
505 * z_get_cursor, write the cursor coordinates into a table.
507 * zargs[0] = address to write information to
511 void z_get_cursor (void)
513 storew ((zword) (zargs[0] + 0), cury);
514 storew ((zword) (zargs[0] + 2), curx);
518 * z_print_table, print ASCII text in a rectangular area.
520 * zargs[0] = address of text to be printed
521 * zargs[1] = width of rectangular area
522 * zargs[2] = height of rectangular area (optional)
523 * zargs[3] = number of char's to skip between lines (optional)
527 void z_print_table (void)
529 zword addr = zargs[0];
533 /* Supply default arguments */
540 /* Write text in width x height rectangle */
544 for (i = 0; i < zargs[2]; i++) {
551 for (j = 0; j < zargs[1]; j++) {
565 #define zB(i) ((((i >> 10) & 0x1F) << 3) | (((i >> 10) & 0x1F) >> 2))
566 #define zG(i) ((((i >> 5) & 0x1F) << 3) | (((i >> 5) & 0x1F) >> 2))
567 #define zR(i) ((((i ) & 0x1F) << 3) | (((i ) & 0x1F) >> 2))
569 #define zRGB(i) (zR(i) << 16 | zG(i) << 8 | zB(i))
572 * z_set_true_colour, set the foreground and background colours
573 * to specific RGB colour values.
575 * zargs[0] = foreground colour
576 * zargs[1] = background colour
577 * zargs[2] = window (-3 is the current one, optional)
581 void z_set_true_colour (void)
583 int zfore = zargs[0];
584 int zback = zargs[1];
587 zfore = zRGB(zargs[0]);
590 zback = zRGB(zargs[1]);
593 garglk_set_zcolors(zfore, zback);
600 static int zcolor_map[] = {
601 -2, /* 0 = current */
602 -1, /* 1 = default */
603 0x0000, /* 2 = black */
604 0x001D, /* 3 = red */
605 0x0340, /* 4 = green */
606 0x03BD, /* 5 = yellow */
607 0x59A0, /* 6 = blue */
608 0x7C1F, /* 7 = magenta */
609 0x77A0, /* 8 = cyan */
610 0x7FFF, /* 9 = white */
611 0x5AD6, /* 10 = light grey */
612 0x4631, /* 11 = medium grey */
613 0x2D6B, /* 12 = dark grey */
616 #define zcolor_NUMCOLORS (13)
619 * z_set_colour, set the foreground and background colours.
621 * zargs[0] = foreground colour
622 * zargs[1] = background colour
623 * zargs[2] = window (-3 is the current one, optional)
627 void z_set_colour (void)
629 int zfore = zargs[0];
630 int zback = zargs[1];
639 zfore = zcolor_map[zfore];
643 if (zfore < zcolor_NUMCOLORS)
644 zfore = zRGB(zcolor_map[zfore]);
655 zback = zcolor_map[zback];
659 if (zback < zcolor_NUMCOLORS)
660 zback = zRGB(zcolor_map[zback]);
665 garglk_set_zcolors(zfore, zback);
673 * z_set_font, set the font for text output and store the previous font.
675 * zargs[0] = number of font or 0 to keep current font
679 void z_set_font (void)
681 zword font = zargs[0];
685 case 0: /* previous font */
686 temp_font = curr_font;
687 curr_font = prev_font;
688 prev_font = temp_font;
689 zargs[0] = 0xf000; /* tickle tickle! */
694 case 1: /* normal font */
695 prev_font = curr_font;
697 zargs[0] = 0xf000; /* tickle tickle! */
702 case 4: /* fixed-pitch font*/
703 prev_font = curr_font;
705 zargs[0] = 0xf000; /* tickle tickle! */
710 case 2: /* picture font, undefined per 1.1 */
711 case 3: /* character graphics font */
712 default: /* unavailable */
719 * z_set_cursor, set the cursor position or turn the cursor on/off.
721 * zargs[0] = y-coordinate or -2/-1 for cursor on/off
722 * zargs[1] = x-coordinate
723 * zargs[2] = window (-3 is the current one, optional)
727 void z_set_cursor (void)
733 if (cury > mach_status_ht) {
734 mach_status_ht = cury;
738 glk_window_move_cursor(gos_upper, curx - 1, cury - 1);
743 * z_set_text_style, set the style for text output.
745 * zargs[0] = style flags to set or 0 to reset text style
749 void z_set_text_style (void)
755 else if (zargs[0] != 0xf000) /* not tickle time */
756 curstyle |= zargs[0];
758 if (h_flags & FIXED_FONT_FLAG || curr_font == 4)
759 style = curstyle | FIXED_WIDTH_STYLE;
763 if (gos_linepending && gos_curwin == gos_linewin)
766 if (style & REVERSE_STYLE)
769 garglk_set_reversevideo(TRUE);
773 if (style & FIXED_WIDTH_STYLE)
775 if (style & BOLDFACE_STYLE && style & EMPHASIS_STYLE)
776 glk_set_style(style_BlockQuote); /* monoz */
777 else if (style & EMPHASIS_STYLE)
778 glk_set_style(style_Alert); /* monoi */
779 else if (style & BOLDFACE_STYLE)
780 glk_set_style(style_Subheader); /* monob */
782 glk_set_style(style_Preformatted); /* monor */
786 if (style & BOLDFACE_STYLE && style & EMPHASIS_STYLE)
787 glk_set_style(style_Note); /* propz */
788 else if (style & EMPHASIS_STYLE)
789 glk_set_style(style_Emphasized); /* propi */
790 else if (style & BOLDFACE_STYLE)
791 glk_set_style(style_Header); /* propb */
793 glk_set_style(style_Normal); /* propr */
799 garglk_set_reversevideo(FALSE);
806 * z_set_window, select the current window.
808 * zargs[0] = window to be selected (-3 is the current one)
812 void z_set_window (void)
818 glk_set_window(gos_lower);
819 gos_curwin = gos_lower;
824 glk_set_window(gos_upper);
825 gos_curwin = gos_upper;
829 enable_scripting = TRUE;
831 enable_scripting = FALSE;
833 zargs[0] = 0xf000; /* tickle tickle! */
838 * z_show_status, display the status line for V1 to V3 games.
844 static void pad_status_line (int column)
847 spaces = (h_screen_cols + 1 - curx) - column;
852 void z_show_status (void)
864 /* One V5 game (Wishbringer Solid Gold) contains this opcode by
865 accident, so just return if the version number does not fit */
870 /* Read all relevant global variables from the memory of the
871 Z-machine into local variables */
874 LOW_WORD (addr, global0)
876 LOW_WORD (addr, global1)
878 LOW_WORD (addr, global2)
880 /* Move to top of the status window, and print in reverse style. */
882 glk_set_window(gos_upper);
883 gos_curwin = gos_upper;
886 glk_window_move_cursor(gos_upper, 0, 0);
888 /* If the screen width is below 55 characters then we have to use
889 the brief status line format */
891 if (h_screen_cols < 55)
894 /* Print the object description for the global variable 0 */
897 print_object (global0);
899 /* A header flag tells us whether we have to display the current
900 time or the score/moves information */
902 if (h_config & CONFIG_TIME) { /* print hours and minutes */
904 zword hours = (global1 + 11) % 12 + 1;
906 pad_status_line (brief ? 15 : 20);
908 print_string ("Time: ");
922 print_char ((global1 >= 12) ? 'p' : 'a');
925 } else { /* print score and moves */
927 pad_status_line (brief ? 15 : 30);
929 print_string (brief ? "S: " : "Score: ");
932 pad_status_line (brief ? 8 : 14);
934 print_string (brief ? "M: " : "Moves: ");
939 /* Pad the end of the status line with spaces */
943 /* Return to the lower window */
945 glk_set_window(gos_lower);
946 gos_curwin = gos_lower;
950 * z_split_window, split the screen into an upper (1) and lower (0) window.
952 * zargs[0] = height of upper window in screen units (V6) or #lines
956 void z_split_window (void)
958 split_window(zargs[0]);