/** * Create a small, single layer background and scroll */ #include #include #include #include #include #include #include #include #include #include #include #include #include "gte.h" #include "apf.h" #include "maze.h" #pragma lint -1 /** Global variable for Tool Locator */ static StartStopRecord startStopRec; static Ref startStopRef; static Handle directPage; static InitialLoadOutputRec loadRec; /* static char *toolname = "9:gtetool"; */ static char *toolname = ":GTEDevel:GTE.Tool:obj:gtetool"; static Word masterMode = fWidth16 | fHeight16; /* Image data */ /* static char *imagename = "9:smbtiles.pnt"; */ static char *imagename = ":GTEDevel:GTE.Tool:test:graphics:smbtiles.pnt"; static Handle graphic = NULL; static LocInfo locInfo; static Word colorTable[16]; /* Object Attribute Memory pointer */ static OAMEntry *OAM; /* Global Event Record */ static EventRecord eventRec; /* This gets blown up into a 30x60 tilemap */ static Word map[1800]; #define MAZE_WIDTH 20 #define MAZE_HEIGHT 10 #define MAZE_SIZE MAZE_WIDTH*MAZE_HEIGHT #define FIELD_BLOCK_WIDTH 16 #define FIELD_BLOCK_HEIGHT 9 /** * Animate the move from one position to another */ void animateFrom( unsigned int from, unsigned int to ) { Word x, y; /* Get the x,y in the maze */ unsigned int x0 = from % MAZE_WIDTH; unsigned int y0 = from / MAZE_WIDTH; unsigned int x1 = to % MAZE_WIDTH; unsigned int y1 = to / MAZE_WIDTH; int dx = (x1 > x0) ? 1 : -1; int dy = (y1 > y0) ? 2 : -2; Word maxx = 8*(3*MAZE_WIDTH - FIELD_BLOCK_WIDTH); Word maxy = 16*(3*MAZE_HEIGHT - FIELD_BLOCK_HEIGHT); /* Convert to the tile map coordinates*/ x0 = 8*(1 + 3*x0); y0 = 16*(1 + 3*y0); x1 = 8*(1 + 3*x1); y1 = 16*(1 + 3*y1); if ( from >= MAZE_SIZE || to >= MAZE_SIZE ) return; while ( x0 != x1 || y0 != y1 ) { /* Pre-increment to avoid "stalls" at waypoints */ if ( x0 != x1 ) x0 += dx; if ( y0 != y1 ) y0 += dy; /* Try and center the location on the screen */ x = ( x0 < 80 ) ? 0 : x0 - 80; y = ( y0 < 100 ) ? 0 : y0 - 100; if ( x > maxx ) x = maxx; if ( y > maxy ) y = maxy; GTESetBG0Origin( x, y ); OAM[0].xpos = x0 - x; OAM[0].ypos = y0 - y; GTEUpdate( 0 ); } } /** * Load the system tools and the GTE tool */ void startUpTools( void ) { startStopRec.flags = 0x0000; startStopRec.videoMode = mode320; startStopRec.numTools = 3; startStopRec.theTools[0].toolNumber = 3; /* Misc Tool */ startStopRec.theTools[0].minVersion = 0x0302; startStopRec.theTools[1].toolNumber = 4; /* QuickDraw II */ startStopRec.theTools[1].minVersion = 0x0307; startStopRec.theTools[2].toolNumber = 6; /* Event Manager */ startStopRec.theTools[2].minVersion = 0x0301; /* Start up the standard tools */ startStopRef = StartUpTools( userid(), refIsPointer, (Long) &startStopRec ); if ( toolerror() ) return; /* Start up the Animation ToolSet */ loadRec = InitialLoad( userid(), (Pointer) c2pstr( toolname ), 0 ); if ( toolerror() ) return; SetTSPtr( userTool, GTE_TOOLNUM, loadRec.startAddr ); if ( toolerror() ) return; directPage = NewHandle( 0x200, userid(), 0xC005, 0 ); if ( toolerror() ) return; GTEStartUp( (Word) *directPage, masterMode, userid() ); } void shutDownTools( void ) { GTEShutDown(); ShutDownTools( refIsPointer, startStopRef ); } void compileSprites(void) { Handle sprite; Rect r; /* Set the bounds on the sprite */ r.h1 = 0; r.v1 = 0; r.h2 = 16; r.v2 = 16; /* Compile the sprite */ sprite = GTENewSprite( &locInfo, &r, NULL, NULL, fNoPriority ); if ( toolerror() || sprite == NULL ) { return; } /* Set the first slot in OAM to this sprite */ OAM[0].xpos = 10; OAM[0].ypos = 10; OAM[0].flags = 0; OAM[0].sprite = (GTESpritePtr) *sprite; } void compileTiles(void) { Handle tile; Point p; /* Set the bounds on the tile */ p.h = 64; p.v = 16; /* Compile two tiles */ tile = GTENewTile( &locInfo, &p, NULL, NULL ); GTESetTile( tile, 0 ); p.h = 288; p.v = 16; tile = GTENewTile( &locInfo, &p, NULL, NULL ); GTESetTile( tile, 1 ); } void setMaze(char *maze) { int x, y, i, j; /* Create a maze map */ createMaze(maze, MAZE_HEIGHT, MAZE_WIDTH ); /* Solve the maze */ solveMaze( maze, MAZE_HEIGHT, MAZE_WIDTH ); for ( i = 0; i < 1800; i++ ) map[i] = 1; /* Map the maze values into a tile map */ i = 61; j = 0; for ( y = 0; y < MAZE_HEIGHT; y++, i += 120 ) { for ( x = 0; x < MAZE_WIDTH; x++, j++, i += 3 ) { map[i] = 0; if (( maze[j] & NORTH ) == 0 ) map[i-60] = 0; if (( maze[j] & SOUTH ) == 0 ) map[i+60] = 0; if (( maze[j] & EAST ) == 0 ) map[i+1] = 0; if (( maze[j] & WEST ) == 0 ) map[i-1] = 0; } } /* Set the map */ GTESetBG0TileMap( (Pointer) map, 30, 60 ); GTESetBG0Origin( 0, 0 ); } int main(int argc, char *argv[]) { Rect r; int i, j; int done = 0; char *maze = (char *) malloc( sizeof( char ) * MAZE_SIZE ); /* Start up the tools */ startUpTools(); if ( toolerror() ) { goto err; } /* Load an image */ graphic = LoadAPF( imagename, &locInfo, colorTable ); if ( toolerror() || graphic == NULL ) { goto err; } /* Set the primary color palette to the loaded image's palette */ SetColorTable( 0, colorTable ); /* Get a pointer to the object attibute memory */ OAM = (OAMEntry *) GTEGetAddress( oamAddr ); /* Create a sprite */ compileSprites(); /* Create the tiles */ compileTiles(); setMaze( maze ); /** * Set up the display to be full screen */ r.h1 = 80 - FIELD_BLOCK_WIDTH*4; r.v1 = 100 - FIELD_BLOCK_HEIGHT*8; r.h2 = r.h1 + FIELD_BLOCK_WIDTH*8; r.v2 = r.v1 + FIELD_BLOCK_HEIGHT*16; GTESetFieldRect( &r ); GTERefreshBG0( 0, 0, FIELD_BLOCK_HEIGHT+1, FIELD_BLOCK_WIDTH+1 ); DisposeHandle( graphic ); /* Hide the mouse pointer */ HideCursor(); /* Wait for user input */ i = 0; while ( !done ) { GetNextEvent( everyEvent, &eventRec ); if ( eventRec.what == keyDownEvt ) { switch (eventRec.message & 0x7F) { case 'q': done = 1; break; } } /* If we're at the end of the maze, maze a new one */ if ( i == MAZE_SIZE - 1 ) { setMaze( maze ); GTERefreshBG0( 0, 0, 13, 21 ); i = 0; continue; } /* Figure out the next spot along the path */ maze[i] &= ~PATH; if (!(maze[i] & NORTH) && (maze[i-MAZE_WIDTH] & PATH)) j = i - MAZE_WIDTH; else if (!(maze[i] & SOUTH) && (maze[i+MAZE_WIDTH] & PATH)) j = i + MAZE_WIDTH; else if (!(maze[i] & EAST) && (maze[i+1] & PATH)) j = i + 1; else if (!(maze[i] & WEST) && (maze[i-1] & PATH)) j = i - 1; /* Animate from point A to point B */ animateFrom( i, j ); i = j; } err: /* Shut down the tools */ shutDownTools(); return 0; }