This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Simple Mouse Class Submitted by

I wrote this class to simplify the use of the mouse in my OpenGL applications, its nothing extravagant, but it gets the job done. The class will work with Win32 or GLUT programs, a simple GLUT demo of the class is included. Do whatever you like with the class or the example. This code is for the beginners out there, the more advanced coders will write their own code :)
Nate Miller
http://nate.scuzzy.net

Currently browsing [mouse.zip] (1,830 bytes) - [mouse.h] - (3,283 bytes)

    /*
       To use this mouse class with GLUT make sure that you #define __GLUTMOUSE__
    before you #include "mouse.h".  I was thinking about having this header check
    for GLUT #defines, but you would have had to #include <glut.h> before this
    header which could lead to problems.
    */
    #ifndef __MOUSEH__
    #define __MOUSEH__

    #ifdef _WIN32
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #endif
    #ifdef __GLUTMOUSE__
    #include <GL/glut.h>
    #endif

    struct mouse_t
    {
       mouse_t() {oldX = oldY = currX = currY = -1; state = 0;}

       // for win32 apps, ste isn't needed
       inline void SetState(int btn, int ste = 0);
       inline void SetNewPos(int x, int y);
       int IsLeft(void) const           {return (state & buttonLeft);}
       int IsRight(void) const          {return (state & buttonRight);}
       int IsMiddle(void) const         {return (state & buttonMiddle);}
       void Reset(void)                 {state = 0;}
       int GetOldX(void) const          {return oldX;}
       int GetOldY(void) const          {return oldY;}
       int GetX(void) const             {return currX;}
       int GetY(void) const             {return currY;}
       int GetDiffX(int x) const        {return x - oldX;}
       int GetDiffY(int y) const        {return y - oldY;}
       int GetDiffX(void) const         {return currX - oldX;}
       int GetDiffY(void) const         {return currY - oldY;}
       void SetOldPos(int x, int y)     {oldX = x; oldY = y;}
    #ifdef _WIN32
       void Capture(HWND hWnd) const    {SetCapture(hWnd);}
       void Release(void) const         {ReleaseCapture();}
    #endif

    protected:
       enum
       {
          buttonLeft = 1,
          buttonRight = 2,
          buttonMiddle = 4,
       };

       int state;
       int oldX;
       int oldY;
       int currX;
       int currY;
    };

    inline void mouse_t::SetState(int btn, int ste)
    {
    #ifdef _WIN32
    #ifndef __GLUTMOUSE__
       switch (btn)
       {
          case WM_LBUTTONDOWN:
             state |= buttonLeft;
          break;
          case WM_RBUTTONDOWN:
             state |= buttonRight;
          break;
          case WM_MBUTTONDOWN:
             state |= buttonMiddle;
          break;
          case WM_LBUTTONUP:
             state &= ~buttonLeft;
          break;
          case WM_RBUTTONUP:
             state &= ~buttonRight;
          break;
          case WM_MBUTTONUP:
             state &= ~buttonMiddle;
          break;
       }
    #endif
    #endif

    #ifdef __GLUTMOUSE__
       if (ste == GLUT_DOWN)
       {
          switch(btn)
          {
             case GLUT_LEFT_BUTTON:
                state |= buttonLeft;
             break;
             case GLUT_RIGHT_BUTTON:
                state |= buttonRight;
             break;
             case GLUT_MIDDLE_BUTTON:
                state |= buttonMiddle;
             break;
          }
       }
       else if (ste == GLUT_UP)
       {
          switch(btn)
          {
             case GLUT_LEFT_BUTTON:
                state &= ~buttonLeft;
             break;
             case GLUT_RIGHT_BUTTON:
                state &= ~buttonRight;
             break;
             case GLUT_MIDDLE_BUTTON:
                state &= ~buttonMiddle;
             break;
          }
       }
    #endif
    }

    inline void mouse_t::SetNewPos(int x, int y)
    {
       currX = x;
       currY = y;
    #ifdef _WIN32
    #ifndef __GLUTMOUSE__
       if(currX & 1 << 15)
          currX -= (1 << 16);

    	if(currY & 1 << 15)
          currY -= (1 << 16);
    #endif
    #endif
    }

    #endif

Currently browsing [mouse.zip] (1,830 bytes) - [main.cpp] - (1,663 bytes)

    #define __GLUTMOUSE__ // important!!!!
    #include "mouse.h"
    #include <stdio.h>
    #include <GL/glut.h>

    int winW = 320;
    int winH = 320;

    mouse_t mouse;

    void glutDisplay(void)
    {
       glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glutSwapBuffers();
    }

    void glutResize (int w, int h)
    {
       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
       glViewport(0, 0, (winW = w), (winH = h));
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity();
       gluPerspective(90, (double) winW / winH, 1, 4096);
       glutPostRedisplay();
    }

    void glutKeyboard(unsigned char key, int x, int y)
    {
       exit(1);
    }

    void glutMouse(int button, int state, int x, int y)
    {
       mouse.SetState(button, state);
       mouse.SetNewPos(x, y);
       printf("Mouse %s At %d %d\n", (state == GLUT_DOWN) ? "Down" : "Up", x, y);
    }

    void glutMotion(int x, int y)
    {
       mouse.SetOldPos(mouse.GetX(), mouse.GetY());
       mouse.SetNewPos(x, y);

       printf("Mouse Drag Button ");

       if (mouse.IsLeft())
          printf("Left\n");
       else if (mouse.IsRight())
          printf("Right\n");
       else
          printf("Middle\n");

       printf("\tFrom   %d %d\n", mouse.GetOldX(), mouse.GetOldY());
       printf("\tTo     %d %d\n", mouse.GetX(), mouse.GetY());
       printf("\tChange %d %d\n", mouse.GetDiffX(), mouse.GetDiffY());
    }

    void main (void)
    {
       glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
       glutInitWindowSize(winW,winH);
       glutCreateWindow("Mouse Test");
       glutDisplayFunc(glutDisplay);
       glutReshapeFunc(glutResize);
       glutKeyboardFunc(glutKeyboard);
       glutMouseFunc(glutMouse);
       glutMotionFunc(glutMotion);
       glutMainLoop();
    }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.