Best Way To Preload Sprites Before Scene?

Started by SilverSpook, Tue 16/02/2016 04:43:39

Previous topic - Next topic

SilverSpook

I have some very large animations and backgrounds that need to be played without slowdown, and they pretty much always do if the sprites are not already loaded. 

Once an animation has played, it seems subsequent plays happen without slowdown.  What would be the best way to go about getting all of the sprites and such loaded into memory before even starting a scene?  Would it be best to just make an object and have it iterate through all of the necessary sprites?  Or some other method?

Thanks!

Mandle

I'm not sure of the best method, yours sounds like it would work fine however. One hint I have to add is that I believe it's probably easiest to set the object's coords to something like x=10000, or something similar that is waaaaaaay out of the game resolution. This should save you from having to hide the object behind a fake background or whatever. There are probably other ways as well, like 100% transparency, but I'm not sure if the game loads graphics when the transparency is 100% (probably does though)...

SilverSpook

Ok thanks!

One other question: how about pre-loading backgrounds?  This also seems to be an issue at 1366x768.  I've been considering just switching through each room in the intro before the game even starts so that the backgrounds are already loaded in memory.  Would that make sense?

Crimson Wizard

#3
Room backgrounds are not common sprites and not stored in the sprite cache; when room is changed, room background gets unloaded, so your solution won't work.

If this really becomes an issue for your game, you could try moving background images to Sprites, and draw them onto room's surface on every "Before room fade-in" event. In such case you can preload these backgrounds same way as you force preload other sprites.

E: Another thing to mention: if you preload a lot of sprites this way, you may want to increase sprite cache size in the game setup, or some of the first sprites may get unloaded while "preloading" latter ones.

Dualnames

Big animations, aka lots of frames, have this issue: Even with increase dynamic sprite cache, if you try and animate a huge ass object like 5000x200 and then 2-3 more it begins having a drop on fps. I'd say for your case that is not the issue, so are you just doing animate? And if so with how many frames of animations playing
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Crimson Wizard

Quote from: Dualnames on Tue 16/02/2016 13:11:12
Big animations, aka lots of frames, have this issue: Even with increase dynamic sprite cache, if you try and animate a huge ass object like 5000x200 and then 2-3 more it begins having a drop on fps.
This case may need require research, but sprite cache can be increased to sizes beyond available in WinSetup, if you manually edit config file. This way you should be able to assign at least couple of GB for it.

Cassiebsg

Quote from: Crimson Wizard on Tue 16/02/2016 14:32:41
but sprite cache can be increased to sizes beyond available in WinSetup, if you manually edit config file. This way you should be able to assign at least couple of GB for it.

Just a pertinent question, if you do manually increase the sprite cache to a couple GB, wouldn't the user/player be able to "accidentally" decrease it, by using Winsetup and setting it to MAX (100mb)?
There are those who believe that life here began out there...

Crimson Wizard

Quote from: Cassiebsg on Tue 16/02/2016 16:25:31
Quote from: Crimson Wizard on Tue 16/02/2016 14:32:41
but sprite cache can be increased to sizes beyond available in WinSetup, if you manually edit config file. This way you should be able to assign at least couple of GB for it.

Just a pertinent question, if you do manually increase the sprite cache to a couple GB, wouldn't the user/player be able to "accidentally" decrease it, by using Winsetup and setting it to MAX (100mb)?

Yep. :p

SilverSpook

The biggest animations I have are around 700x700 or something like that.  There are some scenes with two or three sprites like this.  There are both visual and audio glitches (music skips) often.  I remembered something about using multithreading to help with the audio glitches. 

Thanks Dualnames and CW for looking into this.

Dualnames

Now, i wanna continue this discussion, cause CW made me think of things. He opened up a world of experience. I did try setting up the cache to something else, i think i tried increasing it by 20 mb, and it kind of reverted (the acsetup.cfg) to the max possible of winsetup. I'm using 3.3.4 could i have possible fucked up the number and so it reverted to default/grabbed the number from winsetup? Is there also a way to force this to a certain number un-editable or somehow, not allowed to be changed by user? I was thinking of forcing it when the game loads, but when the game loads it has already increased the sprite cache to a certain point right?
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

SilverSpook

Ok, so I have a question: if you preload, it seems you have to "Wait" for at least one frame when preloading through a sprite otherwise it seems the sprites are not actually preloaded.

For example here's a script I have for preloading the very hi-res and high-sprite-count intro in Neofeud:

