title: detect mouse clicks in OpenGL C++
body: <p>I am a beginner at OpenGL and I am trying to create a game in which as background I have a raw image. When the game starts I display that image and I want to be able to click on it and to display another image afterwards. I tried using the function <code>glutMouseFunc</code> but when I try to run the program I receive a message which says that the program stopped working. 
  Here are some parts of my code: I have a global variable onMouse; if I click the mouse button the variable takes the value 1 and if it has the value 1 I try to load the second image.</p>
  
  <pre><code>int onMouse;
  void mouseClicks(int button, int state, int x, int y) {
      if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN) {
          onMouse = 1;
      }
  }
  </code></pre>
  
  <p>And this is the main function:</p>
  
  <pre><code>//load first texture
  texture = LoadTextureRAW( "2 Lock screen.raw", TRUE );
  glutMouseFunc(mouseClicks);
  if(onMouse == 1){
      //load second texture
      texture = LoadTextureRAW( "in_game.raw", TRUE );
  }
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>EDIT</strong> I have made some changes to the code, but the background image still doesn't change. Here is the new main function:</p>
  
  <pre><code>glutCreateWindow("Game");
  texture = LoadTextureRAW( "2 Lock screen.raw", 1 );
  glutDisplayFunc(display);
  
  glutMouseFunc(mouseClicks);
  glutKeyboardFunc(key);
  
  glutMainLoop();
  
  if(onMouse == 1){
          texture = LoadTextureRAW( "in_game.raw", 2 );
          glutDisplayFunc(display);
  }
  </code></pre>
  
  <p>And this is the display function in which I map the texture to a quad:</p>
  
  <pre><code>glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  glClear( GL_COLOR_BUFFER_BIT );
  
  // setup texture mapping
  glEnable( GL_TEXTURE_2D );
  glBindTexture( GL_TEXTURE_2D, texture );
  
  glPushMatrix();
  glBegin( GL_QUADS );
  glTexCoord2d(1.0,0.0); glVertex3d(1.0,1.0, 1.0);
  glTexCoord2d(0.0,0.0); glVertex3d(-1.0,1.0,1.0);
  glTexCoord2d(0.0,1.0); glVertex3d(-1.0,-1.0, -1.0);
  glTexCoord2d(1.0,1.0); glVertex3d(1.0,-1.0, 1.0);
  glEnd();
  glPopMatrix();
  glutSwapBuffers();
  
  // free the texture
  FreeTexture( texture );
  </code></pre>