Jump to content

  • Log In with Google      Sign In   
  • Create Account

Game Input from an XML file


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 oldgregg   Members   -  Reputation: 231

Like
0Likes
Like

Posted 18 November 2012 - 05:26 PM

Hi guys,

I'm tasked with creating a class that can read in input for a game, from an XML file.
I have no idea where to start, I'm not even sure what to google.

I was wondering has anyone around done this before? And if so do you have any pointers?

Thanks.

Sponsor:

#2 ApochPiQ   Moderators   -  Reputation: 15736

Like
1Likes
Like

Posted 18 November 2012 - 05:31 PM

That's going to depend a lot on how the game's input system is written.

For a well designed system, it could be as simple as reading a file that's just a list of timestamps and actions to trigger.

For a badly designed system, it might mean rewriting a ton of code so that this is even remotely practical.


We'll need a lot more context to really be specifically helpful here I think.

#3 oldgregg   Members   -  Reputation: 231

Like
0Likes
Like

Posted 18 November 2012 - 05:48 PM

Sorry I was a little vague.

A bit more on the background may help.
I'm producing a game engine. That can be used to create multiple different genre games.(Platformer, RPG, Shooter, Etc) So it has to be versatile and not hardcoded in any particular way. The way my group have been told to do this is to hard code parts to read in XML files which we can change in order to give the desired results. (Obviously this will still involve hard coding parts but we need to keep it minimal)

So for example, For the input system, if it was a Shooter game, then say 'A' would be Shoot. And this would be specified in the external XML file.

However if it was a platformer, 'A' might be jump. So it would just be a case of having the XML file say 'A' maps to 'JUMP'.
The code would then read that 'A' was pressed and then link to the XML to find out what should be done.

#4 BeerNutts   Crossbones+   -  Reputation: 2945

Like
2Likes
Like

Posted 18 November 2012 - 06:06 PM

While I've done this work in C++, you can get a generic idea of how it would work with C# and XNA. Here is a configuration file I use to define the user preferences (Note, I have comments wrapped in <!-- --> with #'s before the comments):
<?xml version="1.0" ?>
<GravithonConfig>
  <!--
# Configuration file for Gravithon
# Set the movement keys, mouse sensitivity, and screen resolution here
# Set the Keys that Gravithon Will use for Moving and Firing
# Possible selections: MouseLButton, MouseRButton,
# RCtrl, LCtrl, RShift, LShift, RAlt, LAlt, Space, Return
# CursorRight, CursorLeft, CursorUp, and CursorDown
# other keys are as is on keyboard without shift
# Example: Left: ]
# would set the ']' key to left
# NOTE!! use lower-case keys  !!!
# Default is:
#  <Left>a</Left>
#  <Right>d</Right>
#  <Down>s</Down>
#  <Up>w</Up>
#  <Fire>MouseLButton</Fire>
#  <SpecialFire>MouseRButton</SpecialFire>
# -----------------------------------------
# Set Movement Keys
# -----------------------------------------
-->
  <Left>a</Left>
  <Right>d</Right>
  <Up>w</Up>
  <Down>s</Down>
  <Fire>MouseLButton</Fire>
  <SpecialFire>MouseRButton</SpecialFire>
  <!--
# Set Mouse Sensitivty
# The Higher, the less sensitive
# Default is:
# <MouseSensitivity>300</MouseSensitivity>
# -----------------------------------------
# Set Mouse Sensitivity
# -----------------------------------------
  -->
  <MouseSensitivity>300</MouseSensitivity>
  <!--
# Set the screen resolution
# Set "FullScreen: Yes" to make it be full screen
# Default is:
#  <ScreenWidth>1280</ScreenWidth>
#  <ScreenHeight>960</ScreenHeight>
#  <FullScreen>No</FullScreen>
# -----------------------------------------
# Set Screen Resolution
# -----------------------------------------
  -->
  <ScreenWidth>1680</ScreenWidth>
  <ScreenHeight>1050</ScreenHeight>
  <FullScreen>Yes</FullScreen>

