Skip to content

Commit

Permalink
Refactor so that we can show the level title as soon as it is loaded,
Browse files Browse the repository at this point in the history
and while we read the rest of the level data, reducing the
total load level wait time.
  • Loading branch information
menthal committed Nov 6, 2008
1 parent 65ce8f2 commit 651283d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
48 changes: 28 additions & 20 deletions magicmaze/filemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,34 @@ class FileMap
# Open an old-style filemap for Magic Maze.
# Filename must point to a valid map file.
def initialize( filename, monster_maker = nil )
File.open(filename, 'rb'){|file|
header_data = file.read(MAP_HEADER_SIZE)
unless MAP_FILE_SIGNATURE == header_data[0,MAP_FILE_SIGNATURE.size]
raise ArgumentError, "Map file is invalid: "+filename
end

extract_from_header( header_data )

@real_checksum = 0
@map_rows = []
begin
row = file.read(MAP_ROW_SIZE)
extract_from_row( row ) if row
end while row
@real_checksum &= 0xFFFF
unless @checksum == @real_checksum
raise ArgumentError, "Map file checksum failed: "+
"Excpected #@checksum, found #@real_checksum."
end
}
@file = File.open(filename, 'rb')
header_data = @file.read(MAP_HEADER_SIZE)
unless MAP_FILE_SIGNATURE == header_data[0,MAP_FILE_SIGNATURE.size]
raise ArgumentError, "Map file is invalid: "+filename
end

extract_from_header( header_data )
end

def load_map
@real_checksum = 0
@map_rows = []
begin
row = @file.read(MAP_ROW_SIZE)
extract_from_row( row ) if row
yield row if block_given?
end while row
@real_checksum &= 0xFFFF
unless @checksum == @real_checksum
raise ArgumentError, "Map file checksum failed: "+
"Excpected #@checksum, found #@real_checksum."
end
end

def close
@file.close if @file
@file = nil
end

def convert_string_to_bytes( str )
if str.respond_to?(:bytes) then
Expand Down Expand Up @@ -171,6 +177,8 @@ def is_blocked?( x, y )


def to_gamemap
load_map if @map_rows.nil? and @file
close # close file, no more reading.
@tilehash = {
:object=>DEFAULT_TILES_ID_LOOKUP.dup,
:background=>{}
Expand Down
23 changes: 14 additions & 9 deletions magicmaze/gameloop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ def load_map( level = 1, saved = nil )
) if level.kind_of? Numeric
filemap = MagicMaze::FileMap.new( filename )

@map_title = filemap.title

yield level, @map_title

@map.purge if @map # Clean up old map, if any.

@level = level
@map = filemap.to_gamemap
@map_title = filemap.title

should_reset = @player || @restart_status
@player = Player.new( @map, self ) unless @player
Expand Down Expand Up @@ -269,15 +272,17 @@ def game_loop

def start
begin
load_map( @level )

# Loading message
loading_message = _("Entering level %s") % @level.to_s +
"\n" + _(@map_title) + "\n"+ _("Get ready!")
@graphics.fade_in_and_out do
@graphics.clear_screen
@graphics.show_long_message(loading_message, false, :fullscreen )
@graphics.time_synchronized(1000) do
load_map( @level ) do |level, map_title |
# Loading message as soon as title has been loaded.
loading_message = _("Entering level %s") % level.to_s +
"\n" + _(map_title) + "\n"+ _("Get ready!")
@graphics.clear_screen
@graphics.show_long_message(loading_message, false, :fullscreen )
@graphics.fade_in
end
end
@graphics.fade_out



Expand Down
3 changes: 3 additions & 0 deletions magicmaze/test_filemap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ def test_loading_filemaps
(1..10).each do|level|
filename = sprintf "data/maps/mm_map.%03d", level
filemap = MagicMaze::FileMap.new( filename )
filemap.load_map
assert( filemap )
gamemap = filemap.to_gamemap
filemap.close
assert( gamemap )
assert( filemap.title.size.nonzero? )
print "."
end
end
end

0 comments on commit 651283d

Please sign in to comment.