Center an OpenGL window with GLUT
Asked Answered
R

3

14

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.

Restrained answered 2/2, 2010 at 4:47 Comment(0)
F
26

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);
Frequent answered 2/2, 2010 at 5:7 Comment(1)
Thank you! I had tried using this before and had somehow failed miserably but it is working again with this code thanks to you.Restrained
A
1

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.

Arnitaarno answered 2/2, 2010 at 4:50 Comment(3)
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?Restrained
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.Restrained
Okay I found how to get to project properties and linker but where is User32.lib located?Restrained
P
0

Firstly: glutInitWindowSize(720, 720);

Secondly: glutInitWindowPosition(600, 200);

Finally: glutCreateWindow("OpenGL Window");

Precedency answered 29/4, 2022 at 15:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.