Advertisement

Console Interface?

Started by July 31, 2003 05:13 PM
7 comments, last by norleaf 21 years, 6 months ago
I was just browsing through all the tutorials, and to my horror I found nothing about writing text onscreen to feed the program. Is it so simple that it is taken for granted that people can do that?? Is it not possible? (Naw I know it is, I have played many games, that starts out by asking for your name...) Like I said in another post, I am trying to build a strategy game, and I got most of the graphics sorted out, but I would very much like to have function like std::cin >> myCharArray[128]; which I used when wrinting console apps. My program is running on the NeHe basecode, and has a gameloop and everything, so what I need is a funktion that will: Stop the gameloop running until I hit Enter Take a string from the keyboard and store it in a variable Show the text on the screen while Im writing... -------------------------------------------------------------- What''s the problem? I don''t got a problem, I got fuckin'' problems! -Tim Roth, Four Rooms
--------------------------------------------------------------What's the problem? I don't got a problem, I got fuckin' problems! plurum!-Tim Roth, Four Rooms
You got tutorials on how to display text.. you got tutorials on how to get key input.. now make your own functions to do what you want. Then post it here so other peoples can learn from it.
Advertisement
Roger I could prolly write a program to do that, but it'd be pretty big... I was hoping there'd be some built-in method you could just call or a .dll or whatever... someone somewhere must have built it already. I'm a rookie, if I code it, it'll prolly not be very efficient...

I am imagining I need to do something like:

char string[]; // is a global variable
while(notdone)
{
if(KEYDOWN!=VK_Enter)
//get key
//get stringlength
//store char in string at end
if(KEYDOWN==VK_Enter)
//notdone=false;
//Showcontents of string to screen
}

but I'm not convinced it's quite that easy...

[edited by - norleaf on August 1, 2003 4:28:50 AM]
--------------------------------------------------------------What's the problem? I don't got a problem, I got fuckin' problems! plurum!-Tim Roth, Four Rooms
Such a thing would become pretty long (each character to be precise).

