Skip to main content
GameDev.net gamedev.net
🔒 Locked

WIn32 OpenGL get commands from console

Started by mynameisnafe Feb 10, 2013 at 2:43 PM 0 replies 1.4k views
Original Post
mynameisnafe
mynameisnafe

Hi Guys.

So here is the thing. I have a win32 / opengl application. I've created a class* that opens a console with AllocConsole and ties the stdin, stderr and stdout of the application to it.

So this is awesome, 'cos I can go (from anywhere in the program)



#include <iostream>
...
std::cout << "\nSomething broke!"

Anyways, the point is I now have a console window that I can potentially get custom commands out of, such as "quit" for example.

I'm dubious about hacking something together before I've thought aboutt his so that's why I'm here.

If, on every frame (opengl) I go..



bool Frame() {
  std::string _command;
  std::cin >> command;
  if( command == "quit")
      return false;
  else
      return gfx->frame(); // do rendering
}

Then I'm never going to get to type "quit" because the program will always be recreating the string every seconds-per-frame.

So how do I solve that? I need some way of like holding a buffer of input characters and send/emptying it on a newline / null character? then have a vector of these ? How do I test it?

How I turn AllocConsole() and freopen("CONIN$", "r", stdin) into an interactive console?

mynameisnafe
mynameisnafe

Okay this just changed a whole bunch.. I need to get this on a thread? cin just waits for me to type and as such it frames only when I hit enter.. All my graphics and program loop are held up by cin lmao!


void WinConsole::Frame()
{
    std::string key_buf;
    
    std::cin >> key_buf;

    if( key_buf == "\n" )
        currentCommand = buffer;
    else {
        if( key_buf != "" )
            buffer.append( key_buf );
    }
}
 

Also.. when I type in quit and press enter, it does nothing..

I added two lines to this function concerning the m_Console


bool WinApp::Frame()
{
    /*
        Bail or fail?
    */

    if( ! m_Input->IsKeyDown( VK_ESCAPE )  )
    {
        m_Console->Frame();

        if( m_Console->GetCommand().c_str() == "quit" ) return false;
        if( ! m_GFX->Frame( m_Input ) )                    return false;
    }
    return true;
}
 

So umm yeah.. do I need to like spark a thread and do std::cin on it? Surely that's too funny to be true ?


Topic Locked

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

Sign in to reply to this topic.