For your question, the important piece is from here:
<Left>a</Left>
  <Right>d</Right>
  <Up>w</Up>
  <Down>s</Down>
  <Fire>MouseLButton</Fire>
  <SpecialFire>MouseRButton</SpecialFire>

To read this data and use it, I make calls to my Utilities functions.
Here's Utilities.h
#ifndef _UTILITIES_H
#define _UTILITIES_H
/******************************************************************
*
*  Utilities.h
*
*******************************************************************/
#include "Types.h"
typedef enum
{
    GAME_KEY_LEFT,
    GAME_KEY_RIGHT,
    GAME_KEY_UP,
    GAME_KEY_DOWN,
    GAME_KEY_FIRE,
    GAME_KEY_SPECIAL_FIRE,
    GAME_KEY_MAX
} etGameKeys;
class Utilities
{
public:
static void LoadConfig(char *pcFilename);
static BOOL KeyIsDown(const sf::Input& Input, etGameKeys eQKey);
static sf::Key::Code CheckKeyRange(const sf::Input& Input,
						    sf::Key::Code Key1,
						    sf::Key::Code Key2);
static U32 MouseSensitivityGet(void);
static void ScreenResolutionGet(U32 &u32ScreenX, U32 &u32ScreenY,
						 BOOL &bFullScreen);
static sf::Image *ImageGet(std::string FileName);
static void ImagesFree(void);
};
#endif/* _UTILITIES_H */

