-3

I am attempting to create a game engine using OpenGL and C++ without any external libraries other than OpenGL itself. What would be the best way to receive keyboard / mouse input? I know that std::cin would work, but it stops the thread and putting it on another one is annoying. I appreciate anyone answering this question!

Demitri
  • 11,446
  • 4
  • 34
  • 38
  • 2
    How are you attaching OpenGL to the frame buffer without any external libraries? – Richard Critten Jul 29 '18 at 07:20
  • I'm not sure whether `std::cin` is the best choice for a game engine. Using non-blocking keyboard I/O instead, requires OS specific implementations. At best, you could do input from `std::cin` in a separate thread. (I assume, you don't count standard library as "external library" and it provides `std::thread` as well.) My answer to [SO: I/O in concurrent program](https://stackoverflow.com/a/48097134/7478597) might be of help. – Scheff's Cat Jul 29 '18 at 10:03
  • @RichardCritten "external libaries" is probably unlucky wording. It seems that OP excludes OS libraries as well as C++ std library from this. So, may be, ["3rd party libraries"](https://en.wikipedia.org/wiki/Third-party_software_component) would match better. – Scheff's Cat Jul 29 '18 at 10:09
  • 1
    Btw. there is no mouse input considered in the C++ standard libaries (which I know about). Mouse handling is an essential part of every GUI library/framework (e.g. Qt) although these GUI libraries are usually built on top of OS specific libraries. So, mouse handling can surely be implemented without any GUI library but requires again OS specific implementations. – Scheff's Cat Jul 29 '18 at 10:13

1 Answers1

0

You're making your life a lot harder by not wanting to use any libraries. The only real option you have without libraries is std::cin, and even that is part of the iostream library.

std::cin is actually able to give you input in real time without the user having to press enter. The reason you typically do need to press enter is because the default mode many terminals operate in "cooks" the input. This means that the terminal itself (Not C++) waits for the user to press enter, ("cooking" it by waiting for the user to be done with his command) then hands the input over to C++ to act upon it once enter has been pressed. You can, however, set your terminal to raw output, and std::cin will get the input as it is typed in. Check out this program:

#include <stdlib.h> // for using system()
int main() {
system("stty raw");
    char that;
    while(cin >> that) {
        cout << that;
    
}

It will read input as it is received and immediately print it back out to the user. That first system() command sets the terminal so that it is in raw output mode, i.e. the terminal spits out the input it receives immediately to C++ for handling. That system() command is system dependent ("stty raw" works on Mac OS, for example, but not necessarily other platforms) and the argument you need to give to it will depend on your target OS.

So, in short, std::cin can be used for real time keyboard input.

But it shouldn't be in your case, because you need mouse input too. There are no standard C++ libraries that handle mouse input, and any library that handles mouse input almost certainly handles keyboard input too. Additionally, it is best practice to avoid the system() command in C++ for numerous reasons. It is difficult to make a suggestion as to a library that would work for you since you are also trying to use OpenGL. If I were you I would look into using operating system specific libraries since it will be all but impossible to make your proposal cross-platform without rewriting it. Don't be afraid of using external libraries: they are your friend and were written for a reason.

Shades
  • 726
  • 1
  • 6
  • 17