Misc Routines

Previous topic - Next topic

MrTAToad

I've listed a few of the various routines that Spots uses.

The first loads in all graphics, sounds etc, based on information stored in a INI file - this allows extra stuff to be loaded (or changed), without the need to always compile the main program.  Also included is a function for reading/saving screen parameters.

Code (glbasic) Select
// --------------------------------- //
// Project: Spots
// Start: Thursday, September 11, 2008
// IDE Version: 5.360

// ------------------------------------------------------------- //
// ---  LOADREMOVEFILES  ---
// ------------------------------------------------------------- //
TYPE screenResList
index
width
height
ENDTYPE

TYPE loadDataList
isBumpMap
fileName$
idNumber
xSize
ySize
ENDTYPE

GLOBAL screenRes[] AS screenResList
GLOBAL spriteLoadData[] AS loadDataList
GLOBAL fontLoadData[] AS loadDataList
GLOBAL objectLoadData[] AS loadDataList
GLOBAL NO_DATA$="NO_DATA"

FUNCTION loadRemoveFiles$: load,fileName$
LOCAL errorMessage$

// These values are defined LOCAL:
errorMessage$=""
IF load=TRUE
IF fileName$=""
errorMessage$="No filename has been provided"
ELSE
clearLoadData()
errorMessage$=doLoadData$(fileName$,TRUE)
IF errorMessage$=""
// Load in the list of screen resolutions
clearScreenResList()
errorMessage$=loadScreenResList$(fileName$)
ENDIF
ENDIF
ELSE
SYSTEMPOINTER TRUE
doLoadData$("",FALSE)
ENDIF

RETURN errorMessage$
ENDFUNCTION // LOADREMOVEFILES

FUNCTION doLoadData$:fileName$,doLoad
LOCAL file$
LOCAL loop,secLoop
LOCAL errorMessage$
LOCAL bumpText$="B:"
LOCAL sections$[]; DIM sections$[8]
LOCAL isBumpMap
LOCAL l AS loadDataList

errorMessage$=""
sections$[0]="SPRITES"
sections$[1]="SOUNDS"
sections$[2]="FONTS"
sections$[3]="3DGRAPHICS"
sections$[4]=""

IF doLoad=TRUE
IF DOESFILEEXIST(fileName$)
INIOPEN fileName$
secLoop=0
WHILE sections$[secLoop]<>""
loop=0
file$=INIGET$(sections$[secLoop],INTEGER(loop))
WHILE file$<>NO_DATA$
IF LEFT$(file$,LEN(bumpText$))=bumpText$
isBumpMap=TRUE
file$=MID$(file$,LEN(bumpText$),LEN(file$))
ELSE
isBumpMap=FALSE
ENDIF

IF DOESFILEEXIST(file$)
l.xSize=0
l.ySize=0
l.isBumpMap=FALSE
l.fileName$=file$+"~"
l.idNumber=loop

SELECT sections$[secLoop]
CASE sections$[0]
IF isBumpMap=TRUE
LOADBUMPTEXTURE file$,loop
l.isBumpMap=TRUE
ELSE
LOADSPRITE file$,loop
ENDIF

GETSPRITESIZE loop,l.xSize,l.ySize
DIMPUSH spriteLoadData[],l
CASE sections$[1]
LOADSOUND file$,loop,1
CASE sections$[2]
LOADFONT file$,loop
SETFONT loop
GETFONTSIZE l.xSize,l.ySize
DIMPUSH fontLoadData[],l
CASE sections$[3]
X_LOADOBJ file$,loop
DIMPUSH objectLoadData[],l
ENDSELECT

INC loop,1
file$=INIGET$(sections$[secLoop],INTEGER(loop))
ELSE
errorMessage$="Can NOT find the file : "+file$
file$=NO_DATA$
ENDIF
WEND

IF errorMessage$<>""
BREAK
ENDIF

INC secLoop,1
WEND
ELSE
errorMessage$="Can NOT find INI file : "+fileName$
ENDIF
ELSE
FOREACH l IN spriteLoadData[]
IF l.isBumpMap=TRUE
LOADBUMPTEXTURE l.fileName$,l.idNumber
ELSE
LOADSPRITE l.fileName$,l.idNumber
ENDIF
NEXT

