-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing9.html
executable file
·1031 lines (902 loc) · 36.3 KB
/
listing9.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!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_SetupDSp.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_SetupDSp.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: SetupDSp.c
Contains: Functions to enable building and destorying a DSp fullscreen context
Written by: Geoff Stahl (ggs)
Copyright: Copyright © 1999 Apple Computer, Inc., All Rights Reserved
Change History (most recent first):
<3> 3/26/01 ggs Add DSp version check and other items for full screen on X
<2> 3/26/01 ggs Add new DSp functinality for Mac OS X
<1> 1/19/01 ggs Initial re-add
<7> 3/21/00 ggs Added windowed mode and clean up various implementation details
<6> 2/22/00 ggs fix fades
<5> 1/26/00 ggs Add fade code back in, ensure NULL pointer/context checks are in
<4> 1/24/00 ggs add new disclaimer, protection from NULL dispose, better
software renderer handling
<3> 12/18/99 ggs Fixed err use before init
<2> 12/18/99 ggs Fix headers
<1> 11/28/99 ggs Initial add. Split of just DSp handling functions. Added total
device RAM checks, better step downs using actual supported
resolutions. Need to add user verify for contexts that require
it, integration of this in context step down, and a freq bit
field.
<1> 11/11/99 ggs Initial Add
Disclaimer: You may incorporate this sample code into your applications without
restriction, though the sample code has been provided "AS IS" and the
responsibility for its operation is 100% yours. However, what you are
not permitted to do is to redistribute the source as "DSC Sample Code"
after having made changes. If you're going to re-distribute the source,
we require that you make it clear in the source that the code was
descended from Apple Sample Code, but that you've made changes.
*/
// Usage notes:
// kUseFades enables gamma fades for activates and deactivates
#define kUseFades
//kUseRAMCheck enables estimated video card RAM checks
#define kUseRAMCheck
// system includes ----------------------------------------------------------
#ifdef __APPLE_CC__
#include <Carbon/Carbon.h>
#else
#include <events.h>
#include <sound.h>
#include <fp.h>
#endif
#include <string.h>
// project includes ---------------------------------------------------------
#include "Carbon_Error_Handler.h"
#include "Carbon_SetupDSp.h"
// globals (internal/private) -----------------------------------------------
enum
{
kMaxNumRes = 64, // max number of resolution slots
kMaxRefreshFreq = 75
};
Boolean gDSpStarted = false; // will never be true unless DSp is installed and start succeeds
Boolean gNeedFade = false;
// prototypes (internal/private) --------------------------------------------
DSpContextReference * ReserveUnusedDevices (GDHandle hGD);
OSStatus FreeUnusedDevices (GDHandle hGD, DSpContextReference ** ppContextRefUnused);
void BuildResolutionList (GDHandle hGD, Point * pResList, SInt32 * pFreqList);
OSStatus DoDeviceRAMCheck (pstructGLInfo pcontextInfo, Point * pResList, SInt32 * pFreqList, GLint depthSizeSupport);
Boolean DoContextStepDown (pstructGLInfo pcontextInfo, DSpContextAttributes * pContextAttributes, Point * pResList, SInt32 * pFreqList);
// functions (internal/private) ---------------------------------------------
// ReserveUnusedDevices
// reserves contexts on unused devices to vprevent their selection by DSp, returns list of these devices
DSpContextReference * ReserveUnusedDevices (GDHandle hGD)
{
DSpContextAttributes theContextAttributes;
DSpContextReference * pContextRefUnused = NULL;
GDHandle hDevice = DMGetFirstScreenDevice (true); // check number of screens
DisplayIDType displayID = 0;
short numDevices = 0, indexDevice = 0;
do
{
numDevices++;
hDevice = DMGetNextScreenDevice (hDevice, true);
}
while (hDevice);
numDevices--; // only count unused screens
if (numDevices)
{
pContextRefUnused = (DSpContextReference *) NewPtr ((long) sizeof (DSpContextReference) * numDevices);
hDevice = DMGetFirstScreenDevice (true); // check number of screens
do
{
if (hDevice != hGD) // if this device is not the one the user chose
{
if (noErr == DSpReportError (DMGetDisplayIDByGDevice (hDevice, &displayID, false)))
if (noErr == DSpReportError (DSpGetFirstContext (displayID, &pContextRefUnused [indexDevice]))) // get a context and
if (noErr == DSpReportError (DSpContext_GetAttributes (pContextRefUnused [indexDevice], &theContextAttributes))) // find attributes
DSpReportError (DSpContext_Reserve (pContextRefUnused [indexDevice], &theContextAttributes)); // reserve it
indexDevice++;
}
hDevice = DMGetNextScreenDevice (hDevice, true);
}
while (hDevice);
}
return pContextRefUnused;
}
// --------------------------------------------------------------------------
// FreeUnusedDevices
// frees screen that were previously reserved to prevent selection
OSStatus FreeUnusedDevices (GDHandle hGD, DSpContextReference ** ppContextRefUnused)
{
OSStatus err = noErr;
GDHandle hDevice = DMGetFirstScreenDevice (true); // check number of screens
short indexDevice = 0;
do
{
if (hDevice != hGD) // if this device is not the one the user chose
{
err = DSpContext_Release (*ppContextRefUnused [indexDevice]); // release it
DSpReportError (err);
indexDevice++;
}
hDevice = DMGetNextScreenDevice (hDevice, true);
}
while (hDevice);
if (*ppContextRefUnused)
DisposePtr ((Ptr) *ppContextRefUnused);
*ppContextRefUnused = NULL;
return err;
}
// --------------------------------------------------------------------------
// BuildResolutionList
// builds a list of supported resolutions and frequencies for GDevice
void BuildResolutionList (GDHandle hGD, Point * pResList, SInt32 * pFreqList)
{
DSpContextAttributes theContextAttributes;
DSpContextReference currContext;
OSStatus err;
DisplayIDType displayID = 0;
short i;
for (i = 0; i < kMaxNumRes; i++) // clear resolution list
{
pResList [i].h = 0x7FFF;
pResList [i].v = 0x7FFF;
pFreqList [i] = 0; // some context require certain frequencies find highest for each (not higher than 85
}
err = DMGetDisplayIDByGDevice (hGD, &displayID, true);
if (noErr != err)
ReportErrorNum ("DMGetDisplayIDByGDevice error", err);
else
{
if (noErr == DSpReportError (DSpGetFirstContext (displayID, &currContext)))
do
{
// insertion sort into resolution list
if (noErr == DSpReportError (DSpContext_GetAttributes (currContext, &theContextAttributes)))
{
Point pntTemp;
Boolean fDone = false;
short i = 0;
while ((i < kMaxNumRes) && (!fDone))
{
if ((theContextAttributes.displayWidth == pResList [i].h) && (theContextAttributes.displayHeight == pResList [i].v)) //skip
{
if ((pFreqList [i] == 0) || ((theContextAttributes.frequency <= (kMaxRefreshFreq << 16)) && (theContextAttributes.frequency > pFreqList [i])))
pFreqList [i] = theContextAttributes.frequency;
break;
}
if (theContextAttributes.displayWidth * theContextAttributes.displayHeight < pResList [i].h * pResList [i].v) //insert
{
pntTemp = pResList [i];
pResList [i].h = (short) theContextAttributes.displayWidth;
pResList [i].v = (short) theContextAttributes.displayHeight;
pFreqList [i] = theContextAttributes.frequency;
fDone = true;
}
i++;
}
// i points to next element to switch; finish array swaps (if
while ((i < kMaxNumRes) && (fDone))
{
Point pntSwitch = pResList [i];
pResList [i++] = pntTemp;
pntTemp = pntSwitch;
}
}
err = DSpGetNextContext (currContext, &currContext);
if (noErr != err)
{
if (kDSpContextNotFoundErr != err)
DSpReportError (err);
currContext = 0; // ensure we drop out
}
}
while (currContext);
else
ReportErrorNum ("DSpGetFirstContext error", err);
}
// zeroize unused elements
for (i = 0; i < kMaxNumRes; i++)
if ((pResList [i].h == 0x7FFF) || (pResList [i].v == 0x7FFF))
{
pResList [i].h = 0;
pResList [i].v = 0;
}
}
// --------------------------------------------------------------------------
// DoDeviceRAMCheck
// checks requested allocation against device RAM
// Note: may modify pcontextInfo
// this should be equal or less strigent than OpenGL actual allocation to avoid failing on valid drawables
OSStatus DoDeviceRAMCheck (pstructGLInfo pcontextInfo, Point * pResList, SInt32 * pFreqList, GLint depthSizeSupport)
{
float frontBufferFactor = 1.0f, backBufferFactor = 0.0f; // amount of screen(front) or request(back) sized buffers required, in bytes
Point pntFrontBuffer; // size of front buffer that wil be allocated
short i, indexFrontBuffer;
OSStatus err = noErr;
// must take into account the entire front buffer, so figure out what screen resolution we are really going to use
// find front buffer for request
i = 0;
while (((pResList [i].h < pcontextInfo->width) || (pResList [i].v < pcontextInfo->height)) &&
((pResList [i].h != 0) || (pResList [i].v != 0)) &&
(i < kMaxNumRes))
i++;
// save front buffer sizes
pntFrontBuffer.h = pResList [i].h;
pntFrontBuffer.v = pResList [i].v;
// if we have a valid frequnecy for the context set it (to ensure a good selection
pcontextInfo->freq = pFreqList [i] >> 16;
indexFrontBuffer = i;
// front buffers required
if (16 == pcontextInfo->pixelDepth)
frontBufferFactor *= 2.0;
else if (32 == pcontextInfo->pixelDepth)
frontBufferFactor *= 4.0;
// back buffers required
backBufferFactor = 0.0f;
i = 0;
while (64 > i)
if (AGL_DOUBLEBUFFER == pcontextInfo->aglAttributes[i++])
{
if (16 == pcontextInfo->pixelDepth)
backBufferFactor = 2.0f;
else if (32 == pcontextInfo->pixelDepth)
backBufferFactor = 4.0f;
break;
}
i = 0;
while (64 > i)
if (AGL_DEPTH_SIZE == pcontextInfo->aglAttributes[i++])
{
long requestDepth = pcontextInfo->aglAttributes[i];
GLint bit = 0x00000001;
short currDepth = 0, prevDepth = 0;
// if (depthSizeSupport)
// {
do
{
if (bit & depthSizeSupport) // if the card supports the depth
{
prevDepth = currDepth;
switch (bit)
{
case AGL_1_BIT:
currDepth = 1;
break;
case AGL_2_BIT:
currDepth = 2;
break;
case AGL_3_BIT:
currDepth = 3;
break;
case AGL_4_BIT:
currDepth = 4;
break;
case AGL_5_BIT:
currDepth = 5;
break;
case AGL_6_BIT:
currDepth = 6;
break;
case AGL_8_BIT:
currDepth = 8;
break;
case AGL_10_BIT:
currDepth = 10;
break;
case AGL_12_BIT:
currDepth = 12;
break;
case AGL_16_BIT:
currDepth = 16;
break;
case AGL_24_BIT:
currDepth = 24;
break;
case AGL_32_BIT:
currDepth = 32;
break;
case AGL_48_BIT:
currDepth = 48;
break;
case AGL_64_BIT:
currDepth = 64;
break;
case AGL_96_BIT:
currDepth = 96;
break;
case AGL_128_BIT:
currDepth = 128;
break;
}
}
bit *= 2;
} while (!((requestDepth > prevDepth) && (requestDepth <= currDepth)) && (bit < AGL_128_BIT + 1));
// }
// else // no card depth support info
// currDepth = requestDepth; // we don't have card info thus assume we can support exact depth requested (may fail later but will always be equal or less stringent)
if ((AGL_128_BIT >= bit) && (0 != currDepth))
backBufferFactor += (float) currDepth / 8.0;
break;
}
// What we now have:
// pcontextInfo->width, height: request width and height
// pResList: sorted list of resolutions supported on this display
// pntFrontBuffer : size of front buffer that will currently be allocated
// indexFrontBuffer: position in array of current front buffer request
// frontBufferFactor: number of screen resolution size buffers that will be needed
// backBufferFactor: number of request size buffers that will be needed
// if we see zero VRAM here we must be looking at the software renderer thus this check is moot.
if (pcontextInfo->VRAM == 0)
{
// no changes required
return noErr;
}
// find a context size that can support our texture requirements in the current total VRAM
if ((pcontextInfo->VRAM - pcontextInfo->textureRAM) < (pntFrontBuffer.h * pntFrontBuffer.v * frontBufferFactor +
pcontextInfo->width * pcontextInfo->height * backBufferFactor))
{
if (pcontextInfo->fDepthMust && pcontextInfo->fSizeMust)
{
// cannot accomdate request
ReportError ("Not enough total VRAM for drawable and textures (depth buffer and pixel size must be as requested)");
return err;
}
else if (pcontextInfo->fSizeMust) // if we can adjust the size, try adjusting the
{
// try 16 bit if must size is true
if ((pcontextInfo->pixelDepth > 16) &&
(pcontextInfo->VRAM - pcontextInfo->textureRAM) > (pntFrontBuffer.h * pntFrontBuffer.v * frontBufferFactor / 2.0 +
pcontextInfo->width * pcontextInfo->height * (backBufferFactor - 2.0)))
pcontextInfo->pixelDepth = 16;
else
{
// cannot accomdate request
ReportError ("Not enough total VRAM for drawable and textures");
return err;
}
}
else // can adjust size and might be able to adjust depth
{ // make drawable fit
Boolean fFound = false;
// see if we can just adjust the pixel depth
if ((pcontextInfo->pixelDepth > 16) && // if we are requesting 32 bit
(!pcontextInfo->fDepthMust) && // if we can adjust the pixel depth
(pcontextInfo->VRAM - pcontextInfo->textureRAM) > (pntFrontBuffer.h * pntFrontBuffer.v * frontBufferFactor / 2.0 +
pcontextInfo->width * pcontextInfo->height * (backBufferFactor - 2.0)))
{
fFound = true;
pcontextInfo->pixelDepth = 16;
}
else // pixel depth alone wont do it
{
i = (short) (indexFrontBuffer - 1);
while (i >= 0)
{
//
if ((pcontextInfo->VRAM - pcontextInfo->textureRAM) > (pResList [i].h * pResList [i].v * frontBufferFactor +
pResList [i].h * pResList [i].v * backBufferFactor))
{
fFound = true;
pcontextInfo->width = pResList [i].h;
pcontextInfo->height = pResList [i].v;
pcontextInfo->freq = pFreqList [i] >> 16;
break;
}
else if ((pcontextInfo->pixelDepth > 16) && // if we are requesting 32 bit
(!pcontextInfo->fDepthMust) && // if we can adjust the pixel depth
(pcontextInfo->VRAM - pcontextInfo->textureRAM) > (pResList [i].h * pResList [i].v * frontBufferFactor / 2.0 +
pResList [i].h * pResList [i].v * (backBufferFactor - 2.0)))
{
fFound = true;
pcontextInfo->width = pResList [i].h;
pcontextInfo->height = pResList [i].v;
pcontextInfo->freq = pFreqList [i] >> 16;
pcontextInfo->pixelDepth = 16;
break;
}
i--;
}
// we tried the smallest screen size and still need to use less VRAM, adjust backbuffer to what is available
if ((!fFound) && (((pcontextInfo->VRAM - pcontextInfo->textureRAM) - pResList [0].h * pResList [0].v * frontBufferFactor) > 0))
{
float factor;
fFound = true;
factor = (float) sqrt((float) (pcontextInfo->width * pcontextInfo->height * backBufferFactor) /
(float) ((pcontextInfo->VRAM - pcontextInfo->textureRAM) - pResList [0].h * pResList [0].v * frontBufferFactor));
pcontextInfo->width /= factor;
pcontextInfo->height /= factor;
pcontextInfo->freq = pFreqList [0] >> 16;
}
}
if (!fFound)
{
// cannot accomdate request
ReportError ("Not enough total VRAM for drawable and textures");
return err;
}
}
}
return noErr;
}
// --------------------------------------------------------------------------
// DoContextStepDown
// steps down through frequencies, depths and sizes to try to find a valid context
// bounded by flags for SizeMust and DepthMust
// Note: may modify pcontextInfo
Boolean DoContextStepDown (pstructGLInfo pcontextInfo, DSpContextAttributes * pContextAttributes, Point * pResList, SInt32 * pFreqList)
{
// find current resolution
short i = 0;
while (((pResList [i].h <= pContextAttributes->displayWidth) || (pResList [i].v <= pContextAttributes->displayHeight)) &&
((pResList [i].h != 0) || (pResList [i].v != 0)) &&
(i < kMaxNumRes))
i++;
i--; // i points to index of current resolution
if (pcontextInfo->fSizeMust) // adjust depth only
{
if (pcontextInfo->pixelDepth > 16) // also try pixel depth step down
{
pContextAttributes->displayBestDepth = 16;
pContextAttributes->backBufferBestDepth = 16;
}
else
return false; // no more options to try
}
else if (pcontextInfo->fDepthMust) // adjust size only
{
if (i > 0)
{
i--; // i was pointing at current resolution, now it is pointing at new resolution to try
// set new resolution
pContextAttributes->displayWidth = pResList [i].h;
pContextAttributes->displayHeight = pResList [i].v;
pcontextInfo->freq = pFreqList [i] >> 16;
}
else
return false;
}
else // adjust size and depth
{
if (pContextAttributes->displayBestDepth > 16)
{
pContextAttributes->displayBestDepth = 16;
pContextAttributes->backBufferBestDepth = 16;
}
else if (i > 0)
{
i--; // i was pointing at current resolution, now it is pointing at new resolution to try
// reset pixel depth
pContextAttributes->displayBestDepth = pcontextInfo->pixelDepth;
pContextAttributes->backBufferBestDepth = pcontextInfo->pixelDepth;
// set new resolution
pContextAttributes->displayWidth = pResList [i].h;
pContextAttributes->displayHeight = pResList [i].v;
pcontextInfo->freq = pFreqList [i] >> 16;
}
else
return false;
}
return true;
}
#pragma mark -
// functions (public) -------------------------------------------------------
// GetDSpVersion
// Gets the current version of DSp
NumVersion GetDSpVersion (void)
{
NumVersion versionDSp = { 0, 0, 0, 0 };
OSStatus err = noErr;
if (!gDSpStarted)
err = StartDSp ();
if (noErr == err)
versionDSp = DSpGetVersion ();
return versionDSp;
}
// --------------------------------------------------------------------------
// StartDSp
// handles starting up DrawSprocket
OSStatus StartDSp (void)
{
OSStatus err = noErr;
if (!gDSpStarted)
{
// check for DSp
if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) DSpStartup)
{
ReportError ("DSp not installed");
return kDSpNotInitializedErr;
}
else
{
err = DSpReportError (DSpStartup()); // start DSp
if (noErr != err)
return err;
else
gDSpStarted = true;
}
}
return err;
}
// --------------------------------------------------------------------------
// ShutdownDSpContext
// shuts down DrawSprocket
void ShutdownDSp (void)
{
if (gDSpStarted)
{
DSpShutdown ();
gDSpStarted = false;
}
}
#pragma mark -
// --------------------------------------------------------------------------
// GetDSpDrawable
// Just returns the front buffer
// Inputs: *pdspContext
// pcontextInfo: request and requirements for cotext and drawable
// Outputs: returns CGrafPtr thaat is front buffer of context
// if error: will return NULL
CGrafPtr GetDSpDrawable (DSpContextReference dspContext)
{
CGrafPtr pCGraf = NULL;
if (noErr == DSpReportError (DSpContext_GetFrontBuffer (dspContext, &pCGraf)))
return pCGraf;
else
return NULL;
}
// --------------------------------------------------------------------------
// BuildDSpContext
// contextInfo and tries to allocate the corresponding DSp context
// Inputs: hGD: GDHandle to device to look at
// pcontextInfo: request and requirements for cotext and drawable
// Outputs: *pdspContext as allocated
// pcontextInfo: allocated parameters
// if fail to allocate: pdspContext will be NULL
// if error: will return error pdspContext will be NULL
OSStatus BuildDSpContext (DSpContextReference* pdspContext, GDHandle hGD, GLint depthSizeSupport, pstructGLInfo pcontextInfo)
{
DSpContextAttributes theContextAttributes, foundAttributes;
DSpContextReference * pContextRefUnused;
SInt32 aFreqList [kMaxNumRes];
Point aResList [kMaxNumRes]; // list for resolution information
OSStatus err = noErr;
*pdspContext = 0;
// check for DSp
if (noErr != StartDSp ())
{
ReportError ("DSp startup failed");
return noErr; // already reported
}
// reserve contexts on other screens to prevent their selection
pContextRefUnused = ReserveUnusedDevices (hGD);
// build resolution list
BuildResolutionList (hGD, aResList, aFreqList);
// handle default pixel depths
if (pcontextInfo->pixelDepth == 0) // default
{
pcontextInfo->pixelDepth = (**(**hGD).gdPMap).pixelSize;
if (pcontextInfo->pixelDepth < 16)
pcontextInfo->pixelDepth = 16;
}
#ifdef kUseRAMCheck
if (noErr != DoDeviceRAMCheck (pcontextInfo, aResList, aFreqList, depthSizeSupport))
return err;
#endif // kUseRAMCheck
// Note: DSp < 1.7.3 REQUIRES the back buffer attributes even if only one buffer is required
BlockZero (&theContextAttributes, sizeof (DSpContextAttributes));
// memset(&theContextAttributes, 0, sizeof (DSpContextAttributes));
theContextAttributes.displayWidth = pcontextInfo->width;
theContextAttributes.displayHeight = pcontextInfo->height;
theContextAttributes.displayBestDepth = pcontextInfo->pixelDepth;
theContextAttributes.backBufferBestDepth = pcontextInfo->pixelDepth;
do
{
theContextAttributes.frequency = pcontextInfo->freq * 0x10000;
theContextAttributes.colorNeeds = kDSpColorNeeds_Require;
theContextAttributes.displayDepthMask = kDSpDepthMask_All;
theContextAttributes.backBufferDepthMask = kDSpDepthMask_All;
theContextAttributes.pageCount = 1; // only the front buffer is needed
err = DSpFindBestContext(&theContextAttributes, pdspContext);
if (noErr != err) // if we had any errors, reset for next try
if (!DoContextStepDown (pcontextInfo, &theContextAttributes, aResList, aFreqList))
break; // have run out of options
} while (err == kDSpContextNotFoundErr);
// check find best context errors
if (kDSpContextNotFoundErr == err)
{
*pdspContext = 0;
return noErr;
}
else if (noErr != err)
{
DSpReportError (err);
*pdspContext = 0;
return err;
}
err = DSpReportError (DSpContext_GetAttributes (*pdspContext, &foundAttributes));
if (noErr != err)
{
*pdspContext = 0;
return err;
}
// reset width and height to full screen and handle our own centering
// HWA will not correctly center less than full screen size contexts
theContextAttributes.displayWidth = foundAttributes.displayWidth;
theContextAttributes.displayHeight = foundAttributes.displayHeight;
theContextAttributes.pageCount = 1; // only the front buffer is needed
theContextAttributes.contextOptions = 0 | kDSpContextOption_DontSyncVBL; // no page flipping and no VBL sync needed
err = DSpReportError (DSpContext_Reserve(*pdspContext, &theContextAttributes )); // reserve our context
if (noErr != err)
{
*pdspContext = 0;
return err;
}
if (gNeedFade == true)
{
DSpReportError (DSpContext_CustomFadeGammaOut (NULL, NULL, fadeTicks));
gNeedFade = false;
}
err = DSpReportError (DSpContext_SetState (*pdspContext, kDSpContextState_Active)); // activate our context
if (noErr != err)
{
DSpContext_Release (*pdspContext);
DSpReportError (DSpContext_CustomFadeGammaIn (NULL, NULL, fadeTicks));
*pdspContext = 0;
return err;
}
FreeUnusedDevices (hGD, &pContextRefUnused);
if (!pcontextInfo->fSizeMust) // if we got whatever was available
{
// reset inputs to what was allocated (constrain aspect ratio)
// unless we ask for smaller, then leave the same
if ((pcontextInfo->width > foundAttributes.displayWidth) || (pcontextInfo->height > foundAttributes.displayHeight))
{
float hFactor = (float) pcontextInfo->width / (float) foundAttributes.displayWidth;
float vFactor = (float) pcontextInfo->height / (float) foundAttributes.displayHeight;
if (hFactor > vFactor)
{
pcontextInfo->width = (short) foundAttributes.displayWidth;
pcontextInfo->height /= hFactor;
}
else
{
pcontextInfo->height = (short) foundAttributes.displayHeight;
pcontextInfo->width /= vFactor;
}
}
}
// else still use inputs to allocate drawable
pcontextInfo->freq = foundAttributes.frequency / 0x10000;
pcontextInfo->pixelDepth = foundAttributes.displayBestDepth;
return noErr;
}
//-----------------------------------------------------------------------------------------------------------------------
// Deactivates and dumps context
void DestroyDSpContext (DSpContextReference* pdspContext)
{
if (gDSpStarted)
{
if (*pdspContext)
{
DSpReportError (DSpContext_SetState(*pdspContext, kDSpContextState_Inactive));
DSpReportError (DSpContext_CustomFadeGammaIn (NULL, NULL, fadeTicks));
DSpReportError (DSpContext_Release (*pdspContext));
*pdspContext = NULL;
}
}
}
#pragma mark -
//-----------------------------------------------------------------------------------------------------------------------
OSStatus DSpContext_CustomFadeGammaIn (DSpContextReference inContext, const RGBColor *fadeColor, long fadeTicks)
{
OSStatus err = noErr;
#ifndef kUseFades
#pragma unused (inContext, fadeColor, fadeTicks)
#else
RGBColor inZeroIntensityColor;
UInt32 currTick;
UInt16 step = (UInt16) (800 / fadeTicks);
long x, percent = 0;
if (gDSpStarted)
{
if (fadeTicks == 0)
fadeTicks = 1;
if (fadeColor == NULL)
{
inZeroIntensityColor.red = 0x0000;
inZeroIntensityColor.green = 0x0000;
inZeroIntensityColor.blue = 0x0000;
}
else
inZeroIntensityColor = *fadeColor;
currTick = TickCount ();
for (x = 1; x <= fadeTicks; x++)
{
percent = step * x / 8;
err = DSpContext_FadeGamma(inContext, percent, &inZeroIntensityColor);
if (err != noErr)
break;
while (currTick >= TickCount ()) {}
// SystemTask ();
currTick = TickCount ();
}
if (err == noErr)
err = DSpContext_FadeGamma(inContext, 100, &inZeroIntensityColor);
}
#endif // kUseFades
return err;
}
//-----------------------------------------------------------------------------------------------------------------------
OSStatus DSpContext_CustomFadeGammaOut (DSpContextReference inContext, const RGBColor *fadeColor, long fadeTicks )
{
OSStatus err = noErr;
#ifndef kUseFades
#pragma unused (inContext, fadeColor, fadeTicks)
#else
RGBColor inZeroIntensityColor;
UInt32 currTick;
UInt16 step = (UInt16) (800 / fadeTicks);
long x, percent = 0;
if (gDSpStarted)
{
if (fadeTicks == 0)
fadeTicks = 1; // ensure we do not have zero fade time
if (fadeColor == NULL)
{
inZeroIntensityColor.red = 0x0000;
inZeroIntensityColor.green = 0x0000;
inZeroIntensityColor.blue = 0x0000;
}
else
inZeroIntensityColor = *fadeColor;
currTick = TickCount ();
for (x = fadeTicks - 1; x >= 0; x--)
{
percent = step * x / 8;
err = DSpContext_FadeGamma(inContext, percent, &inZeroIntensityColor);
if (err != noErr)
break;
while (currTick >= TickCount ()) {}
// SystemTask ();
currTick = TickCount ();
}
}
#endif // kUseFades
return err;
}</pre>
<!--googleoff: index -->
</td>
</tr>
</table>
<!-- END WIDE COLUMN -->
<!-- END MAIN CONTENT -->
<table width="680" border="0" cellpadding="0" cellspacing="0">