Ooops
November 25, 2020, 04:54:31 AM

Author Topic: [bb] Basic Game Ini File by Grey Alien [ 1+ years ago ]  (Read 641 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Basic Game Ini File by Grey Alien [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Basic Game Ini File
Author : Grey Alien
Posted : 1+ years ago

Description : This is my Ini file code from Easter Bonus.  You can change the parameters as you see fit but the basic concept is an easy way to store values in a file that you can edit in notepad.

Code :
Code: BlitzBasic
  1. ;Ini Variables                 
  2.         Global IniMusic = 1
  3.         Global IniSound = 1
  4.         Global IniMusicVolume# = 100 ;in percent
  5.         Global IniSoundVolume# = 100 ;in percent
  6.         Global IniFade = 1
  7.         Global IniFlipFalse = 0
  8.         Global Ini16BitGraphics = 0
  9.         Global IniWindowedMode = 1
  10.         Global IniLockKeys = 0
  11.         Global IniGrid = 1 ;always on
  12.         Global IniAlphaBlend = 0 ;always off
  13.         Global IniParticles# = 80
  14.         Global IniInstructionsAtStart = 1      
  15.         Global IniShowPortalSplash = 0
  16.        
  17. ; -----------------------------------------------------------------------------
  18. ; Read Ini File
  19. ; -----------------------------------------------------------------------------
  20. Function IniFileRead(filename$)
  21.         ;load the ini file settings into global variables
  22.         ThePath$ = filename + ".ini"
  23.         Local ini = ccLoadFile(ThePath)
  24.         Local l$
  25.         Local flag$
  26.         Local Value$
  27.        
  28.         While Not Eof(ini)
  29.                 l$ = ReadLine(ini)                     
  30.                 flag$ = Upper(ccIniFirstString(l$))
  31.                 Value$ = ccIniLastString(l$)
  32.                 num% = Int(Value)
  33.        
  34.                 Select True
  35.                         Case flag$ = "MUSIC"
  36.                                 IniMusic = num                         
  37.                         Case flag$ = "SOUND"
  38.                                 IniSound = num                         
  39.                         Case flag$ = "MUSIC VOLUME"
  40.                                 IniMusicVolume = num                           
  41.                         Case flag$ = "SOUND VOLUME"
  42.                                 IniSoundVolume = num                           
  43.                         Case flag$ = "FADE"
  44.                                 IniFade = num
  45.                         Case flag$ = "FLIP FALSE"
  46.                                 IniFlipFalse = num
  47.                         Case flag$ = "16BIT GRAPHICS"
  48.                                 Ini16BitGraphics = num
  49.                         Case flag$ = "WINDOWED MODE"
  50.                                 IniWindowedMode = num
  51.                         Case flag$ = "PARTICLES"
  52.                                 IniParticles = num     
  53.                         Case flag$ = "INSTRUCTIONS AT START"
  54.                                 IniInstructionsAtStart = num   
  55.                         Case flag$ = "SHOW PORTAL SPLASH"
  56.                                 IniShowPortalSplash = num                              
  57.                         Default
  58.                 End Select             
  59.         Wend
  60.         CloseFile(ini)
  61. End Function
  62.  
  63. ; -----------------------------------------------------------------------------
  64. ; Write Ini File
  65. ; -----------------------------------------------------------------------------
  66. Function IniFileWrite(filename$)
  67.         ;write the ini file settings from global variables
  68.         ThePath$ = filename + ".ini"
  69.         Local ini = WriteFile(ThePath)
  70.         Local l$
  71.         Local flag$
  72.         Local Value$
  73.        
  74.         ccWriteIniNumber(ini, "MUSIC", IniMusic)
  75.         ccWriteIniNumber(ini, "SOUND", IniSound)
  76.         ccWriteIniNumber(ini, "MUSIC VOLUME", IniMusicVolume)
  77.         ccWriteIniNumber(ini, "SOUND VOLUME", IniSoundVolume)
  78.         ccWriteIniNumber(ini, "FADE", IniFade)
  79.         ccWriteIniNumber(ini, "FLIP FALSE", IniFlipFalse)
  80.         ccWriteIniNumber(ini, "16BIT GRAPHICS", Ini16BitGraphics)
  81.         ccWriteIniNumber(ini, "WINDOWED MODE", IniWindowedMode)
  82.         ccWriteIniNumber(ini, "PARTICLES", IniParticles)
  83.         ccWriteIniNumber(ini, "INSTRUCTIONS AT START", IniInstructionsAtStart)
  84.         ccWriteIniNumber(ini, "SHOW PORTAL SPLASH", IniShowPortalSplash)
  85.         CloseFile(ini)
  86. End Function
  87.  
  88.  
  89. ; -----------------------------------------------------------------------------
  90. ; Load a file and show error if not found
  91. ; -----------------------------------------------------------------------------
  92. Function ccLoadFile% (ThePath$)
  93.         pointer = ReadFile(ThePath$)
  94.         If Not pointer Then
  95.         RuntimeError ("Error loading file "+ThePath$)
  96.                 End
  97.         Else
  98.         Return Pointer 
  99.         EndIf
  100. End Function
  101.  
  102. ; -----------------------------------------------------------------------------
  103. ; IniFirst String (return first part of string up to = sign)
  104. ; -----------------------------------------------------------------------------
  105. Function ccIniFirstString$(s$)
  106.         ;pass in a string, this will only return the first part up to, but not including, the = sign (or end)          
  107.         Return ccFirstStringToSub(s$, "=")
  108. End Function
  109.  
  110. ; -----------------------------------------------------------------------------
  111. ; IniLast String (return last part of string from = sign)
  112. ; -----------------------------------------------------------------------------
  113. Function ccIniLastString$(s$)
  114.         ;pass in a string, this will only return the last part from, but not including, the = sign
  115.         Return ccLastStringToSub(s$, "=")
  116. End Function
  117.  
  118. ; -----------------------------------------------------------------------------
  119. ; ccWriteIniNumber
  120. ; -----------------------------------------------------------------------------
  121. Function ccWriteIniNumber(ini%, flag$, value%)
  122.         ;use this to write flag=number to an ini file
  123.         WriteLine(ini, Upper(flag)+"="+Str(value))
  124. End Function
  125.  
  126. ; -----------------------------------------------------------------------------
  127. ; ccWriteIniString
  128. ; -----------------------------------------------------------------------------
  129. Function ccWriteIniString(ini%, flag$, value$)
  130.         ;use this to write flag=string to an ini file
  131.         WriteLine(ini, Upper(flag)+"="+value$)
  132. End Function
  133.  
  134. ; -----------------------------------------------------------------------------
  135. ; First String To Sub (return first part of string up to Substring)
  136. ; -----------------------------------------------------------------------------
  137. Function ccFirstStringToSub$(s$, sub$)
  138.         ;pass in a string, this will only return the first part up to, but not including, the substring (or end)
  139.         pos% = Instr(s$, sub$)
  140.         ;If pos = 0 then then end of the was reached, so return the whole thing.
  141.         If pos = 0 Then
  142.                 Return s$
  143.         Else
  144.                 Return Mid(s$, 1, pos-1)
  145.         EndIf
  146. End Function
  147.  
  148. ; -----------------------------------------------------------------------------
  149. ; Last String To Sub (return last part of string from substring)
  150. ; -----------------------------------------------------------------------------
  151. Function ccLastStringToSub$(s$, sub$)
  152.         ;pass in a string, this will only return the last part from, but not including, the substring
  153.         pos% = Instr(s$, sub$)
  154.         ;If pos = 0 then then end of the was reached, so return nothing
  155.         If pos = 0 Then
  156.                 Return ""
  157.         Else
  158.                 Return Mid(s$, pos + Len(sub$), Len(s$)-pos)
  159.         EndIf
  160. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal