Using SCREEN_WIDTH = 1200 and SCREEN_HEIGHT = 800.
First, I draw a window onto the screen (x = 0, y = 0, w = 40, h = 40);
Then I use the handleMouse function to return the x and y coordinates that the mouse clicks on.
The problem is that when the program starts in the mode without a maximized window, when I click (something similar) in the lowest right corner of the window, the returned coordinates are x = 40, y = 32. When I think that it should return x = 40, y = 40.
I do not know if the problem is if it is not drawn correctly, or the functions return the wrong x / y.
I believe I understand how openGL rendering, conversion, and glOrth work, but I could be completely wrong. I saw several suggestions on the Internet suggesting that Windows Decor (using Windows 7) could cause this problem, but made very few explanations and gave no solution.
This is my entire source code. I separated everything from my game to the basics, and the problem still persists :( I added two photos so that people can see my problem. In the NON-MAXIMUM WINDOW (upper image) when clicking on the lower right corner, the returned coordinates are 41.32 The y coordinate is smaller than it should be, and in the MAXIMIZED WINDOW (bottom shot) when you click on the same angle it returns the correct coordinates 40, 40. These results hold for both the source code and the genpfault code.
// Turns out I can't post photos :(, instead of links.
not maximized window !
Maximized window !
main.cpp int main( int argc, char* args[] ) { //Initialize FreeGLUT glutInit( &argc, args ); //Create OpenGL 2.1 context glutInitContextVersion( 2, 1 ); //Create Double Buffered Window glutInitDisplayMode( GLUT_DOUBLE ); glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT ); glutCreateWindow( "OpenGL" ); //Do post window/context creation initialization if( !initGL() ) { printf( "Unable to initialize graphics library!\n" ); return 1; } //initGame(); glutMouseFunc(handleMouse); glutDisplayFunc( render ); glutMainLoop(); return 0; }
Functions.h
#ifndef FUNCTIONS_H #define FUNCTIONS_H bool initGL(); void render(); void handleMouse(int button, int state, int x, int y); #endif
Functions.cpp
bool initGL() { //Initialize clear color glClearColor( 0.f, 0.f, 0.f, 1.f ); //Check for error GLenum error = glGetError(); if( error != GL_NO_ERROR ) { //cout <<"Error initializing OpenGL! " << gluErrorString( error ) << endl; return false; } return true; } void render() { //Clear color buffer glClear( GL_COLOR_BUFFER_BIT ); glColor3f(1.f,1.f,1.f); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluOrtho2D(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glBegin(GL_QUADS); glVertex2f(0,0); glVertex2f(0, 41); glVertex2f(41, 41); glVertex2f(41, 0); glEnd(); glutSwapBuffers(); } void handleMouse(int button, int state, int x, int y) { std::cout << x << '\t' << y << std::endl; }
constants.h
const int SCREEN_WIDTH = 1200; const int SCREEN_HEIGHT = 800;