Image on ButtonImageGadget wont load and display at startup

Just starting out? Need help? Post your questions and find answers here.
User avatar
T4r4ntul4
Posts: 132
Joined: Wed May 21, 2014 1:57 pm
Location: Netherlands
Contact:

Image on ButtonImageGadget wont load and display at startup

Post by T4r4ntul4 »

Problem: Image on ButtonImageGadget wont load and display at startup, only after clicking button: SetGadgetAttribute

Code: Select all

Procedure GadgetEvents()
  
  Select EventGadget()
      
    Case 1 ; set image on image button
      Debug "set image on image button"
      SetGadgetAttribute(2, #PB_Button_Image, ImageID(0))
  
    Case 2 ; image button test
      Debug "image button"
      
    
      
  EndSelect
  
EndProcedure



If OpenWindow(0, 20, 20, 820, 620, "", #PB_Window_SystemMenu | #PB_Window_Background)
  Top = 10
  GadgetHeight = 24
  

  ButtonGadget(1, 223, Top,  180, GadgetHeight, "<<--- SetGadgetAttribute")
  ButtonImageGadget(2, 10, Top, 200, 200, ImageID(0))
  TextGadget(3, 10, 220, 400, 25, "Problem: Image on ButtonImageGadget wont load and display at startup, only after clicking button: SetGadgetAttribute")

  
  BindEvent(#PB_Event_Gadget, @GadgetEvents())

  
  CompilerIf #PB_Compiler_OS <> #PB_OS_Web
    Repeat
      Event = WaitWindowEvent()
    Until Event = #PB_Event_CloseWindow
  CompilerEndIf
EndIf


OpenWindowedScreen(WindowID(0), 500, 10, 100, 100)

Procedure RenderFrame()
  Static x = 100, y = 200
  
  ClearScreen(RGB(0, 0, 0))
  
  If ExamineMouse()
    
   
    If MouseButton(#PB_MouseButton_Left)
      Debug "Left button"
    EndIf
  EndIf
  
  
  FlipBuffers() ; continue the rendering
EndProcedure

Procedure Loading(Type, Filename$, ObjectId)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1 ; The loading of all images and sounds is finished, we can start the rendering
    FlipBuffers()         ; start the rendering
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

LoadSprite(0, "Data/Spider.png")
LoadImage(0, "Data/knop1.png") ; just a png with a color for test purposes
User avatar
T4r4ntul4
Posts: 132
Joined: Wed May 21, 2014 1:57 pm
Location: Netherlands
Contact:

Re: Image on ButtonImageGadget wont load and display at star

Post by T4r4ntul4 »

it works without the openscreen, but with it, it wont display it at startup.
can someone confirm this? using the posted code.
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: Image on ButtonImageGadget wont load and display at star

Post by Fred »

You should create your window only when the resources are all loaded (ie: NbLoadedElements = 2), else ImageID(0) will return 0.

Code: Select all

Procedure GadgetEvents()
  
  Select EventGadget()
      
    Case 1 ; set image on image button
      Debug "set image on image button"
      SetGadgetAttribute(2, #PB_Button_Image, ImageID(0))
      
    Case 2 ; image button test
      Debug "image button"
      
      
      
  EndSelect
  
EndProcedure

Procedure RenderFrame()
  Static x = 0, y = 200
  
  ClearScreen(RGB(0, 0, 0))
  
  DisplaySprite(0, x, x)
  
  x+1
  
  If ExamineMouse()
    
    
    If MouseButton(#PB_MouseButton_Left)
      Debug "Left button"
    EndIf
  EndIf
  
  
  FlipBuffers() ; continue the rendering
EndProcedure

Procedure Loading(Type, Filename$, ObjectId)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 2 ; The loading of all images and sounds is finished, we can start the rendering
    
    If OpenWindow(0, 20, 20, 820, 620, "", #PB_Window_SystemMenu | #PB_Window_Background)
      Top = 10
      GadgetHeight = 24
      
      
      ButtonGadget(1, 223, Top,  180, GadgetHeight, "<<--- SetGadgetAttribute")
      ButtonImageGadget(2, 10, Top, 200, 200, ImageID(0))
      TextGadget(3, 10, 220, 400, 25, "Problem: Image on ButtonImageGadget wont load and display at startup, only after clicking button: SetGadgetAttribute")
      
      
      BindEvent(#PB_Event_Gadget, @GadgetEvents())
      
      OpenWindowedScreen(WindowID(0), 500, 10, 100, 100)
      
      CompilerIf #PB_Compiler_OS <> #PB_OS_Web
        Repeat
          Event = WaitWindowEvent()
        Until Event = #PB_Event_CloseWindow
      CompilerEndIf
    EndIf
    
    FlipBuffers()         ; start the rendering
  EndIf
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
  Debug Filename$ + ": loading error"
EndProcedure

; Register the loading event before calling any resource load command
BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())

LoadSprite(0, "Data/Spider.png")
LoadImage(0, "Data/Spider.png") ; just a png with a color for test purposes
User avatar
T4r4ntul4
Posts: 132
Joined: Wed May 21, 2014 1:57 pm
Location: Netherlands
Contact:

Re: Image on ButtonImageGadget wont load and display at star

Post by T4r4ntul4 »

I took WindowedScreen.sb as framework, there is no explanation to use the loading procedure like that. It is confusing to me.

So if i have more images to load, then i need to add that up to: NbLoadedElements ? (where in the docs is that explained?)
For example i have 14 images to load then the NbLoadedElements = 14 ?
Fred
Site Admin
Posts: 1821
Joined: Mon Feb 24, 2014 10:51 am

Re: Image on ButtonImageGadget wont load and display at star

Post by Fred »

You have to decide when your app is ready and needed elements loaded. Using a resource count is just one way to do it, you do it another way if you want.
Post Reply