Tuesday, 1 May 2012

Panel Icon Optimising

There is a duplicated block of code now which will fit into it's own function.
function SetPanelSprite( thisPos , thisFrame )
   thisSprite = panelIconSprite[ thisPos ]
   if thisFrame = 0
      setSpriteImage( thisSprite , 0 )
      clearSpriteAnimationFrames( thisSprite )
   else
      setSpriteImage( thisSprite , IconImage() )
      setSpriteAnimation( thisSprite , 64 , 64 , 252 )
      setSpriteFrame( thisSprite , thisFrame )
   endif
endfunction
This was lifted straight out of the group loop, so can simply be replaced with the call
SetPanelSprite( thisPos , thisFrame )
thisSprite = panelIconSprite[ thisPos ]
In the members loop for the objects,
thisSprite = panelIconSprite[ nextIcon ]
setSpriteImage( thisSprite , IconImage() )
setSpriteAnimation( thisSprite , 64 , 64 , 252 )
setSpriteFrame( thisSprite , thisFrame )
Can be replaced with,
SetPanelSprite( nextIcon , thisFrame )
thisSprite = panelIconSprite[ nextIcon ]
Finally, in the remainder loop.
thisSprite = panelIconSprite[ i ]
setSpriteImage( thisSprite , 0 )
clearSpriteAnimationFrames( thisSprite )
Is replaced with,
SetPanelSprite( i , 0 )
thisSprite = panelIconSprite[ i ]
So the function now looks like this.
function SetPanelIcons()
   // Process Groups
   thisGroups = len(GameGroups()) / 2.0
   firstGroupIcon = panelIconMax() - thisGroups + 1
   for i=0 to thisGroups-1
      thisCode$ = mid( GameGroups() , i * 2 + 1 , 2 )
      thisObject = Base96Decode( thisCode$ )
      if thisObject = 0
         thisFrame = 0
      else
         thisFrame = ObjectIcon( thisObject )
      endif
      thisPos = firstGroupIcon + i
      SetPanelSprite( thisPos , thisFrame )
      thisSprite = panelIconSprite[ thisPos ]
      if thisObject = PanelGroup()
         setSpriteColorAlpha( thisSprite , PGROUP_ALPHAHI )
      else
         setSpriteColorAlpha( thisSprite , PGROUP_ALPHA )
      endif
   next i
   // Process Objects
   if PanelGroup() > 0
      thisString$ = ObjectDetail( PanelGroup() )
      thisMembers = len(thisString$) / 2.0
      nextIcon = 1
      for i=0 to thisMembers - 1
         thisCode$ = mid( thisString$ , i * 2 + 1 , 2 )
         thisObject = Base96Decode( thisCode$ )
//         if ObjectIsKnown( thisObject ) > 0
            thisFrame = ObjectIcon( thisObject )
            SetPanelSprite( nextIcon , thisFrame )
            thisSprite = panelIconSprite[ nextIcon ]
            setSpriteColorAlpha( thisSprite , PGROUP_ALPHAHI )
            inc nextIcon , 1
            if nextIcon >= firstGroupIcon then exit
//         endif
      next i

      // Reset Remainder
      for i = nextIcon to firstGroupIcon - 1
         SetPanelSprite( i , 0 )
         thisSprite = panelIconSprite[ nextIcon ]
         setSpriteColorAlpha( thisSprite , PGROUP_ALPHA )
      next i
   endif
endfunction

4 comments: