Page 1 of 1

catchImage memory address

Posted: Sat Jul 13, 2024 12:29 pm
by MinerDac
Hello;

New to PureBasic, playing around with CatchImage(). I'm wondering how to get the memory address for the label in the include data section if the label changes?

I have several images in the data section with include section using IncludeBinary, each image is 1Kb in size. But the problem is that when a condition changes the image displayed in ImageGadget will need to change also and I do not know ahead of time which label to select. For example:

Code: Select all

<prior condition selection for label here>

CatchImage(1, ?<image label selected by prior condition>) ; 
ImageGadget(200, 15, 522, 40, 40, ImageID(1))

DataSection
  NoteGIF: : IncludeBinary "D:\load_get.gif"
  USea: : IncludeBinary  "D:\usea.png"
  USeb: : IncludeBinary  "D:\useb.png"
  USec: : IncludeBinary  "D:\usec.png"
  USed: : IncludeBinary  "D:\used.png"
  USee: : IncludeBinary  "D:\usee.png"
  USef: : IncludeBinary  "D:\usef.png"
  USeg: : IncludeBinary  "D:\useg.png"
  USeh: : IncludeBinary  "D:\useh.png"
EndDataSection
But when a certain condition happens (prior CatchImage(...) ) I need to change the image in CatchImage(1, ?<image label selected by prior condition>) to one of the other ones but i do not know ahead of time which image that needs to be until I test the condition that prompts the need for change. So, I do not want to do a long drawn out thing like a 'Select : Case <LabelName> : CatchImage(1, ?<image label selected by prior condition>) ; EndSelect:' or a series of 'If : EndIF' because the number of images needed will expand over time to a greater number and that's a lot of 'Select : Case : EndSelect: or a series of 'If : EndIF'

The prior condition generates the name of the label needed but its a string. So how do I put that into CatchImage(1, ?<image label selected by prior condition>) because its expecting a (number) memory address and not a string?

Thank You

Re: catchImage memory address

Posted: Sat Jul 13, 2024 12:46 pm
by infratec

Code: Select all

Select X
  Case 1 : *ptr = ?NoteGIF
  Case 2 : *ptr = ?USea
  Case 3 : *ptr = ?USeb
  ...  
EndSelect

CatchImage(1, *Ptr)

Re: catchImage memory address

Posted: Sun Jul 14, 2024 1:09 am
by Demivec

Code: Select all

<prior condition selection for label here>

