Glut: how to know when the window is moving?

I have a problem with glut, I would like to have a callback to know when the user is moving the window of my application, and I didn't found anything in glut.

My goal is to make an application only on windows, is it possible to do this with MFC keeping my glut code?

Thanks in advance

13.10.2009 16:32:59

I couldn't find a call back function but you could do it by hand with glutGet:

int glutGet(GLenum state);

An example of what you might do is:

bool window_change(void)
  int init_x = glutGet(GLUT_INIT_WINDOW_X);
  int init_y = glutGet(GLUT_INIT_WINDOW_Y);
  int pos_x = glutGet(GLUT_WINDOW_X);
  int pos_y = glutGet(GLUT_WINDOW_Y);

  return (init_x != pos_x || init_y != pos_y);

This will return true if it's moved from it's initial spot. If you want to see if it's moved since the last check, try:

bool window_change(void)
  static int init_x = glutGet(GLUT_INIT_WINDOW_X);
  static int init_y = glutGet(GLUT_INIT_WINDOW_Y);
  int pos_x = glutGet(GLUT_WINDOW_X);
  int pos_y = glutGet(GLUT_WINDOW_Y);

  bool result = init_x != pos_x || init_y != pos_y;

  init_x = pos_x; 
  init_y = pos_y;

  return result;

You can set the window position by using the function: glutPositionWindow

void glutPositionWindow(int x, int y);
13.10.2009 22:36:45
thanks for your suggestion, but this code can't work because when you drag the window with glut, your program is freezed. Maybe creating a thread just for this? But the code will not work if you only click on the program window bar (i don't remember the correct term) without moving the window.
penpen 14.10.2009 15:31:49
Ya I've checked and there's no way to check if the window is moving with glut, as far as I know. You'll have to make a thread.
Corey 14.10.2009 22:09:59

the idea from Bruce is very good. i think there is not another option while using GLUT. i think this scenario is something that GLUT was not developed for. GLUT is a toolkit for OpenGL, which wraps window- and input-management across platforms. it has a lot of uses, but why should it care, when its window is dragged? i think if you (or your program) care, then you should implement your own window- and input-management anyway.

which leads me to your second question. you can use OpenGL with MFC (although my recommendation strongly depends on what you are planing). you should not use GLUT then, because MFC has its own way of handling input, windows, event management, rendering/drawing ...

if you really want to do it:

13.10.2009 23:49:02
Thanks for you help. It seems that I have no choice and I will have to do what you suggest. That's frustrating because everything works with glut, but there is only this problem....
penpen 14.10.2009 15:33:17

You might try using the glutTimerFunction to periodically call Bruce's example function. It will have to take an integer as a parameter used to identify the source of the callback request. The glutTimerFunction is also designed to only be called once, then it deletes itself. However, it can schedule a call on itself. I ran into this problem using PyOpenGL so I'm not going to go through the C agony of getting this to compile. From Bruce's generous example altered. Obviously a hack, but better than scrapping.

int main(void)
   glutTimerFunc(0, window_change, 0);


void window_change(int id) { int init_x = glutGet(GLUT_INIT_WINDOW_X); int init_y = glutGet(GLUT_INIT_WINDOW_Y); int pos_x = glutGet(GLUT_WINDOW_X); int pos_y = glutGet(GLUT_WINDOW_Y); ...

/* call back again after 1 second has passed */ glutTimerFunc (1000, window_change, id); }

22.12.2009 03:52:25

GLUT is actually fairly limited, and does not expose all window events to the client. However it does provide a lot of boiler code plate for WGL (on Windows) and GLX (on X11) that is probably not worth rewriting.

When I had a hobby project that was using GL, and there were certain events that I couldn't handle via GLUT, what I did is fork the open source freeglut. That is, download the tarball, and edit the library's WindowProc (Windows) or XGetEvent() loop (on X) and simply add the missing callbacks to the library.

You'll have a dependency on your own GLUT modifications, but it'll work, and you won't have to write as much boiler-plate code as if you were targeting WGL or GLX directly.

You'll probably want to link it statically so it doesn't conflict with any other freeglut.

22.12.2009 04:41:04