/************************************************************************* * * Copyright 2008+ Mobile 1UP * All rights reserved. * *************************************************************************/ /* * @(#)application-state-star.inc */ /************************************************************************* * Structures *************************************************************************/ #define MAX_STARS 128 typedef struct StarType { struct { int16 x; int16 y; int16 z; } pos; int16 speed; } StarType; typedef struct StarRunTime { StarType *stars; } StarRunTime; typedef struct StarExtension { StarRunTime *rt; } StarExtension; /************************************************************************* * Globals + Preferences *************************************************************************/ #define GLOBALS_STAR_DEFINE GLOBALS_ACCESS; \ StarExtension *g_star #define GLOBALS_STAR_ACCESS GLOBALS_ACCESS; \ StarExtension *g_star = globals -> pExt[STATE_STAR] //------------------------------------------------------------------------ // --== GLOBALS ARE FORBIDDEN! ==-- // // DAL may support the use of globals on some platforms, however, its not // guaranteed that all the destination platforms allow the use of globals // (variable or static data). use the GlobalsType structure as shown. //------------------------------------------------------------------------ /************************************************************************* * Platform Independent Implementation *************************************************************************/ boolean ApplicationStateStarInitialize() { boolean result; GLOBALS_STAR_DEFINE; // default return value result = false; // entry requirements if (globals == NULL) goto ApplicationStateStarInitialize_DONE; g_star = (StarExtension *)_MemPtrNew(sizeof(StarExtension), false); if (g_star != NULL) { globals -> pExt[STATE_STAR] = (void *)g_star; result = true; } ApplicationStateStarInitialize_DONE:; return result; } boolean ApplicationStateStarInit() { boolean result; GLOBALS_STAR_ACCESS; // default return value result = false; // entry requirements if (g_star == NULL) goto ApplicationStateStarInit_DONE; // allocate the runtime g_star -> rt = _MemPtrNew(sizeof(StarRunTime), false); if (g_star -> rt == NULL) goto ApplicationStateStarInit_DONE; // allocate the memory required for the stars g_star -> rt -> stars = (StarType *)_MemPtrNew(MAX_STARS * sizeof(StarType), false); if (g_star -> rt -> stars == NULL) goto ApplicationStateStarInit_DONE; // configuration successful result = true; ApplicationStateStarInit_DONE:; return result; } boolean ApplicationStateStarHandleEvent(event *e) { boolean result; uint16 *bits, w, h, rb; uint16 *dst; color c; int16 sx, sy; int i; uint8 col; StarType *star; GLOBALS_STAR_ACCESS; // default return value result = false; // entry requirements if ((g_star == NULL) || (g_star -> rt == NULL)) goto ApplicationStateStarHandleEvent_DONE; switch (e -> eType) { case _nilEvent: // get a pointer to the display _FBGetProperties((void **)&bits, &w, &h, &rb); // clear the canvas dst = bits; for (i=0; i> 1); } star = g_star -> rt -> stars; for (i=0; i pos.z <= 0) { STAR_BIRTH:; star -> pos.x = (ApplicationRandom(0) & 0x1fff) - 0x1000; star -> pos.y = (ApplicationRandom(0) & 0x1fff) - 0x1000; star -> pos.z = ((ApplicationRandom(0) & 0x7f) + 0x80) * 16; // speed star -> speed = (ApplicationRandom(0) & 0xff) + 31; } // where is the star on screen? sx = (star -> pos.x << 8) / star -> pos.z + (w >> 1); sy = (star -> pos.y << 8) / star -> pos.z + (h >> 1); // has the star moved out of visible range? if ((sx < 1) || (sx > (w - 1)) || (sy < 1) || (sy > (h - 1))) goto STAR_BIRTH; // what is the color of the star? col = 255 - (star -> pos.z >> 5); c = _FBGetPaletteIndex(col, col, col); // draw the star dst = bits + sx + (sy * (rb >> 1)); *dst = c; if (star -> pos.z < 1536) { // closer stars should be drawn larger *(dst + 1) = c; *(dst + (rb >> 1)) = c; *(dst + (rb >> 1) + 1) = c; } // move the star in 3D space star -> pos.z -= star -> speed; } // the screen must be repainted globals -> dirty = true; result = true; break; default: break; } ApplicationStateStarHandleEvent_DONE:; return result; } void ApplicationStateStarQuit() { GLOBALS_STAR_ACCESS; // entry requirements if (g_star == NULL) goto ApplicationStateStarQuit_DONE; // clean up memory used for stars if (g_star -> rt -> stars != NULL) _MemPtrFree(g_star -> rt -> stars); g_star -> rt -> stars = NULL; if (g_star -> rt != NULL) _MemPtrFree(g_star -> rt); g_star -> rt = NULL; ApplicationStateStarQuit_DONE:; } void ApplicationStateStarTerminate() { GLOBALS_STAR_ACCESS; // entry requirements if (globals == NULL) goto ApplicationStateStarTerminate_DONE; if (g_star != NULL) _MemPtrFree(g_star); globals -> pExt[STATE_STAR] = (void *)NULL; ApplicationStateStarTerminate_DONE:; } #undef GLOBALS_STAR_DEFINE #undef GLOBALS_STAR_ACCESS #undef MAX_STARS /********************************* EOF ***********************************/