📄 glwin.c
字号:
} w->frame_num++;}int glwAttach(glWindow w){ if (CurrentWindow==w) return 1; glwTrace("attach"); if (w==NULL) { glwError("null window"); return GL_FALSE;} if (w->offscreen) { if (!glXMakeCurrent(w->display, w->glx_pixmap, w->cMain)) { glwError("Unable to attach window!"); return 0; } } else { if (!glXMakeCurrent(w->display, w->wMain, w->cMain)) { glwError("Unable to attach window!"); return 0; } } glwTrace("attach"); CurrentWindow=w; return 1;}void glwFlush(glWindow w){ glwTrace("flush"); glXWaitGL(); XFlush(w->display);}void glwProcess(glWindow w,void *info){ GLenum need_redraw; int first=1; glwTrace("process"); if (!glwAttach(w)) return; w->info=info; w->process=1; if (w->control_mode==GLW_EVENT_DRIVEN ) w->drawAllowFlag=(GLenum)1; do { if (w->control_mode==GLW_APPLICATION_DRIVEN ) { glwTrace("redraw"); (*w->RedrawFunc)(w,w->info); need_redraw = GL_FALSE; if (!w->offscreen) { while (XPending(w->display)) { need_redraw |= DoNextEvent(w); } } } else { if (first) { need_redraw=(GLenum)1; first=0; } else need_redraw = DoNextEvent(w); } if ( w->drawAllowFlag && (w->control_mode==GLW_EVENT_DRIVEN ) && need_redraw && w->process ) { XDefineCursor(w->display, w->wMain,w->wait_cursor); glwTrace("redraw"); (*w->RedrawFunc)(w,w->info); XDefineCursor(w->display, w->wMain,w->user_cursor); } } while (w->process); glXWaitGL(); XFlush(w->display); w->drawAllowFlag = GL_FALSE; w->lastEventType= -1; glwTrace("process");}/******************************************************************************/void glwSetRedrawFunc(glWindow w, void (*Func)(glWindow, void *)){ if (w==NULL) { glwError("null window"); return;} w->RedrawFunc = Func;}void glwSetControlMode(glWindow w, int mode){ if (w==NULL) { glwError("null window"); return;} if (!w->offscreen) { w->control_mode=mode; if (w->control_mode==GLW_APPLICATION_DRIVEN) XDefineCursor(w->display,w->wMain, None); else XDefineCursor(w->display, w->wMain,w->user_cursor); }}/******************************************************************************/void glwSetExposeFunc(glWindow w, void (*Func)(glWindow, void *,int, int)){ if (w==NULL) { glwError("null window"); return;} w->ExposeFunc = Func;}/******************************************************************************/void glwSetReshapeFunc(glWindow w,void (*Func)(glWindow, void *,int, int)){ if (w==NULL) { glwError("null window"); return;} w->ReshapeFunc = Func;}/******************************************************************************/void glwSetDisplayFunc(glWindow w, void (*Func)(glWindow, void *)){ if (w==NULL) { glwError("null window"); return;} if (Func!=NULL) { w->RedrawFunc = Func; w->control_mode=GLW_EVENT_DRIVEN; } else w->control_mode=GLW_APPLICATION_DRIVEN; }/******************************************************************************/void glwSetKeyDownFunc(glWindow w,GLenum (*Func)(glWindow, void *,int, GLenum)){ if (w==NULL) { glwError("null window"); return;} w->KeyDownFunc = Func;}/******************************************************************************/void glwSetMouseDownFunc(glWindow w,GLenum (*Func)(glWindow, void *,int, int, GLenum)){ if (w==NULL) { glwError("null window"); return;} w->MouseDownFunc = Func;}/******************************************************************************/void glwSetMouseUpFunc(glWindow w,GLenum (*Func)(glWindow, void *,int, int, GLenum)){ if (w==NULL) { glwError("null window"); return;} w->MouseUpFunc = Func;}/******************************************************************************/void glwSetMouseMoveFunc(glWindow w,GLenum (*Func)(glWindow, void *,int, int, GLenum)){ if (w==NULL) { glwError("null window"); return;} w->MouseMoveFunc = Func;}/******************************************************************************/void glwSetIdleFunc(glWindow w,void (*Func)(glWindow, void *)){ if (w==NULL) { glwError("null window"); return;} if (Func!=NULL) { w->RedrawFunc = Func; w->control_mode=GLW_APPLICATION_DRIVEN; } else w->control_mode=GLW_EVENT_DRIVEN;}/******************************************************************************/void glwDestroy(glWindow w){ int i; glwTrace("destroy:"); if (glwAttach(w)) { glFlush(); glFinish(); } for (i=0;i<GLW_MAX_FONT_SIZE;i++) { if (w->font_base[i]!=0) glDeleteLists(w->font_base[i],w->font_range[i]); } glXMakeCurrent(w->display, w->wMain,NULL); if (GLW_HAS_OVERLAY(w->type)) { glXDestroyContext(w->display, w->cOverlay); XDestroyWindow(w->display, w->wOverlay); XFreeColormap(w->display, w->cMapOverlay); XFree((char *)w->vInfoOverlay); glwTrace("destroy overlay"); } if (w->offscreen) { glXDestroyGLXPixmap(w->display, w->glx_pixmap); XFreePixmap(w->display, w->pixmap); glwTrace("destroy pixmap"); } else { glXDestroyContext(w->display, w->cMain); glwTrace("destroy xwindow"); } XFreeColormap(w->display, w->cMapMain); XFree((char *)w->vInfoMain); XDestroyWindow(w->display, w->wMain); XCloseDisplay(w->display); if (w->title!=NULL) free(w->title); if (w->xtitle!=NULL) free(w->xtitle); if (w->state!=NULL) free(w->state); /* ????MEMORY LEAK free(w);*/ w=NULL; glwTrace("destroy window");}/******************************************************************************/char *glwGetDefault(glWindow w,char *resource, char *dflt){ char *data; data=XGetDefault(w->display,"gltools",resource); if (data==NULL) return dflt; return data;}/******************************************************************************/void glwGetWindowSize(glWindow w, int *width, int *height){ *width=w->w; *height=w->h;}/******************************************************************************/int glwIsMesa(glWindow w){ return w->isMesa;}/******************************************************************************/static glWindow wCreate=NULL;static glWindow wEvent=NULL;static void InitWindow(void){ glWindow w; if (wCreate==NULL) { wCreate=(struct glWindowStruct *)calloc(sizeof(struct glWindowStruct),1); if (wCreate==NULL) { glwError("No space for window struct!"); return; } } else return; w=wCreate; w->display = XOpenDisplay(NULL); if (w->display==NULL) { glwError("Unable to connect to display!"); return; } w->x=atoi(glwGetDefault(w,"xOrigin","0")); w->y=atoi(glwGetDefault(w,"yOrigin","0"));; w->w=(atoi(glwGetDefault(w,"width","352"))/16)*16; /* GLW_MPEG1_PAL */ w->h=(atoi(glwGetDefault(w,"height","288"))/16)*16; /* GLW_MPEG1_PAL */ w->type=(GLenum)(GLW_RGB|GLW_DOUBLE|GLW_DIRECT|GLW_DEPTH); w->dmPolicy=(GLenum)GLW_MINIMUM_CRITERIA; w->title=(char*)malloc(GLW_NAMELEN); w->xtitle=(char*)malloc(GLW_NAMELEN); w->state=NULL; strcpy(w->title,glwGetDefault(w,"title","gltools")); w->control_mode=GLW_EVENT_DRIVEN; w->font_base[0]=0; w->keep_aspect=1; w->offscreen=0; w->fb_num_strings=0; w->fb_buffer=0; w->fb_initial_size=100000; w->wToplevel=0;}void glwInitDisplayMode(GLenum type){ InitWindow(); wCreate->type = type;}void glwInitOffscreen(void){ InitWindow(); wCreate->offscreen=1; wCreate->control_mode=GLW_APPLICATION_DRIVEN;}void glwInitDisplayModePolicy(GLenum type){ InitWindow(); wCreate->dmPolicy = type;}void glwInitToplevel(void *toplevel){ InitWindow(); wCreate->wToplevel=(Window)toplevel;}/******************************************************************************/void glwInitPosition(int x, int y, int width, int height){ InitWindow(); wCreate->x = x; wCreate->y = y; wCreate->w = (width/16)*16; wCreate->h = (height/16)*16;}void glwInitPos(int x, int y){ InitWindow(); wCreate->x = x; wCreate->y = y;}void glwInitTitle(char *title){ InitWindow(); strcpy(wCreate->title,title);}void glwInitAspectKeeping(int yesno){ wCreate->keep_aspect=yesno;} static int ErrorHandler(Display *display, XErrorEvent *event){#ifdef glDebug char buf[80]; glwError("Received X error!"); printf(" Error code : %d\n", event->error_code); printf(" Request code : %d\n", event->request_code); printf(" Minor code : %d\n", event->minor_code); XGetErrorText(display, event->error_code, buf, 80); printf(" Error text : %s\n", buf);#endif return 0;}static XVisualInfo *FindBestMainVisual(glWindow w,GLenum type){ int list[32], i; i = 0; list[i++] = GLX_LEVEL; list[i++] = 0; if (GLW_IS_DOUBLE(type)) { list[i++] = GLX_DOUBLEBUFFER; } if (GLW_IS_RGB(type)) { list[i++] = GLX_RGBA; list[i++] = GLX_RED_SIZE; list[i++] = 1; list[i++] = GLX_GREEN_SIZE; list[i++] = 1; list[i++] = GLX_BLUE_SIZE; list[i++] = 1; if (GLW_HAS_ALPHA(type)) { list[i++] = GLX_ALPHA_SIZE; list[i++] = 1; } if (GLW_HAS_ACCUM(type)) { list[i++] = GLX_ACCUM_RED_SIZE; list[i++] = 1; list[i++] = GLX_ACCUM_GREEN_SIZE; list[i++] = 1; list[i++] = GLX_ACCUM_BLUE_SIZE; list[i++] = 1; if (GLW_HAS_ALPHA(type)) { list[i++] = GLX_ACCUM_ALPHA_SIZE; list[i++] = 1; } } } else if (GLW_IS_INDEX(type)) { list[i++] = GLX_BUFFER_SIZE; list[i++] = 1; } if (GLW_HAS_DEPTH(type)) { list[i++] = GLX_DEPTH_SIZE; list[i++] = 1; } if (GLW_HAS_STENCIL(type)) { list[i++] = GLX_STENCIL_SIZE; list[i++] = 1; } list[i] = (int)None; return glXChooseVisual(w->display, w->screen, list);}static XVisualInfo *FindExactMainVisual(glWindow w, GLenum type){ int i, nvis, val, rval, gval, bval, aval; XVisualInfo *vis_list, *this_vis, *best_vis, sampleVis; int this_score, best_score; /* Get list of visuals for this screen */ sampleVis.screen = w->screen; vis_list = XGetVisualInfo(w->display, VisualScreenMask, &sampleVis, &nvis); /* * Loop through the visuals; find first one that matches the attr * specified in type */ best_score = -1; best_vis = NULL; for ( i = 0; i < nvis; i++ ) { this_vis = &vis_list[i]; if (debug) fprintf(stderr,"glwin: testing visual id 0x%x: ", (unsigned)this_vis->visualid); /* Visual must be supported by GLX */ if ( glXGetConfig(w->display, this_vis, GLX_USE_GL, &val) ) continue; if ( !val )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -