Skip to content

Commit

Permalink
Fix how we measure the screen size (fixes #56)
Browse files Browse the repository at this point in the history
Don't return 255 height as that means infinite and some games will break
  • Loading branch information
curiousdannii committed Nov 29, 2017
1 parent b7210d3 commit 34d3392
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/zvm.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,13 @@ api = {
this.handle_line_input( glk_event.get_field( 2 ), glk_event.get_field( 3 ) );
run = 1;
}

// Arrange events
if ( event_type === 5 )
{
this.update_width();
this.update_screen_size()
}

// glk_fileref_create_by_prompt handler
if ( event_type === 'fileref_create_by_prompt' )
{
Expand Down
66 changes: 48 additions & 18 deletions src/zvm/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,8 +940,8 @@ module.exports = {
// Reset the Xorshift seed
this.xorshift_seed = 0;

// Update the width - in version 3 does not actually set the header variables
this.update_width();
// Update the screen size variables - in version 3 does not actually set the header variables
this.update_screen_size()

// For version 3 we only set Flags 1
if ( this.version3 )
Expand All @@ -964,12 +964,10 @@ module.exports = {
// This is really a word, but we only care about the lower byte
ram.setUint8( 0x11, ram.getUint8( 0x11 ) & 0x57 );

// Screen settings
ram.setUint8( 0x20, 255 ); // Infinite height
// Font height/width in "units"
if ( this.version > 4 )
{
ram.setUint16( 0x24, 255 );
ram.setUint16( 0x26, 0x0101 ); // Font height/width in "units"
ram.setUint16( 0x26, 0x0101 )
}

// Colours
Expand All @@ -985,30 +983,62 @@ module.exports = {
this.extension_table( 4, 0 );
},

update_width: function()
update_screen_size: function()
{
var Glk = this.Glk,
tempwin = Glk.glk_window_open( this.mainwin, 0x12, 0, 4, 204 ),
box = new Glk.RefBox(),
width;
Glk.glk_window_get_size( tempwin || this.mainwin, box );
const Glk = this.Glk
const height_box = new Glk.RefBox()
const width_box = new Glk.RefBox()
const tempwin = Glk.glk_window_open( this.mainwin, 0x12, 0, 4, 0 )
let height = 0
let width = 0

// The main window is proportional, so its width may not be accurate
// If the upper or status window is present, use its width, or else try to make a temp window
// The height is the total of all windows

Glk.glk_window_get_size( this.mainwin, width_box, height_box )
height = height_box.get_value()

if ( this.upperwin )
{
Glk.glk_window_get_size( this.upperwin, width_box, height_box )
height += height_box.get_value()
}
if ( this.statuswin )
{
Glk.glk_window_get_size( this.statuswin, width_box, height_box )
height += height_box.get_value()
}
if ( tempwin )
{
Glk.glk_window_close( tempwin );
Glk.glk_window_get_size( tempwin, width_box, 0 )
Glk.glk_window_close( tempwin )
}
// Get the width but limit to a max of 255
this.io.width = width = Math.min( box.get_value(), 255 );

// Use whichever width was available
width = width_box.get_value()

// Cap the dimensions
// Height is capped to 254 as 255 means infinite, which breaks some games
height = Math.min( height, 254 )
width = this.io.width = Math.min( width, 255 )

// Update the header
if ( this.version > 3 )
{
this.ram.setUint8( 0x21, width );
this.ram.setUint8( 0x20, height )
this.ram.setUint8( 0x21, width )
}
if ( this.version > 4 )
{
this.ram.setUint16( 0x22, width );
this.ram.setUint16( 0x22, width )
this.ram.setUint16( 0x24, height )
}

// Fix the cursor if it is outside the window
if ( this.io.col >= width )
{
this.io.col = width - 1;
this.io.col = width - 1
}
},

Expand Down

1 comment on commit 34d3392

@curiousdannii
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach borrowed from Bocfel. Thanks!

Please sign in to comment.