Working with Pointers

BlitzMax Forums/BlitzMax Programming/Working with Pointers

_Skully(Posted 2010) [#1]
In finalizing the resource reloader in TileMax I realized that in order for this to be seamless to the rest of the code on Graphics mode change, I would need to update TImage and TSound variables that store the pointers to the TImage/TSounds.

But...

I'm having difficulty wrapping my head around this

Global I:TImage=TMSoftLoadImage("image.png",I)

Function SoftLoadImage:TImage(path:string, I:TImage)
  R:TMResource=new TMResource
  R.Path=path
  R.Image=loadimage(path)
  R.Pointer=VarPtr(I)
  return R.Image
End Function



Right now, no matter what I set TMResource.Pointer type to... for example:
Field Pointer:int Ptr will give me a conversion error (unable to convert from TImage Ptr to Int Ptr)
Field Pointer:Object Ptr - illegal pointer type
Field Pointer:TImage Ptr - illegal pointer type

???


GW(Posted 2010) [#2]
If you want the type instance to hold a copy of the image pointer then do somehting like:

R.pointer = varptr(R.image)

'//if R.pointer is an int then

R.pointer = int(varptr(R.image))



_Skully(Posted 2010) [#3]
So I updated it to:

Global I:TImage=TMSoftLoadImage("image.png",I)

Function SoftLoadImage:TImage(path:string, I:TImage)
  R:TMResource=new TMResource
  R.Path=path
  R.Image=loadimage(path)
  R.Pointer=Int(VarPtr(I))
  return R.Image
End Function

Which is storing the address of variable I in an int variable... Now, how do I modify the original variable (I in this case) to the new value of a reloaded image? Which, if I understand correctly, is basically setting the original I value to the pointer value contained in R.Image?


N(Posted 2010) [#4]
I'm not sure why you need a pointer here? Also, Varptr(I) is invalid the moment you leave that function, so it's not like you're going to get anything good out of that. Also, I believe that if you change the graphics driver/graphics settings, Max2D will just invalidate and reload the image frames.

E.g.,



_Skully(Posted 2010) [#5]
Ooops... should have been:

Function SoftLoadImage:TImage(path:string, I:TImage var)
  R:TMResource=new TMResource
  R.Path=path
  R.Image=loadimage(path)
  R.Pointer=Int(VarPtr(I))
  return R.Image
End Function



N(Posted 2010) [#6]
See my above response again. Edit: And there's still no guarantee what you point to is valid, since chances are you're sticking the image in a local. Once that goes out of scope, you're boned.


_Skully(Posted 2010) [#7]
So its best to do the resource management using an array.. thats the interim approach I've taken

TMImage[num]
TMSound[num]

When reloaded.. it just replaces the image/sound accordingly.


N(Posted 2010) [#8]
Except you don't need to do that, at least not with images.


plash(Posted 2010) [#9]
In finalizing the resource reloader in TileMax I realized that in order for this to be seamless to the rest of the code on Graphics mode change, I would need to update TImage and TSound variables that store the pointers to the TImage/TSounds.
As Nilium has already stated, you don't need to do anything for images (not sure about sounds, but I would suspect graphics reinitialization has no effect whatsoever to sounds).


_Skully(Posted 2010) [#10]
Hmm.... I guess some testing is in order then... i was under the impression that a graphics mode switch caused the images to be dumped.


Brucey(Posted 2010) [#11]
A TImage holds a TPixmap copy of the data.
If it does get dumped from VRAM on a mode change, it should be reloaded from the pixmap on the next Draw.
Probably...


Galaxy613(Posted 2010) [#12]
Yep, works perfectly!




_Skully(Posted 2010) [#13]
Oh... well, crap... ok... no thats a good thing actually!