Tuesday 1 May 2012

Panel Icons

To put the icons into the panel will require a new function,
function SetPanelIcons()

endfunction
which will be called each time the icons are changed - particularly when a different group is selected.

First, we get the number of groups with members and store the value.
thisGroups = len(GameGroups()) / 2.0
Since each group is encoded in Base96 which uses 2 chars per object, we divide the length of the encoded list by 2 to get the number of groups.

We determine which panel icon will be the first used by a group.
firstGroupIcon = panelIconMax() - thisGroups + 1
If there are no groups, the following loop will not run.
for i=0 to thisGroups-1
otherwise, this loop processes each one.
thisCode$ = mid( GameGroups() , i * 2 + 1 , 2 )
thisObject = Base96Decode( thisCode$ )
if thisObject > 0
   thisFrame = ObjectIcon( thisObject )
else
   thisFrame = 0
endif
This extracts the coded object index from the group list and decodes it to obtain the object number.

If the object number is greater than zero then the icon field for that object is used for the frame, otherwise a value of zero is used.

The frame number is checked by the next piece of code.
thisPos = firstGroupIcon + i
thisSprite = panelIconSprite[ thisPos ]
if thisFrame = 0
   setSpriteImage( thisSprite , 0 )
   clearSpriteAnimationFrames( thisSprite )
else
   setSpriteImage( thisSprite , IconImage() )
   setSpriteAnimation( thisSprite , 64 , 64 , 252 )
   setSpriteFrame( thisSprite , thisFrame )
endif
If it is zero, indicating for some reason that we don't have an image for this object, then the panel sprite is set back to it's default.

If there is a frame number, then the icon sheet image is assigned to the sprite, animation is enabled for 64 x 64 frames and the correct frame selected.

The loop is closed and the function so far, should look 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
      thisSprite = panelIconSprite[ thisPos ]
      if thisFrame = 0
         setSpriteImage( thisSprite , 0 )
         clearSpriteAnimationFrames( thisSprite )
      else
         setSpriteImage( thisSprite , IconImage() )
         setSpriteAnimation( thisSprite , 64 , 64 , 252 )
         setSpriteFrame( thisSprite , thisFrame )
      endif
      // Change Icon Colour

   next i
   // Process Objects

endfunction
Add a call to change the icons at the start of the case STATE_RUNNING block of code
case STATE_RUNNING
   // Running Code
   if getTextVisible( PanelText() ) = 0
      // Game starting - Activate Transition
      setTextVisible( panelText() , 1 )
      setTextColor( panelText() , 127 , 255 , 127 , 0 )
      PositionPanelIcons()
      setPanelIcons()
      StartTransition( TRANS_FADEIN , MENU_FADE )
   elseif (Transition() = TRANS_FADEIN) or (Transition() = TRANS_FADEOUT)
And the first icons can be tested.

When I try this, the whole thing crashes.  So begins an hour of isolating code and printing variables until the problem can be tracked down.

In this case, it is caused by a bug in AGK, where data is getting corrupted on access.

Since there is a new release imminent with a number of bug fixes, I am hoping this will be picked up.

In the meantime, a workaround (for me at least) is to change the following function.
function GameGroups()
endfunction game.groupObject$
Into this.
function GameGroups()
thisString$ = game.groupObject$
endfunction thisString$
Which does exactly the same thing but with an extra step.

That step seems like enough to prevent the problem.
And now if we increase the columns to 5.
#constant PANEL_COLS   5
It becomes.

No comments:

Post a Comment