Array Question

BlitzMax Forums/BlitzMax Programming/Array Question

WERDNA(Posted 2010) [#1]
Hey!

I'm still fairly new to arrays(I only started using them in the last few months, lol)
and have a quick question about them.

I'm using one in my current game, to hold the level images, and set it up like this.

Global Array:Timage[10] 'Allots room for 10 levels.
Array[0] = loadimage("Graphics/Level1.png")
Array[1] = loadimage("Graphics/Level2.png")

And so on, assigning each segment of the array to a level image.

However there seems to be a slight problem with this.

During the game, I draw the level like so.

Drawimage Array[Level],x,y
so that the correct level image is drawn, depending on what Level I'm at.
However everytime I increase or decrease the 'Level' variable, there is a brief
pause while the game loads the level image. Which is not good, since I need
them to load seamlessly. If I don't use an array, and simply use Global variables
for the level images instead, it loads how I want it to. Also this only happens
the first time I access that segment of the array. If I try to change the 'Level'
variable back to a value that it had previously been, it loads seamlessly with
no slow down.

SO, here is my question.

Why do arrays not 'pre-load'(If that's the correct term) images like Global
variables do?

I've managed to fix this problem by simply putting Drawimage after I assign
an image to each array segment, but would like to know why I need to.


Thanks!


Jesse(Posted 2010) [#2]
I had that problem with my a shmup I am working on. I set the loadImage flags to MASKEDIMAGE|DYNAMICIMAGE and it stopped doing it. try it and see if that helps. I think it's how the graphics are manipulated while accessing the video card but I could be wrong.


therevills(Posted 2010) [#3]
Jesse is correct, the images are loaded into RAM when you declare them, but the lag you are seeing is the transfer from RAM to the graphics card VRAM.

Try this:

' -----------------------------------------------------------------------------
' createFrames: Caches an Image or Anim Image in VRAM
' by Ian Duff
' -----------------------------------------------------------------------------
Function createFrames(image:TImage) 
	'Use this to cache an image or animimage in VRAM for quicker drawing later.
	For Local c%=0 Until image.frames.length
		image.Frame(c)
	Next
End Function


http://www.blitzbasic.co.nz/Community/posts.php?topic=82765#933654

Also you can do this to load your images into your array faster (as in faster coding it):


Const maxLevels:Int = 10
Global Array:Timage[maxLevels] 'Allots room for 10 levels.

For Local i:Int = 0 to maxLevels-1
   Array[i] = loadimage("Graphics/Level"+(i+1)+".png")
Next




Czar Flavius(Posted 2010) [#4]
You should have a TLevel object, which contains a background image (and other things you require) and create an array of them instead. You can have a Load method that loads the images etc required by that level, and a Play method which contains a game loop.


WERDNA(Posted 2010) [#5]
Thanks guys!

That should help out tremendously ;)

@Czar Flavius
A very good idea! Never would have thought of that one, lol.

I'll see how I like that, compared to how I'm doing it now.


xlsior(Posted 2010) [#6]
One thing to keep in mind, depending on how many graphics you actually have, loading them all at once at the beginning could exceed the amount of video memory on low end machines.

Keeping only the current level in memory would lower your system requirements, which could increase the number of machines your game would work on. Not necessarily an issue for small(ish) games, but as the size of a game increases it can easily catch up with you.


WERDNA(Posted 2010) [#7]
Thanks for the heads up xlsior!

I already planned on doing just that ;)