And Utilities.cpp Take note of the functions Utilities::LoadConfig(), UpdateSettings(), and Utilities::KeyIsDown()
/******************************************************************************
* Utilities.cpp
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <chipmunk.h>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <windows.h>
#include "Utilities.h"
#include <string>
#include "tinyxml.h"

#define MOUSE_SENSITIVITY_LOC   (U32)GAME_KEY_MAX
#define SCREEN_WIDTH_LOC	    (MOUSE_SENSITIVITY_LOC+1)
#define SCREEN_HEIGHT_LOC	   (MOUSE_SENSITIVITY_LOC+2)
#define IS_FULL_SCREEN		  (MOUSE_SENSITIVITY_LOC+3)
#define MAX_CONFIG_OPTIONS	  (MOUSE_SENSITIVITY_LOC+4)
// previous config had : after end of name
char *gpCfgStrings[MAX_CONFIG_OPTIONS] =
{
    "Left",
    "Right",
    "Up",
    "Down",
    "Fire",
    "SpecialFire",
    "MouseSensitivity",
    "ScreenWidth",
    "ScreenHeight",
    "FullScreen"
};
typedef struct
{
    BOOL bMouseKey;
    sf::Key::Code SfKey;
    sf::Mouse::Button SFButton;
} tKeyHash;
typedef struct
{
    std::string ConfigName;
    tKeyHash KeyHash;
} tInputConfigNames;
// Available names for config file setting
tInputConfigNames gInputConfigNames[] =
{
    { "MouseLButton", {TRUE, sf::Key::A, sf::Mouse::Left} },
    { "MouseRButton", {TRUE, sf::Key::A, sf::Mouse::Right} },
    { "RCtrl", {FALSE, sf::Key::RControl, sf::Mouse::Left} },
    { "LCtrl", {FALSE, sf::Key::LControl, sf::Mouse::Left} },
    { "RShift", {FALSE, sf::Key::RShift, sf::Mouse::Left} },
    { "LShift", {FALSE, sf::Key::LShift, sf::Mouse::Left} },
    { "RAlt", {FALSE, sf::Key::RAlt, sf::Mouse::Left} },
    { "LAlt", {FALSE, sf::Key::LAlt, sf::Mouse::Left} },
    { "Space", {FALSE, sf::Key::Space, sf::Mouse::Left} },
    { "Return", {FALSE, sf::Key::Return, sf::Mouse::Left} },
    { "CursorRight", {FALSE, sf::Key::Right, sf::Mouse::Left} },
    { "CursorLeft", {FALSE, sf::Key::Left, sf::Mouse::Left} },
    { "CursorUp", {FALSE, sf::Key::Up, sf::Mouse::Left} },
    { "CursorDown", {FALSE, sf::Key::Down, sf::Mouse::Left} },
    { ",", {FALSE, sf::Key::Comma, sf::Mouse::Left} },
    { ".", {FALSE, sf::Key::Period, sf::Mouse::Left} },
    { "/", {FALSE, sf::Key::Slash, sf::Mouse::Left} },
    { ";", {FALSE, sf::Key::SemiColon, sf::Mouse::Left} },
    { "\'", {FALSE, sf::Key::Quote, sf::Mouse::Left} },
    { "[", {FALSE, sf::Key::LBracket, sf::Mouse::Left} },
    { "]", {FALSE, sf::Key::RBracket, sf::Mouse::Left} },
    { "\\", {FALSE, sf::Key::BackSlash, sf::Mouse::Left} }
};

/* strings for certain array locations */
tKeyHash gKeyHash[GAME_KEY_MAX] =
{
    {FALSE, sf::Key::A,   sf::Mouse::Left},
    {FALSE, sf::Key:<img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/biggrin.png' class='bbc_emoticon' alt=':D' />,   sf::Mouse::Left},
    {FALSE, sf::Key::W,   sf::Mouse::Left},
    {FALSE, sf::Key::S,   sf::Mouse::Left},
    {TRUE,  sf::Key::RControl, sf::Mouse::Left},
    {TRUE,  sf::Key::RControl, sf::Mouse::Right},
};
U32  gu32MouseSens    = 270;
U32  gu32ScreenWidth  = 1024;
U32  gu32ScreenHeight = 768;
BOOL gbFullScreen	 = FALSE;
static void UpdateSettings(C8 *pcSetting, U32 u32Option);
std::map<std::string, sf::Image*> gImageMap;
/******************************************************************************
*
* ImageGet() - Return an SFML image from a file name, being sure not to
*   load one twice
*
******************************************************************************/
sf::Image *Utilities::ImageGet(std::string FileName)
{
    // check if we've loaded this one already
    if (gImageMap.count(FileName) == 0)
    {
	    gImageMap[FileName] = new sf::Image();
	    if (gImageMap[FileName]->LoadFromFile(FileName.c_str()))
	    {
		    gImageMap[FileName]->CreateMaskFromColor(sf::Color(255, 0, 255, 255));
	    }
	    else
	    {
		    printf("Failed to load image %s\n", FileName.c_str());
	    }
    }
    return gImageMap[FileName];
}
/******************************************************************************
*
* ImagesFree() - Free all the images
*
******************************************************************************/
void Utilities::ImagesFree(void)
{
    std::map<std::string, sf::Image*>::iterator it;
    for (it = gImageMap.begin(); it != gImageMap.end(); it++)
    {
	    delete it->second;
    }
}
/******************************************************************************
*
* LoadConfig() - Load and parse the config files setting up the users
*   preferences
*
******************************************************************************/
void Utilities::LoadConfig(char *pcFilename)
{
    TiXmlDocument doc(pcFilename);
    // load and check if it was successful
    if (doc.LoadFile())
    {
	    TiXmlElement *root = doc.RootElement();
	    for(TiXmlElement* example = root->FirstChildElement(); example;
		    example = example->NextSiblingElement())
	    {
		    std::string ConfigStr  = example->ValueStr();
		    printf("Found Config %s .. ", ConfigStr.c_str());
		    for (U32 i = 0; i < MAX_CONFIG_OPTIONS; i++)
		    {
			    if (strcmp(ConfigStr.c_str(), gpCfgStrings[i]) == 0)
			    {
				    std::string SettingStr = example->GetText();
				    printf("Map to %s\n", SettingStr.c_str());
				    UpdateSettings((C8 *)SettingStr.c_str(), i);
				    break;
			    }
		    }
	    }
    }
    else
    {
	    printf("Filed opening %s\n", pcFilename);
    }
}
/******************************************************************************
*
* KeyIsDown() - Check if the given enum key is down using the configured
*   keyhash setup during configuration.
*
******************************************************************************/
BOOL Utilities::KeyIsDown(const sf::Input& Input, etGameKeys eQKey)
{
    BOOL bDown = FALSE;
    if (eQKey < GAME_KEY_MAX)
    {
	    if (gKeyHash[eQKey].bMouseKey)
	    {
		    if (Input.IsMouseButtonDown(gKeyHash[eQKey].SFButton))
		    {
			    bDown = TRUE;
		    }
	    }
	    else
	    {
		    if (Input.IsKeyDown(gKeyHash[eQKey].SfKey))
		    {
			    bDown = TRUE;
		    }
	    }
    }
    return bDown;
}
/******************************************************************************
*
* CheckKeyRange() - Checks if a key is down with the range key1 - key2
*
******************************************************************************/
sf::Key::Code Utilities::CheckKeyRange(const sf::Input& Input,
									   sf::Key::Code Key1,
									   sf::Key::Code Key2)
{
    sf::Key::Code KeyDown = sf::Key::Count;
    U32 u32Key1 = static_cast<U32>(Key1);
    U32 u32Key2 = static_cast<U32>(Key2);
    while (u32Key2 >= u32Key1)
    {
	    if (Input.IsKeyDown(Key1))
	    {
		    KeyDown = Key1;
		    break;
	    }
	    u32Key1++;
	    Key1 = static_cast<sf::Key::Code>(u32Key1);
    }
    return KeyDown;
}
/******************************************************************************
*
* MouseSensitivityGet() - Return the mouse sensitivity setting
*
******************************************************************************/
U32 Utilities::MouseSensitivityGet(void)
{
    return gu32MouseSens;
}
/******************************************************************************
*
* ScreenResolutionGet() - Return The Horizontal, Vertical resolution of the
*   screen and the full screen setting
*
******************************************************************************/
void Utilities::ScreenResolutionGet(U32 &u32ScreenX, U32 &u32ScreenY,
						 BOOL &bFullScreen)
{
#warning Check resolutions are valid
    u32ScreenX  = gu32ScreenWidth;
    u32ScreenY  = gu32ScreenHeight;
    bFullScreen = gbFullScreen;
}
/******************************************************************************
*
* UpdateSettings() - Store off the configuration setting read from xml file
*
******************************************************************************/
static void UpdateSettings(C8 *pcSetting, U32 u32Option)
{
    U32 u32Value;
    std::string SettingStr(pcSetting);
    BOOL bFoundName = FALSE;
    if (u32Option >= GAME_KEY_MAX)
    {
	    u32Value = atoi(pcSetting);
	    //printf("Setting option %d value %d\n", u32Option, u32Value);
	    if (u32Option == MOUSE_SENSITIVITY_LOC)
	    {
		    gu32MouseSens = u32Value;
	    }
	    else if (u32Option == SCREEN_WIDTH_LOC)
	    {
		    gu32ScreenWidth = u32Value;
	    }
	    else if (u32Option == SCREEN_HEIGHT_LOC)
	    {
		    gu32ScreenHeight  = u32Value;
	    }
	    else
	    {
		    gbFullScreen = FALSE;
		    /* it's Full Screen, get Yes or No */
		    if (strcmp(pcSetting, "Yes") == 0)
		    {
			    printf("It's Fullscreen!\n");
			    gbFullScreen = TRUE;
		    }
	    }
	    return;
    }
    for(int i = 0; i < sizeof(gInputConfigNames)/sizeof(tInputConfigNames); i++)
    {
	    if (SettingStr == gInputConfigNames[i].ConfigName)
	    {
		    gKeyHash[u32Option] = gInputConfigNames[i].KeyHash;
		    bFoundName = TRUE;
		    break;
	    }
    }
    if (!bFoundName)
    {
	    /* it should be a single character */
	    C8 c8Key = pcSetting[0];
	    if ((c8Key >= 'a') && (c8Key <= 'z'))
	    {
		    gKeyHash[u32Option].SfKey = (sf::Key::Code)c8Key;
	    }
    }
}

Good Luck!
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#5 oldgregg   Members   -  Reputation: 231

Like
0Likes
Like

Posted 18 November 2012 - 06:08 PM

While I've done this work in C++, you can get a generic idea of how it would work with C# and XNA. Here is a configuration file I use to define the user preferences (Note, I have comments wrapped in <!-- --> with #'s before the comments):

