Monday 16 April 2012

Initialisation

After a short break, it's back to the grind with some basics of program operation.

I'll also be changing the way I enter code into the Blog, to try to make it more readable.  Previously I just changed the font, but this mashes up the code and adds quite a lot of unnecessary HTML.

Now I'll be using the pre-format style previously reserved for the code summaries, now in Matrix green to make it stand out.

It's a bit more fiddling to create, but reduces the Faff behind the scenes should make the experience a little better.

Initialisation is getting things ready, and this comes in several types.
  • Program - done once when the program first starts
  • Game - done at the start of a new game
  • Level - done at the start of a new level
At the moment we don't have a game or levels, so this portion is about Program Initialisation.

Setting up the global variables, preparing the arrays using the Dim command and loading the main graphics and sound are only done once, so these are done in a function we will call Initialise().

As with all functions this goes after the main loop, I tend to put this at the end of the main functions just before the return functions as it tends to get changed less once in place.

It contains the permanent code code that we currently do before the main loop, so a larger chunk of this is just copied into the function.

The constants and user defined types stay where they are.
function Initialise()
   // Prepare Globals
   global display as displayType
      display.physWidth# = getDeviceWidth()
      display.physHeight# = getDeviceHeight()
      display.virtWidth# = PhysWidth()
      display.virtHeight# = PhysHeight()
      display.iconSize# = PhysWidth() / PANEL_COLS
      display.textHeight# = PhysHeight() / 20.0
      display.highSprite = 0
      display.iconImage = SafeloadImage( "objects.png" )

   global panel as panelType
      panel.iconMax = PANEL_ROWS * PANEL_COLS
      panel.height# = IconSize() * PANEL_ROWS
      panel.speed# = 0.0
      panel.isOpen = 0
      panel.Image = SafeloadImage( "panel.jpg" )
      panel.sprite = createSprite( PanelImage() )
      panel.text = createText( "Objects" )
      panel.sound = SafeLoadSound("panel.wav")

   setImageWrapU( PanelImage() , 1 )
   setImageWrapV( PanelImage() , 1 )

   uScale# = getImageWidth( PanelImage() ) / PhysWidth()
   vScale# = getImageHeight( PanelImage() ) / PanelHeight()
   setSpriteUVScale( PanelSprite() , uScale# , vScale# )
   setSpriteSize( PanelSprite() , PhysWidth() , PanelHeight() )
   setSpritePosition( PanelSprite() , 0 , PhysHeight() )
   setSpriteDepth( PanelSprite() , DEPTH_PANEL)

   setTextSize( panelText() , textHeight() )
   setTextAlignMent( panelText() , 1)
   setTextPosition( panelText() , PhysWidth() / 2.0 , PhysHeight() - TextHeight() )
   setTextDepth( panelText() , DEPTH_PANEL - 1 )

   // Prepare Arrays

   dim panelIconSprite[ panelIconMax() ]
      for icon = 1 to panelIconMax()
         panelIconSprite[ icon ] = createSprite( 0 )
         setSpriteSize( panelIconSprite[ icon ] , IconSize() - 4, IconSize() - 4)
         setSpriteColor( panelIconSprite[ icon ] , 255 , 255 , 255 , 127 )
      next icon

   dim workObject[ MAX_WORKOBJECTS ] as WorkObjectType
      for thisObject = 1 to MAX_WORKOBJECTS
         workObject[ thisObject ].isFree = 1
         workObject[ thisObject ].objectID = 0
         workObject[ thisObject ].sprite = 0
      next thisObject
   setVirtualResolution( VirtWidth(), VirtHeight() )
   PositionPanelIcons()
endfunction
I have also indented the parts which initialise the fields of the UDT's to highlight that they belong to the global definitions that they follow.

Now the start of the program looks like this.
#constant PANEL_ROWS   3
#constant PANEL_COLS   4

#constant DEPTH_SCORE   20
#constant DEPTH_PANEL   30
#constant DEPTH_WORK   40

#constant MAX_WORKOBJECTS   100

#constant FRAME_HIGHLIGHT   251
#constant FRAME_TRASHCAN      252

type displayType
   physWidth#
   physHeight#
   virtWidth#
   virtHeight#
   iconSize#
   textHeight#
   lastFPS#
   iconImage
   pointDX#
   pointDY#
   pointWX#
   pointWY#
   highSprite
endtype

type panelType
   height#
   speed#
   isOpen
   image
   sprite
   text
   sound
   iconMax
endtype

type WorkObjectType
   isFree
   objectID
   sprite
endtype

global numWorkObjects = 0

Initialise()

// Temporary Objects
With just the call to the Initialise() function where the old code was.

I have left numWorkObjects out of the function temporarily as this really needs to be incorporated into a a larger global variable using UDT, which we will come to next.

No comments:

Post a Comment