Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Configuration/Ini Files...

SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
OK, I've got my 3D game runnign great, but you have to configure it each time you play, yech! I would like to store key configurations and such in ".ini" file format, but I don't know how to read that format. Here's a basic example of what I'd like to do:
[code]
//My Config File
FORWARD = VK_UP
BACKWARD = VK_DOWN
TURN_LEFT = VK_LEFT
TURN_RIGHT = VK_RIGHT
JUMP = 'A'
[/code]
The reason I'd need to do this is because of the way I detect keyboard input. It is basically like this:
[code]
//Global Defines...
bool keys[256] = FALSE;

//In The Callback...
case WM_KEYDOWN:
keys[wParam] = TRUE;
break;
case WM_KEYUP:
keys[wParam] = FALSE;
break;

//In The Main Loop...
if(keys[FORWARD])
{
//Move Forward!
}
if(keys[BACKWARD])
{
//Back Up...
}
etc...
[/code]
Now, how would I properly read and write the ".ini" file format so that if a user adds or moves a line it will still read in properly? Like if I moved FORWARD = blah to the last line, I'd need it to read it in properly anyway. I was also thinking of using defines to read in the keys, something like this:
[code]
//My Config Reading Routine (Not Written)
bool LoadConfig()
{
#define FORWARD
#define JUMP
return TRUE;
}
[/code]
Would that be a decent way to save memory since it wouldn't be creating a whole new slew of variables?
I posted here since the ".ini" format is a part of the Windoze OS, and I thought I could get the best help here. If this belongs elsewhere let me know. As always, thanks for the help.

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • Sephiroth2Sephiroth2 Posts: 423Member
    You can use the GetPrivateProfileString and WritePrivateProfileString functions for this.
  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    : You can use the GetPrivateProfileString and WritePrivateProfileString functions for this.
    :
    Uhm, can I get an example of writing a string then reading it? I have been playing with those functions for hours now and can't get it working. Once again, Borland Help is useless since it tells me what type of var I need at each location, but nothing more. I'm like, REAL ticked at the jerks who wrote the help files right now. Also, it is my understanding that these functions read into buffers, which probably won't work for me. Here's how I use control and why reading a ton of keys into a ton of buffers would destroy my resources.
    [code]
    bool keys[256];
    ...
    case WM_KEYDOWN:
    keys[wParam] = TRUE;
    break;
    case WM_KEYUP:
    keys[wParam] = FALSE;
    break;
    ...
    if(keys[FORWARD])
    {
    Moves forward
    }
    etc
    [/code]
    Right now FORWARD is a define, which is defined as 'W'. I am a hardcore gamer and use the "W/S/A/D" for movement with mouselook on. Now if I create a ton of buffers instead of defines, would that not just totally waste my resources? Mind you the engine is looking slightly better than the Half-Life engine right now and uses a LOT as it is. I need a way of having a configurable setup without using as much ram as the engine itself uses :P. Thanks for the help.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • flupflup Posts: 1Member
    Maybe Get/ReadPrivateProfileInt ist more convenient.
    Bye.


  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [code]
    WritePrivateProfileString (
    "Movement", "Forward", "W", "C:\MyGame\Config.Ini");
    [/code]
    This will give you a file "C:MyGameConfig.Ini" with:
    [Movement]
    Forward=W

    To read it back:
    [code]
    CHAR strKey [4];

    GetPrivateProfileString (
    "Movement", "Forward", "W" /* Default */,
    strKey, sizeof (strKey),
    "C:\MyGame\Config.Ini");
    [/code]
    The 'strKey[0]' will be the letter from .INI file. If no file, no section, no key, ... etc. - anything wrong - you get the default value 'W'. Nothing prevents you from converting this letter to your key code inside your array of keys: "byte keys [256];".

    Cheers!


  • SephirothSephiroth Fayetteville, NC, USAPosts: 1,035Member
    So it would be something like this?
    [code]
    byte keys[256];
    ...read data blah blah
    case WM_KEYDOWN:
    keys[256] = 1;
    break;
    case WM_KEYUP:
    keys[256] = 0;
    break;
    ...
    if(keys[0])
    move forward
    if(keys[1])
    move backward
    etc...
    [/code]
    Now the trouble I see is when I press say 'W', the case statement won't set the bit [0] to 1, so it wouldn't work. Did you mean something else Asm?

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : So it would be something like this?
    : [code]
    : byte keys[256];
    : ...read data blah blah
    : case WM_KEYDOWN:
    : keys[256] = 1;
    : break;
    : case WM_KEYUP:
    : keys[256] = 0;
    : break;
    : ...
    : if(keys[0])
    : move forward
    : if(keys[1])
    : move backward
    : etc...
    : [/code]
    : Now the trouble I see is when I press say 'W', the case statement won't set the bit [0] to 1, so it wouldn't work. Did you mean something else Asm?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
    :
    :
    I see... so you have defines, like:
    [code]#define FORWARD 0[/code]

    so you can say:
    [code]if (keys[FORWARD]) {
    // move forward...
    }[/code]
    ...then how does it work with say 'W'? What if you want to remap 'W' into other key and, say, move forward with 'E'? I am a little lost here... what are you trying to do? You want to be able to re-program keys by some .INI file? If so, then how the case works? Windows will send you the WM_KEY_xxx for 'W' and you can't change this behaviour... Hey, you know what? I'll tell you how I re-programmed keys once for some editor.

    1. Define a function pointer type:
    [code]
    typedef void (*ACTIONPROC) (void*);
    [/code]
    2. Define all action bodies:
    [code]
    void MoveForward (void* pParams /* pass anything here */)
    {
    }

    void MoveBackward (void* pParams /* pass anything here */)
    {
    }
    [/code]
    4. Statically initialize the map with default values:
    [code]
    KEYMAPENTRY arrKeys [] =
    {
    // ...
    MoveForward, // it is in place of 'W' virtual key
    // ...
    MoveBackward, // it is in place of 'S' virtual key
    };
    [/code]
    The index in that array is the same as in your 'keys' array which you set to 1/0 when you receive WM_KEY_xxx messages. Now, you do not need that array at all, so:
    [code]
    case WM_KEYDOWN: // 'wParam' is a virt. key code
    arrKeys [wParam] ( /*parameters here for action */ );
    break; // Direct action call, very efficient!..
    [/code]
    Now, to remap the entries - you do it at the program startup - just read the .INI file and set the entries:
    [code]
    arrKeys [VK_S] = MoveForward;
    arrKeys [VK_W] = MoveBackward;
    [/code]
    See, you remapped, so player will go backward on 'W' and forward on 'S'.

    Well, I hope, I helped a little.

    P.S. If you can't find the excat virt. codes for some keys - just set a breakpoint (I hope you use debugger...) at WM_KEYDOWN and when you press a key and it will stop - look at 'wParam' - that is the code for that key.

    Cheers!

Sign In or Register to comment.