Page 1 of 1

Spritehandles, good or bad ways?

Posted: Sun Sep 11, 2005 10:16 pm
by AJirenius
Just have a beginners question (again) as the documentation is a little vague about this.
If I choose to create a sprite with handle 1 and then create a sprite with handle 100. Do I somehow hold up any memory for those handles I left in between or is it just like variables?

Why Im asking is because I want a sprite bank at 1-10 and then a bank spanning over 100-120. I hope I dont hold up/reserve any spritememory in between then.

Posted: Mon Sep 12, 2005 4:14 pm
by griz
Pure Basic automatically frees up resources when your program ends. You can also do it yourself if you need to by using FreeSprite(). Don't worry about sprites #11-99 as they won't take up memory unless you actually use them.

Posted: Mon Sep 12, 2005 9:41 pm
by AJirenius
Thanks! Makes my life a lot easier... nah but at least my coding.

Thanks again.. back to the dungeon.

Posted: Wed Sep 14, 2005 12:58 pm
by nco2k
@AJirenius
Do I somehow hold up any memory for those handles I left in between or is it just like variables?
yes you do... which means, if you use the highest sprite id, the lower ones, will be kind of reserved, doesnt matter if they are actually used or not:

Code: Select all

Enumeration
  #Sprite0 = 0
  #Sprite1 = 999999 ;#Sprite1 = 1 try both!
EndEnumeration

hwnd.l = OpenWindow(0, 0, 0, 640, 480, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "")
If InitSprite() And OpenWindowedScreen(hwnd, 0, 0, WindowWidth(), WindowHeight(), 0, 0, 0)
  
  If CreateSprite(#Sprite1, WindowWidth(), WindowHeight())
    
    If StartDrawing(SpriteOutput(#Sprite1))
      
      For i = 0 To 1000
        Box(Random(WindowWidth() / 2), Random(WindowHeight() / 2), Random(WindowWidth()), Random(WindowHeight()), RGB(Random(255), Random(255), Random(255)))
      Next
      
      StopDrawing()
      
      Repeat
        WinEvent.l = WindowEvent()
        DisplaySprite(#Sprite1, 0, 0)
        FlipBuffers()
        If WinEvent = 0
          Delay(1)
        EndIf
      Until WinEvent = #PB_Event_CloseWindow
      
    EndIf
    
  EndIf
  
EndIf

End
make the test, try both #Sprite1 = 1 and #Sprite1 = 999999 and have a look in the task manager, compare for yourself the difference in memory use. :wink:

c ya,
nco2k

Posted: Fri Sep 16, 2005 5:15 pm
by griz
You're absolutely correct nco2k. I'm a little surprised at this.

999,999 sprite handles : 25,892 kb

100 sprite handles : 2, 396 kb

2 sprite (0 and 1) handles : 2,392 kb
Don't worry about sprites #11-99 as they won't take up memory unless you actually use them.
My mistake, I'm sorry. AJirenius, looks like a few kb gets wasted for unused handles in your application. It's probably nothing to worry about though.

However, using #pb_any is a different story. I tried the same code above but used #pb_any for sprite1 (and changed it from a constant). #pb_any assigned sprite1 the handle '8396440' which is nearly 10 times higher than the 999,999 used above. The memory consumed was : 2,396 kb

Code: Select all

hwnd.l = OpenWindow(0, 0, 0, 640, 480, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, "")
If InitSprite() And OpenWindowedScreen(hwnd, 0, 0, WindowWidth(), WindowHeight(), 0, 0, 0)

sprite1 = CreateSprite(#PB_Any, WindowWidth(), WindowHeight())
Debug sprite1
   
  If StartDrawing(SpriteOutput(Sprite1))
     
    For i = 0 To 1000
      Box(Random(WindowWidth() / 2), Random(WindowHeight() / 2), Random(WindowWidth()), Random(WindowHeight()), RGB(Random(255), Random(255), Random(255)))
    Next
     
    StopDrawing()
     
    Repeat
      WinEvent.l = WindowEvent()
      DisplaySprite(Sprite1, 0, 0)
      FlipBuffers()
      If WinEvent = 0
        Delay(1)
      EndIf
    Until WinEvent = #PB_Event_CloseWindow
     
  EndIf
   
EndIf

End