Jump to content
  • Advertisement
Sign in to follow this  
The Orange Peanut

What is going on with this code? (glut)

This topic is 3756 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to debug and make sure I correct values, and I'm doing it with regular print statements. However, depending on whether or not I have a print statement, my other print statements won't actually print until the program has stopped. Take a look at this relevant section of code
void mouse(int button, int state, int x, int y)

{   

  switch(button)

  {

    case GLUT_LEFT_BUTTON:

      if(state == GLUT_DOWN) 

	  {

        if(current_pt == true)
        {
           lines[current_line].p1.x = x;
           lines[current_line].p1.y = screenHeight - y;
           current_pt = false;
        }
        else if(current_pt == false)
        {
            lines[current_line].p2.x = x;
            lines[current_line].p2.y = screenHeight - y;
            current_pt = true;

            slope = (lines[0].p2.y - lines[0].p1.y)/(lines[0].p2.x - lines[0].p1.x);

            
        }
        else
        {
            cout << "I messed up somewhere." << endl;
        }
        cout << slope;
        cout << "(" << lines[0].p1.x << ", " << lines[0].p1.y << ") to (" 
        << lines[0].p2.x << ", " << lines[0].p2.y << ")" << endl;  //debugging

                



		glutPostRedisplay();

	  }

   break;

   

	case GLUT_RIGHT_BUTTON:

      if(state == GLUT_DOWN) 

	  {

	 

	

		glutPostRedisplay();

      }

    break;



    default:

    break;

   }

}


Near the end of the case block, see the cout << slope; followed by a two-line cout statement? If I comment out the two-line cout statement, slope won't print until I exit the program. Slope is a global variable that is initialized to 999, so if I click on my screen, say, 3 times, after I terminate the program the output will THEN show-up as 999999999. If I leave the function as it is (with the cout statement NOT commented out), it works. What could be the problem? Also, I have another program where I do the same thing, and it's similar block works perfectly, which is why I'm perplexed as to why this isn't working. And I know the code is a mess... I'll fix the actual code after I can fix my debugging stuff (that's the whole point!) :) Thanks

Share this post


Link to post
Share on other sites
Advertisement
Nevermind. I rarely use C++ and I totally forgot that I had to flush buffer. Of course, when I changed my cout statements to include endl at the end, the buffer was getting flushed and my output was correct. Whoops.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!