Where you check your key presses (the if (keys....) you would need to add an:
if(console_is_down)//Console is down{    DisplayedString = DisplayedString + Key_Being_Pressed    UpdateConsole()}else//Console is not downyour_normal_code_for_this

Note: this is all pseudo code, but this is how I would do it.
This may help, after it gest dusted a bit though! It uses the console(I know, making a console using the console) but only to keep it simple. You might be able to incoprerate it into your own code, send it a key when you recieve the WM_KEYUP/WM_KEYDOWN. It doesn''t have a repeater but does allow you to move within the string using arrow keys. The output looks messed up because i don''t know how to move the cursor in the console, but after pressing enter, all will be revealed!! Hopefully what little commenting should be enough

#include <iostream>#include <string>#include <conio.h> //Used for _getche()#define VK_LEFT 75 //These would normally be done in windows, but for example here#define VK_RIGHT 77#define VK_ESCAPE 27 class Console{public:	Console():cursorPos(0), itsFinished(false) {}	~Console() {}	void KeyPressed(int);	const char * GetStream() const {return textEntered.c_str();}	bool Done() const {return itsFinished;}private:	bool itsFinished;	std::string textEntered;	unsigned int cursorPos;};void Console::KeyPressed(int key){	switch (key)	{		case 224: //Only needed for _getche(), if it returns an arrow key		{			KeyPressed(_getche()); //Second time it returns which arrow key			break;		}		case VK_ESCAPE: //Escape key		{			textEntered.erase(textEntered.begin(), textEntered.end()); //Erase all input entered			itsFinished = true; //Finished entering information			break;		}		case 8: //Backspace		{			if (cursorPos) //Make sure we''re not at begining				textEntered.erase(--cursorPos); //Erase the char before cursor and set cursor to one less			break;		}		case 13: //Enter		{			textEntered += ''\n''; //add newline to the end			itsFinished = true; //Signal we''ve finished entering input			break;		}		case VK_LEFT:		{			if (cursorPos) //Make sure not at begining				--cursorPos; //Move back one space			break;		}		case VK_RIGHT:		{			if (cursorPos <= textEntered.length()) //Make sure not past end				++cursorPos; //Move forward one space			break;		}		default: //Any other key and we''ll add it		{			/*++cursorPos; //Move past last char added			if (cursorPos > textEntered.length()) //Moved passed end of string, so append it				textEntered += char(key);			else //Somewhen in the string				textEntered.insert(&textEntered[cursorPos - 1], key); //insert puts char before place pointed to*/			if (cursorPos >= textEntered.length()) //Moved passed end of string, so append it				textEntered += char(key);			else //Somewhen in the string				textEntered.insert(&textEntered[cursorPos], key); //insert puts char before place pointed to			++cursorPos; //Move to next char			break;		}	}}int main(){	std::cout << "Please enter text:";	Console userInput;	int key = 0;	while (!userInput.Done()) //Just an example	{		key = _getche();		userInput.KeyPressed(key);	}	std::cout << "\nYou entered:" << userInput.GetStream() << std::endl;	return 0;}

void bitmap_output(int x, int y, int z, char *string, void *font){  int length, i;  glRasterPos3f(x, y, z);  length = (int) strlen(string);  for (i = 0; i < length; i++)   {	glutBitmapCharacter(font, string[i]);  }}

Try this, it's what I use. I usually use the font, GLUT_BITMAP_TIMES_ROMAN_24 .
Have fun. Oh, yeah, about there being tutorials on how to do this, I find this the simplest one, and I got it out of someone's program. I wish I could find it so I may give credit to whoever came up with the brilliantly simple function, but I can't, so if you know who it is, tell me so I may thank them. Using GLUT simplifies the junk so that you can focus on the game and not how to write text to the screen.

"Donkey, if it were me, you'd be dead."
I cna ytpe 300 wrods pre mniute.



[edited by - duncanbojangles on August 2, 2003 12:57:10 AM]
"Donkey, if it were me, you'd be dead."I cna ytpe 300 wrods pre mniute.
Advertisement
I've been creating a console for my game lib recently, the source is viewable, created via Doxygen at...

http://www.davesprogramming.com/dgl/doxygen/DGLConsole_8cpp-source.html

and..

http://www.davesprogramming.com/dgl/doxygen/DGLConsole_8h-source.html

http://www.tempURLtest.org
test

Hmmm, can't figure out the link tag, plus can't find any info, sorry about that.

[edited by - TerraX on August 7, 2003 1:11:10 AM]
Thanks for all your help guys...

I ended up writing some code that takes the info in WM_CHAR and stores it in a char, which I then add to the string I am printing using strcat... maybe not the most elegant way in the whole world, but it''s simple enough for my inferior brain to comprehend... if any other newbees want to try this method, remember that you either need a timer or something else so it doesn''t write 30 letters everytime you push a button... personally I used a bool that was set true on WM_KEYUP... that doesn''t allow for repeating the letter by holding down the button, but seriously, who needs that in a game console?

I also added a single line of code that will remove a letter from the string when backspace is pressed... by setting MyString[(strlen(MyString)-1)] = NULL;

Anyways I just want to thank you all for all the input and different suggestions... Specially thanks to the guy who mailed me about this method.
--------------------------------------------------------------What's the problem? I don't got a problem, I got fuckin' problems! plurum!-Tim Roth, Four Rooms
"remember that you either need a timer or something else so it doesn't write 30 letters everytime you push a button... personally I used a bool that was set true on WM_KEYUP... that doesn't allow for repeating the letter by holding down the button, but seriously, who needs that in a game console?
"

You taking the piss outta my code!?

if so, I don't care
http://www.davesprogramming.com/dgl/doxygen/index.html


[edited by - TerraX on August 8, 2003 5:11:48 AM]

This topic is closed to new replies.

Advertisement