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.

AppGameKit Classic Chat / AGK IDE 2017.01.09 Function CreateSprite() does not recognize valid image data

Author
Message
Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 1st Mar 2017 06:27 Edited at: 1st Mar 2017 06:29
This problem is a bit subtle, so bear with me. Consider the following code:

The creation of SPR1 will succeed while the creation of SPR2 crashes. The problem is that although both SRC and DST are of type `integer` and should both be of an imaginary subtype `index to memblock`(as a CreateMemblock function was used for both), somehow AppGameKit knows under the hood that SRC was originally linked to an image while DST was not. Note that I can "fix" the crash by using:

But this should be redundant as DST should already represent an image. It leaves me wondering if there is some kind of memory leak going on. For example, in the above line of code, what happens to the image after it gets passed as a parameter to CreateSprite()? Or what happened to the image generated by LoadImage() when SRC is initialized?

Expected: CreateSprite() treats memblocks as valid images and only crashes if they aren't.
Goo Goo G\'Joob!
Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 1st Mar 2017 06:50
Did some more testing to see if I can figure out how to break the link between images and memblocks. Using this function:

This will crash since IMG.ID and TMP appear to refer to the same object when you would expect them to be distinct. So a memblock could be appearing in the list of memblocks and the list of images, but the user has no way to tell the difference once they are created. If the memblock and image lists were identical, I wouldn't expect the crash in the original post to have happened.
Goo Goo G\'Joob!
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 1st Mar 2017 07:39 Edited at: 1st Mar 2017 08:01
A memblock is a memblock and not an image even if its contents represent those of a valid image.
In order to turn a memblock into an AppGameKit recognisable image you neeed to use:
CreateImageFromMemblock()

You worked that out for yourself in the first post in your 'Note'
Quote: " Note that I can "fix" the crash by using: CreateImageFromMemblock()"

But then decided to dismiss it altogether. You haven't fixed a crash with that code. You have done the correct and expected thing.

This isn't a bug, it's a coding error. Can a mod please change the subject title?

AGK V2 user - Tier 1 (mostly)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 1st Mar 2017 08:44
Quote: "This isn't a bug, it's a coding error. Can a mod please change the subject title?"

Just to play devils advocate, but A memblock is a memblock and not an image even if its contents represent those of a valid image. doesn't explain why
.seems to work
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 1st Mar 2017 14:36
Quote: "seems to work "

So it does!

I retract my comment. More testing is required .....
AGK V2 user - Tier 1 (mostly)
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 1st Mar 2017 15:38
I think it might just be a fluke that the image number returned by Loadimage happens to be the same as the number held in src from the CreateMemblockFromImage command.


Confirmed with the following:



Scraggle was correct. It needs to be an image, not a memblock.
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 1st Mar 2017 17:07
Yeah, I realised this on my drive home.
I was about to post the same thing but you beat me to it
AGK V2 user - Tier 1 (mostly)
Eqqman
9
Years of Service
User Offline
Joined: 15th Jul 2015
Location:
Posted: 1st Mar 2017 19:38 Edited at: 1st Mar 2017 19:40
Quote: "
Scraggle was correct. It needs to be an image, not a memblock.
"


Thank you both. This test makes it more explicit, the call to CreateSprite() fails.


This still leaves me curious as to what is happening as regards images and memory usage. HandsOnAGK 1st Edition, page 929, gives this example code for working with memblocks and images:

So what happened to the two images that were generated as function parameters? Did they get automatically destroyed at the end of the function call, or is this a memory leak? Presumably they don't get destroyed since if you delete a sprite's image it no longer looks correctly. If I need to free up memory and delete a sprite and its image, I can recover the image ID using GetSpriteImageID(), but this is not a property held by memblocks, so how would I delete the image used to spawn it?
Goo Goo G\'Joob!
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 1st Mar 2017 20:43
You are correct. If you need to keep track of the image ID, you either need to specify an image number (i.e. loadimage(1,"MyImage.png")), or capture the return value (i.e. imagenumber = loadimage("myimage.png")). GetSpriteImageID() can come in handy when you need to dynamically assign images to sprites as part of a level loader for example.

Login to post a reply

Server time is: 2024-09-29 23:22:42
Your offset time is: 2024-09-29 23:22:42