<?xml version="1.0" ?>
<GravithonConfig>
  <!--
# Configuration file for Gravithon
# Set the movement keys, mouse sensitivity, and screen resolution here
# Set the Keys that Gravithon Will use for Moving and Firing
# Possible selections: MouseLButton, MouseRButton,
# RCtrl, LCtrl, RShift, LShift, RAlt, LAlt, Space, Return
# CursorRight, CursorLeft, CursorUp, and CursorDown
# other keys are as is on keyboard without shift
# Example: Left: ]
# would set the ']' key to left
# NOTE!! use lower-case keys  !!!
# Default is:
#  <Left>a</Left>
#  <Right>d</Right>
#  <Down>s</Down>
#  <Up>w</Up>
#  <Fire>MouseLButton</Fire>
#  <SpecialFire>MouseRButton</SpecialFire>
# -----------------------------------------
# Set Movement Keys
# -----------------------------------------
-->
  <Left>a</Left>
  <Right>d</Right>
  <Up>w</Up>
  <Down>s</Down>
  <Fire>MouseLButton</Fire>
  <SpecialFire>MouseRButton</SpecialFire>
  <!--
# Set Mouse Sensitivty
# The Higher, the less sensitive
# Default is:
# <MouseSensitivity>300</MouseSensitivity>
# -----------------------------------------
# Set Mouse Sensitivity
# -----------------------------------------
  -->
  <MouseSensitivity>300</MouseSensitivity>
  <!--
