Page 1 of 1

Sprites in a linked list

Posted: Thu Oct 10, 2019 7:52 pm
by Phil
Hi,

I have a linked list with 57 sprites, i can load them and display them, then when the user is pushes a button the window containing the windowed screen is closed and a new window is opened with a new windowed screen.

When i want to display some of the images from the linked list on the new screen, the sprites become invalid sprites. The list still had 57 items.

Re: Sprites in a linked list

Posted: Thu Oct 10, 2019 9:35 pm
by Peter
No problem here so far:

Code: Select all

EnableExplicit

Global ImagesToLoad = 3

Global NewList Image()

Enumeration
  #BackgroundWindow
  #Button
  #Window
EndEnumeration

Procedure CloseWindowEvent()
  
  CloseWindow(#Window)
  
  DisableGadget(#Button, #False)
  
EndProcedure

Procedure OpenImageWindow()
  
  Protected newY
  
  OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 150, 500, "Images", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  newY = 20
  
  ForEach Image()
    ImageGadget(#PB_Any, 10, newY, 128, 128, Image())
    newY + 150
  Next
  
  BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
  
EndProcedure  

Procedure ButtonEvent()
  
  OpenImageWindow()
  
EndProcedure

Procedure Loaded(Type, Filename$, ObjectId)
  
  AddElement(Image())
  
  Image() = ImageID(ObjectId)
  
  If ListSize(Image()) = ImagesToLoad
    OpenImageWindow()
  EndIf
  
EndProcedure

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

OpenWindow(#BackgroundWindow, 0, 0, 0, 0, "", #PB_Window_Background)
ButtonGadget(#Button, 20, 20, 150, 50, "Open Image-Window")
DisableGadget(#Button, #True)
BindGadgetEvent(#Button, @ButtonEvent())

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

LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/apple-watch-blue-icon.png")
LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/owl-icon.png")
LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/snowman-icon.png")

Re: Sprites in a linked list

Posted: Sat Oct 12, 2019 3:53 pm
by Phil
Peter wrote:No problem here so far:

Code: Select all

EnableExplicit

Global ImagesToLoad = 3

Global NewList Image()

Enumeration
  #BackgroundWindow
  #Button
  #Window
EndEnumeration

Procedure CloseWindowEvent()
  
  CloseWindow(#Window)
  
  DisableGadget(#Button, #False)
  
EndProcedure

Procedure OpenImageWindow()
  
  Protected newY
  
  OpenWindow(#Window, #PB_Ignore, #PB_Ignore, 150, 500, "Images", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  newY = 20
  
  ForEach Image()
    ImageGadget(#PB_Any, 10, newY, 128, 128, Image())
    newY + 150
  Next
  
  BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
  
EndProcedure  

Procedure ButtonEvent()
  
  OpenImageWindow()
  
EndProcedure

Procedure Loaded(Type, Filename$, ObjectId)
  
  AddElement(Image())
  
  Image() = ImageID(ObjectId)
  
  If ListSize(Image()) = ImagesToLoad
    OpenImageWindow()
  EndIf
  
EndProcedure

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

OpenWindow(#BackgroundWindow, 0, 0, 0, 0, "", #PB_Window_Background)
ButtonGadget(#Button, 20, 20, 150, 50, "Open Image-Window")
DisableGadget(#Button, #True)
BindGadgetEvent(#Button, @ButtonEvent())

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

LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/apple-watch-blue-icon.png")
LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/owl-icon.png")
LoadImage(#PB_Any, "http://icons.iconarchive.com/icons/thesquid.ink/free-flat-sample/128/snowman-icon.png")
Tnx for your reply, i will try to shorten my code to the root cause of this problem then i will share it.

Re: Sprites in a linked list

Posted: Sat Oct 12, 2019 4:09 pm
by Peter
Phil wrote:i will try to shorten my code to the root cause of this problem then i will share it.
This is always a good idea. ;)

Re: Sprites in a linked list

Posted: Tue Nov 05, 2019 6:45 pm
by Phil
Peter wrote:
Phil wrote:i will try to shorten my code to the root cause of this problem then i will share it.
This is always a good idea. ;)
I found out my problem, it's not the linked list, it's the fact i close my screen/window/render en re-open it later, then the sprites become unusable it seems.

Code: Select all

;
Debug "use arrow keys to move"

OpenWindow(0, 0, 0, 800, 600, "")
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
AddWindowTimer(0, 1, 4000)
Procedure RenderFrame()
  Static x, y
  
  ClearScreen(RGB(0, 0, 0))

  If ExamineKeyboard()
    
    If KeyboardPushed(#PB_Key_Left)
      x-1
    ElseIf KeyboardPushed(#PB_Key_Right)
      x+1
    EndIf
    
    If KeyboardPushed(#PB_Key_Up)
      y-1
    ElseIf KeyboardPushed(#PB_Key_Down)
      y+1
    EndIf
    
    ; Reduce the sprite size before display
    ;
    ZoomSprite(0, 60, 60)
    
    ; Display 100 sprites !
    ;
    For xDelta = 0 To 9
      For yDelta = 0 To 9 
        DisplayTransparentSprite(0, x+xDelta*60, y+yDelta*60, 128) ; half transparency
      Next
    Next
    
    ; and another 100 sprites !
    ;
    For xDelta = 0 To 9
      For yDelta = 0 To 9
        DisplaySprite(0, x*2+xDelta*60, y*2+yDelta*60)
      Next
    Next
    
  EndIf
  
  FlipBuffers() ; continue the rendering
EndProcedure
Procedure WindowTimerEvents()
  If EventWindow() = 0
    UnbindEvent(#PB_Event_RenderFrame, @RenderFrame()) 
    CloseScreen()
    CloseWindow(0)
    OpenWindow(0, 0, 0, 800, 600, "")
    OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)
    BindEvent(#PB_Event_RenderFrame, @RenderFrame())
  EndIf
EndProcedure

Procedure Loading(Type, Filename$)
  Static NbLoadedElements
  
  NbLoadedElements+1
  If NbLoadedElements = 1 ; Finished the loading of all images and sounds, we can start the applications
    FlipBuffers()         ; start the rendering
    AddWindowTimer(0, 1, 1000)
  EndIf
EndProcedure
Procedure LoadingError(Type, Filename$)
  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())
BindEvent(#PB_Event_Timer,          @WindowTimerEvents()) 
LoadSprite(0, "Data/Spider.png")


Re: Sprites in a linked list

Posted: Wed Nov 06, 2019 10:42 am
by Fred
Yes, the sprites are screen dependant. If you close it, you need to reload the sprites