There is such a piece of code.

void Control() { _getch(); switch(button) { case 8: pause++; } } void Start() { Control(); while(pause == false) { Iteration(); Render(); } } 

You must put the program "on pause" when you press the spacebar. The fact is that if you put Control () in a loop with an iterator and a render, then for each frame you have to press the spacebar. What to do next? = /

  • All thanks, I used the _kbhit () function - RomanNVKZ
  • You have complicated the code too much - listen to the first answer. - chudo116
  • Would you tell about your task. Perhaps without pauses this can be realized ... - AseN
  • I write the game of life in the console with the ability to edit. Here is the entire project file. yadisk.cc/d/Nngpp3ub1pV I am writing to the console because I want to prescribe the algorithms first, and then translate it into the window. - RomanNVKZ


3 answers 3

You _getch() replace with kbhit() - life will immediately seem easier. The difference between them is that getch() and its variants block the execution of the stream until it is entered. kbhit() simply polls whether the button is clicked and returns immediately. Accordingly, the CPU load is different when calling these functions.

  • Thank you, I already understood it myself =) - RomanNVKZ

First, why do you need a switch? Is the usual if'om not enough? Secondly, as I understand it. Pause - is this a global variable? If so, that is also not a very good tone. Let it be declared in Control (), and let Control () return unsigned char (if errors can occur in the function, then short, and char then 0xFF to separate). Well, I would solve the problem taking into account everything written above: you make a global infinite loop, and inside you set up a call to the control and use the result of its execution. Pause I would put the usual sleep.

  • It's all in the class, and switch to the future. PS And I already did. Well, see what you can do =) - RomanNVKZ

Make calls Iteration(); Render(); Iteration(); Render(); in a separate thread, start and stop the execution of the stream by pressing the spacebar.

  • I do not agree. We do not know under what OS writes the HARDWARE. It is not enough under DOS, and there is not a word about threads at all :-) - gecube
  • Judging by the link, he writes at least under Windows 95 msdn.microsoft.com/en-us/library/58w7c94c ( v = vs.80). Aspx - fogbit
  • Does _kbhit () not uniquely identify the system? - alexlz