Fixed resize bug
[rodin/chimara.git] / interpreters / frotz / glkscreen.c
1 /******************************************************************************
2  *                                                                            *
3  * Copyright (C) 2006-2009 by Tor Andersson.                                  *
4  *                                                                            *
5  * This file is part of Gargoyle.                                             *
6  *                                                                            *
7  * Gargoyle is free software; you can redistribute it and/or modify           *
8  * it under the terms of the GNU General Public License as published by       *
9  * the Free Software Foundation; either version 2 of the License, or          *
10  * (at your option) any later version.                                        *
11  *                                                                            *
12  * Gargoyle is distributed in the hope that it will be useful,                *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
15  * GNU General Public License for more details.                               *
16  *                                                                            *
17  * You should have received a copy of the GNU General Public License          *
18  * along with Gargoyle; if not, write to the Free Software                    *
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
20  *                                                                            *
21  *****************************************************************************/
22
23 /* screen.c - Generic screen manipulation
24  *
25  *  Portions copyright (c) 1995-1997 Stefan Jokisch.
26  */
27
28 #include "glkfrotz.h"
29
30 static unsigned char statusline[256];
31 static int oldstyle = 0;
32 static int curstyle = 0;
33 static int upperstyle = 0;
34 static int lowerstyle = 0;
35 static int cury = 1;
36 static int curx = 1;
37
38 int curr_fg = 0;
39 int curr_bg = 0;
40
41 /* To make the common code happy */
42
43 int os_char_width (zchar z)
44 {
45         return 1;
46 }
47
48 int os_string_width (const zchar *s)
49 {
50         int width = 0;
51         zchar c;
52         while ((c = *s++) != 0)
53                 if (c == ZC_NEW_STYLE || c == ZC_NEW_FONT)
54                         s++;
55                 else
56                         width += os_char_width(c);
57         return width;
58 }
59
60 void os_prepare_sample (int a)
61 {
62         glk_sound_load_hint(a, 1);
63 }
64
65 void os_finish_with_sample (int a)
66 {
67         glk_sound_load_hint(a, 0);
68 }
69
70 /*
71  * os_start_sample
72  *
73  * Play the given sample at the given volume (ranging from 1 to 8 and
74  * 255 meaning a default volume). The sound is played once or several
75  * times in the background (255 meaning forever). In Z-code 3 the
76  * repeats value is always 0 and the number of repeats is taken from
77  * the sound file itself. The end_of_sound function is called as soon
78  * as the sound finishes.
79  *
80  */
81
82 void os_start_sample (int number, int volume, int repeats, zword eos)
83 {
84         int vol;
85
86         if (!gos_channel)
87         {
88                 gos_channel = glk_schannel_create(0);
89                 if (!gos_channel)
90                         return;
91         }
92
93         switch (volume)
94         {
95         case   1: vol = 0x02000; break;
96         case   2: vol = 0x04000; break;
97         case   3: vol = 0x06000; break;
98         case   4: vol = 0x08000; break;
99         case   5: vol = 0x0a000; break;
100         case   6: vol = 0x0c000; break;
101         case   7: vol = 0x0e000; break;
102         case   8: vol = 0x10000; break;
103         default:  vol = 0x20000; break;
104         }
105
106         /* we dont do repeating or eos-callback for now... */
107         glk_schannel_play_ext(gos_channel, number, 1, 0);
108         glk_schannel_set_volume(gos_channel, vol);
109 }
110
111 void os_stop_sample (int a)
112 {
113         if (!gos_channel)
114                 return;
115         glk_schannel_stop(gos_channel);
116 }
117
118 void os_beep (int volume)
119 {
120 }
121
122 void gos_update_width(void)
123 {
124         glui32 width;
125         if (gos_upper)
126         {
127                 glk_window_get_size(gos_upper, &width, NULL);
128                 h_screen_cols = width;
129                 SET_BYTE(H_SCREEN_COLS, width);
130                 if (curx > width)
131                 {
132                         glk_window_move_cursor(gos_upper, 0, cury-1);
133                         curx = 1;
134                 }
135         }
136 }
137
138 void gos_update_height(void)
139 {
140         glui32 height_upper;
141         glui32 height_lower;
142         if (gos_curwin)
143         {
144                 glk_window_get_size(gos_upper, NULL, &height_upper);
145                 glk_window_get_size(gos_lower, NULL, &height_lower);
146                 h_screen_rows = height_upper + height_lower + 1;
147                 SET_BYTE(H_SCREEN_ROWS, h_screen_rows);
148         }
149 }
150
151 void reset_status_ht(void)
152 {
153         glui32 height;
154         if (gos_upper)
155         {
156                 glk_window_get_size(gos_upper, NULL, &height);
157                 if (mach_status_ht != height)
158                         glk_window_set_arrangement(
159                                 glk_window_get_parent(gos_upper),
160                                 winmethod_Above | winmethod_Fixed,
161                                 mach_status_ht, NULL);
162         }
163 }
164
165 void erase_window (int w)
166 {
167         if (w == 0)
168                 glk_window_clear(gos_lower);
169         else if (gos_upper)
170         {
171                 memset(statusline, ' ', sizeof statusline);
172                 glk_window_clear(gos_upper);
173                 reset_status_ht();
174                 curr_status_ht = 0;
175         }
176 }
177
178 void split_window (int lines)
179 {
180         if (!gos_upper)
181                 return;
182         /* The top line is always set for V1 to V3 games */
183         if (h_version < V4)
184                 lines++;
185
186         if (!lines || lines > curr_status_ht)
187         {
188                 glui32 height;
189
190                 glk_window_get_size(gos_upper, NULL, &height);
191                 if (lines != height)
192                         glk_window_set_arrangement(
193                                 glk_window_get_parent(gos_upper),
194                                 winmethod_Above | winmethod_Fixed,
195                                 lines, NULL);
196                 curr_status_ht = lines;
197         }
198         mach_status_ht = lines;
199         if (cury > lines)
200         {
201                 glk_window_move_cursor(gos_upper, 0, 0);
202                 curx = cury = 1;
203         }
204         gos_update_width();
205
206         if (h_version == V3)
207                 glk_window_clear(gos_upper);
208 }
209
210 void restart_screen (void)
211 {
212         erase_window(0);
213         erase_window(1);
214         split_window(0);
215 }
216
217 /*
218  * statusline overflowed the window size ... bad game!
219  * so ... split status text into regions, reformat and print anew.
220  */
221
222 void packspaces(unsigned char *src, unsigned char *dst)
223 {
224         int killing = 0;
225         while (*src)
226         {
227                 if (*src == ' ')
228                         killing++;
229                 else
230                         killing = 0;
231                 if (killing > 2)
232                         src++;
233                 else
234                         *dst++ = *src++;
235         }
236         *dst = 0;
237 }
238
239 void smartstatusline (void)
240 {
241         unsigned char packed[256];
242         unsigned char buf[256];
243         unsigned char *a, *b, *c, *d;
244         int roomlen, scorelen, scoreofs;
245         int len;
246
247         statusline[curx - 1] = 0; /* terminate! */
248
249         packspaces(statusline, packed);
250         //strcpy(packed, statusline);
251         len = strlen(packed);
252
253         a = packed;
254         while (a[0] == ' ')
255                 a ++;
256
257         b = a;
258         while (b[0] != 0 && !(b[0] == ' ' && b[1] == ' '))
259                 b ++;
260
261         c = b;
262         while (c[0] == ' ')
263                 c ++;
264
265         d = packed + len - 1;
266         while (d[0] == ' ' && d > c)
267                 d --;
268         if (d[0] != ' ' && d[0] != 0)
269                 d ++;
270         if (d < c)
271                 d = c;
272
273         //printf("smart '%s'\n", packed);
274         //printf("smart %d %d %d %d\n",a-packed,b-packed,c-packed,d-packed);
275
276         roomlen = b - a;
277         scorelen = d - c;
278         scoreofs = h_screen_cols - scorelen - 2;
279         if (scoreofs <= roomlen)
280                 scoreofs = roomlen + 2;
281
282         memset(buf, ' ', h_screen_cols);
283         memcpy(buf + 1 + scoreofs, c, scorelen);
284         memcpy(buf + 1, a, roomlen);
285         //if (roomlen >= scoreofs)
286         //      buf[roomlen + 1] = '|';
287
288         glk_window_move_cursor(gos_upper, 0, 0);
289         glk_set_style(style_User1);
290         glk_put_buffer(buf, h_screen_cols);
291         glk_window_move_cursor(gos_upper, cury - 1, curx - 1);
292 }
293
294 void screen_char (zchar c)
295 {
296         if (gos_linepending && (gos_curwin == gos_linewin))
297         {
298                 gos_cancel_pending_line();
299                 if (gos_curwin == gos_upper)
300                 {
301                         curx = 1;
302                         cury ++;
303                 }
304                 if (c == '\n')
305                         return;
306         }
307
308         /* check fixed flag in header, game can change it at whim */
309         if (gos_curwin == gos_lower)
310         {
311                 static int forcefix = -1;
312                 int curfix = h_flags & FIXED_FONT_FLAG;
313                 if (forcefix != curfix)
314                 {
315                         forcefix = curfix;
316                         zargs[0] = 0xf000;      /* tickle tickle! */
317                         z_set_text_style();
318                 }
319         }
320
321         if (gos_upper && gos_curwin == gos_upper)
322         {
323                 if (c == '\n' || c == ZC_RETURN) {
324                         glk_put_char('\n');
325                         curx = 1;
326                         cury ++;
327                 }
328                 else {
329                         if (cury == 1)
330                         {
331                                 if (curx < sizeof statusline)
332                                         statusline[curx - 1] = c;
333                                 curx++;
334                                 if (curx <= h_screen_cols)
335                                         glk_put_char(c);
336                                 else
337                                         smartstatusline();
338                         }
339                         else
340                         {
341                                 glk_put_char(c);
342                                 curx++;
343                                 if (curx > h_screen_cols) {
344                                         curx = 1;
345                                         cury++;
346                                 }
347                         }
348                 }
349         }
350         else if (gos_curwin == gos_lower)
351         {
352                 if (c == ZC_RETURN)
353                         glk_put_char('\n');
354                 else glk_put_char(c);
355         }
356 }
357
358 void screen_new_line (void)
359 {
360         screen_char('\n');
361 }
362
363 void screen_word (const zchar *s)
364 {
365         zchar c;
366         while ((c = *s++) != 0)
367                 if (c == ZC_NEW_FONT)
368                         s++;
369                 else if (c == ZC_NEW_STYLE)
370                         s++;
371                 else
372                         screen_char (c); 
373 }
374
375 void screen_mssg_on (void)
376 {
377         if (gos_curwin == gos_lower)
378         {
379                 oldstyle = curstyle;
380                 glk_set_style(style_Preformatted);
381                 glk_put_string("\n    ");
382         }
383 }
384
385 void screen_mssg_off (void)
386 {
387         if (gos_curwin == gos_lower)
388         {
389                 glk_put_char('\n');
390                 zargs[0] = 0;
391                 z_set_text_style();
392                 zargs[0] = oldstyle;
393                 z_set_text_style();
394         }
395 }
396
397 /*
398  * z_buffer_mode, turn text buffering on/off.
399  *
400  *              zargs[0] = new text buffering flag (0 or 1)
401  *
402  */
403
404 void z_buffer_mode (void)
405 {
406 }
407
408 /*
409  * z_erase_line, erase the line starting at the cursor position.
410  *
411  *              zargs[0] = 1 + #units to erase (1 clears to the end of the line)
412  *
413  */
414
415 void z_erase_line (void)
416 {
417         int i;
418
419         if (gos_upper && gos_curwin == gos_upper)
420         {
421                 for (i = 0; i < h_screen_cols + 1 - curx; i++)
422                         glk_put_char(' ');
423                 glk_window_move_cursor(gos_curwin, curx - 1, cury - 1);
424         }
425 }
426
427 /*
428  * z_erase_window, erase a window or the screen to background colour.
429  *
430  *              zargs[0] = window (-3 current, -2 screen, -1 screen & unsplit)
431  *
432  */
433
434 void z_erase_window (void)
435 {
436         short w = zargs[0];
437         if (w == -2)
438         {
439                 if (gos_upper) {
440                         glk_set_window(gos_upper);
441 #ifdef GARGLK
442                         garglk_set_zcolors(curr_fg, curr_bg);
443 #endif /* GARGLK */
444                         glk_window_clear(gos_upper);
445                         glk_set_window(gos_curwin);
446                 }
447                 glk_window_clear(gos_lower);
448         }
449         if (w == -1)
450         {
451                 if (gos_upper) {
452                         glk_set_window(gos_upper);
453 #ifdef GARGLK
454                         garglk_set_zcolors(curr_fg, curr_bg);
455 #endif /* GARGLK */
456                         glk_window_clear(gos_upper);
457                 }
458                 glk_window_clear(gos_lower);
459                 split_window(0);
460                 glk_set_window(gos_lower);
461                 gos_curwin = gos_lower;
462         }
463         if (w == 0)
464                 glk_window_clear(gos_lower);
465         if (w == 1 && gos_upper)
466                 glk_window_clear(gos_upper);
467 }
468
469 /*
470  * z_get_cursor, write the cursor coordinates into a table.
471  *
472  *              zargs[0] = address to write information to
473  *
474  */
475
476 void z_get_cursor (void)
477 {
478         storew ((zword) (zargs[0] + 0), cury);
479         storew ((zword) (zargs[0] + 2), curx);
480 }
481
482 /*
483  * z_print_table, print ASCII text in a rectangular area.
484  *
485  *              zargs[0] = address of text to be printed
486  *              zargs[1] = width of rectangular area
487  *              zargs[2] = height of rectangular area (optional)
488  *              zargs[3] = number of char's to skip between lines (optional)
489  *
490  */
491
492 void z_print_table (void)
493 {
494         zword addr = zargs[0];
495         zword x;
496         int i, j;
497
498         /* Supply default arguments */
499
500         if (zargc < 3)
501                 zargs[2] = 1;
502         if (zargc < 4)
503                 zargs[3] = 0;
504
505         /* Write text in width x height rectangle */
506
507         x = curx;
508
509         for (i = 0; i < zargs[2]; i++) {
510
511                 if (i != 0) {
512                         cury += 1;
513                         curx = x;
514                 }
515
516                 for (j = 0; j < zargs[1]; j++) {
517
518                         zbyte c;
519
520                         LOW_BYTE (addr, c)
521                         addr++;
522
523                         print_char (c);
524                 }
525
526                 addr += zargs[3];
527         }
528 }
529
530 /*
531  * z_set_colour, set the foreground and background colours.
532  *
533  *              zargs[0] = foreground colour
534  *              zargs[1] = background colour
535  *              zargs[2] = window (-3 is the current one, optional)
536  *
537  */
538
539 void z_set_colour (void)
540 {
541         int zfore = zargs[0];
542         int zback = zargs[1];
543
544
545         if (!(zfore == 0 && zback == 0)) {
546 #ifdef GARGLK
547                 garglk_set_zcolors(zfore, zback);
548 #endif /* GARGLK */
549         }
550
551         curr_fg = zfore;
552         curr_bg = zback;
553 }
554
555 /*
556  * z_set_font, set the font for text output and store the previous font.
557  *
558  *               zargs[0] = number of font or 0 to keep current font
559  *
560  */
561
562 void z_set_font (void)
563 {
564 }
565
566 /*
567  * z_set_cursor, set the cursor position or turn the cursor on/off.
568  *
569  *              zargs[0] = y-coordinate or -2/-1 for cursor on/off
570  *              zargs[1] = x-coordinate
571  *              zargs[2] = window (-3 is the current one, optional)
572  *
573  */
574
575 void z_set_cursor (void)
576 {
577         cury = zargs[0];
578         curx = zargs[1];
579
580         if (gos_upper) {
581                 if (cury > mach_status_ht) {
582                         mach_status_ht = cury;
583                         reset_status_ht();
584                 }
585
586                 glk_window_move_cursor(gos_upper, curx - 1, cury - 1);
587         }
588 }
589
590 /*
591  * z_set_text_style, set the style for text output.
592  *
593  *               zargs[0] = style flags to set or 0 to reset text style
594  *
595  */
596
597 void z_set_text_style (void)
598 {
599         int style;
600
601         if (zargs[0] == 0)
602                 curstyle = 0;
603         else if (zargs[0] != 0xf000) /* not tickle time */
604                 curstyle |= zargs[0];
605
606         if (h_flags & FIXED_FONT_FLAG)
607                 style = curstyle | FIXED_WIDTH_STYLE;
608         else
609                 style = curstyle;
610
611         if (gos_linepending && gos_curwin == gos_linewin)
612                 return;
613
614         if (style & REVERSE_STYLE)
615         {
616                 if (gos_curwin == gos_upper && gos_upper) {
617                         glk_set_style(style_User1);
618                 }
619 #ifdef GARGLK
620                 garglk_set_reversevideo(TRUE);
621 #endif /* GARGLK */
622         }
623         else if (style & FIXED_WIDTH_STYLE)
624                 glk_set_style(style_Preformatted);
625         else if (style & BOLDFACE_STYLE && style & EMPHASIS_STYLE)
626                 glk_set_style(style_Alert);
627         else if (style & BOLDFACE_STYLE)
628                 glk_set_style(style_Subheader);
629         else if (style & EMPHASIS_STYLE)
630                 glk_set_style(style_Emphasized);
631         else
632                 glk_set_style(style_Normal);
633
634         if (curstyle == 0) {
635 #ifdef GARGLK
636                 garglk_set_reversevideo(FALSE);
637 #endif /* GARGLK */
638         }
639 }
640
641 /*
642  * z_set_window, select the current window.
643  *
644  *              zargs[0] = window to be selected (-3 is the current one)
645  *
646  */
647
648 void z_set_window (void)
649 {
650         int win = zargs[0];
651
652         if (gos_curwin == gos_lower)
653                 lowerstyle = curstyle;
654         else
655                 upperstyle = curstyle;
656
657         if (win == 0)
658         {
659                 glk_set_window(gos_lower);
660                 gos_curwin = gos_lower;
661                 curstyle = lowerstyle;
662         }
663         else
664         {
665                 if (gos_upper)
666                         glk_set_window(gos_upper);
667                 gos_curwin = gos_upper;
668                 curstyle = upperstyle;
669         }
670
671         if (win == 0)
672             enable_scripting = TRUE;
673         else
674             enable_scripting = FALSE;
675 }
676
677 /*
678  * z_show_status, display the status line for V1 to V3 games.
679  *
680  *              no zargs used
681  *
682  */
683
684 static void pad_status_line (int column)
685 {
686         int spaces;
687         spaces = (h_screen_cols + 1 - curx) - column;
688         while (spaces-- > 0)
689                 print_char(' ');
690 }
691
692 void z_show_status (void)
693 {
694         zword global0;
695         zword global1;
696         zword global2;
697         zword addr;
698
699         bool brief = FALSE;
700
701         if (!gos_upper)
702                 return;
703
704         /* One V5 game (Wishbringer Solid Gold) contains this opcode by
705            accident, so just return if the version number does not fit */
706
707         if (h_version >= V4)
708                 return;
709
710         /* Read all relevant global variables from the memory of the
711            Z-machine into local variables */
712
713         addr = h_globals;
714         LOW_WORD (addr, global0)
715         addr += 2;
716         LOW_WORD (addr, global1)
717         addr += 2;
718         LOW_WORD (addr, global2)
719
720         /* Move to top of the status window, and print in reverse style. */
721
722         glk_set_window(gos_upper);
723         gos_curwin = gos_upper;
724
725         curx = cury = 1;
726         glk_window_move_cursor(gos_upper, 0, 0);
727         glk_set_style(style_User1);
728
729         /* If the screen width is below 55 characters then we have to use
730            the brief status line format */
731
732         if (h_screen_cols < 55)
733                 brief = TRUE;
734
735         /* Print the object description for the global variable 0 */
736
737         print_char (' ');
738         print_object (global0);
739
740         /* A header flag tells us whether we have to display the current
741            time or the score/moves information */
742
743         if (h_config & CONFIG_TIME) {           /* print hours and minutes */
744
745                 zword hours = (global1 + 11) % 12 + 1;
746
747                 pad_status_line (brief ? 15 : 20);
748
749                 print_string ("Time: ");
750
751                 if (hours < 10)
752                         print_char (' ');
753                 print_num (hours);
754
755                 print_char (':');
756
757                 if (global2 < 10)
758                         print_char ('0');
759                 print_num (global2);
760
761                 print_char (' ');
762
763                 print_char ((global1 >= 12) ? 'p' : 'a');
764                 print_char ('m');
765
766         } else {                                                                /* print score and moves */
767
768                 pad_status_line (brief ? 15 : 30);
769
770                 print_string (brief ? "S: " : "Score: ");
771                 print_num (global1);
772
773                 pad_status_line (brief ? 8 : 14);
774
775                 print_string (brief ? "M: " : "Moves: ");
776                 print_num (global2);
777
778         }
779
780         /* Pad the end of the status line with spaces */
781
782         pad_status_line (0);
783
784         /* Return to the lower window */
785
786         glk_set_window(gos_lower);
787         gos_curwin = gos_lower;
788 }
789
790 /*
791  * z_split_window, split the screen into an upper (1) and lower (0) window.
792  *
793  *              zargs[0] = height of upper window in screen units (V6) or #lines
794  *
795  */
796
797 void z_split_window (void)
798 {
799         split_window(zargs[0]);
800 }
801