# Set the screen resolution
# Set "FullScreen: Yes" to make it be full screen
# Default is:
#  <ScreenWidth>1280</ScreenWidth>
#  <ScreenHeight>960</ScreenHeight>
#  <FullScreen>No</FullScreen>
# -----------------------------------------
# Set Screen Resolution
# -----------------------------------------
  -->
  <ScreenWidth>1680</ScreenWidth>
  <ScreenHeight>1050</ScreenHeight>
  <FullScreen>Yes</FullScreen>

For your question, the important piece is from here:
<Left>a</Left>
  <Right>d</Right>
  <Up>w</Up>
  <Down>s</Down>
  <Fire>MouseLButton</Fire>
  <SpecialFire>MouseRButton</SpecialFire>

To read this data and use it, I make calls to my Utilities functions.
Here's Utilities.h
#ifndef _UTILITIES_H
#define _UTILITIES_H
/******************************************************************
*
*  Utilities.h
*
*******************************************************************/
#include "Types.h"
typedef enum
{
	GAME_KEY_LEFT,
	GAME_KEY_RIGHT,
	GAME_KEY_UP,
	GAME_KEY_DOWN,
	GAME_KEY_FIRE,
	GAME_KEY_SPECIAL_FIRE,
	GAME_KEY_MAX
} etGameKeys;
class Utilities
{
public:
static void LoadConfig(char *pcFilename);
static BOOL KeyIsDown(const sf::Input& Input, etGameKeys eQKey);
static sf::Key::Code CheckKeyRange(const sf::Input& Input,
							sf::Key::Code Key1,
							sf::Key::Code Key2);
static U32 MouseSensitivityGet(void);
static void ScreenResolutionGet(U32 &u32ScreenX, U32 &u32ScreenY,
						 BOOL &bFullScreen);
static sf::Image *ImageGet(std::string FileName);
static void ImagesFree(void);
};
#endif/* _UTILITIES_H */

