#include "WindowManager.h" void TransWindow(WindowRef w, Boolean vis) { WindowTransitionAction Action; if(vis) Action = kWindowShowTransitionAction; else Action = kWindowHideTransitionAction; TransitionWindow(w, kWindowZoomTransitionEffect, Action, NULL); } #pragma mark - static void SetOGLSetttings(Rect * bounds) { GLfloat ratio; glLineWidth(1.0); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.0f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); /* Wireframe mode */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); if ((bounds->right - bounds->left) > (bounds->bottom - bounds->top)) { ratio = ((GLfloat) (bounds->right - bounds->left) / (bounds->bottom - bounds->top)); glFrustum(-ratio, ratio, -1.0, 1.0, 1.5, 100.0); } else { ratio = ((GLfloat) (bounds->bottom - bounds->top) / (bounds->right - bounds->left)); glFrustum(-1.0, 1.0, -ratio, ratio, 1.5, 100.0); } glMatrixMode(GL_MODELVIEW); } void CreateWindowedContext(VWindow * w) { GLint Attribs[] = {AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 24, AGL_NO_RECOVERY, AGL_ACCELERATED, AGL_PIXEL_SIZE, 32, AGL_NONE}; AGLPixelFormat Format; Rect windowBounds; Format = aglChoosePixelFormat(NULL, 0, Attribs); w->Windowed = aglCreateContext(Format, w->FullScreen); if(w->FullScreen) { aglSetCurrentContext(NULL); aglSetDrawable(w->FullScreen, NULL); aglDestroyContext(w->FullScreen); w->FullScreen = NULL; } aglSetDrawable(w->Windowed, GetWindowPort(w->TheWindow)); aglSetCurrentContext(w->Windowed); aglDestroyPixelFormat(Format); GetWindowPortBounds(w->TheWindow, &windowBounds); SetOGLSetttings(&windowBounds); w->IsFullScreen = false; glClear(GL_COLOR_BUFFER_BIT); } void CreateFullScreenContext(VWindow * w) { GLint Attribs[] = {AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 24, AGL_FULLSCREEN, AGL_NO_RECOVERY, AGL_ACCELERATED, AGL_PIXEL_SIZE, 32, AGL_NONE}; AGLPixelFormat Format; GDHandle Display; Display = GetMainDevice(); Format = aglChoosePixelFormat(&Display, 1, Attribs); w->FullScreen = aglCreateContext(Format, w->Windowed); if(w->Windowed) { aglSetCurrentContext(NULL); aglSetDrawable(w->Windowed, NULL); aglDestroyContext(w->Windowed); w->Windowed = NULL; } aglSetFullScreen(w->FullScreen, (**Display).gdRect.right, (**Display).gdRect.bottom, 0, 0); aglSetCurrentContext(w->FullScreen); aglDestroyPixelFormat(Format); SetOGLSetttings(&(**Display).gdRect); w->IsFullScreen = true; glClear(GL_COLOR_BUFFER_BIT); } static void InitWindowContext(VWindow * w) { w->FullScreen = NULL; w->Windowed = NULL; CreateWindowedContext(w); } #pragma mark - static pascal OSStatus WindowWasClosed(EventHandlerCallRef nextEvent, EventRef theEvent, void * userData) { #pragma unused(nextEvent, theEvent) VWindow * w; w = (VWindow *) userData; if (w->quitOnClose) QuitApplicationEventLoop(); return eventNotHandledErr; } void InitWindow(VWindow * w) { GDHandle mainDevice; Point screenSize; Rect Bounds; EventTypeSpec eventType; mainDevice = GetMainDevice(); screenSize.h = (**mainDevice).gdRect.right; screenSize.v = (**mainDevice).gdRect.bottom; /* This will center the window onscreen. */ SetRect(&Bounds, ((screenSize.h / 2) - WINDOWH/2), ((screenSize.v / 2) - WINDOWV/2), ((screenSize.h / 2) + WINDOWH/2), ((screenSize.v / 2) + WINDOWV/2)); CreateNewWindow(kDocumentWindowClass, kWindowCloseBoxAttribute + kWindowCollapseBoxAttribute, &Bounds, &w->TheWindow); SetWindowTitleWithCFString(w->TheWindow, CFSTR("Vectorized")); InstallStandardEventHandler(GetWindowEventTarget(w->TheWindow)); eventType.eventClass = kEventClassWindow; eventType.eventKind = kEventWindowClosed; InstallWindowEventHandler(w->TheWindow, NewEventHandlerUPP(WindowWasClosed), 1, &eventType, w, NULL); TransWindow(w->TheWindow, true); InitWindowContext(w); w->quitOnClose = 0; } void CleanUpWindow(VWindow * w) { TransWindow(w->TheWindow, false); if(w->FullScreen) { aglSetCurrentContext(NULL); aglSetDrawable(w->FullScreen, NULL); aglDestroyContext(w->FullScreen); w->FullScreen = NULL; } if(w->Windowed) { aglSetCurrentContext(NULL); aglSetDrawable(w->Windowed, NULL); aglDestroyContext(w->Windowed); w->Windowed = NULL; } DisposeWindow(w->TheWindow); w->TheWindow = NULL; }