2

I am using a C++ graphics library which has functions void WaitForMouseDown() and void WaitForMouseUp(). The WaitForMouseDown function waits until the mouse button is pressed and then returns. WaitForMouseUp waits for the button to be released. There is another function in the library bool isMouseDown() which returns true if the mouse button is currently down.

Now, I have to start doing something in a loop when the user clicks the first time, and keep looping until user presses the mouse the second time.

I have tried the following approach, but for some reason the program crashes after the second click. Any ideas, what I am doing wrong?

int main(){ WaitForMouseUp(); while(!isMouseDown()){ //do something } return 0; } 
7
  • 2
    How does it crash? What message do you get? And are you sure it isn't the do something part itself that does the crashing rather than the mouse routines? Commented Jan 11, 2012 at 12:56
  • The xcode console and the graphics window both freeze. I dont get any error message. I guess you are right, if I comment out the do something, then it works fine. On the other hand, the do something code seems to run fine until user makes the second click. Commented Jan 11, 2012 at 13:02
  • Have you tried adding a sleep in the loop? like a sleep(100). Just a suggestion! Commented Jan 11, 2012 at 13:06
  • NOTE: this probably isn't relevant but shouldn't it be int main(){ and not int main{ ? Commented Jan 11, 2012 at 13:09
  • By the way, if your description is accurate (program must wait for mousedown, then do something until a second mousedown), your program code doesn't match: it immediately starts doing something until the first mousedown and then exits. Commented Jan 11, 2012 at 13:15

2 Answers 2

3

If //do something does not include polling the mouse or acting on windowing system events (i.e. no call to your library), you've got the obvious infinite loop here: isMouseDown will always return true because the event that would set it to false sleeps in your event queue.

Sign up to request clarification or add additional context in comments.

2 Comments

I am not sure, if I understand correctly. Isn't isMouseDown always false, that is why the loop keeps on running. Now, when I make the second click 'isMouseDown' becomes true and hence the loop condition !isMouseDown becomes false and breaks the loop. This works absolutely fine if I comment out do something.
@stressed_geek: Yes, but isMouseDown doesn't magically become true, but rather gets so when the GUI library processes a mouse event. Which it might do in a parallel thread, but for most frameworks something like a call to process_pending_events() is necessary.
1

I think it should be

int main() { WaitForMouseUp(); bool done = false; while(!done) { //do something done = isMouseDown(); } 

1 Comment

Which is equivalent to do { // do something } while ( ! isMouseDown() );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.