Skip to main content
gamedev.net
🔒 Locked

Password. . .

Started by jtecin Jun 29, 2000 at 5:44 PM 3 replies 300+ views
Original Post
jtecin
jtecin
I want to implement something into my game that let''s the user type in a password and go to a certain area. I am using directx and some gdi functions to draw text. What would be the easiest way to have the user type in the password? I know how the check for a password and then how to send them to the right level, but what would be the best way to get input from the user and display that on the screen?
jtecin
jtecin
One other question, what would be a good way to draw text on multiple lines? Right now I have to use the same function every time to draw each line. Thanks in advance.
jtecin
jtecin
Did I post this in the wrong forum or something?
cyberben
cyberben
Nope, just there are so many contraversil (sp?) ways to do it it''s ridiculous. Some people say don''t use AYN gdi calls which makes sense... aparently true type font''s use bezier curve and line and stuff to draw so there not wickedly fast actually there slow.

So theoretically (I''ve never done this yet) this is how you''d do it...
1.) Create a string to hold your keyboard input of the maximum length of the password or just some huge number like 255 characters (That number is often chosen by programmers).

2.)Now you must implement a timing loop for text input something like the frame rate limiter.. so you only check for a new key press every 100ms or something (repeat rate)... now if you sense a character being pressed add that to the end of the string (The closet null to the beggining of the string) if you encounter a backspace being pressed delete one character from the end of the string... I''m assuming a one line password. Enter would jump to your password switch statement.

3.) When displaying the text you could just use your command and use GDI and such or you could load a bitmap font... simply put it''s 224 character which corresponds to binary values 32 through 255.. then you simply loop through the string painting the bitmap at the position of the binary value - 32.

Oops gotta go, hope this inspires you or gets you going...
Or maybe somebody with some C++ experience could come along.. Asusuming your using C++

Bye,
Ben
__________________________Mencken's Law:"For every human problem, there is a neat, simple solution; and it's always wrong."
"Computers in the future may weigh no more than 1.5 tons."- Popular Mechanics, forecasting the relentless march of science in 1949
Sheltem
Sheltem
I threw together something for things like that awhile back. It is very crude. I haven't touched it since I've made it. I haven't really looked into optimizations and the spacing is all off but you can fix it up if you want...this is just to give you a general idea. I didn't have any comments so I just threw some up there real quick to hopefully make some sense of it. Well, here it is:

                                                    
char* GetStringBox(char *boxtext, int max_digits, int start_x, int start_y)
{


//get the width of the text. Note that I assume the text will be 13 pixels wide but

//this is very untrue in my case. GDI fonts don't have a fixed width I just go with the widest to be safe. put in whatever width your font is

int text_width = strlen(boxtext) * 13;

//get the width the input box needs to be

int box_width = max_digits * 13;

//get the total width of the window

int window_width = text_width + box_width + 50;

//this will hold the text be written in.

char text[100] = {0};

int index = 0;

while(1)
{
Input::UpdateKeyboardState();

if(Input::KeyPressed(KEY_RETURN))
return(text);

if(Input::KeyPressed(KEY_ESCAPE))
return("0");
//only process another character if the length of the text string is less than max digits

if(strlen(text) < max_digits)
{

if(Input::KeyPressed(KEY_A))
{

text[index] = 'a';

index++;

}

if(Input::KeyPressed(KEY_B))
{

text[index] = 'b';

index++;

}


//put in whatever characters you want to be recongnized here

}

if(Input::KeyPressed(KEY_BACKSPACE))
{
//if any characters are in the string erase the last one so it won't show up in the input box and set the index back so the next character will overwrite it

if(strlen(text) > 0)
{
text[index - 1] = 0;
index--;

}

}


Graphics::FillBackBuffer(_16BITCOLOR(255, 255, 255));
//put in whatever you want to show up in the background here

DrawScreen();

//for drawline the arguments are x,y position of the upper left corner, the width and height of
//the line, the width of the border, the fill color and then the border color.
Graphics::DrawLine(start_x, start_y, window_width, 40, 1, RGB_16BIT_BLACK, RGB_16BIT_BLUE);

Graphics::DrawLine(start_x + text_width + 20, start_y + 10, box_width, 20, 1, RGB_16BIT_BLACK, RGB_16BIT_BLUE);

//arguments for display text are the text to display, the x and y position, the text color and then the background color of the text
Graphics::DisplayText(boxtext, start_x + 10, start_y + 10, RGB(255,255,255), 0);

Graphics::DisplayText(text, start_x + text_width + 25, start_y + 10, RGB(255, 255, 255), 0);

Graphics::FlipBuffers();

}



This function can be used with pretty much any graphical API or whatever. You will have to fill in the abstract functions I use here like DisplayText, DrawLine, KeyPressed, etc.. If you want the source to them I can give them to you as well (I use Direct X for everything except the fonts where I use GDI...because I'm too lazy to make my own fonts ) But you should get the general idea form this source, I hope. To use the function you just do something like this:

char* password = GetStringBox("password:", 12, 300, 300);

The 1st argument is what text you want to be displayed in front of the input box where the text gets displayed as the user types and the 2nd argument is the number of characters you will allow to be proccessed. The 3rd and 4th are the x and y position of the upper left corner of the dialouge box.

Edited by - Sheltem on June 30, 2000 3:14:09 AM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.