Thread: Read string in directdraw

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    14

    Question Read string in directdraw

    I need to read a string from the keyboard in my directdraw-game.. For all other input I've used keyboardstate, but how would I do this?
    I use c#, by the way.

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Disclaimer: I've never used C# and I'm assuming that there is a message loop, etc, like in a C++ program.

    You could use DirectInput. There are some tutorials on that here and here and here.

    Or, if you're just trying to read individual keypresses, and not actually read a string, you could handle the WM_KEYPRESS message in your message loop. (Requires knowledge of the WinAPI)
    Away.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    14
    I was hoping for some other way of doing it, but I guess I'd have to use a C# form to avoid it.
    Thanks, though!

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Do not call the WinAPI from within DirectX. Those functions are not optimized for games and/or speed. Use DirectInput.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    14
    The input in question would only handle the user typing his/her name, so speed isn't really important. I think I got directinput working alright now, tho, so no need for winAPI.

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I've had Windows do some nasty stuff if I call the WinAPI too much from within DirectX. They don't get along very well at all, even though MS claims that they should.

    You can even see retail games that use the WinAPI to put text on menus. The mouse slows to a crawl and it feels like your dragging it through the mud, to say nothing of actually being able to click on something.

    DirectX has everything you need. Stay away from the WinAPI in games unless you are creating/managing threads or accessing the disk - there are more exceptions but...general rule - stay out of the WinAPI while in DirectX.

  7. #7
    Create a function that checks input from user (keyboardstate is correct) and log that input into a string.

    example:

    Create a function in your input class. You'll need a string in your class to store the string as its created. The function will check each legal character to see if it is in the down state. If so, add it to the string. i.e. If 'A' is down, strcat "A" to your string, same for "B", etc, etc. Check for 'shift' to determine upper or lower case. Just keep doing this until the user presses enter (or whatever).

    Call this function every frame. Create a boolean flag to like BOOL isUserTypingAString. Set to true when you want, set to false when they press 'enter'. Make your function return without processing anything if !isUserTypingAString.
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    14
    Bubba: Yeah, I've seen examples of this I think. Didn't know that was the reason, tho.

    lightatdawn: That's what I did, and also a check to see if the current input is the same as the last (the same keys are still down) to prevent a whole bunch of characters to be input. Works well.

  9. #9
    >>also a check to see if the current input is the same as the last

    This would prevent the user from typing a strings with the same characters in a row however. An alternate method would be to create a timer. Trip the timer (reset to 0) when a key is pressed. Dont log any further input to your string until the timer has reached a certain delay (maybe 0.2 seconds or something).
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    14
    The check doesn't prevent you from typing the same letter more than once, it just won't let you type more than one letter without releasing the key in between. When the key is released, input will be empty and if you press the key again, the input will differ from the last input..

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Heh, I made a whole big thinger for Windows blocking-keyboard-IO for use in my game (i.e. displaying help, hiscores, getting the username, etc.) using Windows API since I didn't feel like learning DirectDraw or anything. Heres the code, if you're interested (and no, it's not the best way of doing it, just the way I came up with first):

    **EDIT**
    Ahh shoot, it uses a GFX global variable that I made, and in globals.h I have "extern GFX gfx;"... I guess you can still use the code if you convert the code using "gfx" to plain GDI stuff or DirectDraw as the case might be... or you can get the GFX code from the link in my sig (Space Shooterz DX), it might help to understand what's going on under the "gfx.____" calls. It shouldn't be too hard to filter out the GFX stuff, it's only a few lines.
    Last edited by Hunter2; 08-02-2003 at 11:37 AM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    May 2003
    Posts
    14
    Thanks, but the one I did works ok I think. Maybe I'll try to improve it later on, though.

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yeah, me too Didn't realize 'til now just how application-specific that code was...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Replies: 1
    Last Post: 05-30-2003, 02:31 AM
  5. Replies: 1
    Last Post: 07-24-2002, 06:33 AM