• 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
oldgregg

Game Input from an XML file

6 posts in this topic

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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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):
[code]
<?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>
[/code]

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

To read this data and use it, I make calls to my Utilities functions.
Here's Utilities.h
[code]
#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 */
[/code]

And Utilities.cpp Take note of the functions Utilities::LoadConfig(), UpdateSettings(), and Utilities::KeyIsDown()
[code]
/******************************************************************************
* 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::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;
}
}
}
[/code]

Good Luck!
2

Share this post


Link to post
Share on other sites
[quote name='BeerNutts' timestamp='1353283590' post='5002167']
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):
[code]
<?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>
[/code]

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

To read this data and use it, I make calls to my Utilities functions.
Here's Utilities.h
[code]
#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 */
[/code]

And Utilities.cpp Take note of the functions Utilities::LoadConfig(), UpdateSettings(), and Utilities::KeyIsDown()
[code]
/******************************************************************************
* 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::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;
}
}
}
[/code]

Good Luck!
[/quote]


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

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
0

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