开发者

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


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);


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: http://www.codeproject.com/KB/openGL/OpenGL_MFC_AppWizard.aspx


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); }

http://www.opengl.org/resources/faq/technical/glut.htm


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