FOREACH l IN fontLoadData[]
LOADFONT l.fileName$,l.idNumber
NEXT
ENDIF

RETURN errorMessage$
ENDFUNCTION

FUNCTION clearScreenResList:
LOCAL s AS screenResList

FOREACH s IN screenRes[]
DELETE s
NEXT
ENDFUNCTION

FUNCTION clearLoadData:
LOCAL l AS loadDataList

FOREACH l IN spriteLoadData[]
DELETE l
NEXT

FOREACH l IN fontLoadData[]
DELETE l
NEXT

FOREACH l IN objectLoadData[]
DELETE l
NEXT
ENDFUNCTION

FUNCTION loadScreenResList$:setupFile$
LOCAL s AS screenResList
LOCAL SCREENRES_SECTION$="SCREENRESOLUTIONS"
LOCAL keyIndex
LOCAL temp$
LOCAL pos
LOCAL width
LOCAL height
LOCAL errorMessage$
LOCAL notsorted
LOCAL i
LOCAL swap AS screenResList
LOCAL count

errorMessage$=""
keyIndex=0
INIOPEN setupFile$

temp$=INIGET$(SCREENRES_SECTION$,keyIndex)
WHILE temp$<>NO_DATA$
pos=INSTR(temp$,",")
IF pos<>-1
width=LEFT$(temp$,pos)
height=MID$(temp$,pos+1,LEN(temp$))

s.width=width
s.height=height
DIMPUSH screenRes[],s

INC keyIndex,1
temp$=INIGET$(SCREENRES_SECTION$,keyIndex)
ENDIF
WEND

IF keyIndex=0
errorMessage$="NO screen resolutions found."
ELSE
// Now we sort in width order
count = BOUNDS(screenRes[], 0)
notsorted=TRUE
  WHILE notsorted
    notsorted = FALSE
    FOR i = 0 TO count - 2
      IF screenRes[i].width > screenRes[i+1].width AND _
          screenRes[i].height > screenRes[i+1].height
        swap = screenRes[i]
        screenRes[i] = screenRes[i+1]
        screenRes[i+1] = swap
        notsorted = TRUE
        ENDIF
        NEXT
      WEND

      // Now we set the index
      i=0
      FOREACH s IN screenRes[]
      s.index=i
      INC i,1
      NEXT
    ENDIF

RETURN errorMessage$
ENDFUNCTION

FUNCTION setupDisplay:setupFile$,BYREF screenWidth,BYREF screenHeight,BYREF fullScreen,BYREF mVolume,BYREF sVolume,doLoadData
LOCAL DISPLAY_SECTION$="DISPLAY"
LOCAL DISPLAY_SCREENWIDTH$="SCREENWIDTH"
LOCAL DISPLAY_SCREENHEIGHT$="SCREENHEIGHT"
LOCAL DISPLAY_FULLSCREEN$="SCREENFULLSCREEN"
LOCAL VOLUME_SECTION$="VOLUME"
LOCAL VOLUME_MUSIC$="MUSICVOLUME"
LOCAL VOLUME_SFX$="MUSICSFX"

LOCAL width$
LOCAL height$
LOCAL fullScreen$
LOCAL musicVolume$
LOCAL sfxVolume$

INIOPEN setupFile$
IF doLoadData=TRUE
width$=INIGET$(DISPLAY_SECTION$,DISPLAY_SCREENWIDTH$)
height$=INIGET$(DISPLAY_SECTION$,DISPLAY_SCREENHEIGHT$)
fullScreen$=INIGET$(DISPLAY_SECTION$,DISPLAY_FULLSCREEN$)
musicVolume$=INIGET$(VOLUME_SECTION$,VOLUME_MUSIC$)
sfxVolume$=INIGET$(VOLUME_SECTION$,VOLUME_SFX$)

