📄 glutwindow.cpp
字号:
/*********************************************************** * Copyright (C) 1997, Be Inc. Copyright (C) 1999, Jake Hamby. * * This program is freely distributable without licensing fees * and is provided without guarantee or warrantee expressed or * implied. This program is -not- in the public domain. * * * FILE: glutWindow.cpp * * DESCRIPTION: all the routines for dealing with GlutWindows ***********************************************************//*********************************************************** * Headers ***********************************************************/#include <GL/glut.h>#include <stdlib.h>#include "glutint.h"#include "glutState.h"#include "glutBlocker.h"/*********************************************************** * FUNCTION: getUnusedWindowSlot * * DESCRIPTION: helper function to get a new window slot ***********************************************************/static intgetUnusedWindowSlot(){ int i; /* Look for allocated, unused slot. */ for (i = 0; i < gState.windowListSize; i++) { if (!gState.windowList[i]) { return i; } } /* Allocate a new slot. */ gState.windowListSize++; gState.windowList = (GlutWindow **) realloc(gState.windowList, gState.windowListSize * sizeof(GlutWindow *)); if (!gState.windowList) __glutFatalError("out of memory."); gState.windowList[gState.windowListSize - 1] = NULL; return gState.windowListSize - 1;}/*********************************************************** * FUNCTION: __glutDefaultDisplay * __glutDefaultReshape * * DESCRIPTION: default display and reshape functions ***********************************************************/static void__glutDefaultDisplay(void){ /* XXX Remove the warning after GLUT 3.0. */ __glutWarning("The following is a new check for GLUT 3.0; update your code."); __glutFatalError( "redisplay needed for window %d, but no display callback.", gState.currentWindow->num + 1);}void__glutDefaultReshape(int width, int height){ /* Adjust the viewport of the window */ glViewport(0, 0, (GLsizei) width, (GLsizei) height);}/*********************************************************** * CLASS: GlutWindow * * FUNCTION: (constructor) * * DESCRIPTION: creates a new GLUT window * note: subwindows don't resize, but top-level windows * follow all sides ***********************************************************/GlutWindow::GlutWindow(GlutWindow *nparent, char *name, int x, int y, int width, int height, ulong options) : BGLView( (nparent ? BRect(x,y,x+width-1,y+height-1) : BRect(0,0,width-1,height-1)), name, (nparent ? B_FOLLOW_NONE : B_FOLLOW_ALL_SIDES), B_WILL_DRAW|B_FRAME_EVENTS|B_FULL_UPDATE_ON_RESIZE|B_PULSE_NEEDED, options){ // add myself to window list num = getUnusedWindowSlot(); gState.windowList[num] = this; // set up parent/children relationships parent = nparent; if (parent) { siblings = parent->children; parent->children = this; } else { siblings = 0; } children = 0; // initialize variables cursor = GLUT_CURSOR_INHERIT; // default cursor for (int i = 0; i < GLUT_MAX_MENUS; i++) { menu[i] = 0; } m_width = width; m_height = height; m_buttons = 0; // clear callbacks display = __glutDefaultDisplay; reshape = __glutDefaultReshape; mouse = 0; motion = 0; passive = 0; entry = 0; keyboard = 0; visibility = 0; special = 0; windowStatus = 0; // clear event counters anyevents = 1; displayEvent = 1; // get a reshape and a display event right away reshapeEvent = 1; mouseEvent = 0; motionEvent = 0; passiveEvent = 0; entryEvent = 0; keybEvent = 0; windowStatusEvent = 0; // DirectConnected() will report change in visState = -1; // visibility specialEvent = 0; statusEvent = 0; menuEvent = 0; visible = true; gBlock.QuickNewEvent(); // if i'm a subwindow, add me to my parent view if (parent) { parent->Window()->Lock(); parent->AddChild(this); parent->Window()->Unlock(); } else { // if I'm a top-level window, create my BWindow GlutBWindow *mybwindow = new GlutBWindow( BRect(x,y,x+width-1,y+height-1), name); mybwindow->AddChild(this); mybwindow->bgl = this; mybwindow->Show(); } // give me the keyboard focus (focus follows mouse, X style, as // implemented in GlutWindow::MouseMoved()) Window()->Lock(); MakeFocus(); Window()->Unlock(); // make myself the default window __glutSetWindow(this);}/*********************************************************** * FUNCTION: glutCreateWindow (4.1) * * DESCRIPTION: creates a new GLUT window ***********************************************************/int glutCreateWindow(const char *name) { if (!be_app) __glutInit(); ulong options; if (!__glutConvertDisplayMode(&options)) { __glutWarning("visual with necessary capabilities not found."); } // if X or Y is negative, then start at a reasonable position bool defaultxy = (gState.initX < 0) || (gState.initY < 0); GlutWindow *window = new GlutWindow(0, const_cast<char*>(name), (defaultxy ? 50 : gState.initX), (defaultxy ? 50 : gState.initY), gState.initWidth, gState.initHeight, options); return window->num + 1;}/*********************************************************** * FUNCTION: glutCreateSubWindow (4.2) * * DESCRIPTION: creates a new GLUT subwindow * Note: a subwindow is a GlutWindow (which is actually * a BGLView) without its own BWindow ***********************************************************/int glutCreateSubWindow(int win, int x, int y, int width, int height) { ulong options; if (!__glutConvertDisplayMode(&options)) { __glutFatalError("visual with necessary capabilities not found."); } GlutWindow *window = new GlutWindow(gState.windowList[win-1], "child", x, y, width, height, options); return window->num + 1;}/*********************************************************** * FUNCTION: __glutSetWindow * * DESCRIPTION: set the current window (utility function) ***********************************************************/void__glutSetWindow(GlutWindow * window){ if (gState.currentWindow) gState.currentWindow->UnlockGL(); gState.currentWindow = window; gState.currentWindow->LockGL();}/*********************************************************** * FUNCTION: glutSetWindow (4.3) * glutGetWindow * * DESCRIPTION: set and get the current window ***********************************************************/void glutSetWindow(int win) { GlutWindow *window; if (win < 1 || win > gState.windowListSize) { __glutWarning("glutSetWindow attempted on bogus window."); return; } window = gState.windowList[win - 1]; if (!window) { __glutWarning("glutSetWindow attempted on bogus window."); return; } __glutSetWindow(window);}int glutGetWindow() { if (gState.currentWindow) { return gState.currentWindow->num + 1; } else { return 0; }}/*********************************************************** * FUNCTION: __glutDestroyWindow * * DESCRIPTION: recursively set entries to 0 ***********************************************************/static void__glutDestroyWindow(GlutWindow *window, GlutWindow *initialWindow) { // first, find all children recursively and set their entries to 0 GlutWindow *cur = window->children; while (cur) { GlutWindow *siblings = cur->siblings; __glutDestroyWindow(cur, initialWindow); cur = siblings; } /* Remove from parent's children list (only necessary for non-initial windows and subwindows!). */ GlutWindow *parent = window->parent; if (parent && parent == initialWindow->parent) { GlutWindow **prev = &parent->children; cur = parent->children; while (cur) { if (cur == window) { *prev = cur->siblings; break; } prev = &(cur->siblings); cur = cur->siblings; } } // finally, check if we are the current window, and set to 0 if (gState.currentWindow == window) { gState.currentWindow = 0; } gState.windowList[window->num] = 0;}/*********************************************************** * FUNCTION: glutDestroyWindow (4.4) * * DESCRIPTION: destroy window and all its children ***********************************************************/void glutDestroyWindow(int win) { // can't destroy a window if another window has the GL context if (gState.currentWindow) gState.currentWindow->UnlockGL(); // lock the window GlutWindow *window = gState.windowList[win-1]; BWindow *bwindow = window->Window(); bwindow->Lock(); // if win is the current window, set current window to 0 if (gState.currentWindow == window) { gState.currentWindow = 0; } // recursively set child entries to 0 __glutDestroyWindow(window, window); // try flushing OpenGL window->LockGL(); glFlush();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -