• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Dimension10

Using INI files

7 posts in this topic

So I'm working with DirectX, and I'm wanting to have most of my main configuration constants stored in an .ini file.

 

I used this method originally:

http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File

 

I combined the two classes into a single class with a single header and .cpp file, with a couple of my own tweaks. Worked like a charm! It's nice beting able to choose file/section/key/value, and declare each file as its own object.

 

 

However, when trying to implement this into windows programs, it seems to work differently.

 

 

In the case here:

 

GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); 

 

I get errors because it's trying to pass char* pointers, but I guess the winAPI version looks for an LPCTSTR instead. 

 

So if I change all char* to LPCTSTR, then memset and memcpy have issues, since they are looking for a char*

 

 

 

 

 

All that aside, is there an easy method for working with .ini files? I know they're considered outdated, but it's used heavily in the Unreal Engines, and I prefer that method of configuration over using the registry as M$ recommends.

0

Share this post


Link to post
Share on other sites

Microsoft does not recommend using the registry and hasn't for a very long time now.

 

You can try calling GetPrivateProfileStringA directly if it exists, but it would be much cleaner to sort out how your inconsistent attitudes towards unicode in the build settings.

0

Share this post


Link to post
Share on other sites

I used the same guide myself when creating mine. Here's my version of getting a string value, if it helps:

string CConfig::ReadStringValue( const char* section, const char* key, const char* defaultVal )
{
	char* temp = new char[255];

	GetPrivateProfileString( section, key, defaultVal, temp, 255, mFilename );

	string value = temp;
	delete temp;

	return value;
}

But as BitMaster has already said, it would be better if you correct your project settings if you want to use the multi-byte version of GetPrivateProfileString.

0

Share this post


Link to post
Share on other sites

I used the same guide myself when creating mine. Here's my version of getting a string value, if it helps:

string CConfig::ReadStringValue( const char* section, const char* key, const char* defaultVal )
{
	char* temp = new char[255];

	GetPrivateProfileString( section, key, defaultVal, temp, 255, mFilename );

	string value = temp;
	delete temp;

	return value;
}

But as BitMaster has already said, it would be better if you correct your project settings if you want to use the multi-byte version of GetPrivateProfileString.

There is no need to use a new in that code char temp[255]; will achieve the same thing and then passing &temp[0] to the function, it avoids you having to remember to delete your dynamic memory.

2

Share this post


Link to post
Share on other sites

Also, using plain delete after new[] is undefined behavior. Sadly it generally does not cause problems on MSVC but will as soon as you switch compilers, platforms or maybe next version.

2

Share this post


Link to post
Share on other sites

XML can be a nice solution, but if you just need regular settings like screen.width = 100, then you want a good old-fashioned .ini file that is human readable and has nice ASCII art smile.png

Just look at unreal, at least back when i tinkered with it. If you want a hierarchy, then the . character is that hierarchy:

##################

#        Rendering       #

##################

renderer.width         =   100

renderer.height        =   200   # some comment

renderer.fullscreen  =  true

 

Then create a static config:: system that either just treats the variables as plain text, which is just fine, or relates to specific variables, depending on your needs smile.png

XML is fine too, if you have a configuration tool for your game/application. I admit it's very easy today to make that kind of tool in .NET, and it might be a huge timesaver, depending on how familiar you are with the .NET framework.

 

If you want a solution in C++, here is one of mine:

Config.hpp

#ifndef __CONFIG_HPP
#define __CONFIG_HPP

#include <map>
#include <utility>
#include <string>

namespace platformer
{
	class Config
	{
		static std::map<std::string, std::string> kv;
		
	public:
		static bool get(const std::string, const bool);
		static int get(const std::string, const int);
		static float get(const std::string, const float);
		static std::string get(const std::string, const std::string);

		//Changing the value in the key map.
		template <typename T>
		static void set( std::string v, T b )
		{
			if ( Config::kv.find(v) == Config::kv.end()) 
				return;
			
			kv[v] = b;
		}
		static bool load( std::string file );
	};
}

#endif

Config.cpp

#include "config.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

namespace platformer
{	
	std::map<std::string, std::string> Config::kv;
	
	//Gets a bool value from map
	bool Config::get(const std::string v, const bool def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		bool b;
		std::istringstream convert( kv[v] );
		if ( !(convert >> b) )
			return def;
		return b;
	}
	
	//Gets an int value from map
	int Config::get(const std::string v, const int def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		int i;
		std::istringstream convert( kv[v] );
		if ( !(convert >> i) )
			return def;
		return i;
	}
	
	//Gets a float value from map
	float Config::get(const std::string v, const float def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		float f;
		std::istringstream convert( kv[v] );
		if ( !(convert >> f) )
			return def;
		return f;
	}
	//Gets a string value from map
	std::string Config::get(const std::string v, const std::string def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		return kv[v];
	}
	//Loading the config file into the keymap.
	bool Config::load( std::string f )
	{
		std::fstream filestream;
		filestream.open( f.c_str(), std::fstream::in );
		
		if (!filestream) return false; // could not open file
		
		//Reads one line in config file at a time and place it in the map.
		while( !filestream.eof() )
		{
			char line[256];
			filestream.getline( line, 256 );
			std::string test( line );
			std::string en;
			std::string to;
			bool temp = true;
			//en is the value name and to is the value.
			for ( std::string::iterator it=test.begin(); it!=test.end(); ++it)
			{
				if( *it == '#' ) 
					break;
				if( *it == '=' )
					temp = false;
				if( *it != '=' && *it != ' ' )
				{
					if(temp)
						en += *it;
					else
						to += *it;
				}
			}
			if( en.length() > 0)
				kv.insert( std::pair<std::string,std::string>(en,to) );
		}
		filestream.close();
		return true;
	}

}

It's not very efficient, but it doesn't have to be!

Edited by Kaptein
0

Share this post


Link to post
Share on other sites

There is no need to use a new in that code char temp[255]; will achieve the same thing and then passing &temp[0] to the function, it avoids you having to remember to delete your dynamic memory.

 

 

Also, using plain delete after new[] is undefined behavior. Sadly it generally does not cause problems on MSVC but will as soon as you switch compilers, platforms or maybe next version.

 

Two very good points that I had missed, thanks :)

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0