0% found this document useful (0 votes)
15 views

Lab5 - Mouse Handler

The document describes a keyboard demo program that allows a user to move a red box around the screen using arrow keys or WASD keys. The program initializes OpenGL settings, defines key press and release functions to track input, and uses a timer to continuously redraw the box based on the current key presses.

Uploaded by

mariumazam8
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lab5 - Mouse Handler

The document describes a keyboard demo program that allows a user to move a red box around the screen using arrow keys or WASD keys. The program initializes OpenGL settings, defines key press and release functions to track input, and uses a timer to continuously redraw the box based on the current key presses.

Uploaded by

mariumazam8
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

// Keyboard demo.

Shows how to use more than one key on the keyboard


// at a time. Demo has a red box on a black background and user can
// use arrow keys or the 'w', 'a', 's', 'd' keys to move the box
// around the screen, the point being that you can hold more than
// one key down at a time and the box will move accordingly (ie.
// diagonally).

//#include <windows.h>
#include <stdio.h>
#include <vector>
#include "glut.h"
#include "Point2.h"
using namespace std;
vector<Point2> myPointList;
void myMouse(int,int,int,int);
int screenHeight = 480;
// Initial square position and size
float xPos = 320.0;
float yPos = 240.0;
GLsizei rsize = 25;
#define FALSE 0
#define TRUE 1
int up = FALSE;
int down = FALSE;
int left = FALSE;
int right = FALSE;

void moveObject(void)
{
if(left)
{
xPos -= 4.0;
// Keep it inside of the box
if(xPos-rsize < 0.0)
xPos = 0.0+rsize;
}
if(right)
{
xPos += 4.0;
// Keep it inside of the box
if(xPos+rsize > 640.0)
xPos = 640.0-rsize;
}
if(up)
{
yPos += 4.0;
// Keep it inside of the box
if(yPos+rsize > 480.0)
yPos = 480.0-rsize;
}
if(down)
{
yPos -= 4.0;
// Keep it inside of the box
if(yPos-rsize < 0.0)
yPos = 0.0+rsize;
}
}

void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
moveObject();
glRectf(xPos-rsize, yPos-rsize, xPos+rsize, yPos+rsize);
glutSwapBuffers();
}

void TimerFunc(int value)


{
myDisplay();
glutPostRedisplay();
glutTimerFunc(33,TimerFunc, 1);
}

void myInit(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glColor3f(0.0f, 0.0f, 0.0f);
glPointSize(1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 640.0, 0.0, 480.0);
}

void pressKey(unsigned char key, int x, int y)


{
switch (key)
{
case 'a':
left = TRUE;
break;
case 'd':
right = TRUE;
break;
case 'w':
up = TRUE;
break;
case 's':
down = TRUE;
break;
}
}

void releaseKey(unsigned char key, int x, int y)


{
switch (key)
{
case 'a':
left = FALSE;
break;
case 'd':
right = FALSE;
break;
case 'w':
up = FALSE;
break;
case 's':
down = FALSE;
break;
}
}

void pressKeySpecial(int key, int x, int y)


{
switch (key)
{
case GLUT_KEY_LEFT :
left = TRUE;
break;
case GLUT_KEY_RIGHT :
right = TRUE;
break;
case GLUT_KEY_UP :
up = TRUE;
break;
case GLUT_KEY_DOWN :
down = TRUE;
break;
}
}

void releaseKeySpecial(int key, int x, int y)


{
switch (key)
{
case GLUT_KEY_LEFT :
left = FALSE;
break;
case GLUT_KEY_RIGHT :
right = FALSE;
break;
case GLUT_KEY_UP :
up = FALSE;
break;
case GLUT_KEY_DOWN :
down = FALSE;
break;
}
}

int main(int argc, char** argv)


{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow("KeyBoard Test");
glutDisplayFunc(myDisplay);
glutTimerFunc(33, TimerFunc, 1);
glutSpecialFunc(pressKeySpecial);
glutSpecialUpFunc(releaseKeySpecial);
glutKeyboardFunc(pressKey);
glutMouseFunc(myMouse);
glutKeyboardUpFunc(releaseKey);
myInit();
glutMainLoop();
return 1;
}

void myMouse(int button, int state, int x, int y)


{
static int numCorners = 0; // initial value is 0
std::vector<Point2>::iterator it = myPointList.begin();
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
myPointList.insert(it,Point2(x,screenHeight - y));
numCorners++; // have another point
if(numCorners == 2)
{
glColor3f(1,1,1);
glRecti(myPointList[numCorners-2].getX(), myPointList[numCorners-2].getY(),
myPointList[numCorners-1].getX(), myPointList[numCorners-1].getY());
numCorners = 0; // back to 0 corners
}
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glFlush();
}

///class point 2

#pragma once
class Point2
{
public:
Point2() { x = y = 0.0f;} // constructor 1
Point2(float xx,float yy) { x = xx; y = yy;} // constructor 2
void set(float xx,float yy) { x = xx; y = yy;}
void setX(float xx) { x=xx;}
void setY(float yy) { y=yy;}
float getX() { return x;}
float getY() { return y;}
void draw(void)
{ glBegin(GL_POINTS); // draw this point
glVertex2f((GLfloat)x, (GLfloat)y);
glEnd(); }// end draw
private:
float x, y;
}; // end class Point2

You might also like