tags:

views:

237

answers:

2

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.

+1  A: 

You need to make sure you're linking against User32.lib, the static library where GetSystemMetrics() is defined. Open up your project settings and make sure the User32.lib is listed among all of the .libs that you're linking against.

Adam Rosenfield
Where is project settings in Visual C++? I didn't mess with anything except the actual code so if this worked before how come it would all of a sudden stop working?
Aaron McKellar
If you know an alternate way of centering a OpenGL window I would appreciate that as well. Whichever works will be fine, just that this did work and the compiler error is beyond my knowledge.
Aaron McKellar
Okay I found how to get to project properties and linker but where is User32.lib located?
Aaron McKellar
+4  A: 

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);
Kornel Kisielewicz
Thank you! I had tried using this before and had somehow failed miserably but it is working again with this code thanks to you.
Aaron McKellar