Code: ags
function doPreloadingIntro(){
  int i = 206;
  gPreloader.Visible = true;
  btnPreloader.Visible = true;
  while(i< 280){
    
    //oPreloader.Graphic = i;
    btnPreloader.NormalGraphic = i;
    i++;
    Wait(1);
  }
  gPreloader.Visible = false;
}


Is there a faster way to preload that doesn't require doing the "Wait(1)"?  Thanks.

Mandle

I believe that the Wait(1) does not mean that you are waiting for the next frame. It means that you are waiting for AGS to loop back through all the routines it is running during 1/40th of a second.

I'm no expert, in fact I'm a complete fly-by-my-ass, but I think you need the Wait(1) to tell AGS to complete another cycle before loading the next sprite otherwise AGS will not repeat the "while" command. And I assume this is why loading screens exist with their meters and all.

Did you try literally loading every sprite into the game manually without the "while" command...I guess that would mean copy/pasting a line of code for every single sprite and changing the number each time...But it should complete within a single loop...

I think though that this (OMG @ four-in-a-row "th" alliteration) will not help your cause much time-wise, and the "Wait(1)" would also allow you to advance a loading screen meter if needed...

I might be full of bull of course...

Erm....Khris?!....CW?!

Crimson Wizard

#12
Quote from: Mandle on Sat 20/02/2016 12:27:17
I'm no expert, in fact I'm a complete fly-by-my-ass, but I think you need the Wait(1) to tell AGS to complete another cycle before loading the next sprite otherwise AGS will not repeat the "while" command.
No, this is wrong.
"While" repeats without wait. Why you need wait: because you need every sprite to get drawn. If you won't have Waits there, AGS won't redraw sprite until the function ends, or Wait command is called. If there is no Wait, then AGS draws only the last assigned sprite.
And since sprites are not redrawn, they do not get cached.
This is why you have to call Wait.

And of course that would be slow, but not because of Wait command, but because of multiple sprite loading in one go.

E:
In case this is not clear.
When you assign sprite number to "btnPreloader.NormalGraphic", the sprite is NOT loaded. Nothing happens actually.
In AGS the sprite gets loaded only when it is actually used first time, that is usually when it is first time draw on screen.

E2:
Investigating the engine code shows that there are sprites that are being preloaded at the game start. These are sprites in views that are initially assigned to characters. I would experiment by creating a dummy view with all your hi-res animations, and assigning it as a normal view to a dummy character.

Mandle

Thank you for the insight CW!

I love being wrong in this friendly community instead of in my RL world where being wrong means being stressed.

SilverSpook

Ok, so if Wait(1) takes 1/40 of a second, the max sprites you could preload that way is 40 per second using a single object or GUI thing etc, correct?  Is there a way to redraw a sprite for only a single game frame, so you could load faster?  Or would it be better to have say 2, or 5 objects preloading sprites so you can do 2x40 or 5x40 sprites per second?

Danvzare

Quote from: SilverSpook on Sat 20/02/2016 17:34:02
Ok, so if Wait(1) takes 1/40 of a second, the max sprites you could preload that way is 40 per second using a single object or GUI thing etc, correct?  Is there a way to redraw a sprite for only a single game frame, so you could load faster?  Or would it be better to have say 2, or 5 objects preloading sprites so you can do 2x40 or 5x40 sprites per second?

I don't know about redrawing a sprite for only a single game frame, but if you wanted to speed up the game to its absolute maximum, you'd use SetGameSpeed(1000).

Crimson Wizard

#16
Quote from: SilverSpook on Sat 20/02/2016 17:34:02
Ok, so if Wait(1) takes 1/40 of a second, the max sprites you could preload that way is 40 per second using a single object or GUI thing etc, correct?  Is there a way to redraw a sprite for only a single game frame, so you could load faster?  Or would it be better to have say 2, or 5 objects preloading sprites so you can do 2x40 or 5x40 sprites per second?
That will make several sprites cached per "Wait", but it does not necessarily mean you can make thousands of sprites loaded per second. Sprite loading also takes time. Only experimentation will show.

And yes, SetGameSpeed may reduce the duration of Wait, but what I said about sprite loading time still stands.

SilverSpook

#17
One other question: the game at present is getting rather large (1.2 gigs) is.there a way to compress files to get some space savings?
And if so what kind of slowdown speed-wise could you expect?
Thanks

ToothpasteBruva

Hey SilverSpoon,

Not sure if this is still an issue for you, but there's a setting in General Settings like "Compress Sprites" or something. If you turn that on it should help. My game went from 100 MB to 17 and works fine.

SMF spam blocked by CleanTalk