Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Question about using LoadImage and whether I need to always use DeleteImage

Author
Message
Paxi
User Offline
Joined: 18th Sep 2020
Location:
Posted: 4th Feb 2021 20:26
Hello guys,

Question about using LoadImage...

If I have multiple levels in my game, and I use a function to load my level up like this:

Function loadlevel1 ()
myimage = LoadImage("pic.png")
myType[x].spriteID = CreateSprite(myimage)
EndFunction

Then when I clean up at the end of the level I do this...

DeleteSprite (myType[x].spriteID )

but say I don't do DeleteImage(myimage) and then call loadlevel1 () again, what is the consequence of this? I only ask because I am doing this and everything seems to work fine. Does calling it again just overwrite it in memory?

Should I definitely be doing DeleteImage as well?

Many thanks!

blink0k
Moderator
8
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 4th Feb 2021 20:54 Edited at: 4th Feb 2021 21:52
Quote: " Does calling it again just overwrite it in memory?"

No you will have a memory leak. You should be able to see this by runnting task manager and looking at the process while unloading/loading a level over and over again
Paxi
User Offline
Joined: 18th Sep 2020
Location:
Posted: 4th Feb 2021 21:50
Thanks blink! I thought that was the case, but I wanted to be sure.
n00bstar
17
Years of Service
User Offline
Joined: 9th Feb 2004
Location: Montreal, Canada.
Posted: 4th Feb 2021 22:58
It's good practice to always clean up after yourself. In coding, and life in general

With high level languages like AppGameKit, many things are taken care of for you and this can lead to lazy coding and bad habits. Add to this the fact that most AppGameKit projects use but a fraction of a modern machine's memory/cpu/whatever and you've got a recipe for disaster once your project reaches a certain size. If you code a pong clone and keep loading unnecessary images and sprites, you might get a memory footprint that keeps getting bigger and bigger but you'll never catch it because it would need to run for 12 hours on a machine with 16gb of ram....which will probably never happen, especially while developing. But if you embark on a large project, let's say some big RPG, and you don't keep things tidy and neat, at some point your project gets big enough that it will start crashing or having weird problems regularly. These problems can take a long while to isolate properly, and you will have TONS of code to fix when you finally catch it.

So yeah.. clean up whenever you can. More code now, less problems later.
-----------------------------------------------------------------------------
We all got a chicken duck woman thing waiting for us
Paxi
User Offline
Joined: 18th Sep 2020
Location:
Posted: 5th Feb 2021 09:14
Thanks guys that's all good advice! I guess I'm asking now because my project is at a point where I'm wondering whether I should be deleting loaded images as well as sprites.

My media folder is 25MB in total, so I'm wondering what is an acceptable threshold to load media just once at run time and then create/delete sprites from that?

tarkusAB
4
Years of Service
User Offline
Joined: 15th May 2016
Location: Tokyo, Japan
Posted: 5th Feb 2021 13:04 Edited at: 5th Feb 2021 13:09
Quote: "My media folder is 25MB in total, so I'm wondering what is an acceptable threshold to load media just once at run time and then create/delete sprites from that?"


A few things:

* The 25 MB is the size in compression. When AppGameKit loads the images, they are uncompressed. Print GetImageMemoryUsage() to the screen to see of how much GPU memory your loaded images are currently using.

* An "acceptable threshold" will vary between systems, because not all devices have the same GPU.

* It's generally good practice and good habit to only load images you are currently using and ones you want to be able to use quickly and not suspend the game to load. Delete images once you are not using them and will not need to reference them quickly any time soon. Regarding the point of "quickly referencing", when you load image(s), your program will briefly hang. Basically that loop of the program takes longer than normal because it takes time to load images, so the sync is called late. Try loading several images during gameplay and you will see what I mean. So to avoid this, run image loading at times when the program hanging would not be noticeable, like on a black transition screen of some sort, or perhaps, a "loading screen". You should load all the images you would need for that "scene". That's why we have loading screens
n00bstar
17
Years of Service
User Offline
Joined: 9th Feb 2004
Location: Montreal, Canada.
Posted: 5th Feb 2021 14:39
The most obvious way to let the user know the program is managing stuff is obviously to add a loading screen as Tarkus said.

But there are ways to hide when your application is crunching/loading/managing data. Usually you hide those when the game comes to a natural pause for the user. After a fight in an JRPG , you might want to delete some sprite data... get a catchy victory jingle to play and display a small text box with valuable info the user is going to want to read (you gain XP and Gold! yay you!) and it'll buy you enough time to manage some stuff behind the scenes, and if the user presses buttons during that time and nothing happens, it won't feel like the app is hung up on computing.
-----------------------------------------------------------------------------
We all got a chicken duck woman thing waiting for us
Paxi
User Offline
Joined: 18th Sep 2020
Location:
Posted: 9th Feb 2021 20:52
Thanks guys for the continued advise. Using the command that tarkusAB suggested, I was able to properly see the usage. I then went through the code and began doing lots of clean up stuff. This is definitely something I will pay closer attention to going forward. I was easily able to see that I was deleting all the things necessary and avoid memory leaks.

The upside is that by doing this now I've given my project more scope to add more levels - thanks again for all the advise.

Login to post a reply

Server time is: 2021-04-14 08:49:22
Your offset time is: 2021-04-14 08:49:22