/*************************************************************************** * __________ __ ___. * Open \______ \ ____ ____ | | _\_ |__ _______ ___ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ * \/ \/ \/ \/ \/ * $Id$ * * Copyright (C) 2005 Kevin Ferrare * * Fire demo plugin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY * KIND, either express or implied. * ****************************************************************************/ #include "plugin.h" #include "lib/helper.h" #ifdef HAVE_LCD_BITMAP #include "lib/pluginlib_actions.h" #include "lib/fixedpoint.h" #ifndef HAVE_LCD_COLOR #include "lib/grey.h" #endif #if (LCD_WIDTH == 112) && (LCD_HEIGHT == 64) /* Archos has not enough plugin RAM for full-width fire :( */ #define FIRE_WIDTH 106 #define FIRE_XPOS 3 #else #define FIRE_WIDTH LCD_WIDTH #define FIRE_XPOS 0 #endif PLUGIN_HEADER #ifndef HAVE_LCD_COLOR GREY_INFO_STRUCT static unsigned char draw_buffer[FIRE_WIDTH]; #endif /* Key assignement */ const struct button_mapping* plugin_contexts[]= { generic_increase_decrease, generic_directions, #if defined(HAVE_REMOTE_LCD) remote_directions, #endif generic_actions }; #define PLA_ARRAY_COUNT sizeof(plugin_contexts)/sizeof(plugin_contexts[0]) #define FIRE_QUIT PLA_QUIT #define FIRE_SWITCH_FLAMES_TYPE PLA_LEFT #define FIRE_SWITCH_FLAMES_MOVING PLA_RIGHT #define FIRE_INCREASE_MULT PLA_INC #define FIRE_DECREASE_MULT PLA_DEC #define MIN_FLAME_VALUE 0 #define COOL_MAX (440/LCD_HEIGHT+2) #ifndef HAVE_LCD_COLOR static unsigned char palette[256]; void color_palette_init(unsigned char* palette) { int i; for(i=0;i<=160;i++)//palette[i]=(3/2)*i palette[i]=(i*3)/2; /* 'regular' fire doesn't exceed this value */ for(;i<=255;i++)//palette[i]=(3/20)*i+216 palette[i]=(i*3+20*217)/20; } #else static fb_data palette[256]; /* * Color palette generation algorithm taken from * the "The Demo Effects Collection" GPL project * Copyright (C) 2002 W.P. van Paassen */ void color_palette_init(fb_data* palette) { int i; for (i = 0; i < 32; i++){ /* black to blue, 32 values*/ palette[i]=LCD_RGBPACK(0, 0, 2*i); /* blue to red, 32 values*/ palette[i + 32]=LCD_RGBPACK(8*i, 0, 64 - 2*i); /* red to yellow, 32 values*/ palette[i + 64]=LCD_RGBPACK(255, 8*i, 0); /* yellow to white, 162 values */ palette[i + 96]=LCD_RGBPACK(255, 255, 0 + 4*i); palette[i + 128]=LCD_RGBPACK(255, 255, 64 + 4*i); palette[i + 160]=LCD_RGBPACK(255, 255, 128 + 4*i); palette[i + 192]=LCD_RGBPACK(255, 255, 192 + i); palette[i + 224]=LCD_RGBPACK(255, 255, 224 + i); } #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256) rb->lcd_pal256_update_pal(palette); #endif } #endif static void tab_init_rand(unsigned char *tab, unsigned int tab_size, int rand_max) { unsigned char *end = tab + tab_size; while(tab < end) *tab++ = (unsigned char)rb->rand() % rand_max; } struct fire { unsigned char fire[LCD_HEIGHT+3][FIRE_WIDTH]; unsigned char cooling_map[LCD_HEIGHT][FIRE_WIDTH]; int flames_type; bool moving; unsigned int mult; }; /* makes the instance a global variable since it's too big to fit on the target's stack */ static struct fire fire; static inline void fire_convolve(struct fire* fire) { unsigned int pixel_value; unsigned int cooling_value; unsigned char *ptr, *end, *cool; unsigned int mult=fire->mult; rb->yield(); /* Convolve the pixels and handle cooling (to add nice shapes effects later) */ cool = &fire->cooling_map[0][0]; ptr = &fire->fire[0][0]; end = ptr + LCD_HEIGHT*FIRE_WIDTH; switch (fire->flames_type){ case 0: do{ pixel_value = ptr[FIRE_WIDTH-1] /* fire[y+1][x-1] */ + ptr[2*FIRE_WIDTH] /* fire[y+2][x] */ + ptr[FIRE_WIDTH+1] /* fire[y+1][x+1] */ + ptr[3*FIRE_WIDTH]; /* fire[y+3][x] */ pixel_value = FMULU(pixel_value, mult) >> 10; cooling_value = *cool++; if (cooling_value <= pixel_value) pixel_value -= cooling_value; /* else it's too cold, don't frost the pixels !!! */ if (pixel_value > 255) pixel_value = 255; *ptr++ = pixel_value; }while (ptr < end); break; case 1: mult -= 2; do{ pixel_value = ptr[FIRE_WIDTH-1] /* fire[y+1][x-1] */ + ptr[FIRE_WIDTH] /* fire[y+1][x] */ + ptr[FIRE_WIDTH+1] /* fire[y+1][x+1] */ + ptr[2*FIRE_WIDTH]; /* fire[y+2][x] */ pixel_value = FMULU(pixel_value, mult) >> 10; cooling_value = *cool++; if (cooling_value <= pixel_value) pixel_value -= cooling_value; /* else it's too cold, don't frost the pixels !!! */ if (pixel_value > 255) pixel_value = 255; *ptr++ = pixel_value; }while (ptr < end); break; default: /* We should never reach this */ break; } rb->yield(); } static void fire_generate_bottom_seed(struct fire* fire) { unsigned char *ptr, *end; ptr = &fire->fire[LCD_HEIGHT][0]; end = ptr + FIRE_WIDTH; do{ *ptr++ = (MIN_FLAME_VALUE + rb->rand() % (256-MIN_FLAME_VALUE)); }while (ptr < end); } static inline void fire_step(struct fire* fire) { if(fire->moving){ /* Randomize the bottom line */ fire_generate_bottom_seed(fire); /* Add here further effects like fire letters, ball ... */ } fire_convolve(fire); } static void fire_init(struct fire* fire) { fire->mult = 261; fire->flames_type=0; fire->moving=true; rb->memset(&fire->fire[0][0], 0, sizeof(fire->fire)); tab_init_rand(&fire->cooling_map[0][0], LCD_HEIGHT*FIRE_WIDTH, COOL_MAX); fire_generate_bottom_seed(fire); } static inline void fire_draw(struct fire* fire) { #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256) rb->lcd_blit_pal256((unsigned char*)&fire->fire[0][0],0,0,0,0,LCD_WIDTH,LCD_HEIGHT); #else int y; unsigned char *src = &fire->fire[0][0]; #ifndef HAVE_LCD_COLOR unsigned char *dest, *end; #else fb_data *dest, *end; #endif for (y = 0; y < LCD_HEIGHT; y++){ #ifndef HAVE_LCD_COLOR dest = draw_buffer; #else dest = rb->lcd_framebuffer + LCD_WIDTH * y + FIRE_XPOS; #endif end = dest + FIRE_WIDTH; do *dest++ = palette[*src++]; while (dest < end); #ifndef HAVE_LCD_COLOR grey_ub_gray_bitmap(draw_buffer, 0, y, FIRE_WIDTH, 1); #endif } #ifdef HAVE_LCD_COLOR rb->lcd_update(); #endif #endif } void cleanup(void *parameter) { (void)parameter; #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(false); #endif #ifndef HAVE_LCD_COLOR grey_release(); #endif /* Turn on backlight timeout (revert to settings) */ backlight_use_settings(); /* backlight control in lib/helper.c */ } #ifndef HAVE_LCD_COLOR int init_grey(void) { unsigned char *gbuf; size_t gbuf_size = 0; /* get the remainder of the plugin buffer */ gbuf = (unsigned char *) rb->plugin_get_buffer(&gbuf_size); if (!grey_init(gbuf, gbuf_size, GREY_ON_COP, FIRE_WIDTH, LCD_HEIGHT, NULL)){ rb->splash(HZ, "not enough memory"); return PLUGIN_ERROR; } /* switch on greyscale overlay */ grey_set_position(FIRE_XPOS, 0); grey_show(true); return PLUGIN_OK; } #endif int main(void) { int action; #ifndef HAVE_LCD_COLOR if(init_grey()!=PLUGIN_OK) return(PLUGIN_ERROR); #endif color_palette_init(palette); #ifdef HAVE_ADJUSTABLE_CPU_FREQ rb->cpu_boost(true); #endif fire_init(&fire); while (true){ fire_step(&fire); fire_draw(&fire); rb->yield(); action = pluginlib_getaction(0, plugin_contexts, PLA_ARRAY_COUNT); switch(action){ case FIRE_QUIT: cleanup(NULL); return PLUGIN_OK; case FIRE_INCREASE_MULT: ++fire.mult; break; case FIRE_DECREASE_MULT: if (fire.mult > 0) --fire.mult; break; case FIRE_SWITCH_FLAMES_TYPE: fire.flames_type = (fire.flames_type + 1) % 2; break; case FIRE_SWITCH_FLAMES_MOVING: fire.moving = !fire.moving; break; default: if (rb->default_event_handler_ex(action, cleanup, NULL) == SYS_USB_CONNECTED) return PLUGIN_USB_CONNECTED; } } } /*************************** Plugin entry point ****************************/ enum plugin_status plugin_start(const void* parameter) { int ret; (void)parameter; #if LCD_DEPTH > 1 rb->lcd_set_backdrop(NULL); #endif /* Turn off backlight timeout */ backlight_force_on(); /* backlight control in lib/helper.c */ #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256) rb->lcd_set_mode(LCD_MODE_PAL256); #endif ret = main(); #if defined(HAVE_LCD_MODES) && (HAVE_LCD_MODES & LCD_MODE_PAL256) rb->lcd_set_mode(LCD_MODE_RGB565); #endif return ret; } #endif /* #ifdef HAVE_LCD_BITMAP */ 5' href='#n215'>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
/*
** $Id$
** Type definitions for Lua objects
** See Copyright Notice in lua.h
*/
#ifndef lobject_h
#define lobject_h
#include <stdarg.h>
#include "llimits.h"
#include "lua.h"
/* tags for values visible from Lua */
#define LAST_TAG LUA_TTHREAD
#define NUM_TAGS (LAST_TAG+1)
/*
** Extra tags for non-values
*/
#define LUA_TPROTO (LAST_TAG+1)
#define LUA_TUPVAL (LAST_TAG+2)
#define LUA_TDEADKEY (LAST_TAG+3)
/*
** Union of all collectable objects
*/
typedef union GCObject GCObject;
/*
** Common Header for all collectable objects (in macro form, to be
** included in other objects)
*/
#define CommonHeader GCObject *next; lu_byte tt; lu_byte marked
/*
** Common header in struct form
*/
typedef struct GCheader {
CommonHeader;
} GCheader;
/*
** Union of all Lua values
*/
typedef union {
GCObject *gc;
void *p;
lua_Number n;
int b;
} Value;
/*
** Tagged Values
*/
#define TValuefields Value value; int tt
typedef struct lua_TValue {
TValuefields;
} TValue;
/* Macros to test type */
#define ttisnil(o) (ttype(o) == LUA_TNIL)
#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
#define ttisstring(o) (ttype(o) == LUA_TSTRING)
#define ttistable(o) (ttype(o) == LUA_TTABLE)
#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION)
#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
/* Macros to access values */
#define ttype(o) ((o)->tt)
#define gcvalue(o) check_exp(iscollectable(o), (o)->value.gc)
#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p)
#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n)
#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value.gc->ts)
#define tsvalue(o) (&rawtsvalue(o)->tsv)
#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value.gc->u)
#define uvalue(o) (&rawuvalue(o)->uv)
#define clvalue(o) check_exp(ttisfunction(o), &(o)->value.gc->cl)
#define hvalue(o) check_exp(ttistable(o), &(o)->value.gc->h)
#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b)
#define thvalue(o) check_exp(ttisthread(o), &(o)->value.gc->th)
#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
/*
** for internal debug only
*/
#define checkconsistency(obj) \
lua_assert(!iscollectable(obj) || (ttype(obj) == (obj)->value.gc->gch.tt))
#define checkliveness(g,obj) \
lua_assert(!iscollectable(obj) || \
((ttype(obj) == (obj)->value.gc->gch.tt) && !isdead(g, (obj)->value.gc)))
/* Macros to set values */
#define setnilvalue(obj) ((obj)->tt=LUA_TNIL)
#define setnvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.n=(x); i_o->tt=LUA_TNUMBER; }
#define setpvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.p=(x); i_o->tt=LUA_TLIGHTUSERDATA; }
#define setbvalue(obj,x) \
{ TValue *i_o=(obj); i_o->value.b=(x); i_o->tt=LUA_TBOOLEAN; }
#define setsvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TSTRING; \
checkliveness(G(L),i_o); }
#define setuvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUSERDATA; \
checkliveness(G(L),i_o); }
#define setthvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTHREAD; \
checkliveness(G(L),i_o); }
#define setclvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TFUNCTION; \
checkliveness(G(L),i_o); }
#define sethvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TTABLE; \
checkliveness(G(L),i_o); }
#define setptvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TPROTO; \
checkliveness(G(L),i_o); }
#define setobj(L,obj1,obj2) \
{ const TValue *o2=(obj2); TValue *o1=(obj1); \
o1->value = o2->value; o1->tt=o2->tt; \
checkliveness(G(L),o1); }
/*
** different types of sets, according to destination
*/
/* from stack to (same) stack */
#define setobjs2s setobj
/* to stack (not from same stack) */
#define setobj2s setobj
#define setsvalue2s setsvalue
#define sethvalue2s sethvalue
#define setptvalue2s setptvalue
/* from table to same table */
#define setobjt2t setobj
/* to table */
#define setobj2t setobj
/* to new object */
#define setobj2n setobj
#define setsvalue2n setsvalue
#define setttype(obj, tt) (ttype(obj) = (tt))
#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
typedef TValue *StkId; /* index to stack elements */
/*
** String headers for string table
*/
typedef union TString {
L_Umaxalign dummy; /* ensures maximum alignment for strings */
struct {
CommonHeader;
lu_byte reserved;
unsigned int hash;
size_t len;
} tsv;
} TString;
#define getstr(ts) cast(const char *, (ts) + 1)
#define svalue(o) getstr(rawtsvalue(o))
typedef union Udata {
L_Umaxalign dummy; /* ensures maximum alignment for `local' udata */
struct {
CommonHeader;
struct Table *metatable;
struct Table *env;
size_t len;
} uv;
} Udata;
/*
** Function Prototypes
*/
typedef struct Proto {
CommonHeader;
TValue *k; /* constants used by the function */
Instruction *code;
struct Proto **p; /* functions defined inside the function */
int *lineinfo; /* map from opcodes to source lines */
struct LocVar *locvars; /* information about local variables */
TString **upvalues; /* upvalue names */
TString *source;
int sizeupvalues;
int sizek; /* size of `k' */
int sizecode;
int sizelineinfo;
int sizep; /* size of `p' */
int sizelocvars;
int linedefined;
int lastlinedefined;
GCObject *gclist;
lu_byte nups; /* number of upvalues */
lu_byte numparams;
lu_byte is_vararg;
lu_byte maxstacksize;
} Proto;
/* masks for new-style vararg */
#define VARARG_HASARG 1
#define VARARG_ISVARARG 2
#define VARARG_NEEDSARG 4
typedef struct LocVar {
TString *varname;
int startpc; /* first point where variable is active */
int endpc; /* first point where variable is dead */
} LocVar;
/*
** Upvalues
*/
typedef struct UpVal {
CommonHeader;
TValue *v; /* points to stack or to its own value */
union {
TValue value; /* the value (when closed) */
struct { /* double linked list (when open) */
struct UpVal *prev;
struct UpVal *next;
} l;
} u;
} UpVal;
/*
** Closures
*/
#define ClosureHeader \
CommonHeader; lu_byte isC; lu_byte nupvalues; GCObject *gclist; \
struct Table *env
typedef struct CClosure {
ClosureHeader;
lua_CFunction f;
TValue upvalue[1];
} CClosure;
typedef struct LClosure {
ClosureHeader;
struct Proto *p;
UpVal *upvals[1];
} LClosure;
typedef union Closure {
CClosure c;
LClosure l;
} Closure;
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->c.isC)
#define isLfunction(o) (ttype(o) == LUA_TFUNCTION && !clvalue(o)->c.isC)
/*
** Tables
*/
typedef union TKey {
struct {
TValuefields;
struct Node *next; /* for chaining */
} nk;
TValue tvk;
} TKey;
typedef struct Node {
TValue i_val;
TKey i_key;
} Node;
typedef struct Table {
CommonHeader;
lu_byte flags; /* 1<<p means tagmethod(p) is not present */
lu_byte lsizenode; /* log2 of size of `node' array */
struct Table *metatable;
TValue *array; /* array part */
Node *node;
Node *lastfree; /* any free position is before this position */
GCObject *gclist;
int sizearray; /* size of `array' array */
} Table;
/*
** `module' operation for hashing (size is always a power of 2)
*/
#define lmod(s,size) \
(check_exp((size&(size-1))==0, (cast(int, (s) & ((size)-1)))))
#define twoto(x) (1<<(x))
#define sizenode(t) (twoto((t)->lsizenode))
#define luaO_nilobject (&luaO_nilobject_)
LUAI_DATA const TValue luaO_nilobject_;
#define ceillog2(x) (luaO_log2((x)-1) + 1)
LUAI_FUNC int luaO_log2 (unsigned int x);
LUAI_FUNC int luaO_int2fb (unsigned int x);
LUAI_FUNC int luaO_fb2int (int x);
LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
va_list argp);
LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
LUAI_FUNC void luaO_chunkid (char *out, const char *source, size_t len);
#endif