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
|
/** 32 bit DrawSprockets pluggable framework
*
* Jason Rohrer, 4-28-99
*
* Mods:
* Jason Rohrer 11-8-99 Changed to use GraphicBuffer object as screen buffer
* Jason Rohrer 12-15-99 Added support for keyboard querying
* Jason Rohrer 3-21-2000 Added support for mouse querying
*
*/
#include <DrawSprocket.h>
#include <QuickDraw.h>
#include <Events.h> // for keyboard events
#include <stdlib.h>
#include <string.h>
#include "swapBuffers.h" // interface for swapping buffers
#include "getKey.h" // interface for getting key states
#include "getMouse.h" // interface for getting mouse state
//#include "ALifeGuiRunner.h"
#include "GoGUIRunner.h"
#include "GraphicBuffer.h"
// Constants
#define BallWidth 20
#define BallHeight 20
#define BobSize 8 /* Size of text in each ball */
//MT Tutorial
#define rWindow 128 // resource ID number for window
// Globals
Rect windRect;
WindowPtr w;
DSpContextReference theContext;
unsigned long *double_buffer = NULL; // the double buffer
unsigned long *screen_buffer = NULL;
GraphicBuffer *graphicDoubleBuffer;
short bufferHigh = 480;
short bufferWide = 640;
// prototypes
void Initialize(void);
void graphixPicker(void); // our mother function, picks effect order, etc.
void initVideo(DSpContextReference *theContext);
void MyInitAttributes(DSpContextAttributes*);
void CleanUp(DSpContextReference theContext);
int main() {
Initialize(); // setup a window
MaxApplZone();
initVideo( &theContext ); // set up and switch video
graphixPicker();
CleanUp( theContext);
}
void graphixPicker() { // our "mother function"
// selects which effect to run next
// graphix plug-in format:
// myGraphix( bufferPtr, bufferHigh, bufferWide, colorBits, numFrames)
// function can cll "swapBuffer32" internally whenever it needs the back buffer
// sent to the screen.
// jcrTorus( double_buffer, 480, 640, 32, 100);
// jcrVoxels( double_buffer, 480, 640, 32, 230);
// jcrTorus( double_buffer, 480, 640, 32, 50);
//jcrBloom( double_buffer, 480, 640, 32, 100);
// GraphicBuffer passed in contains all needed info about screen
// pass in number of frames to run for
//ALifeGuiRunner( *graphicDoubleBuffer, 1000 );
GoGUIRunner( *graphicDoubleBuffer, 0 );
}
// Initialize the draw sprockets
// set up the video, fade out display, and switch video modes
void initVideo(DSpContextReference *theContext) {
CGrafPtr backBuffer;
PixMapHandle bufferMap;
DSpContextAttributes theDesiredAttributes;
OSStatus theError;
MyInitAttributes(&theDesiredAttributes);
theDesiredAttributes.displayWidth = 640;
theDesiredAttributes.displayHeight = 480;
theDesiredAttributes.colorNeeds = kDSpColorNeeds_Require;
theDesiredAttributes.backBufferDepthMask = kDSpDepthMask_32;
theDesiredAttributes.displayDepthMask = kDSpDepthMask_32;
theDesiredAttributes.backBufferBestDepth = 32;
theDesiredAttributes.displayBestDepth = 32;
theError = DSpFindBestContext(&theDesiredAttributes, theContext);
// add page flipping by video card to request
theDesiredAttributes.contextOptions |= kDSpContextOption_PageFlip;
//theDesiredAttributes.contextOptions |= kDSpContextOption_DontSyncVBL;
// Reserver a display
theError = DSpContext_Reserve(*theContext, &theDesiredAttributes);
// fade out
theError = DSpContext_FadeGammaOut(NULL, NULL);
theError = DSpContext_SetState(*theContext, kDSpContextState_Active);
ShowCursor();
HideCursor();
// setup global pointers to screen and back buffer
theError = DSpContext_GetBackBuffer(*theContext, kDSpBufferKind_Normal, &backBuffer);
bufferMap = (PixMapHandle)GetGWorldPixMap(backBuffer);
double_buffer = (unsigned long*)(**bufferMap).baseAddr;
// create GraphicBuffer object
graphicDoubleBuffer = new GraphicBuffer( double_buffer, 640, 480 );
theError = DSpContext_GetFrontBuffer(*theContext, &backBuffer);
bufferMap = (PixMapHandle)GetGWorldPixMap(backBuffer);
screen_buffer = (unsigned long*)(**bufferMap).baseAddr;
theError = DSpContext_FadeGammaIn(NULL, NULL);
}
// initialize a set of Display attributes to zero
void MyInitAttributes (DSpContextAttributes *inAttributes)
{
if (NULL == inAttributes)
DebugStr("\pStimpy! You Idiot!");
inAttributes->frequency = 0;
inAttributes->displayWidth = 0;
inAttributes->displayHeight = 0;
inAttributes->reserved1 = 0;
inAttributes->reserved2 = 0;
inAttributes->colorNeeds = 0;
inAttributes->colorTable = NULL;
inAttributes->contextOptions = 0;
inAttributes->backBufferDepthMask = 0;
inAttributes->displayDepthMask = 0;
inAttributes->backBufferBestDepth = 0;
inAttributes->displayBestDepth = 0;
inAttributes->pageCount = 0;
inAttributes->gameMustConfirmSwitch = false;
inAttributes->reserved3[0] = 0;
inAttributes->reserved3[1] = 0;
inAttributes->reserved3[2] = 0;
inAttributes->reserved3[3] = 0;
}
void CleanUp(DSpContextReference theContext) {
OSStatus theError;
theError = DSpContext_FadeGammaOut(NULL, NULL);
/* put the context into the inactive state */
theError = DSpContext_SetState(theContext, kDSpContextState_Inactive);
/* fade back in */
theError = DSpContext_FadeGammaIn(NULL, NULL);
/* release the context */
theError = DSpContext_Release(theContext);
ShowCursor();
}
// swap bufferB to the screen (32 bit version)
void swapBuffers32( GraphicBuffer &bufferB ) {
OSStatus theError;
// swap buffers
theError = DSpContext_SwapBuffers(theContext, NULL, 0);
// get pointer to new back buffer and return it
CGrafPtr backBuffer;
PixMapHandle bufferMap;
theError = DSpContext_GetBackBuffer(theContext, kDSpBufferKind_Normal, &backBuffer);
bufferMap = (PixMapHandle)GetGWorldPixMap(backBuffer);
// replace the buffer in bufferB with the new double buffer
bufferB.setBuffer( (unsigned long*)(**bufferMap).baseAddr );
// memcpy(screen_buffer, bufferPtrB, 4*bufferWide*bufferHigh);
} // end of swapBuffers
// returns true if key represented by given key code is down
char getKeyDown( int vKeyCode ) {
unsigned char keyArray [16];
GetKeys( (long *)keyArray );
return ((keyArray[vKeyCode>>3] >> (vKeyCode & 7)) & 1);
}
// returns true if key is up
char getKeyUp( int vKeyCode ) {
unsigned char keyArray [16];
GetKeys( (long *)keyArray );
return !((keyArray[vKeyCode>>3] >> (vKeyCode & 7)) & 1);
}
void getMouse( int *x, int *y ) {
Point mousePoint;
DSpGetMouse( &mousePoint );
*x = mousePoint.h;
*y = mousePoint.v;
}
void Initialize(void)
{
WindowPtr mainPtr;
OSErr error;
SysEnvRec theWorld;
//
// Test the computer to be sure we can do color.
// If not we would crash, which would be bad.
// If we cant run, just beep and exit.
//
error = SysEnvirons(1, &theWorld);
if (theWorld.hasColorQD == false) {
// SysBeep(50);
ExitToShell(); // If no color QD, we must leave.
}
// Initialize all the needed managers.
InitGraf(&qd.thePort);
// InitFonts();
InitWindows();
InitMenus();
TEInit();
InitDialogs(nil);
InitCursor();
//
// To make the Random sequences truly random, we need to make the seed start
// at a different number. An easy way to do this is to put the current time
// and date into the seed. Since it is always incrementing the starting seed
// will always be different. Dont for each call of Random, or the sequence
// will no longer be random. Only needed once, here in the init.
//
GetDateTime((unsigned long*) &qd.randSeed);
//
// Make a new window for drawing in, and it must be a color window.
// The window is full screen size, made smaller to make it more visible.
//
mainPtr = GetNewCWindow(rWindow, nil, (WindowPtr) -1); // MW Tutorial
windRect = mainPtr->portRect;
SetPort(mainPtr); // set window to current graf port
TextSize(BobSize); // smaller font for drawing.
}
|