--- ./apps/plugins/rockblox.old.c 2005-12-15 08:20:21.643902400 +0100 +++ ./apps/plugins/rockblox.c 2005-12-15 08:17:39.355382200 +0100 @@ -36,72 +36,79 @@ #define ROCKBLOX_RIGHT BUTTON_RIGHT #endif -static const int start_x = 5; -static const int start_y = 5; -static const int max_x = 4 * 17; -static const int max_y = 3 * 10; -static const short level_speeds[10] = { - 1000, 900, 800, 700, 600, 500, 400, 300, 250, 200 -}; -static const int blocks = 7; -static const int block_frames[7] = {1,2,2,2,4,4,4}; - -static int current_x, current_y, current_f, current_b; -static int level, score; -static int next_b, next_f; -static short lines; -static char virtual[LCD_WIDTH * LCD_HEIGHT]; +static int xsz; /* # pixels per block in x */ +static int ysz; /* # pixels per block in y */ +static const int numx = 10; /* # blocks in x */ +static const int numy = 17; /* # blocks in y */ +static short virtmap[17][10]; /* holds the block map */ +static int sep = 1; /* separate blocks from each other */ +static int xs, ys; +static int horizontal; /* default: vertical block flow */ +static int preview = 1; /* default: show 'coming next' */ +static int use_gray = 1; /* default: do not draw grayed blocks */ +static int start_game; +static int level; +static int lines; +static int curr_x, curr_y, curr_b, curr_a, curr_c, next_b, next_a, next_c; +static const short level_speeds[8] = { 20, 18, 16, 14, 12, 10, 8, 6 }; static struct plugin_api* rb; +#ifdef HAVE_LCD_COLOR +static struct rgb color = { 0x80, 0x80, 0x80 }; +#endif + /* - block_data is built up the following way + block_data is built up the following way: - first array index specifies the block number - second array index specifies the rotation of the block - third array index specifies: + 1st array index (0...6) specifies the primitive number + 2nd array index (0...3) specifies the rotation of the primitive + 3rd array index (0...1) specifies: 0: x-coordinates of pixels 1: y-coordinates of pixels - fourth array index specifies the coordinate of a pixel + 4th array index (0...3) specifies the coordinate of a block - each block consists of four pixels whose relative coordinates are given + each primitive consists of four blocks whose relative coordinates are given with block_data */ +static const int blocks = 7; + static const char block_data[7][4][2][4] = { - { - {{0,1,0,1},{0,0,1,1}} - }, - { - {{0,1,1,2},{1,1,0,0}}, - {{0,0,1,1},{0,1,1,2}} - }, - { - {{0,1,1,2},{0,0,1,1}}, - {{1,1,0,0},{0,1,1,2}} - }, - { - {{1,1,1,1},{0,1,2,3}}, - {{0,1,2,3},{2,2,2,2}} - }, - { - {{1,1,1,2},{2,1,0,0}}, - {{0,1,2,2},{1,1,1,2}}, - {{0,1,1,1},{2,2,1,0}}, - {{0,0,1,2},{0,1,1,1}} - }, - { - {{0,1,1,1},{0,0,1,2}}, - {{0,1,2,2},{1,1,1,0}}, - {{1,1,1,2},{0,1,2,2}}, - {{0,0,1,2},{2,1,1,1}} - }, - { - {{1,0,1,2},{0,1,1,1}}, - {{2,1,1,1},{1,0,1,2}}, - {{1,0,1,2},{2,1,1,1}}, - {{0,1,1,1},{1,0,1,2}} - } + { {{1,2,1,2},{1,1,2,2}}, /* o o */ + {{1,2,1,2},{1,1,2,2}}, /* o o */ + {{1,2,1,2},{1,1,2,2}}, + {{1,2,1,2},{1,1,2,2}} }, + + { {{2,1,1,0},{1,1,2,2}}, /* o o */ + {{1,1,2,2},{0,1,1,2}}, /* o o */ + {{2,1,1,0},{1,1,2,2}}, + {{1,1,2,2},{0,1,1,2}} }, + + { {{0,1,1,2},{1,1,2,2}}, /* o o */ + {{2,2,1,1},{0,1,1,2}}, /* o o */ + {{0,1,1,2},{1,1,2,2}}, + {{2,2,1,1},{0,1,1,2}} }, + + { {{1,1,1,1},{0,1,2,3}}, /* o */ + {{0,1,2,3},{2,2,2,2}}, /* o */ + {{1,1,1,1},{0,1,2,3}}, /* o */ + {{0,1,2,3},{2,2,2,2}} }, /* o */ + + { {{1,2,1,1},{0,0,1,2}}, /* o o */ + {{0,1,2,2},{1,1,1,2}}, /* o */ + {{1,1,0,1},{0,1,2,2}}, /* 0 */ + {{0,0,1,2},{0,1,1,1}} }, + + { {{0,1,1,1},{0,0,1,2}}, /* o o */ + {{2,0,1,2},{0,1,1,1}}, /* o */ + {{1,1,1,2},{0,1,2,2}}, /* o */ + {{0,1,2,0},{1,1,1,2}} }, + + { {{1,0,1,2},{0,1,1,1}}, /* o */ + {{1,1,2,1},{0,1,1,2}}, /* o o o */ + {{0,1,2,1},{1,1,1,2}}, + {{1,1,0,1},{0,1,1,2}} } }; static int t_rand(int range) @@ -109,86 +116,112 @@ return *rb->current_tick % range; } -static void draw_frame(int fstart_x,int fstop_x,int fstart_y,int fstop_y) +static void rockblox_set_foreground(int col) { - rb->lcd_drawline(fstart_x, fstart_y, fstop_x, fstart_y); - rb->lcd_drawline(fstart_x, fstop_y, fstop_x, fstop_y); + (void)col; - rb->lcd_drawline(fstart_x, fstart_y, fstart_x, fstop_y); - rb->lcd_drawline(fstop_x, fstart_y, fstop_x, fstop_y); +#if LCD_DEPTH > 1 +#ifdef HAVE_LCD_COLOR + rb->lcd_set_foreground(color); +#else + rb->lcd_set_foreground(col); +#endif +#endif +} + +static void draw_frame(int x, int y, int nx, int ny) +{ + int xb, yb, ix, iy; + + if(horizontal) /* rotate image */ + { + xb = ys - y - ny + ysz - 1; + yb = xs + x - 1 - sep; + ix = ny + 2; + iy = nx + 2 + sep; + } + else + { + xb = xs + x - 1; + yb = ys + y - 1; + ix = nx + 2 + sep; + iy = ny + 2; + } - rb->lcd_drawline(fstart_x - 1, fstart_y + 1, fstart_x - 1, fstop_y + 1); - rb->lcd_drawline(fstart_x - 1, fstop_y + 1, fstop_x - 1, fstop_y + 1); + rb->lcd_drawrect(xb, yb, ix, iy); + + /* draw first shadow line*/ + rb->lcd_drawline(xb, yb+iy, xb+ix, yb+iy); + /* draw secnd shadow line*/ + if(horizontal) rb->lcd_drawline(xb-1, yb, xb-1, yb+iy); + else rb->lcd_drawline(xb+ix, yb, xb+ix, yb+iy); } -static void draw_block(int x, int y, int block, int frame, bool clear) +static void clearrect(int x, int y, int nx, int ny) { - int i, a, b; + rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); + rb->lcd_fillrect(x, y, nx, ny); + rb->lcd_set_drawmode(DRMODE_SOLID); +} + +static void draw_block(int xs, int ys, int x, int y, int block, int angl, int col, int clear) +{ + int i; + void (*drawrect)(int x, int y, int nx, int ny) = clear ? clearrect : rb->lcd_fillrect; + + rockblox_set_foreground(3 - col); + + for(i=0; i<4; i++) + { + if(y + block_data[block][angl][1][i] >= 0) - for(i=0;i < 4;i++) { - if (clear) - { - rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); - for (a = 0; a < 3; a++) - for (b = 0; b < 4; b++) - rb->lcd_drawpixel(start_x + x + block_data[block][frame][1][i] * 4 - b, - start_y + y + block_data[block][frame][0][i] * 3 + a); - rb->lcd_set_drawmode(DRMODE_SOLID); - } - else { - for (a = 0; a < 3; a++) - for (b = 0; b < 4; b++) - rb->lcd_drawpixel(start_x+x+block_data[block][frame][1][i] * 4 - b, - start_y+y+block_data[block][frame][0][i] * 3 + a); + if(horizontal) /* rotate image */ + drawrect(sep + ys - (y + block_data[block][angl][1][i]) * ysz, + xs + (x + block_data[block][angl][0][i]) * xsz, + ysz-sep, xsz-sep); + else + drawrect(sep + xs + (x + block_data[block][angl][0][i]) * xsz, + ys + (y + block_data[block][angl][1][i]) * ysz, + xsz-sep, ysz-sep); } } + + rockblox_set_foreground(0); } -static void to_virtual(void) +static void to_virtmap(unsigned char color) { - int i, a, b; + int i; - for(i = 0; i < 4; i++) - for (a = 0; a < 3; a++) - for (b = 0; b < 4; b++) - *(virtual + - (current_y + block_data[current_b][current_f][0][i] * 3 + a) * - max_x + current_x + block_data[current_b][current_f][1][i] * - 4 - b) = current_b + 1; + /* set the appropriate pixel */ + for(i=0; i<4; i++) + if(curr_y + block_data[curr_b][curr_a][1][i] >= 0) + virtmap[curr_y + block_data[curr_b][curr_a][1][i]] + [curr_x + block_data[curr_b][curr_a][0][i]] = color; } -static bool block_touch (int x, int y) +static bool block_touch(int x, int y) { - int a,b; - for (a = 0; a < 4; a++) - for (b = 0; b < 3; b++) - if (*(virtual + (y + b) * max_x + (x - a)) != 0) - return true; - return false; + if(y < 0) + return false; + + return virtmap[y][x] ? true : false; } static bool gameover(void) { int i; - int frame, block, y, x; - - x = current_x; - y = current_y; - block = current_b; - frame = current_f; - - for(i = 0; i < 4; i++){ + + for(i=0; i<4; i++) + { /* Do we have blocks touching? */ - if(block_touch(x + block_data[block][frame][1][i] * 4, - y + block_data[block][frame][0][i] * 3)) + if(block_touch(curr_x + block_data[curr_b][curr_a][0][i], + curr_y + block_data[curr_b][curr_a][1][i])) { /* Are we at the top of the frame? */ - if(x + block_data[block][frame][1][i] * 4 >= max_x - 16) - { - /* Game over ;) */ - return true; - } + if(curr_y + block_data[curr_b][curr_a][1][i] < 2) + return true; /* Game over */ } } return false; @@ -197,204 +230,201 @@ static bool valid_position(int x, int y, int block, int frame) { int i; - for(i=0;i < 4;i++) - if ((y + block_data[block][frame][0][i] * 3 > max_y - 3) || - (x + block_data[block][frame][1][i] * 4 > max_x - 4) || - (y + block_data[block][frame][0][i] * 3 < 0) || - (x + block_data[block][frame][1][i] * 4 < 4) || - block_touch (x + block_data[block][frame][1][i] * 4, - y + block_data[block][frame][0][i] * 3)) - { + + for(i=0; i<4; i++) + if((x + block_data[block][frame][0][i] >= numx) || + (y + block_data[block][frame][1][i] >= numy) || + (x + block_data[block][frame][0][i] < 0) || + block_touch(x + block_data[block][frame][0][i], + y + block_data[block][frame][1][i])) return false; - } - return true; + + return true; } -static void from_virtual(void) +static void from_virtmap(void) { - int x,y; + int x, y, col; + void (*drawrect)(int x, int y, int nx, int ny); + + for(y=0; ylcd_fillrect : clearrect; - for(y = 0; y < max_y; y++) - for(x = 1; x < max_x - 1; x++) - if(*(virtual + (y * max_x) + x) != 0) - { - rb->lcd_drawpixel(start_x + x, start_y + y); - } + rockblox_set_foreground(3 - col); + + if(horizontal) /* rotate image */ + drawrect(sep + ys - y * ysz, xs + x * xsz, ysz-sep, xsz-sep); else - { - rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); - rb->lcd_drawpixel(start_x + x, start_y + y); - rb->lcd_set_drawmode(DRMODE_SOLID); - } + drawrect(sep + xs + x * xsz, ys + y * ysz, xsz-sep, ysz-sep); + } + } + + rockblox_set_foreground(0); } -static void move_block(int x,int y,int f) +static void move_block(int x, int y, int angl) { - int last_frame = current_f; - if(f != 0) - { - current_f += f; - if(current_f > block_frames[current_b]-1) - current_f = 0; - if(current_f < 0) - current_f = block_frames[current_b]-1; - } - - if(valid_position(current_x + x, current_y + y, current_b, current_f)) + int last_a = curr_a; + + curr_a = (curr_a + angl) % 4; /* rotate 90 deg */ + + if(valid_position(curr_x + x, curr_y + y, curr_b, curr_a)) { - draw_block(current_x,current_y,current_b,last_frame,true); - current_x += x; - current_y += y; - draw_block(current_x,current_y,current_b,current_f,false); + draw_block(xs, ys, curr_x, curr_y, curr_b, last_a, 0, true); + curr_x += x; + curr_y += y; + draw_block(xs, ys, curr_x, curr_y, curr_b, curr_a, curr_c, false); rb->lcd_update(); } else - current_f = last_frame; + curr_a = last_a; } static void new_block(void) { - current_b = next_b; - current_f = next_f; - current_x = max_x - 16; - current_y = (int)12; + curr_b = next_b; + curr_a = next_a; + curr_c = next_c; next_b = t_rand(blocks); - next_f = t_rand(block_frames[next_b]); + next_a = t_rand(4); + next_c = use_gray ? t_rand(3) + 1 : 3; + curr_x = 4; + curr_y = -block_data[next_b][next_a][1][3]; - rb->lcd_drawline (max_x + 7, start_y - 1, max_x + 29, start_y - 1); - rb->lcd_drawline (max_x + 29, start_y, max_x + 29, start_y + 14); - rb->lcd_drawline (max_x + 29, start_y + 14, max_x + 7, start_y + 14); - rb->lcd_drawline (max_x + 7, start_y + 14, max_x + 7, start_y - 1); - rb->lcd_drawline (max_x + 6, start_y + 15, max_x + 6, start_y); - rb->lcd_drawline (max_x + 6, start_y + 15, max_x + 28, start_y + 15); - - draw_block(max_x + 9, start_y - 4, current_b, current_f, true); - draw_block(max_x + 9, start_y - 4, next_b, next_f, false); - if(!valid_position(current_x, current_y, current_b, current_f)) + if(preview) { - draw_block(current_x, current_y, current_b, current_f, false); - rb->lcd_update(); + if(horizontal) /* rotate image */ + { + draw_block(xs+(numx-4)*xsz/2, ys+4*ysz+6, 0, 0, curr_b, curr_a, 0, true); + draw_block(xs+(numx-4)*xsz/2, ys+4*ysz+6, 0, 0, next_b, next_a, next_c, false); + } + else + { + draw_block(xs+(numx+2)*xsz, ys, 0, 0, curr_b, curr_a, 0, true); + draw_block(xs+(numx+2)*xsz, ys, 0, 0, next_b, next_a, next_c, false); + } } - else - draw_block(current_x, current_y, current_b, current_f, false); + + draw_block(xs, ys, curr_x, curr_y, curr_b, curr_a, curr_c, false); + + rb->lcd_update(); } static int check_lines(void) { - int x,y,i,j; - bool line; - int lines = 0; - for(x = 0; x < max_x; x++) + int x, y, i, lines = 0; + + for(y=0; y0; i--) + rb->memcpy(virtmap+i, virtmap+i-1, sizeof(virtmap[0])); + + y--; /* re-check current line */ } } - - return lines / 4; + + return lines; } static void move_down(void) { - int l; - char s[25]; - - if(!valid_position(current_x - 4, current_y, current_b, current_f)) + int l; + char s[20]; + + if(!valid_position(curr_x, curr_y+1, curr_b, curr_a)) { - to_virtual(); + to_virtmap(curr_c); l = check_lines(); if(l) { lines += l; - level = (int)lines/10; - if(level > 9) - level = 9; - from_virtual(); - score += l*l; + level = (int)lines / 10; + if(level > 7) + level = 7; + from_virtmap(); + } + + if(horizontal) /* rotate image */ + { + rb->snprintf(s, sizeof(s), "%d Rows - Level %d", lines, level); + rb->lcd_putsxy(8, xs+numx*xsz+10, s); + } + else + { + rb->snprintf(s, sizeof(s), "Rows:%2d", lines); + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+ 4, s); + rb->snprintf(s, sizeof(s), "Level:%d", level); + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+14, s); } - rb->snprintf(s, sizeof(s), "%d Rows - Level %d", lines, level); - rb->lcd_putsxy(2, 42, s); + for(l=0; lbutton_get_w_tmo(HZ/10); - switch(button) + /* skip all button release events */ + while((button = rb->button_get_w_tmo(HZ/10)) & BUTTON_REL); + + if(button == BUTTON_OFF) return PLUGIN_OK; + else if(button & butt[0]) move_block(0,0,1); /* rotat */ + else if(button & butt[1]) move_down(); /* down */ + else if(button & butt[2]) move_block( 1,0,0); /* right */ + else if(button & butt[3]) move_block(-1,0,0); /* left */ + else if(rb->default_event_handler(button) == SYS_USB_CONNECTED) + { *ret = PLUGIN_USB_CONNECTED; return false; } + + if(gameover()) { - case ROCKBLOX_OFF: - return PLUGIN_OK; - - case ROCKBLOX_UP: - case ROCKBLOX_UP | BUTTON_REPEAT: - move_block(0,-3,0); - break; - - case ROCKBLOX_DOWN: - case ROCKBLOX_DOWN | BUTTON_REPEAT: - move_block(0,3,0); - break; - - case ROCKBLOX_RIGHT: - case ROCKBLOX_RIGHT | BUTTON_REPEAT: - move_block(0,0,1); - break; - - case ROCKBLOX_LEFT: - case ROCKBLOX_LEFT | BUTTON_REPEAT: - move_down(); - break; - - default: - if (rb->default_event_handler(button) == SYS_USB_CONNECTED) - return PLUGIN_USB_CONNECTED; - break; + if(horizontal) + rb->lcd_putsxy(8, xs+numx*xsz+10, " Game over! "); + else + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+24, "Game over!"); + rb->lcd_update(); + rb->sleep(HZ * 2); + if(horizontal) + rb->lcd_putsxy(8, xs+numx*xsz+10, " NewGame >> "); + else + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+24, "NewGame >>"); + rb->lcd_update(); + rb->button_clear_queue(); + + if((button = rb->button_get(true)) & BUTTON_RIGHT) + return true; + + if(rb->default_event_handler(button) == SYS_USB_CONNECTED) + *ret = PLUGIN_USB_CONNECTED; + + return false; } - - count++; } - - if(gameover()) - { - rb->lcd_set_drawmode(DRMODE_SOLID|DRMODE_INVERSEVID); - rb->lcd_fillrect(0, 52, LCD_WIDTH, LCD_HEIGHT - 52); - rb->lcd_set_drawmode(DRMODE_SOLID); - rb->lcd_putsxy(2, 52, "You lose!"); - rb->lcd_update(); - rb->sleep(HZ * 3); - return false; - } - move_down(); } @@ -403,22 +433,44 @@ static void init_rockblox(void) { - rb->memset(&virtual, 0, sizeof(virtual)); + rb->memset(virtmap, 0, sizeof(virtmap)); + + level = 0; + lines = 0; + next_b = t_rand(blocks); + next_a = t_rand(4); + next_c = use_gray ? t_rand(3) + 1 : 3; + +#if defined(IRIVER_H100) || defined(IRIVER_H120) + xsz = 7; /* # pixels per block in x */ + ysz = 7; /* # pixels per block in y */ + xs = horizontal ? 8 : 20; /* rotated? */ + ys = horizontal ? numy*ysz-2 : 4; /* rotated? */ +#else /* ARCHOS with 112 x 64 pixel */ + xsz = horizontal ? 3 : 4; /* rotated? */ + ysz = horizontal ? 4 : 3; /* rotated? */ + xs = horizontal ? 4 : 4; /* rotated? */ + ys = horizontal ? numy*ysz+8 : 5; /* rotated? */ +#endif - current_x = 0; - current_y = 0; - current_f = 0; - current_b = 0; - level = 0; - lines = 0; - score = 0; - next_b = 0; - next_f = 0; + if(horizontal && !preview) + { + xsz++; /* increase blocksize */ + ysz++; /* increase blocksize */ + ys += numy; + } +#if !defined(IRIVER_H100) && !defined(IRIVER_H120) + else + sep = 0; /* don't separate blocks at small blocksizes */ +#endif } enum plugin_status plugin_start(struct plugin_api* api, void* parameter) { - int ret; + int butt, pos, npos, xm, ym, ret = PLUGIN_OK; + const char *vstg[] = { "Vertical: Yes", "Vertical: No" }; + const char *pstg[] = { "Preview: No", "Preview: Yes" }; + const char *cstg[] = { "GrayBlock: No", "GrayBlock: Yes" }; TEST_PLUGIN_API(api); @@ -428,16 +480,72 @@ /* Lets use the default font */ rb->lcd_setfont(FONT_SYSFIXED); - init_rockblox(); + xm = LCD_WIDTH /2 - 42; /* center menu in x */ + ym = LCD_HEIGHT/2 - 26; /* center menu in y */ + pos = 4; /* menu position */ +#if defined(IRIVER_H100) || defined(IRIVER_H120) + npos = 4; /* # menu entries */ +#else + npos = 3; /* # menu entries */ +#endif - draw_frame(start_x, start_x + max_x - 1, start_y - 1, start_y + max_y); - rb->lcd_putsxy(2, 42, "0 Rows - Level 0"); - rb->lcd_update(); + while(!start_game) + { + switch(butt = rb->button_get_w_tmo(HZ/10)) + { + case BUTTON_OFF: return PLUGIN_OK; + case BUTTON_UP: if(pos > 1 ) pos--; break; + case BUTTON_DOWN: if(pos < npos) pos++; break; + + case BUTTON_LEFT: + case BUTTON_RIGHT: if(pos == 1 ) horizontal ^= 1; + if(pos == 2 ) preview ^= 1; + if(pos == 3 ) use_gray ^= 1; + if(pos == npos) start_game = 1; + break; - next_b = t_rand(blocks); - next_f = t_rand(block_frames[next_b]); - new_block(); - ret = game_loop(); + default: if(rb->default_event_handler(butt) == SYS_USB_CONNECTED) + return PLUGIN_USB_CONNECTED; /* immediately */ + } + + rb->lcd_clear_display(); + rb->lcd_putsxy(xm, ym + 0*11, "-- Settings --"); + rb->lcd_putsxy(xm, ym + 1*11, vstg[horizontal]); + rb->lcd_putsxy(xm, ym + 2*11, pstg[preview] ); + rb->lcd_putsxy(xm, ym + 3*11, cstg[use_gray] ); + rb->lcd_putsxy(xm, ym + npos*11, "Play Game >> "); + rb->lcd_set_drawmode(DRMODE_COMPLEMENT); + rb->lcd_fillrect(xm-6, ym+pos*11-1, 16*6, 10); + rb->lcd_set_drawmode(DRMODE_SOLID); + rb->lcd_update(); + } + + do + { + rb->lcd_clear_display(); + init_rockblox(); + draw_frame(0, 0, xsz*numx, ysz*numy); /* main frame */ + + if(horizontal) + { + if(preview) + draw_frame((numx-4)*xsz/2, -4*ysz-6, 4*xsz, 4*ysz); /* preview frame */ + + rb->lcd_putsxy(8, xs+numx*xsz+10, "0 Rows - Level 0"); + } + else + { + if(preview) + draw_frame(xsz*numx+2*xsz, 0, 4*xsz, 4*ysz); /* preview frame */ + + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+ 4, "Rows: 0"); + rb->lcd_putsxy(xs+xsz*numx+10, ys+ysz*5+14, "Level:0"); + } + + rb->lcd_update(); + new_block(); + } + while(game_loop(&ret)); rb->lcd_setfont(FONT_UI);