Sign in to follow this  
Slothrop

WINAPI/C++ string input question

Recommended Posts

I have a basic C++ program that creates an OpenGL window and displays some basic points and lines. How would I go about storing string input from the user? I can handle single key strokes, but would like the user to be able to enter a full string to delete a specfic class instance by name that is displayed on the screen. For instance, if a graph with a node named 'dog' is displayed on screen, I would like the user to be able to type 'dog', press enter, and 'dog' is stored in a string. Any hints, tips, tricks, resources, tutorials, articles that you can recommended or point me towards? Thanks in advance. Here is my WinMain:
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
	            LPSTR lpCmdLine, int nCmdHow)
{
	MSG		msg;
	int		done = 0;

	char* name = "OpenGL";
	int len = strlen(name)+1;
	wchar_t *wName = new wchar_t[len];
	memset(wName,0,len);
	::MultiByteToWideChar(  CP_ACP, NULL,name, -1, wName,len );
	
	if ( !createGLWindow( wName, windowWidth, windowHeight, 16 ))
	{
		killWindow();
		return 0;
	}
	initGL(windowWidth, windowHeight);
	
	while ( !done )
	{
		if ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				done = TRUE;
			else
			{
				TranslateMessage(&msg);
				DispatchMessage(&msg);
			}
		}
		drawScene();
		if ( checkKeys() )
		    done = TRUE;
	}
	killWindow();
	return (int)(msg.wParam);
}

Share this post


Link to post
Share on other sites
You could create a win32 console project and replace
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,

LPSTR lpCmdLine, int nCmdHow)


with a standard main function, spawn a thread to grab user input via the console and pass messages between that thread and the game thread via a thread safe queue. Is this what you were thinking or have I got it totally wrong?

Share this post


Link to post
Share on other sites
I think you are interested in the WM_CHAR message. This translates the keypresses that would result in actual character entry, into characters (which are stored in some field of the message). You can grab the characters one at a time and append them to your string, stopping at a newline for example. (Please use a real string type, i.e. std::string.)

However, you probably don't really want to do things at such a low level if you don't really need to. You might be interested in libraries such as "wxWidgets".

Share this post


Link to post
Share on other sites
Quote:
Original post by CmpDev
You could create a win32 console project and replace
*** Source Snippet Removed ***
with a standard main function, spawn a thread to grab user input via the console and pass messages between that thread and the game thread via a thread safe queue. Is this what you were thinking or have I got it totally wrong?


Thanks for the reply. I will look into the thread safe queue. That sounds like exactly what I am looking for. I don't really care how it is done, just that it is, well, done! I appreciate it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this