PDA

View Full Version : Mouse handler events



Raymond
04-17-2016, 09:57 AM
Hello i am trying to make a mouse handler for a calculator so that when i click a specific box or a square i have already drawn it stores it value in in a variable and prints 1 (if 1 is pressed by mouse ) then when clicked + it clears what is written and write again , then when clicked = it clears all and show the result ... i did the calculator with textures but i am not so good with mouse handles so please if help with ideas :D
Note : when i tried without x and y restriction for the position the text appeared and vanished in 2sec ;
this is my code example





#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <windows.h>
#include <gl/glut.h>
#include <fstream>
#include <string>
#define PI 3.14159
using namespace std;

const int IMG_WIDTH = 256;
const int IMG_HEIGHT = 202;

GLuint LoadTextureRAW(const char * filename, int wrap)
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;

file = fopen(filename, "r");

if (file == NULL)
{
return 0;
}

width = IMG_WIDTH;
height = IMG_HEIGHT;

data = (byte*)malloc(width * height * 3);

fread(data, width * height * 3, 1, file);

fclose(file);

glGenTextures(1, &texture);

glBindTexture(GL_TEXTURE_2D, texture);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP);

gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

glEnable(GL_TEXTURE_2D);

return texture;
}

void Set_Transformations()
{
glClearColor(1.0, 1.0, 1.0, 0.0); //setting the window's background for white color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-100, 100, -100, 100);
glMatrixMode(GL_MODELVIEW);
}
void drawquad(int x1 , int y1 , int x2, int y2, int x3, int y3, int x4, int y4)
{
glBegin(GL_QUADS);
glTexCoord2f(1.0, 1.0);
glVertex2f(x1, y1);
glTexCoord2f(0.0, 1.0);
glVertex2f(x2,y2);
glTexCoord2f(0.0, 0.0);
glVertex2f(x3,y3);
glTexCoord2f(1.0, 0.0);
glVertex2f(x4,y4);
glEnd();
}
void drawnum (int x, int y, int width, int height)
{
glBegin(GL_QUADS);
glTexCoord2f(1.0, 1.0);
glVertex2f(x, y);
glTexCoord2f(0.0, 1.0);
glVertex2f(x+width, y);
glTexCoord2f(0.0, 0.0);
glVertex2f(x+width , y-height);
glTexCoord2f(1.0, 0.0);
glVertex2f(x, y-height);
glEnd();
}

void drawBitmapText(string input, float x, float y, float z)
{

glRasterPos3f(x, y, z);

for (int i = 0; i< input.length(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, input[i]);
}
}
void WriteText(string s) {
drawBitmapText(s, -36, 47, 0);
}
void myMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
if (x < 0)
{
glColor3f(0.3, 0.4, 0.5);
WriteText("Here is my text");
glutPostRedisplay();
glFlush();
}
}
}
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT); //Clear the colour buffer
glLoadIdentity();// Load the Identity Matrix to reset our drawing locations

glColor3f(0.5, 0.5, 0.5);
drawquad(-40, 70, 40, 70, 40, -70, -40, -70);
glPushMatrix();
{

glColor3f(1.0, 1.0, 1.0);
drawquad(-37, 57, 37, 57, 37, 40, -37, 40);

}
glPushMatrix();
{
glColor3f(0.0, 0.0, 1.0);
GLuint texture1 = LoadTextureRAW("0000.bmp", 0);
glBindTexture(GL_TEXTURE_2D, texture1);
drawquad(-40, 70, 40, 70, 40, 60, -40, 60);
}
glPopMatrix();

glPopMatrix();
GLuint texture2 = LoadTextureRAW("b.bmp", 0);
GLuint texture3 = LoadTextureRAW("c.bmp", 0);
GLuint texture4 = LoadTextureRAW("d.bmp", 0);
GLuint texture5 = LoadTextureRAW("m.bmp", 0);
GLuint texture6 = LoadTextureRAW("7.bmp", 0);
GLuint texture7 = LoadTextureRAW("8.bmp", 0);
GLuint texture8 = LoadTextureRAW("9.bmp", 0);
GLuint texture9 = LoadTextureRAW("-.bmp", 0);
GLuint texture10 = LoadTextureRAW("4.bmp", 0);
GLuint texture11 = LoadTextureRAW("5.bmp", 0);
GLuint texture12 = LoadTextureRAW("6.bmp", 0);
GLuint texture13 = LoadTextureRAW("+.bmp", 0);
GLuint texture14 = LoadTextureRAW("1.bmp", 0);
GLuint texture15 = LoadTextureRAW("2.bmp", 0);
GLuint texture16 = LoadTextureRAW("3.bmp", 0);
GLuint texture17 = LoadTextureRAW("e.bmp", 0);
GLuint texture18= LoadTextureRAW("0.bmp", 0);
GLuint texture19 = LoadTextureRAW("212.bmp", 0);




glColor3f(1.0, 0.0, 1.0);
glBindTexture(GL_TEXTURE_2D, texture2);
drawnum(-37, 30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture3);
drawnum(-20, 30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture4);
drawnum(-3, 30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture5);
drawnum(22, 30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture6);
drawnum(-37, 10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture7);
drawnum(-20, 10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture8);
drawnum(-3, 10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture9);
drawnum(22, 10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture10);
drawnum(-37, -10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture11);
drawnum(-20, -10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture12);
drawnum(-3, -10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture13);
drawnum(22, -10, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture14);
drawnum(-37, -30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture15);
drawnum(-20, -30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture16);
drawnum(-3, -30, 15, 15);
glBindTexture(GL_TEXTURE_2D, texture17);
drawnum(22, -30, 15, 35);
glBindTexture(GL_TEXTURE_2D, texture18);
drawnum(-37, -50, 32, 15);
glBindTexture(GL_TEXTURE_2D, texture19);
drawnum(-3, -50, 15, 15);


glutSwapBuffers();
glFlush();// Flush the OpenGL buffers to the window
glDisable(GL_TEXTURE_2D);

}
//initialisation
void Initialize(int argc, char *argv[])
{
glutInit(&argc, argv); //setup the glut library
glutInitDisplayMode(GLUT_RGBA); //It tells the GLUT library what type of display mode to use when creating the window
glutInitWindowPosition(100, 100); //It specifies the top-left corner of the window, measured in pixels, from the top-left corner of the screen.(pop-up window position)
glutInitWindowSize(800, 600); //It specifies the initial height and width, in pixels, of the window on the screen.
glutCreateWindow("walking car"); //window title
glutDisplayFunc(Draw);
Set_Transformations();
glutMouseFunc(myMouse);
glutMainLoop(); //maintaining window operations while running...
}

//main
int main(int argc, char* argv[])
{
Initialize(argc, argv);
return 0;
}