/************************************************************************* * * Copyright 2008+ Mobile 1UP * All rights reserved. * *************************************************************************/ /* * @(#)application-state-fire.inc */ /************************************************************************* * Structures *************************************************************************/ #define FIRE_UNDER_ROWS 4 typedef struct FireRunTime { color palette[256]; uint8 *canvas; boolean initialized; } FireRunTime; typedef struct FireExtension { FireRunTime *rt; } FireExtension; /************************************************************************* * Globals + Preferences *************************************************************************/ #define GLOBALS_FIRE_DEFINE GLOBALS_ACCESS; \ FireExtension *g_fire #define GLOBALS_FIRE_ACCESS GLOBALS_ACCESS; \ FireExtension *g_fire = globals -> pExt[STATE_FIRE] //------------------------------------------------------------------------ // --== 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 ApplicationStateFireInitialize() { boolean result; GLOBALS_FIRE_DEFINE; // default return value result = false; // entry requirements if (globals == NULL) goto ApplicationStateFireInitialize_DONE; g_fire = (FireExtension *)_MemPtrNew(sizeof(FireExtension), false); if (g_fire != NULL) { globals -> pExt[STATE_FIRE] = (void *)g_fire; result = true; } ApplicationStateFireInitialize_DONE:; return result; } boolean ApplicationStateFireInit() { boolean result; uint16 w, h; color *p; int16 i; uint32 size; GLOBALS_FIRE_ACCESS; // default return value result = false; // entry requirements if (g_fire == NULL) goto ApplicationStateFireInit_DONE; // allocate the runtime g_fire -> rt = _MemPtrNew(sizeof(FireRunTime), false); if (g_fire -> rt == NULL) goto ApplicationStateFireInit_DONE; // initialize the palette p = g_fire -> rt -> palette; *p++ = _FBGetPaletteIndex(0,0,0); for (i=1; i<=32; i++) *p++ = _FBGetPaletteIndex((i<<3)-1,0x00, 0x00); for (i=1; i<=64; i++) *p++ = _FBGetPaletteIndex(0xff, (i<<2)-1,0x00); for (i=1; i<=64; i++) *p++ = _FBGetPaletteIndex(0xff, 0xff, (i<<2)-1); for (i=1; i< 96; i++) *p++ = _FBGetPaletteIndex(0xff, 0xff, 0xff); // get a pointer to the display _FBGetProperties(NULL, &w, &h, NULL); // allocate memory for fire canvas size = w * (h + FIRE_UNDER_ROWS) * sizeof(uint8); g_fire -> rt -> canvas = (uint8 *)_MemPtrNew(size, false); if (g_fire -> rt -> canvas == NULL) goto ApplicationStateFireInit_DONE; // we have an empty display right now _MemSet(g_fire -> rt -> canvas, 0, size); g_fire -> rt -> initialized = false; // configuration successful result = true; ApplicationStateFireInit_DONE:; return result; } boolean ApplicationStateFireHandleEvent(event *e) { boolean result; uint16 *bits, w, h, rb; uint16 *dst, x; uint8 *v; int i, j, k, r; GLOBALS_FIRE_ACCESS; // default return value result = false; // entry requirements if ((g_fire == NULL) || (g_fire -> rt == NULL)) goto ApplicationStateFireHandleEvent_DONE; switch (e -> eType) { case _nilEvent: // get a pointer to the display _FBGetProperties((void **)&bits, &w, &h, &rb); k = 0; INIT_LOOP:; // lets inject our canvas with fire spots (two rows) v = g_fire -> rt -> canvas; v += (w * (h+1)); for (i=0; i rt -> canvas; v += (10 * w); for (j=10 - (FIRE_UNDER_ROWS >> 1); j> 2; // the following adds a more realistic view of the fire if (x > 2) { x--; r = (ApplicationRandom(0) % 3); if (r == 0) x--; // cool if (r == 1) x++; // heat } else x = 0; // save the new heat value *(v - w) = (uint8)x; } } k += 3; if ((!g_fire -> rt -> initialized) && (k < h)) goto INIT_LOOP; g_fire -> rt -> initialized = true; // lets map out indexed color to the display dst = bits; v = g_fire -> rt -> canvas; for (j=0; j rt -> palette[*v++]; dst += (rb >> 1) - w; } // the screen must be repainted globals -> dirty = true; result = true; break; default: break; } ApplicationStateFireHandleEvent_DONE:; return result; } void ApplicationStateFireQuit() { GLOBALS_FIRE_ACCESS; // entry requirements if (g_fire == NULL) goto ApplicationStateFireQuit_DONE; // release memory used by canvas if (g_fire -> rt -> canvas != NULL) _MemPtrFree(g_fire -> rt -> canvas); g_fire -> rt -> canvas = NULL; if (g_fire -> rt != NULL) _MemPtrFree(g_fire -> rt); g_fire -> rt = NULL; ApplicationStateFireQuit_DONE:; } void ApplicationStateFireTerminate() { GLOBALS_FIRE_ACCESS; // entry requirements if (globals == NULL) goto ApplicationStateFireTerminate_DONE; if (g_fire != NULL) _MemPtrFree(g_fire); globals -> pExt[STATE_FIRE] = (void *)NULL; ApplicationStateFireTerminate_DONE:; } #undef GLOBALS_FIRE_DEFINE #undef GLOBALS_FIRE_ACCESS #undef FIRE_UNDER_ROWS /********************************* EOF ***********************************/