;x = (FindString("USea*USea*USea*USea*USea*USea*USea*USea*", name$ + "*") - 1) / 5 ;all sub strings are the same length
CatchImage(1, PeekI(?ImageTable + SizeOf(Integer) * x) ; x = 0 to imageCount - 1
ImageGadget(200, 15, 522, 40, 40, ImageID(1))

DataSection
  NoteGIF: : IncludeBinary "D:\load_get.gif"
  ImagesTable:
  Data.i ?USea, ?USeab ?USec, ?USed, ?USee,  ?USef, ?USeg, ?USeh
  USea: : IncludeBinary  "D:\usea.png"
  USeb: : IncludeBinary  "D:\useb.png"
  USec: : IncludeBinary  "D:\usec.png"
  USed: : IncludeBinary  "D:\used.png"
  USee: : IncludeBinary  "D:\usee.png"
  USef: : IncludeBinary  "D:\usef.png"
  USeg: : IncludeBinary  "D:\useg.png"
  USeh: : IncludeBinary  "D:\useh.png"
EndDataSection

Re: catchImage memory address

Posted: Sun Jul 14, 2024 9:58 am
by mk-soft
There are no names (string) of labels, variables at runtime.
These are available at compile time and the addresses are used.

Re: catchImage memory address

Posted: Sun Jul 14, 2024 6:55 pm
by MinerDac
thanks for the responses folks.

After playing around with it some more I ended up going with that posted by infratec above.

I kind of wanted to avoid a big 'Select : EndSelect' section, hoping to convert the string name (countrycode$ below) to a memory address flag pointer instead (i.e. "US" + "Flag" to ?ADFlag) but it turns out the Select : EndSelect section is not so bad for use. So here is what I ended up with:

Code: Select all

flagtooltip$ = nationName$ + " (" + countrycode$ + ")"
  Select countrycode$
    Case "AD" : *Flgptr = ?ADFlag
    Case "AE" : *Flgptr = ?AEFlag
    Case "AL" : *Flgptr = ?ALFlag
    Case "AM" : *Flgptr = ?AMFlag
    Case "AR" : *Flgptr = ?ARFlag
    Case "AT" : *Flgptr = ?ATFlag
    Case "AU" : *Flgptr = ?AUFlag
    Case "BA" : *Flgptr = ?BAFlag
    Case "BD" : *Flgptr = ?BDFlag
    Case "BE" : *Flgptr = ?BEFlag
    Case "BG" : *Flgptr = ?BGFlag
    Case "BO" : *Flgptr = ?BOFlag
    Case "BR" : *Flgptr = ?BRFlag
    Case "BS" : *Flgptr = ?BSFlag
    Case "CA" : *Flgptr = ?CAFlag
    Case "CH" : *Flgptr = ?CHFlag
    Case "CL" : *Flgptr = ?CLFlag
    Case "CN" : *Flgptr = ?CNFlag
    Case "CO" : *Flgptr = ?COFlag
    Case "CR" : *Flgptr = ?CRFlag
    Case "CY" : *Flgptr = ?CYFlag
    Case "CZ" : *Flgptr = ?CZFlag
    Case "DE" : *Flgptr = ?DEFlag
    Case "DK" : *Flgptr = ?DKFlag
    Case "DZ" : *Flgptr = ?DZFlag
    Case "EC" : *Flgptr = ?ECFlag
    Case "EE" : *Flgptr = ?EEFlag
    Case "EG" : *Flgptr = ?EGFlag
    Case "ES" : *Flgptr = ?ESFlag
    Case "FI" : *Flgptr = ?FIFlag
    Case "FR" : *Flgptr = ?FRFlag
    Case "GB" : *Flgptr = ?GBFlag
    Case "GE" : *Flgptr = ?GEFlag
    Case "GL" : *Flgptr = ?GLFlag
    Case "GR" : *Flgptr = ?GRFlag
    Case "GT" : *Flgptr = ?GTFlag
    Case "HK" : *Flgptr = ?HKFlag
    Case "HR" : *Flgptr = ?HRFlag
    Case "HU" : *Flgptr = ?HUFlag
    Case "ID" : *Flgptr = ?IDFlag
    Case "IE" : *Flgptr = ?IEFlag
    Case "IL" : *Flgptr = ?ILFlag
    Case "IM" : *Flgptr = ?IMFlag
    Case "IN" : *Flgptr = ?INFlag
    Case "IS" : *Flgptr = ?ISFlag
    Case "IT" : *Flgptr = ?ITFlag
    Case "JP" : *Flgptr = ?JPFlag
    Case "KH" : *Flgptr = ?KHFlag
    Case "KR" : *Flgptr = ?KRFlag
    Case "KZ" : *Flgptr = ?KZFlag
    Case "LI" : *Flgptr = ?LIFlag
    Case "LK" : *Flgptr = ?LKFlag
    Case "LT" : *Flgptr = ?LTFlag
    Case "LU" : *Flgptr = ?LUFlag
    Case "LV" : *Flgptr = ?LVFlag
    Case "MA" : *Flgptr = ?MAFlag
    Case "MC" : *Flgptr = ?MCFlag
    Case "MD" : *Flgptr = ?MDFlag
    Case "ME" : *Flgptr = ?MEFlag
    Case "MK" : *Flgptr = ?MKFlag
    Case "MN" : *Flgptr = ?MNFlag
    Case "MO" : *Flgptr = ?MOFlag
    Case "MT" : *Flgptr = ?MTFlag
    Case "MX" : *Flgptr = ?MXFlag
    Case "MY" : *Flgptr = ?MYFlag
    Case "NG" : *Flgptr = ?NGFlag
    Case "NL" : *Flgptr = ?NLFlag
    Case "NO" : *Flgptr = ?NOFlag
    Case "NP" : *Flgptr = ?NPFlag
    Case "NZ" : *Flgptr = ?NZFlag
    Case "PA" : *Flgptr = ?PAFlag
    Case "PE" : *Flgptr = ?PEFlag
    Case "PH" : *Flgptr = ?PHFlag
    Case "PL" : *Flgptr = ?PLFlag
    Case "PT" : *Flgptr = ?PTFlag
    Case "QA" : *Flgptr = ?QAFlag
    Case "RO" : *Flgptr = ?ROFlag
    Case "RS" : *Flgptr = ?RSFlag
    Case "SA" : *Flgptr = ?SAFlag
    Case "SE" : *Flgptr = ?SEFlag
    Case "SG" : *Flgptr = ?SGFlag
    Case "SI" : *Flgptr = ?SIFlag
    Case "SK" : *Flgptr = ?SKFlag
    Case "TR" : *Flgptr = ?TRFlag
    Case "TW" : *Flgptr = ?TWFlag
    Case "UA" : *Flgptr = ?UAFlag
    Case "US" : *Flgptr = ?USFlag
    Case "UY" : *Flgptr = ?UYFlag
    Case "VE" : *Flgptr = ?VEFlag
    Case "VN" : *Flgptr = ?VNFlag
    Case "ZA" : *Flgptr = ?ZAFlag
    Default
      *Flgptr = ?OhNoFlag ; in case no flag from country code passed so it doesn't crash
      flagtooltip$ = "No Flag Available"
  EndSelect
  
  CatchImage(100, *Flgptr)
  ImageGadget(200, 15, 522, 40, 40, ImageID(100))
  GadgetToolTip(200, flagtooltip$)
It wasn't so bad to create. I used powershell to do it - fed it the .json and built the strings in a loop and it spit it out for me to copy-n-paste.

Re: catchImage memory address

Posted: Sun Jul 14, 2024 8:15 pm
by Little John
MinerDac wrote: I kind of wanted to avoid a big 'Select : EndSelect' section, hoping to convert the string name (countrycode$ below) to a memory address flag pointer instead
You can use GetRuntimeInteger().
However, I think Demivec's approach is more elegant.

Code: Select all

; PB 6.04 LTS

EnableExplicit

Define countrycode$, *Flgptr
Define.i ADFlag, AEFlag, ALFlag
Runtime  ADFlag, AEFlag, ALFlag

ADFlag = ?ADFlag
AEFlag = ?AEFlag
ALFlag = ?ALFlag

countrycode$ = "AE"  ; example

*Flgptr = GetRuntimeInteger(countrycode$ + "Flag")
Debug *Flgptr

; CatchImage(100, *Flgptr)
; ImageGadget(200, 15, 522, 40, 40, ImageID(100))

DataSection
   ADFlag:
   
   AEFlag:
   
   ALFlag:
   
EndDataSection

Re: catchImage memory address

Posted: Sun Jul 14, 2024 9:06 pm
by Little John
You can also use a Map.

Code: Select all

; PB 6.04 LTS

EnableExplicit

Define countrycode$, *Flgptr
NewMap Label.i()

Label("AD") = ?ADFlag
Label("AE") = ?AEFlag
Label("AL") = ?ALFlag

countrycode$ = "AE"  ; example

*Flgptr = Label(countrycode$)
Debug *Flgptr

; CatchImage(100, *Flgptr)
; ImageGadget(200, 15, 522, 40, 40, ImageID(100))

DataSection
   ADFlag:
   
   AEFlag:
   
   ALFlag:
   
EndDataSection

Re: catchImage memory address

Posted: Sun Jul 14, 2024 9:27 pm
by Demivec
Little John wrote: Sun Jul 14, 2024 8:15 pm You can use GetRuntimeInteger().
I had also looked at a possible solution using Runtime but was disappointed there wasn't an option to directly use a label. I hope that is possible someday. I am happy to see your variation amongst the possible solutions. It's good to have different options.

Re: catchImage memory address

Posted: Mon Jul 15, 2024 7:07 am
by Little John
Demivec wrote: Sun Jul 14, 2024 9:27 pm I had also looked at a possible solution using Runtime but was disappointed there wasn't an option to directly use a label. I hope that is possible someday.
That's exactly what I was thinking. :)

Re: catchImage memory address

Posted: Sat Jul 20, 2024 7:08 pm
by MinerDac
Little John wrote: Sun Jul 14, 2024 8:15 pm
MinerDac wrote: I kind of wanted to avoid a big 'Select : EndSelect' section, hoping to convert the string name (countrycode$ below) to a memory address flag pointer instead
You can use GetRuntimeInteger().
However, I think Demivec's approach is more elegant.

Code: Select all

; PB 6.04 LTS

EnableExplicit

Define countrycode$, *Flgptr
Define.i ADFlag, AEFlag, ALFlag
Runtime  ADFlag, AEFlag, ALFlag

ADFlag = ?ADFlag
AEFlag = ?AEFlag
ALFlag = ?ALFlag

countrycode$ = "AE"  ; example

*Flgptr = GetRuntimeInteger(countrycode$ + "Flag")
Debug *Flgptr

; CatchImage(100, *Flgptr)
; ImageGadget(200, 15, 522, 40, 40, ImageID(100))

DataSection
   ADFlag:
   
   AEFlag:
   
   ALFlag:
   
EndDataSection
I too liked the approach Demivec applied. Wasn't able to get it to work out though, not sure why but I'd put money on it that's its something I'm not doing or did wrong 😀

And the Runtime approach is out because it can't use a flag directly.

And as you can see from my other post the number of images is actually greater than my initial post somewhat envisioned. So for right now, for the straight forward simplicity of it I'm going with the 'Select:EndSelect' approach put forth by infratec even though I wanted to avoid that by creating the flag (or label) pointers directly and loading up the images instead of having to send it through the filtering of the Select:EndSelect to get the pointers, but it worked out well anyway.

The 'Map' approach put forth by Little John is enticing, and since it seems I'm going to need to end up assigning labels for for all the images anyway if I decide to change it up I might do the Map thing if I can't make the Demivec approach work out reasonably.

Re: catchImage memory address

Posted: Sat Jul 20, 2024 7:40 pm
by wilbert
What also might work is to use IncludeBinary to embed an archive containing all images into the data section.
Then use CatchPack to open it and UncompressPackMemory to uncompress the image you want into a buffer and use CatchImage on that buffer.
It might seem a bit complicated and I haven't tried it but I guess it should be possible and you can use a string for UncompressPackMemory to choose the right file.

Re: catchImage memory address

Posted: Sat Jul 20, 2024 10:42 pm
by Demivec
MinerDac wrote: Sat Jul 20, 2024 7:08 pm I too liked the approach Demivec applied. Wasn't able to get it to work out though, not sure why but I'd put money on it that's its something I'm not doing or did wrong 😀
--- snipped ---

The 'Map' approach put forth by Little John is enticing, and since it seems I'm going to need to end up assigning labels for for all the images anyway if I decide to change it up I might do the Map thing if I can't make the Demivec approach work out reasonably.
If it helps, here is a more fleshed out example with the details you added incorporated into the code:

Code: Select all

  x = (FindString("--*" +
                  "AD*AE*AL*AM*AR*AT*AU*BA*BD*BE*" +
                  "BG*BO*BR*BS*CA*CH*CL*CN*CO*CR*" +
                  "CY*CZ*DE*DK*DZ*EC*EE*EG*ES*FI*" +
                  "FR*GB*GE*GL*GR*GT*HK*HR*HU*ID*" +
                  "IE*IL*IM*IN*IS*IT*JP*KH*KR*KZ*" +
                  "LI*LK*LT*LU*LV*MA*MC*MD*ME*MK*" +
                  "MN*MO*MT*MX*MY*NG*NL*NO*NP*NZ*" +
                  "PA*PE*PH*PL*PT*QA*RO*RS*SA*SE*" +
                  "SG*SI*SK*TR*TW*UA*US*UY*VE*VN*" +
                  "ZA*", nationName$ + "*") - 1) / 3 ;all sub strings are the same length

 *Flgptr = PeekI(?ImageTable + SizeOf(Integer) * x) ; x = 0 to imageCount, 0=ohNoFlag
  If *Flgptr = ?OhNoFlag
    flagtooltip$ = "No Flag Available"
  Else
    flagtooltip$ = nationName$ + " (" + countrycode$ + ")"
  EndIf
  CatchImage(100, *Flgptr)
  ImageGadget(200, 15, 522, 40, 40, ImageID(100))
  GadgetToolTip(200, flagtooltip$)

DataSection
  NoteGIF: : IncludeBinary "D:\load_get.gif"
  ImagesTable:
  Data.i ?OhNoFlag ;in case no flag from country code passed so it doesn't crash
  Data.i ?ADFlag, ?AEFlag, ?ALFlag, ?AMFlag, ?ARFlag, ?ATFlag, ?AUFlag, ?BAFlag, ?BDFlag, ?BEFlag,
         ?BGFlag, ?BOFlag, ?BRFlag, ?BSFlag, ?CAFlag, ?CHFlag, ?CLFlag, ?CNFlag, ?COFlag, ?CRFlag,
         ?CYFlag, ?CZFlag, ?DEFlag, ?DKFlag, ?DZFlag, ?ECFlag, ?EEFlag, ?EGFlag, ?ESFlag, ?FIFlag,
         ?FRFlag, ?GBFlag, ?GEFlag, ?GLFlag, ?GRFlag, ?GTFlag, ?HKFlag, ?HRFlag, ?HUFlag, ?IDFlag,
         ?IEFlag, ?ILFlag, ?IMFlag, ?INFlag, ?ISFlag, ?ITFlag, ?JPFlag, ?KHFlag, ?KRFlag, ?KZFlag,
         ?LIFlag, ?LKFlag, ?LTFlag, ?LUFlag, ?LVFlag, ?MAFlag, ?MCFlag, ?MDFlag, ?MEFlag, ?MKFlag,
         ?MNFlag, ?MOFlag, ?MTFlag, ?MXFlag, ?MYFlag, ?NGFlag, ?NLFlag, ?NOFlag, ?NPFlag, ?NZFlag,
         ?PAFlag, ?PEFlag, ?PHFlag, ?PLFlag, ?PTFlag, ?QAFlag, ?ROFlag, ?RSFlag, ?SAFlag, ?SEFlag,
         ?SGFlag, ?SIFlag, ?SKFlag, ?TRFlag, ?TWFlag, ?UAFlag, ?USFlag, ?UYFlag, ?VEFlag, ?VNFlag,
         ?ZAFlag
  ;For each of the above country labels in the ImageTable include in any order each label and its associated IncludeBinary file
EndDataSection

Re: catchImage memory address

Posted: Sat Jul 20, 2024 11:14 pm
by RASHAD
Why don't you create one image for all your small images then just grab the needed image when needed
I think it will be fast enough

Re: catchImage memory address

Posted: Mon Jul 22, 2024 10:54 am
by MinerDac
RASHAD wrote: Sat Jul 20, 2024 11:14 pm Why don't you create one image for all your small images then just grab the needed image when needed
I think it will be fast enough

Because the displayed images will change over time for different conditions and purposes thus more complexity in the code and more 'refreshes' and a new "one image for all your small images" would be needed each time the images change.

Re: catchImage memory address

Posted: Mon Jul 22, 2024 11:06 am
by MinerDac
Demivec wrote: Sat Jul 20, 2024 10:42 pm
MinerDac wrote: Sat Jul 20, 2024 7:08 pm I too liked the approach Demivec applied. Wasn't able to get it to work out though, not sure why but I'd put money on it that's its something I'm not doing or did wrong 😀
--- snipped ---

The 'Map' approach put forth by Little John is enticing, and since it seems I'm going to need to end up assigning labels for for all the images anyway if I decide to change it up I might do the Map thing if I can't make the Demivec approach work out reasonably.
If it helps, here is a more fleshed out example with the details you added incorporated into the code:

Code: Select all

  x = (FindString("--*" +
                  "AD*AE*AL*AM*AR*AT*AU*BA*BD*BE*" +
                  "BG*BO*BR*BS*CA*CH*CL*CN*CO*CR*" +
                  "CY*CZ*DE*DK*DZ*EC*EE*EG*ES*FI*" +
                  "FR*GB*GE*GL*GR*GT*HK*HR*HU*ID*" +
                  "IE*IL*IM*IN*IS*IT*JP*KH*KR*KZ*" +
                  "LI*LK*LT*LU*LV*MA*MC*MD*ME*MK*" +
                  "MN*MO*MT*MX*MY*NG*NL*NO*NP*NZ*" +
                  "PA*PE*PH*PL*PT*QA*RO*RS*SA*SE*" +
                  "SG*SI*SK*TR*TW*UA*US*UY*VE*VN*" +
                  "ZA*", nationName$ + "*") - 1) / 3 ;all sub strings are the same length

 *Flgptr = PeekI(?ImageTable + SizeOf(Integer) * x) ; x = 0 to imageCount, 0=ohNoFlag
  If *Flgptr = ?OhNoFlag
    flagtooltip$ = "No Flag Available"
  Else
    flagtooltip$ = nationName$ + " (" + countrycode$ + ")"
  EndIf
  CatchImage(100, *Flgptr)
  ImageGadget(200, 15, 522, 40, 40, ImageID(100))
  GadgetToolTip(200, flagtooltip$)

DataSection
  NoteGIF: : IncludeBinary "D:\load_get.gif"
  ImagesTable:
  Data.i ?OhNoFlag ;in case no flag from country code passed so it doesn't crash
  Data.i ?ADFlag, ?AEFlag, ?ALFlag, ?AMFlag, ?ARFlag, ?ATFlag, ?AUFlag, ?BAFlag, ?BDFlag, ?BEFlag,
         ?BGFlag, ?BOFlag, ?BRFlag, ?BSFlag, ?CAFlag, ?CHFlag, ?CLFlag, ?CNFlag, ?COFlag, ?CRFlag,
         ?CYFlag, ?CZFlag, ?DEFlag, ?DKFlag, ?DZFlag, ?ECFlag, ?EEFlag, ?EGFlag, ?ESFlag, ?FIFlag,
         ?FRFlag, ?GBFlag, ?GEFlag, ?GLFlag, ?GRFlag, ?GTFlag, ?HKFlag, ?HRFlag, ?HUFlag, ?IDFlag,
         ?IEFlag, ?ILFlag, ?IMFlag, ?INFlag, ?ISFlag, ?ITFlag, ?JPFlag, ?KHFlag, ?KRFlag, ?KZFlag,
         ?LIFlag, ?LKFlag, ?LTFlag, ?LUFlag, ?LVFlag, ?MAFlag, ?MCFlag, ?MDFlag, ?MEFlag, ?MKFlag,
         ?MNFlag, ?MOFlag, ?MTFlag, ?MXFlag, ?MYFlag, ?NGFlag, ?NLFlag, ?NOFlag, ?NPFlag, ?NZFlag,
         ?PAFlag, ?PEFlag, ?PHFlag, ?PLFlag, ?PTFlag, ?QAFlag, ?ROFlag, ?RSFlag, ?SAFlag, ?SEFlag,
         ?SGFlag, ?SIFlag, ?SKFlag, ?TRFlag, ?TWFlag, ?UAFlag, ?USFlag, ?UYFlag, ?VEFlag, ?VNFlag,
         ?ZAFlag
  ;For each of the above country labels in the ImageTable include in any order each label and its associated IncludeBinary file
EndDataSection
Thank You Demivec.

I did get it working though finally after I posted that I could not get it to work. The problem was me.