-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing13.html
executable file
·1314 lines (1164 loc) · 47.4 KB
/
listing13.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>OpenGLMovieQT - /Carbon SetupGL/Carbon_SetupGL_Test.c</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/LegacyTechnologies/index.html">Legacy Documents</a> > <a href="../../samplecode/LegacyTechnologies/idxQuickTime-date.html">QuickTime</a> > <A HREF="javascript:location.replace('index.html');">OpenGLMovieQT</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<div style="width:100%; position:fixed;"><div align="center" id="watermark" style="position: relative; margin-left:auto; margin-right:auto; z-index:20; width:500px;"><div class="legacybox"><h1>Legacy Document<span class=closebutton><a href="javascript:closeWatermark()"><img src="../../images/closebutton.png" width="14" height="14" border="0" alt="close button"></a></span></h1>
<p><strong>Important: </strong>This document is part of the Legacy section of the ADC Reference Library. This information should not be used for new development.</p>
<div class="reflibtopic">
<p>Current information on this Reference Library topic can be found here:</p>
<ul>
<li><a href="http://developer.apple.com/referencelibrary/QuickTime/idxGraphicsImaging-date.html" target="_blank">QuickTime > Graphics & Imaging</a></li>
</ul>
</div>
</div></div></div>
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">OpenGLMovieQT</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Carbon SetupGL/Carbon_SetupGL_Test.c</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/Carbon Include.h</option>
<option value="listing2.html">/Carbon Resource.r</option>
<option value="listing3.html">/Carbon SetupGL/aglString/aglString.c</option>
<option value="listing4.html">/Carbon SetupGL/aglString/aglString.h</option>
<option value="listing5.html">/Carbon SetupGL/Carbon_Error_Handler.c</option>
<option value="listing6.html">/Carbon SetupGL/Carbon_Error_Handler.h</option>
<option value="listing7.html">/Carbon SetupGL/Carbon_Include.h</option>
<option value="listing8.html">/Carbon SetupGL/Carbon_Resource.r</option>
<option value="listing9.html">/Carbon SetupGL/Carbon_SetupDSp.c</option>
<option value="listing10.html">/Carbon SetupGL/Carbon_SetupDSp.h</option>
<option value="listing11.html">/Carbon SetupGL/Carbon_SetupGL.c</option>
<option value="listing12.html">/Carbon SetupGL/Carbon_SetupGL.h</option>
<option value="listing13.html">/Carbon SetupGL/Carbon_SetupGL_Test.c</option>
<option value="listing14.html">/OpenGL Movie Controls.c</option>
<option value="listing15.html">/OpenGL Movie Main.c</option>
<option value="listing16.html">/OpenGL Movie.c</option>
<option value="listing17.html">/OpenGLMovie.h</option></select>
</p>
</form>
<p><strong><a href="OpenGLMovieQT.zip">Download Sample</a></strong> (“OpenGLMovieQT.zip”, 139.4K)<BR>
<strong><a href="OpenGLMovieQT.dmg">Download Sample</a></strong> (“OpenGLMovieQT.dmg”, 197.4K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">/* File: SetupGL Main Windowed.c Contains: An example of the use of the SeupGL utility code for windowed applications. Written by: Geoff Stahl Copyright: 2000 Apple Computer, Inc., All Rights Reserved Change History (most recent first): Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the Apple Software, with or without modifications, in source and/or binary forms; provided that if you redistribute the Apple Software in its entirety and without modifications, you must retain this notice and the following text and disclaimers in all such redistributions of the Apple Software. Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to endorse or promote products derived from the Apple Software without specific prior written permission from Apple. Except as expressly stated in this notice, no other rights or licenses, express or implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. <10> 4/20/01 ggs add menu command and code to demonstrate shared texturing <9> 9/3/00 ggs Multi-window handling <8> 3/22/00 ggs Carbon take one <7> 2/24/00 ggs Windowed mode support <6> 1/26/00 ggs Add preflight support, handle failure conditions throughout app better <5> 1/24/00 ggs fixed Apple menu selection, fixed aglfont texture row length problem, fixed suspend/resume, fixed update <4> 12/18/99 ggs Fix headers <3> 12/18/99 ggs Added window/fullscreen toggle support and error window <2> 11/28/99 ggs Standard toolbox application <1> 11/28/99 ggs Initial Add <6> 9/14/99 GGS Corrected buffer rect handling and cleaned up code <5> 7/14/99 GGS Fixed multi-monitor window centering <4> 7/13/99 GGS Add work around for over zealous checking in single buffer DSp context attributes <3> 7/5/99 GGS Now correctly handle multi-monitor (DSp front buffer for single device; Window on top of context for multiple devices) <2> 5/28/99 GGS Added better multi-monitor support, clean code, corrected blanking bug, added timing, correct pixel formats <1> ? ? Initial build*/// system includes ----------------------------------------------------------#ifdef __APPLE_CC__ #include "Carbon_Include.h" #include <Carbon/Carbon.h>#else #include <Devices.h> #include <Dialogs.h> #include <DriverServices.h> #include <Events.h> #include <Gestalt.h> #include <LowMem.h> #include <PictUtils.h> #include <TextEdit.h> #include <ToolUtils.h> #include <QDOffscreen.h> #include <Windows.h>#endif#include <math.h>#include <stdio.h>#include <string.h>#ifdef __APPLE_CC__ #include <OpenGL/gl.h>#else #include <gl.h>#endif// project includes ---------------------------------------------------------#include "aglString.h"#include "Carbon_SetupGL.h"#include "Carbon_Error_Handler.h"// prototypes ---------------------------------------------------------------static void IntializeWindowList (void); // zeroize list's WindowPtrs (does not dispose)static long FindFirstFreeWindowIndex (void); // return index of first free WindowPtrstatic void DisposeGLWindow (WindowPtr pWindow); // Dispose a single window and it's GL contextstatic void DisposeGLWindowList (void); // Dispose entire window list and GL contextsvoid LoadTextureRes (short resID, GLbyte ** ppBuffer, short * pWidth, short * pHeight);void InitToolbox(void);void CreateGLWindow (void);Boolean SetUp (void);void DoMenu (SInt32 menuResult);void DoKey (SInt8 theKey, SInt8 theCode);void DoUpdate (WindowPtr pWindow);void DoEvent (void);void CleanUp (void);void drawGL(WindowPtr pWindow);#if TARGET_API_MAC_CARBONpascal void IdleTimer (EventLoopTimerRef inTimer, void* userData);EventLoopTimerUPP GetTimerUPP (void);#endif// statics/globals (internal only) ------------------------------------------struct structWindowInfo{ structGLWindowInfo glInfo; AGLContext aglContext; GLuint fontList; char strContext [256];};typedef struct structWindowInfo structWindowInfo;typedef struct structWindowInfo * structWindowInfoPtr;// Menu defsenum { kMaxWindows = 200, kMenuApple = 128, kMenuFile = 129, kAppleAbout = 1, kFileNew = 1, kFileClose, kFileBufferRect, kFileTexture, kFileFlipTexture, kFileQuit};#if TARGET_API_MAC_CARBONenum { kForegroundSleep = 10, kBackgroundSleep = 10000};EventLoopTimerRef gTimer = NULL;#elseenum { kForegroundSleep = 0, kBackgroundSleep = 100};#endifSInt32 gSleepTime = kForegroundSleep;Boolean gDone = false, gfFrontProcess = true;Rect rectWin, rectInitWin = {50, 5, 150, 105}; // initial window rectangleWindowPtr gpWindowList [kMaxWindows];char gStringWindow [kMaxWindows][32];long gFrameWindow [kMaxWindows];AbsoluteTime gTimeWindow [kMaxWindows];float gRotation [kMaxWindows];Boolean gBufferRect = false;Boolean gTexture = false;GLuint nameTexture[2] = { 0, 0 };short gFirstTexture = 0;Boolean gMenu = false;AGLContext gTextureSharingContext [2] = { NULL, NULL };// functions (internal/private) ---------------------------------------------static void IntializeWindowList (void) // zeroize list's WindowPtrs (does not dispose){ long i; for (i = 0; i < kMaxWindows; i++) gpWindowList[i] = NULL;}// --------------------------------------------------------------------------static long FindFirstFreeWindowIndex (void) // return index of first free WindowPtr{ long i = 0; while ((gpWindowList[i] != NULL) && (kMaxWindows > i)) i++; if (kMaxWindows == i) return -1; else return i;}// --------------------------------------------------------------------------static void DisposeGLWindow (WindowPtr pWindow) // Dispose a single window and it's GL context{ if (pWindow) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (pWindow); SetWRefCon (pWindow, NULL); if (pWindowInfo) { DeleteFontGL (pWindowInfo->fontList); // must clean up failure to do so will result in an Unmapped Memory Exception if (gTextureSharingContext[0] == pWindowInfo->aglContext) // insure we do not try to share textures from invalid context gTextureSharingContext[0] = NULL; if (gTextureSharingContext[1] == pWindowInfo->aglContext) // insure we do not try to share textures from invalid context gTextureSharingContext[1] = NULL; DestroyGLFromWindow (&pWindowInfo->aglContext, &pWindowInfo->glInfo); DisposePtr ((Ptr) pWindowInfo); } DisposeWindow (pWindow); }}// --------------------------------------------------------------------------static void DisposeGLWindowList (void) // Dispose entire window list and GL contexts{ long i; for (i = 0; i < kMaxWindows; i++) { DisposeGLWindow (gpWindowList[i]); gpWindowList[i] = NULL; }}// --------------------------------------------------------------------------#pragma mark -void LoadTextureRes (short resID, GLbyte ** ppBuffer, short * pWidth, short * pHeight){ PicHandle hPic = NULL; PictInfo thePictInfo; GWorldPtr pGW = NULL; long stride = 0; char * baseaddr = NULL; short i, j; GDHandle saveDevice; CGrafPtr saveWorld; GetGWorld (&saveWorld, &saveDevice); hPic = GetPicture (resID); // get background picture if (hPic) { GetPictInfo (hPic, &thePictInfo, 0, 0, 0, 0); NewGWorld (&pGW, 32, &(thePictInfo.sourceRect), NULL, NULL, 0 ); if (pGW && LockPixels (GetGWorldPixMap (pGW))) { SetGWorld (pGW, NULL); DrawPicture (hPic, &(thePictInfo.sourceRect)); SetGWorld (saveWorld, saveDevice); //ensure gdevice is restored *pWidth = thePictInfo.sourceRect.right - thePictInfo.sourceRect.left; *pHeight = thePictInfo.sourceRect.bottom - thePictInfo.sourceRect.top; stride = GetPixRowBytes (GetGWorldPixMap (pGW)); baseaddr = GetPixBaseAddr (GetGWorldPixMap (pGW)); *ppBuffer = (signed char *)NewPtr (3 * *pWidth * *pHeight); if (*ppBuffer) { for (i = 0; i < *pHeight; i++) for (j = 0; j < *pWidth; j++) { *(*ppBuffer + (i * *pWidth + j) * 3 + 0) = *(baseaddr + (i * stride) + j * 4 + 1); *(*ppBuffer + (i * *pWidth + j) * 3 + 1) = *(baseaddr + (i * stride) + j * 4 + 2); *(*ppBuffer + (i * *pWidth + j) * 3 + 2) = *(baseaddr + (i * stride) + j * 4 + 3); } } } } DisposeGWorld (pGW); ReleaseResource ((Handle) hPic);}// --------------------------------------------------------------------------#pragma mark -static pascal OSErr QuitAppleEventHandler( const AppleEvent *appleEvt, AppleEvent* reply, SInt32 refcon ){ #pragma unused (appleEvt, reply, refcon) gDone = true; return false;}// --------------------------------------------------------------------------void InitToolbox(void){ OSErr err; long response; MenuHandle menu; #if !TARGET_API_MAC_CARBON MaxApplZone (); InitGraf((Ptr) &qd.thePort); InitFonts(); InitWindows(); InitMenus(); TEInit(); InitDialogs(nil);#endif InitCursor(); #if !TARGET_API_MAC_CARBON qd.randSeed = TickCount(); // ? Carbon Equivalent ?#endif // Init Menus menu = NewMenu (kMenuApple, "\p\024"); // new apple menu InsertMenu (menu, 0); // add menu to end#if !TARGET_API_MAC_CARBON AppendResMenu(menu, 'DRVR');#endif menu = NewMenu (kMenuFile, "\pFile"); // new menu InsertMenu (menu, 0); // add menu to end // insert application menus here AppendMenu (menu, "\pNew Window/N"); AppendMenu (menu, "\pClose Window/W"); AppendMenu (menu, "\pToggle Buffer Rect/B"); AppendMenu (menu, "\pToggle Texturing/T"); AppendMenu (menu, "\pFlip Textures/F"); // add quit if not under Mac OS X err = Gestalt (gestaltMenuMgrAttr, &response); if ((err == noErr) && !(response & gestaltMenuMgrAquaLayoutMask)) AppendMenu (menu, "\pQuit/Q"); // add quit DrawMenuBar(); { err = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(QuitAppleEventHandler), 0, false ); if (err != noErr) ExitToShell(); } rectWin = rectInitWin; IntializeWindowList (); { // initialize frame timing long i; for (i = 0; i < kMaxWindows; i++) { gFrameWindow [i] = 0; gTimeWindow [i].hi = 0; gTimeWindow [i].lo = 0; gRotation [i] = 0.0; } }}// --------------------------------------------------------------------------void CreateGLWindow (void){ short i; long indexWin; short fNum; indexWin = FindFirstFreeWindowIndex (); if (indexWin >= 0) // valid index { GDHandle hGDWindow; Rect rectSect; structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) NewPtrClear (sizeof (structWindowInfo));#if TARGET_API_MAC_CARBON if (noErr != CreateNewWindow (kDocumentWindowClass, kWindowStandardDocumentAttributes, &rectWin, &(gpWindowList [indexWin]))) { DebugStr ("\pCreate window error"); return; }#else gpWindowList [indexWin] = (WindowPtr) NewCWindow (NULL, &rectWin, "\pWindow", false, kWindowFullZoomGrowDocumentProc, (WindowPtr)-1, true, 0);#endif // offset next window rectangle OffsetRect (&rectWin, 120, 30); FindGDHandleFromRect (&rectWin, &hGDWindow); SectRect (&rectWin, &(**hGDWindow).gdRect, &rectSect); if (!EqualRect (&rectWin, &rectSect)) { OffsetRect (&rectInitWin, 30, 7); FindGDHandleFromRect (&rectInitWin, &hGDWindow); SectRect (&rectInitWin, &(**hGDWindow).gdRect, &rectSect); if (!EqualRect (&rectInitWin, &rectSect)) SetRect (&rectInitWin, 5, 50, 505, 450); rectWin = rectInitWin; } { // set title to indicate index number Str255 pstrTitle = "\p"; NumToString (indexWin, pstrTitle); SetWTitle (gpWindowList [indexWin], pstrTitle); } SetWRefCon (gpWindowList [indexWin], (long) pWindowInfo); ShowWindow (gpWindowList [indexWin]); SetPortWindowPort (gpWindowList [indexWin]); pWindowInfo->glInfo.fAcceleratedMust = false; // must renderer be accelerated? pWindowInfo->glInfo.VRAM = 0 * 1048576; // minimum VRAM (if not zero this is always a required minimum) pWindowInfo->glInfo.textureRAM = 0 * 1048576; // minimum texture RAM (if not zero this is always a required minimum) if (!CheckMacOSX ()) // this is false on Mac OS 9 since Mac OS 9 does not support dragging conttexts with shared txtures between to different vendor's renderers. pWindowInfo->glInfo.fDraggable = false; // should a pixel format that supports all monitors be chosen? else pWindowInfo->glInfo.fDraggable = true; // should a pixel format that supports all monitors be chosen? pWindowInfo->glInfo.fmt = 0; // output pixel format i = 0; pWindowInfo->glInfo.aglAttributes [i++] = AGL_RGBA; pWindowInfo->glInfo.aglAttributes [i++] = AGL_DOUBLEBUFFER; pWindowInfo->glInfo.aglAttributes [i++] = AGL_ACCELERATED; pWindowInfo->glInfo.aglAttributes [i++] = AGL_NO_RECOVERY; pWindowInfo->glInfo.aglAttributes [i++] = AGL_DEPTH_SIZE; pWindowInfo->glInfo.aglAttributes [i++] = 16; pWindowInfo->glInfo.aglAttributes [i++] = AGL_NONE; BuildGLFromWindow (gpWindowList [indexWin], &pWindowInfo->aglContext, &pWindowInfo->glInfo, gTextureSharingContext [indexWin & 0x01]); if (!pWindowInfo->aglContext) { DestroyGLFromWindow (&pWindowInfo->aglContext, &pWindowInfo->glInfo); sprintf (pWindowInfo->strContext, "No context"); } else { Rect rectPort; #if TARGET_API_MAC_CARBON GetWindowPortBounds (gpWindowList [indexWin], &rectPort);#else rectPort = gpWindowList [indexWin]->portRect;#endif // TARGET_API_MAC_CARBON aglSetCurrentContext (pWindowInfo->aglContext); aglReportError (); aglUpdateContext (pWindowInfo->aglContext); aglReportError (); // Set Texture mapping parameters glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); glClearColor(0.15f, 0.15f, 0.15f, 1.0f); // Clear color buffer to dark grey glClear (GL_COLOR_BUFFER_BIT); glReportError (); aglSwapBuffers (pWindowInfo->aglContext); aglReportError (); if (gBufferRect) { GLint bufferRect[4] = {0, 0, 0, 0}; bufferRect [0] = 10; bufferRect [1] = 10; bufferRect [2] = rectPort.right - rectPort.left - 20; bufferRect [3] = rectPort.bottom - rectPort.top - 20; aglSetInteger (pWindowInfo->aglContext, AGL_BUFFER_RECT, bufferRect); aglReportError (); aglEnable (pWindowInfo->aglContext, AGL_BUFFER_RECT); aglReportError (); glViewport (0, 0, bufferRect [2], bufferRect [3]); glReportError (); sprintf (pWindowInfo->strContext, "%ld x %ld", bufferRect [2], bufferRect [3]); } else { aglDisable (pWindowInfo->aglContext, AGL_BUFFER_RECT); aglReportError (); glViewport (0, 0, rectPort.right - rectPort.left, rectPort.bottom - rectPort.top); glReportError (); sprintf (pWindowInfo->strContext, "%d x %d", rectPort.right - rectPort.left, rectPort.bottom - rectPort.top); } GetFNum("\pMonaco", &fNum); // build font pWindowInfo->fontList = BuildFontGL (pWindowInfo->aglContext, fNum, normal, 9); aglUpdateContext (pWindowInfo->aglContext); aglReportError (); if (NULL == gTextureSharingContext [indexWin & 0x01] ) gTextureSharingContext [indexWin & 0x01] = pWindowInfo->aglContext; } }}// --------------------------------------------------------------------------Boolean SetUp (void){ InitToolbox (); if (PreflightGL (false)) CreateGLWindow ();#if TARGET_API_MAC_CARBON if (!gTimer) InstallEventLoopTimer (GetCurrentEventLoop(), 0, 0.000001, GetTimerUPP (), 0, &gTimer);#endif return true;}// --------------------------------------------------------------------------void DoMenu (SInt32 menuResult){#if !TARGET_API_MAC_CARBON Str255 daName;#endif SInt16 theMenu; SInt16 theItem; MenuRef theMenuHandle; theMenu = HiWord(menuResult); theItem = LoWord(menuResult); theMenuHandle = GetMenuHandle(theMenu); switch (theMenu) { case kMenuApple: switch (theItem) { case kAppleAbout: break; default:#if !TARGET_API_MAC_CARBON GetMenuItemText (theMenuHandle, theItem, daName); OpenDeskAcc(daName);#endif break; } break; case kMenuFile: switch (theItem) { case kFileNew: CreateGLWindow (); break; case kFileClose: { WindowPtr whichWindow = FrontWindow (); long i = 0; while ((whichWindow != gpWindowList[i]) && (kMaxWindows > i)) {i++;} if (kMaxWindows != i) { DisposeGLWindow (gpWindowList[i]); gpWindowList[i] = NULL; gFrameWindow [i] = 0; gTimeWindow [i].hi = 0; gTimeWindow [i].lo = 0; gRotation [i] = 0.0; } } break; case kFileBufferRect: { long i = 0; gBufferRect = 1 - gBufferRect; for (i = 0; kMaxWindows > i; i++) { if (gpWindowList[i] != NULL) { Rect rectPort; structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [i]); #if TARGET_API_MAC_CARBON GetWindowPortBounds (gpWindowList [i], &rectPort);#else rectPort = gpWindowList [i]->portRect;#endif // TARGET_API_MAC_CARBON aglSetCurrentContext(pWindowInfo->aglContext); if (gBufferRect) { GLint bufferRect[4] = {0, 0, 0, 0}; bufferRect [0] = 10; bufferRect [1] = 10; bufferRect [2] = rectPort.right - rectPort.left - 20; bufferRect [3] = rectPort.bottom - rectPort.top - 20; aglSetInteger (pWindowInfo->aglContext, AGL_BUFFER_RECT, bufferRect); aglEnable (pWindowInfo->aglContext, AGL_BUFFER_RECT); glViewport (0, 0, bufferRect [2], bufferRect [3]); sprintf (pWindowInfo->strContext, "%ld x %ld", bufferRect [2], bufferRect [3]); } else { aglDisable (pWindowInfo->aglContext, AGL_BUFFER_RECT); glViewport (0, 0, rectPort.right - rectPort.left, rectPort.bottom - rectPort.top); sprintf (pWindowInfo->strContext, "%d x %d", rectPort.right - rectPort.left, rectPort.bottom - rectPort.top); } aglUpdateContext (pWindowInfo->aglContext); } } } break; case kFileTexture: // enable shared texturing. For mac OS X 10.0.1 and earlier this requires creating all contexts first { long i = 0; GLbyte * pBuffer = NULL; short width = 0, height = 0; gTexture = 1 - gTexture; if (gTexture) { if (gpWindowList[0] != NULL) // load the texture for the first window { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [0]); aglSetCurrentContext(pWindowInfo->aglContext); glEnable (GL_TEXTURE_2D); if (nameTexture [0]) glDeleteTextures (2, nameTexture); glGenTextures (2, nameTexture); // load first texture glBindTexture(GL_TEXTURE_2D, nameTexture[0]); LoadTextureRes (1000 + gFirstTexture, &pBuffer, &width, &height); glTexImage2D (GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); DisposePtr ((Ptr)pBuffer); } if (gpWindowList[1] != NULL) // load the texture for the first window { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [1]); aglSetCurrentContext (pWindowInfo->aglContext); glEnable (GL_TEXTURE_2D); // load second texture glBindTexture(GL_TEXTURE_2D, nameTexture [1]); LoadTextureRes (1000 + 1 - gFirstTexture, &pBuffer, &width, &height); glTexImage2D (GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); DisposePtr ((Ptr)pBuffer); } } for (i = 0; kMaxWindows > i; i++) // enable texturing for all windows but do not load texture again { if (gpWindowList[i] != NULL) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [i]); aglSetCurrentContext(pWindowInfo->aglContext); if (gTexture) { glEnable (GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, nameTexture[i & 0x01]); } else glDisable (GL_TEXTURE_2D); } } } break; case kFileFlipTexture: // enable shared texturing. For mac OS X 10.0.1 and earlier this requires creating all contexts first { long i = 0; GLbyte * pBuffer = NULL; short width = 0, height = 0; gFirstTexture = 1 - gFirstTexture; if (gTexture) { if (gpWindowList[0] != NULL) // load the texture for the first window { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [0]); aglSetCurrentContext(pWindowInfo->aglContext); glEnable (GL_TEXTURE_2D); // load first texture glBindTexture(GL_TEXTURE_2D, nameTexture[0]); LoadTextureRes (1000 + gFirstTexture, &pBuffer, &width, &height); glTexImage2D (GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); DisposePtr ((Ptr)pBuffer); } if (gpWindowList[1] != NULL) // load the texture for the first window { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [1]); aglSetCurrentContext (pWindowInfo->aglContext); glEnable (GL_TEXTURE_2D); // load second texture glBindTexture(GL_TEXTURE_2D, nameTexture [1]); LoadTextureRes (1000 + 1 - gFirstTexture, &pBuffer, &width, &height); glTexImage2D (GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pBuffer); DisposePtr ((Ptr)pBuffer); } } for (i = 0; kMaxWindows > i; i++) // enable texturing for all windows but do not load texture again { if (gpWindowList[i] != NULL) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (gpWindowList [i]); aglSetCurrentContext(pWindowInfo->aglContext); if (gTexture) { glEnable (GL_TEXTURE_2D); glBindTexture (GL_TEXTURE_2D, nameTexture[i & 0x01]); } else glDisable (GL_TEXTURE_2D); } } } break; case kFileQuit: gDone = true; break; } break; } HiliteMenu(0); DrawMenuBar();}// -------------------------------------------------------------------------- void DoKey (SInt8 theKey, SInt8 theCode){ #pragma unused (theCode, theKey) // do nothing}// --------------------------------------------------------------------------#if TARGET_API_MAC_CARBONpascal void IdleTimer (EventLoopTimerRef inTimer, void* userData){ #pragma unused (inTimer, userData) long i; if (!gMenu) for (i = 0; i < kMaxWindows; i++) if (gpWindowList[i]) DoUpdate (gpWindowList[i]);}// --------------------------------------------------------------------------EventLoopTimerUPP GetTimerUPP (void){ static EventLoopTimerUPP sTimerUPP = NULL; if (sTimerUPP == NULL) sTimerUPP = NewEventLoopTimerUPP (IdleTimer); return sTimerUPP;}#endif// --------------------------------------------------------------------------void DoUpdate (WindowPtr pWindow){ if (pWindow) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (pWindow); if (pWindowInfo) { aglSetCurrentContext (pWindowInfo->aglContext); if (gMenu) aglUpdateContext (pWindowInfo->aglContext); drawGL (pWindow); } }}// --------------------------------------------------------------------------void DoEvent (void){ EventRecord theEvent; Rect rectTemp; WindowRef whichWindow; SInt16 whatPart; if (WaitNextEvent(everyEvent, &theEvent, gSleepTime, NULL)) { { switch (theEvent.what) { case mouseDown: whatPart = FindWindow (theEvent.where, &whichWindow); switch (whatPart) { case inContent: if (whichWindow != FrontWindow()) SelectWindow (whichWindow); break; case inMenuBar: { SInt32 menuResult; gMenu = true; menuResult = MenuSelect (theEvent.where); gMenu = false; if (HiWord (menuResult) != 0) DoMenu (menuResult); } break; case inDrag: {#if !TARGET_API_MAC_CARBON DragWindow (whichWindow, theEvent.where, &(**LMGetGrayRgn()).rgnBBox);#else GetRegionBounds(GetGrayRgn(), &rectTemp); SetPort((GrafPtr) GetWindowPort(whichWindow)); DragWindow(whichWindow, theEvent.where, &rectTemp);#endif // !TARGET_API_MAC_CARBON aglUpdateContext (((structWindowInfoPtr) GetWRefCon (whichWindow))->aglContext); } break; case inGrow: { long grow; SetRect (&rectTemp, 100, 100, 20000, 20000); grow = GrowWindow (whichWindow, theEvent.where, &rectTemp); if (grow) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (whichWindow); SizeWindow (whichWindow, grow & 0x0000FFFF, grow >> 16, true); // do content stuff here SetPort ((GrafPtr) GetWindowPort(whichWindow));#if !TARGET_API_MAC_CARBON InvalRect (&whichWindow->portRect); // redraw all#else InvalWindowRect (whichWindow, GetWindowPortBounds (whichWindow, &rectTemp));#endif // !TARGET_API_MAC_CARBON if ((pWindowInfo) && (pWindowInfo->aglContext)) { if (gBufferRect) { GLint bufferRect[4] = {0, 0, 0, 0};#if TARGET_API_MAC_CARBON GetWindowPortBounds (whichWindow, &rectTemp);#else rectTemp = whichWindow->portRect;#endif // TARGET_API_MAC_CARBON bufferRect [0] = 10; bufferRect [1] = 10; bufferRect [2] = rectTemp.right - rectTemp.left - 20; bufferRect [3] = rectTemp.bottom - rectTemp.top - 20; aglSetCurrentContext (pWindowInfo->aglContext); // ensure the context we are working with is current aglSetInteger (pWindowInfo->aglContext, AGL_BUFFER_RECT, bufferRect); aglUpdateContext (pWindowInfo->aglContext); glViewport (0, 0, bufferRect [2], bufferRect [3]); sprintf (pWindowInfo->strContext, "%ld x %ld", bufferRect [2], bufferRect [3]); } else {#if TARGET_API_MAC_CARBON GetWindowPortBounds (whichWindow, &rectTemp);#else rectTemp = whichWindow->portRect;#endif // TARGET_API_MAC_CARBON aglSetCurrentContext (pWindowInfo->aglContext); // ensure the context we are working with is current aglUpdateContext (pWindowInfo->aglContext); glViewport (0, 0, rectTemp.right - rectTemp.left, rectTemp.bottom - rectTemp.top); sprintf (pWindowInfo->strContext, "%d x %d", rectTemp.right - rectTemp.left, rectTemp.bottom - rectTemp.top); } } } } break; case inGoAway: { long i = 0; while ((whichWindow != gpWindowList[i]) && (kMaxWindows > i)) {i++;} if (kMaxWindows == i) DisposeWindow (whichWindow); else { DisposeGLWindow (gpWindowList[i]); gpWindowList[i] = NULL; gFrameWindow [i] = 0; gTimeWindow [i].hi = 0; gTimeWindow [i].lo = 0; gRotation [i] = 0.0; } } break; case inZoomIn: case inZoomOut: { if (TrackBox (whichWindow, theEvent.where, whatPart)) { structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (whichWindow); SetPort ((GrafPtr) GetWindowPort (whichWindow)); // window must be current port#if TARGET_API_MAC_CARBON EraseRect (GetWindowPortBounds (whichWindow, &rectTemp)); // inval/erase because of ZoomWindow bug ZoomWindow (whichWindow, whatPart, true); InvalWindowRect (whichWindow, GetWindowPortBounds (whichWindow, &rectTemp)); #else EraseRect (&whichWindow->portRect); // inval/erase because of ZoomWindow bug ZoomWindow (whichWindow, whatPart, true); InvalRect (&whichWindow->portRect); #endif // TARGET_API_MAC_CARBON if ((pWindowInfo) && (pWindowInfo->aglContext)) { if (gBufferRect) { GLint bufferRect[4] = {0, 0, 0, 0};#if TARGET_API_MAC_CARBON GetWindowPortBounds (whichWindow, &rectTemp);#else rectTemp = whichWindow->portRect;#endif // TARGET_API_MAC_CARBON bufferRect [0] = 10; bufferRect [1] = 10; bufferRect [2] = rectTemp.right - rectTemp.left - 20; bufferRect [3] = rectTemp.bottom - rectTemp.top - 20; aglSetCurrentContext (pWindowInfo->aglContext); // ensure the context we are working with is current aglSetInteger (pWindowInfo->aglContext, AGL_BUFFER_RECT, bufferRect); aglUpdateContext (pWindowInfo->aglContext); glViewport (0, 0, bufferRect [2], bufferRect [3]); sprintf (pWindowInfo->strContext, "%ld x %ld", bufferRect [2], bufferRect [3]); } else {#if TARGET_API_MAC_CARBON GetWindowPortBounds (whichWindow, &rectTemp);#else rectTemp = whichWindow->portRect;#endif // TARGET_API_MAC_CARBON aglSetCurrentContext (pWindowInfo->aglContext); // ensure the context we are working with is current aglUpdateContext (pWindowInfo->aglContext); glViewport (0, 0, rectTemp.right - rectTemp.left, rectTemp.bottom - rectTemp.top); sprintf (pWindowInfo->strContext, "%d x %d", rectTemp.right - rectTemp.left, rectTemp.bottom - rectTemp.top); } } } } break; case inSysWindow:#if !TARGET_API_MAC_CARBON SystemClick(&theEvent, whichWindow);#endif // !TARGET_API_MAC_CARBON break; } break; case keyDown: case autoKey: { SInt8 theKey; SInt8 theCode; theKey = theEvent.message & charCodeMask; theCode = (theEvent.message & keyCodeMask) >> 8; if ((theEvent.modifiers & cmdKey) != 0) { SInt32 menuResult = MenuKey (theKey); if (HiWord(menuResult) != 0) DoMenu (menuResult); } else DoKey (theKey, theCode); } break; case activateEvt: break; case updateEvt: { GrafPtr pGrafSave; whichWindow = (WindowRef) theEvent.message; GetPort (&pGrafSave); SetPort((GrafPtr) GetWindowPort(whichWindow)); BeginUpdate(whichWindow); DoUpdate(whichWindow); SetPort((GrafPtr) GetWindowPort(whichWindow)); EndUpdate(whichWindow); SetPort (pGrafSave); } break; case diskEvt: break; case osEvt: if (theEvent.message & 0x01000000) // Suspend/resume event { if (theEvent.message & 0x00000001) // Resume { gSleepTime = kForegroundSleep; gfFrontProcess = true; } else { gSleepTime = kBackgroundSleep; // Suspend gfFrontProcess = false; } } break; case kHighLevelEvent: AEProcessAppleEvent (&theEvent); break; } } }#if !TARGET_API_MAC_CARBON else { long i; for (i = 0; i < kMaxWindows; i++) if (gpWindowList[i]) DoUpdate (gpWindowList[i]); }#endif // !TARGET_API_MAC_CARBON}// --------------------------------------------------------------------------void CleanUp (void){ MenuHandle hMenu; DisposeGLWindowList ();#if TARGET_API_MAC_CARBON if (gTimer) { RemoveEventLoopTimer(gTimer); gTimer = NULL; }#endif // TARGET_API_MAC_CARBON hMenu = GetMenuHandle (kMenuFile); DeleteMenu (kMenuFile); DisposeMenu (hMenu); hMenu = GetMenuHandle (kMenuApple); DeleteMenu (kMenuApple); DisposeMenu (hMenu);}// --------------------------------------------------------------------------int main (void){ if (SetUp ()) while (!gDone) DoEvent (); CleanUp (); return 0;}#pragma mark -//-----------------------------------------------------------------------------------------------------------------------// OpenGL Drawingvoid drawGL(WindowPtr pWindow){ float f = 0.0; structWindowInfoPtr pWindowInfo = (structWindowInfoPtr) GetWRefCon (pWindow); long indexWindow = 0; // find which window in list to match frame time and number while ((pWindow != gpWindowList [indexWindow]) && (kMaxWindows > indexWindow)) {indexWindow++;} if (kMaxWindows == indexWindow) // not in list indexWindow = -1; glClearColor(0.25f, 0.25f, 0.25f, 1.0f); // Clear color buffer to dark grey glClear(GL_COLOR_BUFFER_BIT); if (0 <= indexWindow) { gRotation [indexWindow] += 0.5; f = gRotation [indexWindow]; } glMatrixMode (GL_MODELVIEW); glLoadIdentity (); glRotated (f, 0.0, 0.0, 1.0); glEnable (GL_TEXTURE_2D); glBegin(GL_QUADS); // Draw textured polygon if (gTexture) { glTexCoord2f(0.0, 0.0); glColor3d(1.0, 1.0, 1.0); glVertex3d(0.7, 0.7, 0.0); glTexCoord2f(0.0, 1.0); glColor3d(1.0, 1.0, 1.0); glVertex3d(-0.7, 0.7, 0.0); glTexCoord2f(1.0, 1.0); glColor3d(1.0, 1.0, 1.0); glVertex3d(-0.7, -0.7, 0.0); glTexCoord2f(1.0, 0.0); glColor3d(1.0, 1.0, 1.0); glVertex3d(0.7, -0.7, 0.0); } else { glColor3d(1.0, 0.0, 0.0); glVertex3d(0.7, 0.7, 0.0); glColor3d(0.0, 1.0, 0.0); glVertex3d(-0.7, 0.7, 0.0); glColor3d(0.0, 0.0, 1.0); glVertex3d(-0.7, -0.7, 0.0); glColor3d(0.7, 0.7, 0.7); glVertex3d(0.7, -0.7, 0.0); } glEnd(); glDisable (GL_TEXTURE_2D); // draw info { Rect rectPort; GLint matrixMode;#if TARGET_API_MAC_CARBON GetWindowPortBounds (pWindow, &rectPort);#else rectPort = pWindow->portRect;#endif // TARGET_API_MAC_CARBON glGetIntegerv (GL_MATRIX_MODE, &matrixMode); glMatrixMode (GL_PROJECTION); glPushMatrix(); glLoadIdentity (); glMatrixMode (GL_MODELVIEW); glPushMatrix(); glLoadIdentity (); glScalef (2.0 / (rectPort.right - rectPort.left), -2.0 / (rectPort.bottom - rectPort.top), 1.0); glTranslatef (-(rectPort.right - rectPort.left) / 2.0, -(rectPort.bottom - rectPort.top) / 2.0, 0.0); glColor3f (1.0, 1.0, 1.0); glRasterPos3d (10, 12, 0); if (0 <= indexWindow) MultiWinDrawFrameRate (pWindowInfo->fontList, gStringWindow[indexWindow], &gFrameWindow[indexWindow], &gTimeWindow[indexWindow]); glRasterPos3d (10, 24, 0); DrawCStringGL (pWindowInfo->strContext, pWindowInfo->fontList); glRasterPos3d (10, (rectPort.bottom - rectPort.top) - 15, 0); DrawCStringGL ((char*) glGetString (GL_VENDOR), pWindowInfo->fontList); glRasterPos3d (10, (rectPort.bottom - rectPort.top) - 3, 0); DrawCStringGL ((char*) glGetString (GL_RENDERER), pWindowInfo->fontList); glPopMatrix(); // GL_MODELVIEW glMatrixMode (GL_PROJECTION); glPopMatrix(); glMatrixMode (matrixMode); } aglSwapBuffers(pWindowInfo->aglContext); // send swap command}</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div style="width: 100%; height: 1px; background-color: #919699; margin-top: 5px; margin-bottom: 15px"></div></td>
</tr>
<tr>
<td align="center"><br/>
<table border="0" cellpadding="0" cellspacing="0" class="graybox">
<tr>
<th>Did this document help you?</th>
</tr>
<tr>
<td>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=1&url=/samplecode/OpenGLMovieQT/listing13.html%3Fid%3DDTS10000847-1.0&media=dvd" target=_new>Yes</a>: Tell us what works for you.</div>
<div style="margin-bottom: 8px"><a href="http://developer.apple.com/feedback/?v=2&url=/samplecode/OpenGLMovieQT/listing13.html%3Fid%3DDTS10000847-1.0&media=dvd" target=_new>It’s good, but:</a> Report typos, inaccuracies, and so forth.</div>
<div><a href="http://developer.apple.com/feedback/?v=3&url=/samplecode/OpenGLMovieQT/listing13.html%3Fid%3DDTS10000847-1.0&media=dvd" target=_new>It wasn’t helpful</a>: Tell us what would have helped.</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<!-- START BOTTOM APPLE NAVIGATION -->
<!--#include virtual="/includes/footer"-->
<!-- END BOTTOM APPLE NAVIGATION -->
<!-- START CENTER CLOSE -->
</center>
<!-- END CENTER CLOSE -->
</body>
</html>