Continuing program while waiting for input C++ [duplicate] - c++

This question already has answers here:
Non-blocking console input C++
(13 answers)
Closed 6 years ago.
First of all let me state that I am new to C++. I received an assignment to make a mini "snake" game meaning I need to make a grid and have a box move around in it. It is a simple 1x1 box, and no fruits to catch or anything.
I am able to create the grid, and have a box exist in it that I can move up, down, left, right using user input. My problem is I need the box to move automatically every second in the direction it was headed. I have no problems with the sleep command (I think), my problem is that when I am waiting for the user to enter his direction the program stops until that input is given. I need a way to have the box continue moving while I wait for input. For this I need either to just have the code continue while waiting for the input, or to redirect it to doing a function while waiting for input.
I have been googling this and trying things for some hours now and all I keep finding is multi-threading. I tried using thread, pthread, boost/thread.hpp but I can't get any of them to work.
In any case multi-threading is way more advanced than what my class is doing and there is no chance that is what my professor want's us to do. He said something about having a function of cin that allows such things, but I can't find it anywhere.
Is there a simple way of doing this?

From what I'm understanding from the question, it seems that you need to implement a timer of sorts that will move the move the box every second. I'm not sure if g++ has something similar to System::Timers as visual studio does but here's a link that shows how to make a timer yourself (though it's written in C). With a timer in place (created, started, and your code to get input and moving of the box happens within the timer's tick function) you could use a following if statement to keeping moving the box. Here's a link to the .NET's timer for more information on how it works. This has code for a timer class that Matthew Hoggan has created for C++ in linux. Hope this helps.
if (user input)
{
change direction
}
move the box

Related

How to detect whether tying is enabled in the currently selected/active interface in C++

In Windows 7(I am not sure about other OS) when you are on a webpage, pressing the space-bar scrolls a few pixels down the page. But when you are in an interface where typing can be done(like an input element, textarea, word editor, code editor, search bar, etc), pressing the space-bar obviously types a space.
Similarly, when all the open windows or menus are minimized, and you are viewing the desktop, and you press a letter key, instead of the letter being typed somewhere, a beep sound is produced.
This shows that the availability of a typing functionality can be "detected". And if it can be detected, it can most likely be done so using C++.
I don't know what to call this. I tried to find out using google but everything I got seemed unrelated to this. I was probably not using the correct keywords.
Whatever this is, I am creating a C++ program where I need to be able to detect it(in an if condition). Something like this
if (/*typing can be done*/) {
//Do something..........
}
Or this
if (/*typing can NOT be done*/) {
if( GetKeyState(VK_SPACE) & 0x8000 ) {
//Do something..........
}
}
And I need to be able to do so natively. Not specific to a particular console window or UI.
If you can help me in any way in figuring this out than please do so. And please feel free to make or suggest relevant edits to improve this question and make it less vague and more detailed and to-the-point.
The functionality you describe belongs to the program running the window with focus, not to the operating system (although the operating system will, at least in part, ultimately power that functionality). It shouldn't come as a surprise that programs can detect keystrokes, because otherwise you wouldn't be able to use your keyboard to input any characters into your computer.
However, you cannot just "detect" a random event with an if statement. "if" is not "when". Your computer will not repeatedly check all if statements in your program and jump to that location in the code whenever one matches. Imagine the chaos!
A program sufficiently complex to have a graphical interface almost certainly has an "event loop", be this in its own code or buried within an API call (as in the case of native Windows applications); such an event loop typically polls for keys being sent to the window(s) managed by the program. If you do not have an event loop (and if your operating system cannot generate a "signal" when a keystroke otherwise goes unhandled), you will have to make one.
Exact specifics are beyond the scope of a Stack Overflow answer, but by pointing you in the direction of a textbook about how to create graphical programs, I am enabling you to discover how input and output is handled in those cases.

I'm making a roguelike in visual c++ 2010, how can I speed up the print rate?

I'm trying to make a roguelike game in visual c++ 2010, which involves printing out a screen every time the player makes a move. Unfortunately, it uses 8000 characters, and so it takes a second or so to print them out every refresh. It might not seem like much, but given the number of moves involved in the game, it adds up.
I've tried unsyncing with stdio, compiling all the characters into a string and then printing the string with cout, and printing the characters with _putch();, but there was still a significant printing time for each method. I tried printing out an unchanging string repeatedly to test if it was something else causing the delay, but there was still a delay when the only task was printing.
My question is, is there anything I could try that could potentially speed up the process? A friend of mine suggested ncurses, would that be worth a try? If so, how would I do it, and if not, what else could I try?
I would agree with cornstalks. However if you are looking to make a pure console game I would suggest you take advantage of the built in ctime library. Learn from a library (such as SDL1.2) or a source on how to handle hard-coding time based events with entity systems. There are many efficient ways to store these characters and print them in a jiff. Or you can use SDL and make the game window seem like a console and handle everything as raster-graphics.

Automatically filling a form and running a program

A friend sent me 3 programs, which are written in C or C++. Every program has a form, which is filled and then a button is pressed to perform some calculation. The programs were written in LabWindows/CVI which I've never heard of. He asked me, if it was possible to automatically fill the forms and run the calculation to get the output. I said that one way would be, to modify the program to a console one and run it with parameters (if the number of fields is low).
Does there exist a tool, which could be used to automate the execution of such programs or, if possible, does the LabWindows/CVI have anything which could help me, besides modifying the source?
I thank you in advance for you help.
you could use some automated test programs such as http://www.sikuli.org (this is written in java), it is also nice because you can add any java code you want to improve the test and modify it if you really need something extraordinary.
another way would be creating your own "automated test program" using c++ (since you posted this with c++ tag) to create a mouse and keyboard hook that will move the mouse and type characters on your forms.
the following links might help if you want to create your hooks C++ mouse click on certain spot in window, Global keyboard hook with WH_KEYBOARD_LL and keybd_event (windows)

c++ ubuntu detecting real time keypress event

I'm writing a program that needs to respond towards a key press (say by printing "hello key press") in real time (the program runs in a giant loop that takes around 2 seconds to complete). I have found one potential answer Detecting keydown and keyup events on Linux C++ but the answers weren't very clear to me (I also looked into the 4 answer that are linked via duplicates). Can somebody please provide a simple code example of how to make a linux program respond towards a keypress by printing a single line (or doing whatever) without having to check for it each loop?
Take a look at SDL Input events. Simple DirectMedia Layer (SDL) provides a cross platform API developed for things like gaming. It does provide a lot of low level keyboard, mouse etc functionality. A link can be found here.
There are essentially to ways:
If the long loop is long in time, but short in code (that is: it contains another working inner loop that keeps all the time) you can place a event presence check in the most inner loop.
If you cannot rework the long loop, you have most likely to split the application in two distinct threads: one performing event detection and immediate actions, and another one to which to delegate the lengthy operations.

Why is my paintBox Canvas being erased when my program is "Not Responding"?

I have written a small program using Borland's C++ builder, and along the way, everything seemed fine. My program has a map window and a table window, and when a user presses a button, a long process is started that reads in all the map and table information and then displays that. Every time i ran it through the debugger, I had no issues. Then today, I decided to test it without running it through the debugger. To my horror, The program reads in the map information and then displays it on the paintbox canvas without a problem, but when it loads the information for the grid, the map gets erased!!! It appears to happen during the load phase for the table. this takes about 4 seconds, and during which time, the window tells me that it isnt responding. This is when the map gets erased. Anyone have any ideas on why this is happening? Its driving me nuts, and I dont really understand whats going on under the hood here.
UPDATE:
I have fixed the problem to some degree. I was poking around and found this: Avoiding "(Not Responding)" label in windows while processing lots of data in one lump
I added the code to run once in the middle of the data read in for the table. this fixed my problems. however, I was wondering if anyone knows why this is the case? why does my program going unresponsive cause my canvases to be erased?
Marcus Junglas wrote a detailed explanation of the problem, which affects both Delphi and C++Builder.
When programming an event handler in
Delphi (like the OnClick event of a
TButton), there comes the time when
your application needs to be busy for
a while, e.g. the code needs to write
a big file or compress some data.
If you do that you'll notice that your
application seems to be locked. Your
form cannot be moved anymore and the
buttons are showing no sign of life.
It seems to be crashed.
The reason is that a Delpi application
is single threaded. The code you are
writing represents just a bunch of
procedures which are called by
Delphi's main thread whenever an event
occured. The rest of the time the main
thread is handling system messages and
other things like form and component
handling functions.
So, if you don't finish your event
handling by doing some lengthy work,
you will prevent the application to
handle those messages.
You can reduce the problem by calling Application->ProcessMessages(), while loading your map data, however I recomend using a separate thread to load the data.
I have never used C++ Builder, but i used Delphi. I think the libraries are the same.
Does that component you use store the image data? It may only draw to the screen. Try covering the window of your app with another window. If it erases it, you have to use a component which stores the image.
See this, it is for Delphi, but it may help. There should be a Image component in C++ Builder. Try using that instead of PaintBox.
You can solve the unresponsivenes problem by running the time consuming task in a separate thread or calling some function that processes the window's messages.