Posted below is the complete code for the picture-displaying module that I’ve been working on, bolted onto a one-room game. Briefly, it works like this: the game displays a thumbnail image in the main text window. When the player clicks on the thumbnail, the game opens up a graphics window and displays the full-size picture. When the player clicks on the picture OR presses any key, the graphics window disappears and play resumes in the main text window.
It mostly works, except for the following problems:
- I can’t get the game to accept a ‘press any key’ event.
- I can’t get the status line to revert to normal behavior after the picture goes away.
- The game won’t print a command line after the picture goes away (minor - I can fake this)
I’m so close to getting this working, but I’ve kind of hit a wall. I’m posting the entire code in the hopes someone with a better knowledge of glk might have some insight. I would be incredibly grateful.
Note: the Windows Inform IDE doesn’t support graphics in the main text window, apparently, so to actually see this in action you have to fire up the blorb and run it in Glulx interpreter. I’ve attached the blorb file at the bottom of this post, along with the two graphics files in case anyone wants to try compiling this at home.
[spoiler][code]Include Basic Screen Effects by Emily Short.
Include Glulx Entry Points by Emily Short.
Include Glulx Text Effects by Emily Short.
[The overall concept is this: At various points throughout the game, small “thumbnail” graphics will appear in the main window. These are hyperlinked. If the player clicks a thumbnail:
1) The game opens up a graphical window that covers the main window entirely (it splits the main window, but takes up 100% of the window space) and displays a larger illustration.
2) The game also displays a caption for the picture in the status line.
3) If the player clicks on the larger illustration, OR presses any key, the graphical window closes and the player is returned to normal gameplay.
Here are the problems so far:
1. I cannot get the game to accept a character event while the graphical window is open. The mouse-click works fine, but game does not respond to keyboard input at all until after the graphical window is closed.
2. If the player presses the ENTER key while the graphical window is open, the status line reverts to its normal behavior (displaying the room name), while the graphical window stays up.
2.5 Related to item 2, after the graphics window closes, the status line won't rever to its normal behavior UNTIL the player hits the Enter key.
3. When the player clicks on the illustration, the window closes and gameplay resumes as it should, BUT the game won't print a command prompt (">") on its own, and the player's input is in the wrong font. I can fake this if I have to, but it'd be nice to know why this is happening.]
Rule for constructing the status line:
center “[the player’s surroundings]” at row 1;
rule succeeds.
Test Chamber is a room. “[Figure - Red Button]Not much to do here, [if Figure - Red Button has been displayed]now that you’ve pushed[otherwise]other than push[end if] that big, shiny red button in the corner.” The big shiny red button is scenery in the Test Chamber. Instead of doing something with the button, say “Just click it with your mouse.”
Figure - Red Button is the file “red_button.png”.
Figure - Gorilla Shark is the file “gorilla_shark.png”.
Section - the table of illustrations
[In the full game, this table will match up all the full illustrations with their thumbnails and caption text.]
Table of Illustrations
link-number illustration-name thumb-name display-status caption
102 Figure - Gorilla Shark Figure - Red Button false “Totally Awesome!!”
To decide if (F - a figure-name) has been displayed:
if the display-status corresponding to a thumb-name of F in the Table of Illustrations is false, no;
otherwise yes.
Section - displaying thumbnails
[This part all works pretty much the way it’s supposed to, assuming the interpreter supports graphics in the main window.]
To say (F - a figure-name):
if F has been displayed:
do nothing;
otherwise:
let N be the link-number corresponding to a thumb-name of F in the Table of Illustrations;
display F with link N.
To display (F - a figure-name) with link (N - a number):
(- DrawThumb({F},{N}); -).
Include (-
[ DrawThumb F N;
glk_request_hyperlink_event(gg_mainwin);
glk_set_hyperlink(N);
glk_image_draw(gg_mainwin, ResourceIDsOfFigures–>F, imagealign_MarginLeft, 0);
glk_set_hyperlink(0);
];
-).
Section - handling input
[The handling for the hyperlink event works more or less as it should. The code that puts the figure caption in the the status line works, but causes the main window to malfunction once the graphical window is closed.]
Glulx input handling rule for a hyperlink-event:
let N be the character code returned;
let C be the caption corresponding to a link-number of N in the Table of Illustrations;
let F be the illustration-name corresponding to a link-number of N in the Table of Illustrations;
reveal the full illustration of F;
clear only the status line;
center “[C]” at row 1;
now the display-status corresponding to a link-number of N in the Table of Illustrations is true.
Glulx input handling rule for a mouse-event: [this rule does what it should]
return to play.
Glulx input handling rule for a char-event when the illustration window is open: [this rule never fires while the graphical window is open]
return to play.
To decide if the illustration window is open:
(- (gg_illustration_window) -).
[This is the routine that should run when the player clicks on the big illustration or hits any key. It gets called in the former case but never in the latter. It should do the following things:
1. close the illustration window;
3. reconstruct the standard status line;
2. clear the main window screen;
4. process a LOOK command, so that the player can see the room description.
Note, steps 1 and 3 don't have to happen in any particular order, as long as they work.
This is working more-or-less properly, except that a) the game doesn’t print a command prompt on its own, and b) the status line goes blank and won’t revert to its normal behavior until the player hits ENTER. If I manually reset the status line (by carrying out the constructing a status line activity or telling Inform to center “[location]” at row 1), it works, but then the game won’t return functionality to the main window at all, and the ‘try looking’ and ‘say’ commands don’t do anything.]
To return to play:
close the illustration window;
redraw the status line; [this blanks out the status line but doesn’t print the location until the player]
clear the screen; [hits enter.]
try looking;
say “>[input style][run paragraph on]”. [this line prints a new command prompt and sets the font to input style, because]
[the game won’t do it on its own for some reason.]
To redraw the status line:
(- DrawStatusLine(); -).
Section - opening the full illustration window
Include (-
Constant GG_ILLUSTRATION_ROCK 210;
Global gg_illustration_window = 0;
-) after “Global Variables” in “Output.i6t”.
[This part mostly does what it should. It’s supposed to:
1. open a graphical window that takes up 100% of the main window;
2. fill the window with black;
3. draw the appropriate image in the window;
4. wait for a mouse-click;
5. OR any keyboard input
After a mouse-click or a keypress, the game should run the "return to play" procedure, above.]
To reveal the full illustration of (F - a figure-name):
(-
if (gg_illustration_window == 0) {
gg_illustration_window = glk_window_open(gg_mainwin, (winmethod_Above+winmethod_Proportional), 100, wintype_Graphics, GG_ILLUSTRATION_ROCK );
}
if (gg_illustration_window) { ! testing to see if the window exists
BlackBackground();
DisplayPicture(ResourceIDsOfFigures–>{F});
glk_request_mouse_event(gg_illustration_window); ! wait for a mouse-click event
glk_request_char_event(gg_mainwin); ! OR press any key
}
-).
[It’s step 5 that doesn’t work. The glk_request_char_event line does nothing, as far as I can tell; the interpreter does allow keyboard input (if you can set the window percentage to something less than 100%, you can see the keyboard input being printed in the main window), but the event handling rules never fire.]
Section - blacking out the background
[This is copied nearly verbatim from Emily Short’s Simple Graphical Windows extension. It works just fine.]
Include (-
[ BlackBackground color result graph_width graph_height;
if (gg_illustration_window) {
result = glk_window_get_size(gg_illustration_window, gg_arguments, gg_arguments+WORDSIZE);
graph_width = gg_arguments–>0;
graph_height = gg_arguments–>1;
glk_window_fill_rect(gg_illustration_window, 0, 0, 0, graph_width, graph_height);
}
];
-).
Section - scaling and displaying the picture
[This is also swiped from SGW. It also works exactly as it should.]
Include (-
[ DisplayPicture cur_pic result graph_width graph_height img_width img_height h_total w_total h_offset w_offset;
if (gg_illustration_window) {
result = glk_window_get_size(gg_illustration_window, gg_arguments, gg_arguments+WORDSIZE);
graph_width = gg_arguments-->0;
graph_height = gg_arguments-->1;
result = glk_image_get_info( cur_pic, gg_arguments, gg_arguments+WORDSIZE);
img_width = gg_arguments-->0;
img_height = gg_arguments-->1;
w_total = img_width;
h_total = img_height;
if (graph_height - h_total < 0) { ! if the image won't fit, find the scaling factor
w_total = (graph_height * w_total)/h_total;
h_total = graph_height;
}
if (graph_width - w_total < 0) {
h_total = (graph_width * h_total)/w_total;
w_total = graph_width;
}
w_offset = (graph_width - w_total)/2; if (w_offset < 0) w_offset = 0;
h_offset = (graph_height - h_total)/2; if (h_offset < 0) h_offset = 0;
glk_image_draw_scaled(gg_illustration_window, cur_pic, w_offset, h_offset, w_total, h_total);
}
];
-).
Section - removing the illustration
[Finally, this part just closes the graphics window. As far as I can tell, it’s doing its job.]
To close the illustration window:
(-
if (gg_illustration_window) {
glk_window_close(gg_illustration_window, 0);
}
-).
[/code][/spoiler]
story.gblorb (667 KB)