Sprites in a linked list

Just starting out? Need help? Post your questions and find answers here.
Phil
Posts: 4
Joined: Thu Oct 10, 2019 7:49 pm

Sprites in a linked list

Post 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.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Sprites in a linked list

Post 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")
Phil
Posts: 4
Joined: Thu Oct 10, 2019 7:49 pm

Re: Sprites in a linked list

Post 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.
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Sprites in a linked list

Post 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. ;)
Phil
Posts: 4
Joined: Thu Oct 10, 2019 7:49 pm

Re: Sprites in a linked list

Post 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")

Fred
Site Admin
Posts: 1506
Joined: Mon Feb 24, 2014 10:51 am

Re: Sprites in a linked list

Post by Fred »

Yes, the sprites are screen dependant. If you close it, you need to reload the sprites
Post Reply