IF width$=NO_DATA$ OR height$=NO_DATA$ OR fullScreen$=NO_DATA$
// setup the default screen width
screenWidth=640
screenHeight=480
fullScreen=TRUE
ELSE
screenWidth=width$
screenHeight=height$
fullScreen=fullScreen$
ENDIF

IF musicVolume$=NO_DATA$
mVolume=1.0
ELSE
mVolume=musicVolume$
ENDIF

IF sfxVolume$=NO_DATA$
sVolume=1.0
ELSE
sVolume=sfxVolume$
ENDIF
ELSE
INIPUT DISPLAY_SECTION$,DISPLAY_SCREENWIDTH$,INTEGER(screenWidth)
INIPUT DISPLAY_SECTION$,DISPLAY_SCREENHEIGHT$,INTEGER(screenHeight)
INIPUT DISPLAY_SECTION$,DISPLAY_FULLSCREEN$,INTEGER(fullScreen)
INIPUT VOLUME_SECTION$,VOLUME_MUSIC$,mVolume
INIPUT VOLUME_SECTION$,VOLUME_SFX$,sVolume
SETSCREEN screenWidth,screenHeight,fullScreen
ENDIF

SETSCREEN screenWidth,screenHeight,fullScreen
BLACKSCREEN
LIMITFPS 0
SYSTEMPOINTER FALSE

RETURN TRUE
ENDFUNCTION


The next is a modified BlitzMax routine (the original is by Leadwerks) to allow fixed step movement irrespective of computer speed :

Code (glbasic) Select
// --------------------------------- //
// Project: Spots
// Start: Thursday, September 11, 2008
// IDE Version: 5.360

FUNCTION initAppTime:
AppTime_UPS=0
AppTime_Iterator=0
AppTime_CurrentTime=0
AppTime_PauseStart=FALSE
AppTime_Speed=1.0
AppTime_DesiredLoopTime=1000.0/60.0
AppTime_LastUpdateTime=0
AppTime_LastUPSTime=0
AppTime_DesiredFrequency=0
ENDFUNCTION

FUNCTION updateAppTime:frameRate
LOCAL time
LOCAL elapsed

IF AppTime_PauseStart<>0
RETURN
ENDIF

AppTime_DesiredFrequency=frameRate
AppTime_DesiredLoopTime=1000.0/frameRate

time=GETTIMERALL()

IF AppTime_LastUpdateTime=0
AppTime_Speed=1
AppTime_LastUpdateTime=time
AppTime_CurrentTime=time
AppTime_LastUPSTime=time
ELSE
elapsed=time-AppTime_LastUpdateTime
IF elapsed=0
elapsed=1
SLEEP 1
INC time,1
ENDIF

AppTime_Speed=elapsed/AppTime_DesiredLoopTime
AppTime_CurrentTime=time
AppTime_LastUpdateTime=time
ENDIF

INC AppTime_Iterator,1

IF AppTime_CurrentTime-AppTime_LastUPSTime>=1000
AppTime_UPS=AppTime_Iterator/((AppTime_CurrentTime-AppTime_LastUPSTime)/1000)
AppTime_LastUPSTime=AppTime_CurrentTime
AppTime_Iterator=0
ENDIF

RETURN AppTime_Speed
ENDFUNCTION

FUNCTION pauseUnPauseAppTime:pause
LOCAL elapsed

IF pause=TRUE
IF AppTime_PauseStart=0
AppTime_PauseStart=GETTIMERALL()
AppTime_UPS=0
AppTime_Speed=0
ENDIF
ELSE
IF AppTime_PauseStart<>0
IF AppTime_LastUpdateTime<>0
elapsed=GETTIMERALL()-AppTime_PauseStart
INC AppTime_LastUpdateTime,elapsed
ENDIF

AppTime_PauseStart=0
updateAppTime(AppTime_DesiredFrequency)
ENDIF
ENDIF
ENDFUNCTION

Quentin

nice ones. thx for sharing.

One thing:
instead of
Code (glbasic) Select

FOREACH l IN spriteLoadData[]
DELETE l
NEXT


you can simply use
Code (glbasic) Select

DIM spriteLoadData[0]


It does the same

MrTAToad