And Utilities.cpp Take note of the functions Utilities::LoadConfig(), UpdateSettings(), and Utilities::KeyIsDown()
/******************************************************************************
* Utilities.cpp
******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <chipmunk.h>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <math.h>
#include <windows.h>
#include "Utilities.h"
#include <string>
#include "tinyxml.h"

#define MOUSE_SENSITIVITY_LOC   (U32)GAME_KEY_MAX
#define SCREEN_WIDTH_LOC		(MOUSE_SENSITIVITY_LOC+1)
#define SCREEN_HEIGHT_LOC	   (MOUSE_SENSITIVITY_LOC+2)
#define IS_FULL_SCREEN		  (MOUSE_SENSITIVITY_LOC+3)
#define MAX_CONFIG_OPTIONS	  (MOUSE_SENSITIVITY_LOC+4)
// previous config had : after end of name
char *gpCfgStrings[MAX_CONFIG_OPTIONS] =
{
	"Left",
	"Right",
	"Up",
	"Down",
	"Fire",
	"SpecialFire",
	"MouseSensitivity",
	"ScreenWidth",
	"ScreenHeight",
	"FullScreen"
};
typedef struct
{
	BOOL bMouseKey;
	sf::Key::Code SfKey;
	sf::Mouse::Button SFButton;
} tKeyHash;
typedef struct
{
	std::string ConfigName;
	tKeyHash KeyHash;
} tInputConfigNames;
// Available names for config file setting
tInputConfigNames gInputConfigNames[] =
{
	{ "MouseLButton", {TRUE, sf::Key::A, sf::Mouse::Left} },
	{ "MouseRButton", {TRUE, sf::Key::A, sf::Mouse::Right} },
	{ "RCtrl", {FALSE, sf::Key::RControl, sf::Mouse::Left} },
	{ "LCtrl", {FALSE, sf::Key::LControl, sf::Mouse::Left} },
	{ "RShift", {FALSE, sf::Key::RShift, sf::Mouse::Left} },
	{ "LShift", {FALSE, sf::Key::LShift, sf::Mouse::Left} },
	{ "RAlt", {FALSE, sf::Key::RAlt, sf::Mouse::Left} },
	{ "LAlt", {FALSE, sf::Key::LAlt, sf::Mouse::Left} },
	{ "Space", {FALSE, sf::Key::Space, sf::Mouse::Left} },
	{ "Return", {FALSE, sf::Key::Return, sf::Mouse::Left} },
	{ "CursorRight", {FALSE, sf::Key::Right, sf::Mouse::Left} },
	{ "CursorLeft", {FALSE, sf::Key::Left, sf::Mouse::Left} },
	{ "CursorUp", {FALSE, sf::Key::Up, sf::Mouse::Left} },
	{ "CursorDown", {FALSE, sf::Key::Down, sf::Mouse::Left} },
	{ ",", {FALSE, sf::Key::Comma, sf::Mouse::Left} },
	{ ".", {FALSE, sf::Key::Period, sf::Mouse::Left} },
	{ "/", {FALSE, sf::Key::Slash, sf::Mouse::Left} },
	{ ";", {FALSE, sf::Key::SemiColon, sf::Mouse::Left} },
	{ "\'", {FALSE, sf::Key::Quote, sf::Mouse::Left} },
	{ "[", {FALSE, sf::Key::LBracket, sf::Mouse::Left} },
	{ "]", {FALSE, sf::Key::RBracket, sf::Mouse::Left} },
	{ "\\", {FALSE, sf::Key::BackSlash, sf::Mouse::Left} }
};

/* strings for certain array locations */
tKeyHash gKeyHash[GAME_KEY_MAX] =
{
	{FALSE, sf::Key::A,   sf::Mouse::Left},
	{FALSE, sf::Key:<img src='http://public.gamedev.net//public/style_emoticons/<#EMO_DIR#>/biggrin.png' class='bbc_emoticon' alt=':D' />,   sf::Mouse::Left},
	{FALSE, sf::Key::W,   sf::Mouse::Left},
	{FALSE, sf::Key::S,   sf::Mouse::Left},
	{TRUE,  sf::Key::RControl, sf::Mouse::Left},
	{TRUE,  sf::Key::RControl, sf::Mouse::Right},
};
U32  gu32MouseSens	= 270;
U32  gu32ScreenWidth  = 1024;
U32  gu32ScreenHeight = 768;
BOOL gbFullScreen	 = FALSE;
static void UpdateSettings(C8 *pcSetting, U32 u32Option);
std::map<std::string, sf::Image*> gImageMap;
/******************************************************************************
*
* ImageGet() - Return an SFML image from a file name, being sure not to
*   load one twice
*
******************************************************************************/
sf::Image *Utilities::ImageGet(std::string FileName)
{
	// check if we've loaded this one already
	if (gImageMap.count(FileName) == 0)
	{
		gImageMap[FileName] = new sf::Image();
		if (gImageMap[FileName]->LoadFromFile(FileName.c_str()))
		{
			gImageMap[FileName]->CreateMaskFromColor(sf::Color(255, 0, 255, 255));
		}
		else
		{
			printf("Failed to load image %s\n", FileName.c_str());
		}
	}
	return gImageMap[FileName];
}
/******************************************************************************
*
* ImagesFree() - Free all the images
*
******************************************************************************/
void Utilities::ImagesFree(void)
{
	std::map<std::string, sf::Image*>::iterator it;
	for (it = gImageMap.begin(); it != gImageMap.end(); it++)
	{
		delete it->second;
	}
}
/******************************************************************************
*
* LoadConfig() - Load and parse the config files setting up the users
*   preferences
*
******************************************************************************/
void Utilities::LoadConfig(char *pcFilename)
{
	TiXmlDocument doc(pcFilename);
	// load and check if it was successful
	if (doc.LoadFile())
	{
		TiXmlElement *root = doc.RootElement();
		for(TiXmlElement* example = root->FirstChildElement(); example;
			example = example->NextSiblingElement())
		{
			std::string ConfigStr  = example->ValueStr();
			printf("Found Config %s .. ", ConfigStr.c_str());
			for (U32 i = 0; i < MAX_CONFIG_OPTIONS; i++)
			{
				if (strcmp(ConfigStr.c_str(), gpCfgStrings[i]) == 0)
				{
					std::string SettingStr = example->GetText();
					printf("Map to %s\n", SettingStr.c_str());
					UpdateSettings((C8 *)SettingStr.c_str(), i);
					break;
				}
			}
		}
	}
	else
	{
		printf("Filed opening %s\n", pcFilename);
	}
}
/******************************************************************************
*
* KeyIsDown() - Check if the given enum key is down using the configured
*   keyhash setup during configuration.
*
******************************************************************************/
BOOL Utilities::KeyIsDown(const sf::Input& Input, etGameKeys eQKey)
{
	BOOL bDown = FALSE;
	if (eQKey < GAME_KEY_MAX)
	{
		if (gKeyHash[eQKey].bMouseKey)
		{
			if (Input.IsMouseButtonDown(gKeyHash[eQKey].SFButton))
			{
				bDown = TRUE;
			}
		}
		else
		{
			if (Input.IsKeyDown(gKeyHash[eQKey].SfKey))
			{
				bDown = TRUE;
			}
		}
	}
	return bDown;
}
/******************************************************************************
*
* CheckKeyRange() - Checks if a key is down with the range key1 - key2
*
******************************************************************************/
sf::Key::Code Utilities::CheckKeyRange(const sf::Input& Input,
									   sf::Key::Code Key1,
									   sf::Key::Code Key2)
{
	sf::Key::Code KeyDown = sf::Key::Count;
	U32 u32Key1 = static_cast<U32>(Key1);
	U32 u32Key2 = static_cast<U32>(Key2);
	while (u32Key2 >= u32Key1)
	{
		if (Input.IsKeyDown(Key1))
		{
			KeyDown = Key1;
			break;
		}
		u32Key1++;
		Key1 = static_cast<sf::Key::Code>(u32Key1);
	}
	return KeyDown;
}
/******************************************************************************
*
* MouseSensitivityGet() - Return the mouse sensitivity setting
*
******************************************************************************/
U32 Utilities::MouseSensitivityGet(void)
{
	return gu32MouseSens;
}
/******************************************************************************
*
* ScreenResolutionGet() - Return The Horizontal, Vertical resolution of the
*   screen and the full screen setting
*
******************************************************************************/
void Utilities::ScreenResolutionGet(U32 &u32ScreenX, U32 &u32ScreenY,
						 BOOL &bFullScreen)
{
#warning Check resolutions are valid
	u32ScreenX  = gu32ScreenWidth;
	u32ScreenY  = gu32ScreenHeight;
	bFullScreen = gbFullScreen;
}
/******************************************************************************
*
* UpdateSettings() - Store off the configuration setting read from xml file
*
******************************************************************************/
static void UpdateSettings(C8 *pcSetting, U32 u32Option)
{
	U32 u32Value;
	std::string SettingStr(pcSetting);
	BOOL bFoundName = FALSE;
	if (u32Option >= GAME_KEY_MAX)
	{
		u32Value = atoi(pcSetting);
		//printf("Setting option %d value %d\n", u32Option, u32Value);
		if (u32Option == MOUSE_SENSITIVITY_LOC)
		{
			gu32MouseSens = u32Value;
		}
		else if (u32Option == SCREEN_WIDTH_LOC)
		{
			gu32ScreenWidth = u32Value;
		}
		else if (u32Option == SCREEN_HEIGHT_LOC)
		{
			gu32ScreenHeight  = u32Value;
		}
		else
		{
			gbFullScreen = FALSE;
			/* it's Full Screen, get Yes or No */
			if (strcmp(pcSetting, "Yes") == 0)
			{
				printf("It's Fullscreen!\n");
				gbFullScreen = TRUE;
			}
		}
		return;
	}
	for(int i = 0; i < sizeof(gInputConfigNames)/sizeof(tInputConfigNames); i++)
	{
		if (SettingStr == gInputConfigNames[i].ConfigName)
		{
			gKeyHash[u32Option] = gInputConfigNames[i].KeyHash;
			bFoundName = TRUE;
			break;
		}
	}
	if (!bFoundName)
	{
		/* it should be a single character */
		C8 c8Key = pcSetting[0];
		if ((c8Key >= 'a') && (c8Key <= 'z'))
		{
			gKeyHash[u32Option].SfKey = (sf::Key::Code)c8Key;
		}
	}
}

Good Luck!



This is exactly what I needed, Thank you so much!
You've more than answered my question :D!

#6 NightCreature83   Crossbones+   -  Reputation: 2826

Like
1Likes
Like

Posted 19 November 2012 - 04:21 AM

What you are doing is called an actionmap and if you google for this you will find a lot of information about it. Generally the idea is your engine only responds to actions you define somewhere and then the actionmap binds the actual gamepad or keyboard inputs to them.
Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#7 Shaquil   Members   -  Reputation: 819

Like
0Likes
Like

Posted 19 November 2012 - 10:22 AM

pugixml is an extremely useful little C library for reading, writing and editing XML files. I've used it with some C++ games:

http://pugixml.org/documentation/

It shouldn't be too hard to pick up, either.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS