I'm programming a Perl Tk game that uses a scrolled canvas as the visible area of the game. It's a tilebased maze game with 32x32 pixel tiles (think Chip's Challenge if you remember it).
The map data is a scalar, and it kind of looks like this:
The columns are split horizontally at the colon, and the rows are split at the line break.
In each cell are two symbols, the first one is the tile which appears on top, and below is the tile which is hidden underneath the first. In this case, # is a wall and a space is a floor tile.
I decided that a game map should be 70x70 tiles large, or with 4900 tiles, which each set on top of 4900 hidden tiles beneath them. So we're looking at 9800 canvas items that need to be drawn for any one map in the game.
This works fine so far. The items can all be drawn in a couple of seconds and the game can go from there.
The problem is with closing out of the program. On Perl's exit command, it has to delete everything in the main window before shutting down, which freezes the window for quite some time.
I tried a Destroy handler to make it destroy the canvas first, but it gives the same result. The canvas has to destroy all its items before being destroyed itself.
The only solution I could come up with to make the shutting down process go easier is to actually go through and delete each item from the canvas, one at a time, and updating the main window between each. So visually, you see each tile disappear one at a time down the line. This will stop any confusion by the end user thinking the program is completely frozen when it's not.
This still took bloody ages though, I estimate it took at least a full minute for the canvas to be fully cleared, during which most of the time the CPU usage was up to 100% until the canvas was about 3/4ths cleared, at which time the deletion also sped up.
The other solution was to shrink the map size to 40x40 tiles rather than 70x70 which allowed the program to complete this process much sooner.
So the question is, is there any way to destroy all items on a canvas in a quick and easy way? If 9800 items can be drawn in a second, why can't they be destroyed in a second too?
Any help would be appreciated.
The map data is a scalar, and it kind of looks like this:
Code:
##:##:##:##
##: : :##
##: : :##
##:##:##:##
The columns are split horizontally at the colon, and the rows are split at the line break.
In each cell are two symbols, the first one is the tile which appears on top, and below is the tile which is hidden underneath the first. In this case, # is a wall and a space is a floor tile.
I decided that a game map should be 70x70 tiles large, or with 4900 tiles, which each set on top of 4900 hidden tiles beneath them. So we're looking at 9800 canvas items that need to be drawn for any one map in the game.
This works fine so far. The items can all be drawn in a couple of seconds and the game can go from there.
The problem is with closing out of the program. On Perl's exit command, it has to delete everything in the main window before shutting down, which freezes the window for quite some time.
I tried a Destroy handler to make it destroy the canvas first, but it gives the same result. The canvas has to destroy all its items before being destroyed itself.
The only solution I could come up with to make the shutting down process go easier is to actually go through and delete each item from the canvas, one at a time, and updating the main window between each. So visually, you see each tile disappear one at a time down the line. This will stop any confusion by the end user thinking the program is completely frozen when it's not.
This still took bloody ages though, I estimate it took at least a full minute for the canvas to be fully cleared, during which most of the time the CPU usage was up to 100% until the canvas was about 3/4ths cleared, at which time the deletion also sped up.
The other solution was to shrink the map size to 40x40 tiles rather than 70x70 which allowed the program to complete this process much sooner.
So the question is, is there any way to destroy all items on a canvas in a quick and easy way? If 9800 items can be drawn in a second, why can't they be destroyed in a second too?
Any help would be appreciated.