typing text to screen with directx 10
I am trying to add the ability to type text into my game, sort of like a chat box only there wont be anyone responding for now, and was wondering what a good way of going about it is. Right now i'm doing something that seems brute force..ish, which is using the GetAsyncKeyState function to check if a key is being pressed and saving the letter/number/symbol of the key being pressed to a char value. Then I am updating a string with the char value from the key being pressed and printing the string to the screen with Directx's DrawText function. This does get whatever I type on the keyboard to the screen but right now i'm having to use the GetAsyncKeyState function for every single key and then there is also no blinking line thingy(such a scientific name...) to show where the text is going to come in at.
Constructing strings from individual keystrokes is an ugly business. Your way is as good as most. I'm sure you'll get more suggestions.
The .. uh .. "thingy" is the text cursor (like a mouse cursor only for text). If you want a plain one, just add an underscore ( _ ) to the end of the string. If you want it blinking, set a timer (for say 1 second) and:
1. add the underscore & display
2. don't add the underscore & display
3. add the underscore .. you get the idea.
EDIT: Be careful to remove the underscore before you add another character (of course). And be mindful that the user may actually type an underscore which needs to be added to the string.
The .. uh .. "thingy" is the text cursor (like a mouse cursor only for text). If you want a plain one, just add an underscore ( _ ) to the end of the string. If you want it blinking, set a timer (for say 1 second) and:
1. add the underscore & display
2. don't add the underscore & display
3. add the underscore .. you get the idea.
EDIT: Be careful to remove the underscore before you add another character (of course). And be mindful that the user may actually type an underscore which needs to be added to the string.
I implemented my simple console by redirecting my wndproc data to a console object. The blinking thingy isnt too hard to achieve. The best way is to separate what you read and store and what you render. Where when rendering you simple grab the text to write and append the blinker or not.
Dont forget to add intellisence ! ;)
Dont forget to add intellisence ! ;)
The blinking thing is called "caret"
I'd either put a small line instead of a character, but the underscore is okay, just place it a little lower by 2-3 pixels.
The problem with using the text itself to add the caret: what happens, if you want to move the corsor in the string? (it's a basic feature, easy to add)
You'll get this:
And yes, that's brute force is the way to do it.
I'd either put a small line instead of a character, but the underscore is okay, just place it a little lower by 2-3 pixels.
The problem with using the text itself to add the caret: what happens, if you want to move the corsor in the string? (it's a basic feature, easy to add)
You'll get this:
text_ tex_t te_xt t_ext
Not the most elegant thing.And yes, that's brute force is the way to do it.
I am not quite sure what the answer is you are looking for, and judging from some of the responses I may be a bit off key, but I will try my best to add some insight here for you.
As for being "brute force", you will want to encapsulate all of this into the general input mechanism for the game. I usually build a simple state machine for just this function. If an element that requires input is in focus for any reason, it will redirect all text input to the object in focus. The text is then stored in a buffer, which is then drawn to the screen at the targets location.
Also an important step you either failed to mention or have not run into yet is in regards to the string size. Every time that input buffer changes you will want to make a call to MeasureString (not sure if thats the exact API call referance, but I am too busy to look it up).
As far as the caret marker. simply draw a single line the height of a single line of text, right after the width of the last string. you only need to do that when the object is in focus.
Hopefully this helps a little.
As for being "brute force", you will want to encapsulate all of this into the general input mechanism for the game. I usually build a simple state machine for just this function. If an element that requires input is in focus for any reason, it will redirect all text input to the object in focus. The text is then stored in a buffer, which is then drawn to the screen at the targets location.
Also an important step you either failed to mention or have not run into yet is in regards to the string size. Every time that input buffer changes you will want to make a call to MeasureString (not sure if thats the exact API call referance, but I am too busy to look it up).
As far as the caret marker. simply draw a single line the height of a single line of text, right after the width of the last string. you only need to do that when the object is in focus.
Hopefully this helps a little.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement