File Parser...Anyone?

Hey everyone,

I'm currently working on learning the basics of game development using a (very nice) low-level graphics library, SDL. I'm trying to use OOP as much as possible and now I'm trying to design classes for state changes in games, such as menus and buttons. Now, my problem is actually managing menus. I belive having a whole bunch of variables whose sole purpose is to handle a a simple main menu is kinda messy (please do correct me if you dont think my position is correct), so I thought about using external files to manage stuff such as the string which should appear on a button, its font and font size, it's colors when in different states, etc...
Now, how do I decide on which format to use on my files?
I came up with this kind of format, please do give your opinion and sugestions:

Note: This is just a draft, a real file would have more properties.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#
#	file name: main_menu.mnu
#	Main Menu buttons's properties

MENU_TITLE:
TEXT: "Tic-Tac-Toe"
SIZE: 36
SPACING: 3

MENU_ITEM_0:
TEXT: "Single Player"
SIZE: 28
SPACING: 1.5

MENU_ITEM_1:
TEXT: "Two Players"
SIZE: 28
SPACING: 1.5

MENU_ITEM_2:
TEXT: "Options"
SIZE: 28
SPACING: 1.5

MENU_ITEM_3:
TEXT: "Credits"
SIZE: 28
SPACING: 1.5


If I was to make my menus using this method I belive I'd need some sort of parser, am I right? My current idea for a parser is a class with a bunch of data members representing the menu item's different properties, such as strings, colors and fonts togheter with some sort of void parse(ifstream inFile) member function. Please do correct me if I'm wrong and please do suggest me other solutions either way, I want to broaden my knowledge as much as possible ;).

Looking forward to hearing from you and best regards,
~Deimos
hmmm.. so your game creates menus at run time, correct??
because if the menus are static then you dont need an external file as once created they will stay as they are.

but if you are creating them dynamically then i suggest you use .ini files or a .xml file. a normal test file will be very slow. if the file is very long then it will effect your game performance as you might not be able to design a very optimal parsing algo.

.ini files if you are working on windows will be good and easy.
you can design them something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[MENU_TITLE]
TEXT=Tic-Tac-Toe
SIZE=36
SPACING=3

[MENU_ITEM]
MENU_ITEM_0=MENU_0
MENU_ITEM_1=MENU_1
MENU_ITEM_2=MENU_2

[MENU_0]
TEXT=Single Player
SIZE=28
SPACING=1.5

[MENU_1]
TEXT=Two Players
SIZE=28
SPACING=1.5

...
... other options...


similarly xml file can be like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<GAME_OPTIONS>
<MENU_TITLE>
	<TEXT="Tic-Tac-Toe"/>
	SIZE value="36"/>
	SPACING value="3"/>
</MENU_TITLE>
<MENU_ITEM>
	<MENU_ITEM_1>
		<TEXT="Single Player"/>
		<SIZE value="28"/>
		<SPACING value="1.5"/>
	</MENU_ITEM_1>
</MENU_ITEM>
</GAME_OPTIONS>





but if you are working on linux/unix then a .xml file will be best, infact .xml file will be faster then a .ini file. you can use any parser to parse an xml file like xerces or expat xml parser. they are not very difficult to learn.
for .ini files you dont need any learning.. there are only a couple of functions.

what you say??
Separately stored configuration parameters are often called resources. Resources can be in a number of formats and are supported by many common libraries/applications. For example, internationalization of software is accomplished via a localization strategy that involves factoring out resources and translating them for differing locales.
Last edited on
hmmm.. so your game creates menus at run time, correct??


Yes, their properties are only set when the game state object they are into is instantiated, or at least thats's the idea. I thought about making them static but since I'm trying to create a menu class that I'll be able to use on games to come, I want to make it as easy to use and flexible as possible.

what you say??


I think I'll go with XML, seen that is seems flexible and relatively easy to use. I know this might sound silly, but I want to escape from anything windows, so *.ini files are out.

Thank you for your replies and best regards,
~Deimos
If you also want to change to a more oo graphics/multimedia library sooner go here: http://www.sfml-dev.org/
hahahaha... me too will go with xml..

because its not related to windows
and its fast too.. its very flexible and once programmed properly it will help you in future also, no rework required.

happy programming... :)
Topic archived. No new replies allowed.