Here is what I use for my config files.
Make a config.ini file that contains this
---screen resolution
screen_w=1024
screen_h=768
fullscreen=1
---Player 1 controls
P1_up=200
P1_down=208
P1_left=203
P1_right=205
P1_fire=57
P1_accel=29
P1_reverse=56
Now the code part. What I do is assign every variable a "default" value, and then call the readconfig() function. This way I know that all values are correctly assigned in case the config file is missing/wrong. You should also check the read values to make sure they are "legal" for whatever they are being assigned.
Note that anything that is not an explicitly stated variable is simply ignored, so you can easily add comments.
;Ini Variables
Global screen_w = 640
Global screen_h = 480
Global fullscreen = 0
Global P1_up = 200
Global P1_down = 208
Global P1_left = 203
Global P1_right = 205
Global P1_fire = 57
Global P1_accel = 29
Global P1_reverse =56
readconfig()
printvalues()
;writeconfig()
WaitKey
End
Function Printvalues()
Print "Screen width: " + screen_w
Print "Screen height: " +screen_h
Print "fullscreen: " + fullscreen
Print "P1 up: " + P1_up
Print "P1 down: " + P1_down
Print "P1 left: " + P1_left
Print "P1 right: " + P1_right
Print "P1 fire: " + P1_fire
Print "P1 accel: " + P1_accel
Print "P1 reverse: " + P1_reverse
End Function
Function ReadConfig()
Local label$
Local value
Local location
filename = ReadFile ("config.ini") ;load the config file
If filename <> 0 ;only continue if the file exists
While Not Eof (filename) ;keep reading until the end of the file
val$ = ReadLine(filename) ;read the full line
location = Instr( val$,"=",1) ;find the location of the = sign
If Not location = 0 ;if no = found in the line, ignore it.
label$ = Left$ (val$, location-1) ; get the label part of the line
value = Int( Right$ (val$, Len(val$) -location)) ;get the value part of the line
Select True
Case label$ = "screen_w"
screen_w = value
Case label$ = "screen_h"
screen_h = value
Case label$ = "fullscreen"
fullscreen = value
Case label$ = "P1_up"
P1_up = value
Case label$ = "P1_down"
P1_down = value
Case label$ = "P1_left"
P1_left = value
Case label$ = "P1_right"
P1_right = value
Case label$ = "P1_fire"
P1_fire = value
Case label$ = "P1_accel"
P1_accel = value
Case label$ = " P1_reverse"
P2_reverse = value
Default ; no known variable, so do nothing.
End Select
EndIf
Wend
CloseFile (filename) ; all done reading, so close the file
info$ = "config file opened"
Else ; file does not exist, take alternative action
info$ = "load failed, or no .cfg file present"
EndIf
End Function
Function writeconfig()
filename = WriteFile ("config2.ini")
WriteLine (filename, "---screen resolution")
WriteLine (filename, "screen_w=" + Str(screen_w))
WriteLine (filename, "screen_h=" + Str(screen_h))
WriteLine (filename, "fullscreen=" + Str(fullscreen))
WriteLine (filename, " ")
WriteLine (filename, "---Player 1 controls")
WriteLine (filename, "P1_up=" + Str(P1_up))
WriteLine (filename, "P1_down=" + Str(P1_down))
WriteLine (filename, "P1_left=" + Str(P1_left))
WriteLine (filename, "P1_right=" + Str(P1_right))
WriteLine (filename, "P1_fire=" + Str(P1_fire))
WriteLine (filename, "P1_accel=" + Str(P1_accel))
WriteLine (filename, "P1_reverse=" + Str(P1_reverse))
CloseFile( filename )
End Function