Lab5 - Mouse Handler
Lab5 - Mouse Handler
//#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 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);
}
///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