Center an OpenGL window with GLUT

Aaron McKellar picture Aaron McKellar · Feb 2, 2010 · Viewed 11.3k times · Source

I have an openGL window that is 640x480 that I need to center in the middle of the screen. I previously used:

glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN)-640)/2,
                       (GetSystemMetrics(SM_CYSCREEN)-480)/2);

which WORKED.

But now all of a sudden when I compile...

Linking...
1>Project1.obj : error LNK2028: unresolved token (0A000372) "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics@@$$J14YGHH@Z) referenced in function "int __cdecl main(int,char * *)" (?main@@$$HYAHHPAPAD@Z)
1>Project1.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall GetSystemMetrics(int)" (?GetSystemMetrics@@$$J14YGHH@Z) referenced in function "int __cdecl main(int,char * *)" (?main@@$$HYAHHPAPAD@Z)
1>C:\Users\My Computer\Documents\School Stuff\CS445\Project1\Debug\Project1.exe : fatal error LNK1120: 2 unresolved externals

Someone please help. This is very annoying and frustrating for me as I don't know a lot about OpenGL and GLUT.

Answer

Kornel Kisielewicz picture Kornel Kisielewicz · Feb 2, 2010

Also, instead of linking user32.lib you can do it solely using glut:

glutGet(GLUT_SCREEN_WIDTH) // returns Screen width

and

glutGet(GLUT_SCREEN_HEIGHT) // returns Screen height

Why depend on Windows when you can be cross-platform?

Hence, your code would look:

glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-640)/2,
                       (glutGet(GLUT_SCREEN_HEIGHT)-480)/2);