Data Converter
Posted: Sun Apr 17, 2005 6:46 am
I had been looking for an easy way to pull data from a file and use it in the game as something other than Ogre::String. After looking through the forums, I found no answer, but did find a few people who wanted to know the same thing. So Im posting this code here so that other people can use it.
The Header File
Calling the program
Change this line:
to this:
To retrieve a variable from a file (MyConfigFile.cfg) use the following syntax:
The Header File
Code: Select all
#ifndef __SETUP_FILE_VARIABLES__
#define __SETUP_FILE_VARIABLES__
#include "Ogre.h"
#include "OgreConfigFile.h"
#include "OgreStringConverter.h"
#include <stdlib.h>
class SetupFileVar
{
public:
float SetupFileFloat(const char* file, const char* value)
{
ConfigFile setupfile;
setupfile.load(file);
Ogre::String resultOgreString = setupfile.getSetting(value);
const char* resultString = resultOgreString.c_str();
float result = atof(resultString);
return result;
}
int SetupFileInt(const char* file, const char* value)
{
ConfigFile setupfile;
setupfile.load(file);
Ogre::String resultOgreString = setupfile.getSetting(value);
const char* resultString = resultOgreString.c_str();
int result = atoi(resultString);
return result;
}
const char* SetupFileString(const char* file, const char* value)
{
ConfigFile setupfile;
setupfile.load(file);
Ogre::String resultOgreString = setupfile.getSetting(value);
const char* resultString = resultOgreString.c_str();
return resultString;
}
bool SetupFileBool(const char* file, const char* value)
{
ConfigFile setupfile;
setupfile.load(file);
Ogre::String resultOgreString = setupfile.getSetting(value);
const char* resultString = resultOgreString.c_str();
//bool result;
if(resultString = "TRUE")
{
return true;
}else{
return false;
}
}
Ogre::String SetupFileOgreString(const char* file, const char* value)
{
ConfigFile setupfile;
setupfile.load(file);
Ogre::String resultOgreString = setupfile.getSetting(value);
return resultOgreString;
}
};
#endif
Change this line:
Code: Select all
class RSApplication : public ExampleApplication
Code: Select all
class RSApplication : public ExampleApplication, SetupFileVar
- Float
Code: Select all
float myfloat = SetupFileVars::SetupFileFloat("MyConfigFile.cfg", "MyFloat"); - Integer
Code: Select all
int myInt = SetupFileVars::SetupFileInt("MyConfigFile.cfg", "MyInt"); - C String
Code: Select all
const char* myString = SetupFileVars::SetupFileString("MyConfigFile.cfg", "MyString"); - Integer
Code: Select all
bool myBool = SetupFileVars::SetupFileBool("MyConfigFile.cfg", "MyBool"); - Integer
Code: Select all
Ogre::String myInt = SetupFileVars::SetupFileOgreString("MyConfigFile.cfg", "MyOgreString");