/* MasterPiece translated to C by Jared */ #ifndef __MPC__H_ #define __MPC__H_ #include"mx.h" #ifdef FOR_PSP int platform_wait = 3; #else int platform_wait = 40; #endif unsigned long data_length = 0x200; unsigned long data_w = 0x20; unsigned long data_h = 0x10; SDL_Surface *mp_surf = 0, *mpbg_surf = 0; struct mxWin *mp_window = 0; int mp_dim = 0; int game_screen = 2; enum { D_DOWN = 1, D_UP = 2 }; enum { STARTX = 185 , STARTY = 95 }; enum { BLOCK_BLACK = 0, BLOCK_YELLOW, BLOCK_ORANGE, BLOCK_LTBLUE, BLOCK_DBLUE, BLOCK_PURPLE, BLOCK_PINK,BLOCK_GRAY,BLOCK_RED,BLOCK_GREEN,BLOCK_CLEAR, BLOCK_FADE }; // the game object const int BR = 5; void proccess_move_down(); struct Color { int c1,c2,c3; }; struct GameBlock { struct Color col; int x,y; }; char usr_sn[12] = {0}; static int Tiles[8+25][17+25] = { 0 }; static struct GameBlock gblock,nblock; static int score = 0, lines = 0; void fill_rect(int x, int y, int w, int h, unsigned long color) { SDL_Rect rc = { x,y,w,h }; SDL_FillRect(mp_surf, &rc, color); } unsigned long map_rgb(unsigned char r, unsigned char g, unsigned char b) { return SDL_MapRGB(mp_surf->format, r,g,b); } void block_randcolor(struct GameBlock *c) { do { c->col.c1 = 1+(rand()%8); c->col.c2 = 1+(rand()%8); c->col.c3 = 1+(rand()%8); } while ( c->col.c1 == c->col.c2 && c->col.c1 == c->col.c3 ); } void block_copy(struct GameBlock *in, struct GameBlock *out) { out->col.c1 = in->col.c1; out->col.c2 = in->col.c2; out->col.c3 = in->col.c3; } void shiftcolor(struct GameBlock *c, int dir) { int ic1; int ic2; int ic3; ic1 = c->col.c1; ic2 = c->col.c2; ic3 = c->col.c3; if(dir) { c->col.c1 = ic3; c->col.c2 = ic1; c->col.c3 = ic2; } else { c->col.c1 = ic2; c->col.c2 = ic3; c->col.c3 = ic1; } } int rand_table[10]; void proc_move(); void draw_grid(); void move_left(); void move_right(); void init_matrix(); int key_event(struct mxEvent *e); int mp_win(struct mxEvent *e) { static int counter = 0; switch(e->type) { case SDL_RENDER: { static SDL_Rect rc = { 0,0,480,272 }, rc2 = { 0,0,480,248 }; static char buf[256]; sprintf(buf,"Score: %d", score); if(game_screen == 0) { SDL_BlitSurface(mpbg_surf, 0, mp_surf, &mp_window->rc); draw_grid(); if((++counter % platform_wait) == 0) proc_move(); SDL_PrintText(mp_surf, font, 10,10,map_rgb(255,0,0),buf); stretch_blt(mp_surf, &rc, e->cur_surface, &rc2); } else if(game_screen == 1) { static SDL_Rect rc2 = { 0,0,480,200 }; static char buf[1024+100]; SDL_BlitSurface(mpbg_surf, 0, mp_surf,0); if(keyboard.keyboard_up == -1) { keyboard.keyboard_up = 0; } sprintf(buf,"Enter your name: %s%s", keyboard.keybuffer, rand()%2==0 ? "_" : ""); SDL_FillRect(e->cur_surface,0,0); SDL_BlitSurface(mp_surf, 0, e->cur_surface, 0); if(keyboard.keyboard_up != 1) SDL_PrintText(e->cur_surface,font,80,25,SDL_MapRGB(e->cur_surface->format, rand()%255,rand()%255,rand()%255), "[ Press Button 10 to Open keyboard ]"); SDL_PrintText(e->cur_surface,font,80,50,SDL_MapRGB(e->cur_surface->format, 255,255,255), buf); } else if(game_screen == 2) { static SDL_Rect rc2 = {0,0,480,200}; static char buf[256]; if(strlen(usr_sn) == 0) { sprintf(buf,"Click the MasterPiece Icon to Play"); } else { sprintf(buf,"Hello %s, welcome this program is written to practice, so how did you do ? your score was 0x%x\n my high score is 0xFFFFFFFFL hehe\n try agian", usr_sn, score); } SDL_FillRect(e->cur_surface, 0, 0); SDL_FillRect(e->cur_surface,0,0); SDL_BlitSurface(mpbg_surf, 0, mp_surf, &mp_window->rc); SDL_BlitSurface(mp_surf, 0, e->cur_surface, &rc2); SDL_PrintText(e->cur_surface, font, 10,10,SDL_MapRGB(e->cur_surface->format, 255,255,255), buf); } } break; case SDL_MENU_CLICKED: { game_screen = 0; init_matrix(); keyboard.event = key_event; } break; case SDL_JOYBUTTONDOWN: { switch(e->button) { case 0: shiftcolor(&gblock, 0); break; case 1: shiftcolor(&gblock, 1); break; case 6: proc_move(); break; case 7: move_left(); break; case 8: break; case 9: move_right(); break; case 10: { //game_screen = 1; } break; } } break; } return 0; } int key_event(struct mxEvent *e) { if(e->button == 10) game_screen = 1; if(e->type == SDL_KBPRESS) { if(e->key == '=') { strncpy(usr_sn, keyboard.keybuffer, 10); if(keyboard.key_offset > 10) usr_sn[10] = 0; else if(keyboard.key_offset < 1) { strcpy(usr_sn, "Anon"); } else { usr_sn[keyboard.key_offset] = 0; } game_screen = 2; e->key = 0; } } return 0; } void init_window() { static int init_already = 0; if(init_already == 0) { SDL_Rect rc = { 0,0,480,272 }; mp_window = init_windows(0,&rc,0x0,0x0,0x0,"",mp_win); mp_window->full = 1; mp_dim = add_new_dim("mp",mp_window); dimensions[mp_dim].wall = SDL_LoadBMP("mp_bg.bmp"); mp_window->active = 1, mp_window->show = 1; init_already = 1; mp_surf = SDL_CreateRGBSurface(SDL_HWSURFACE, 480,272, front->format->BitsPerPixel,front->format->Rmask, front->format->Gmask, front->format->Bmask, front->format->Amask); mpbg_surf = dimensions[mp_dim].wall; } SDL_WM_SetCaption("MasterXPiece", 0); } void free_mp() { SDL_FreeSurface(mp_surf); } void init_matrix() { int i,z; int total = 0; init_window(); for(i = 0; i < 8; i++) { for(z = 0; z < 17; z++) Tiles[i][z] = 0; } rand_table[1] = map_rgb(255,0,0); rand_table[2] = map_rgb(0,255,0); rand_table[3] = map_rgb(0,0,255); rand_table[4] = map_rgb(255,255,255); rand_table[5] = map_rgb(0,255,255); rand_table[6] = map_rgb(255,0,255); rand_table[7] = map_rgb(255,255,0); rand_table[8] = map_rgb(0x80,0x00,0x80); // ##800080 rand_table[9] = map_rgb(rand()%0xBD,rand()%0x2A,rand()%0xFF); block_randcolor(&gblock); block_randcolor(&nblock); gblock.x = 3; gblock.y = 0; Tiles[16][0] = 0; Tiles[16][3] = 0; // check to see if its all good } // color constants dont stand for the acutal colors of blocks void draw_block(int x, int y, int type) { //draw_bitmap_colored(data_bits, x,y,data_w,data_h,data_w,data_h, 0xFFFFFF00L, colours[type]/2); fill_rect(x,y,data_w,data_h, rand_table[type]); } void draw_grid() { int offset_x = 100, offset_y = 0, offset_y2 = 0; int i,z; fill_rect(100,0,265,282, 0x0); for(i = 0; i < 8; i++) { for(z = 0; z < 17; z++) { if(Tiles[i][z] != 0) draw_block(offset_x+i,offset_y+z, Tiles[i][z]); if(gblock.x == i && gblock.y == z) { draw_block(offset_x+i,offset_y+z, gblock.col.c1); draw_block(offset_x+i,offset_y+z+data_h, gblock.col.c2); draw_block(offset_x+i,offset_y+z+data_h*2,gblock.col.c3); } offset_y += data_h; } offset_y2 = offset_y; offset_y = 0; offset_x += data_w; } } void release_block() { block_copy(&nblock,&gblock); block_randcolor(&nblock); gblock.x = 3; gblock.y = 0; } void game_over() { game_screen = 1; } void setblock() { if(gblock.y <= 3) { // game over game_over(); //init_matrix(); return; } Tiles[gblock.x][gblock.y] = gblock.col.c1; Tiles[gblock.x][gblock.y+1] = gblock.col.c2; Tiles[gblock.x][gblock.y+2] = gblock.col.c3; release_block(); } void proc_move() { static int wait = 0; int platform_wait = 30; proccess_move_down(); if( gblock.y + 3 >= 16 ) { setblock(); return; } if( Tiles[ gblock.x ][ gblock.y + 3] != 0 ) { setblock(); return; } #ifndef FOR_PSP platform_wait = 0; #endif if( gblock.y < 16-3 ) { gblock.y++; } } void move_left() { if( Tiles [ gblock.x-1 ] [ gblock.y ] == 0 && Tiles [ gblock.x-1 ] [ gblock.y + 1 ] == 0 && Tiles [gblock.x-1 ] [ gblock.y + 1 ] == 0 && Tiles [ gblock.x-1 ] [ gblock.y + 3 ] == 0) if(gblock.x > 0) { gblock.x--; } } void move_right() { if( Tiles [ gblock.x+1 ] [ gblock.y ] == 0 && Tiles [ gblock.x+1 ] [ gblock.y + 1 ] == 0 && Tiles [ gblock.x+1 ] [ gblock.y + 1 ] == 0 && Tiles [ gblock.x+1 ] [ gblock.y + 3 ] == 0) if(gblock.x < 7) gblock.x++; } void proc_blocks() { int i,z; for(i = 0; i < 8; i++) { for(z = 0; z < 15; z++) { if(Tiles[i][z] != 0 && Tiles[i][z+1] == 0) { Tiles[i][z+1] = Tiles[i][z]; Tiles[i][z] = 0; } } } } void addline() { score += 5; lines++; } int bounds(int x, int y) { if(x >= 0 && y >= 0) return 1; return 0; } void proccess_move_down() { int i,z; for(i = 0; i < 8; i++) { for(z = 0; z < 17; z++) { int c = Tiles[i][z]; if(c != 0) { if(c == Tiles[i][z+1] && c == Tiles[i][z+2]) { Tiles[i][z] = 0; Tiles[i][z+1] = 0; Tiles[i][z+2] = 0; addline(); return; } if(c == Tiles[i+1][z] && c == Tiles[i+2][z]) { Tiles[i][z] = 0; Tiles[i+1][z] = 0; Tiles[i+2][z] = 0; addline(); return; } if(c == Tiles[i+1][z+1] && c == Tiles[i+2][z+2]) { Tiles[i][z] = 0; Tiles[i+1][z+1] = 0; Tiles[i+2][z+2] = 0; addline(); return; } if(bounds(i-2,z-2) && c == Tiles[i-1][z-1] && c == Tiles[i-2][z-2]) { Tiles[i][z] = 0; Tiles[i-1][z-1] = 0; Tiles[i-2][z-2] = 0; addline(); return ; } if(bounds(i-2,z) && c == Tiles[i-1][z+1] && Tiles[i-2][z+2] == c) { Tiles[i][z] = 0; Tiles[i-1][z+1] = 0; Tiles[i-2][z+2] = 0; addline(); return; } if(bounds(i,z-2) && c == Tiles[i+1][z-1] == c && Tiles[i+2][z-2] == c) { Tiles[i][z] = 0; Tiles[i+1][z-1] = 0; Tiles[i+2][z-2] = 0; addline(); return; } } } } proc_blocks(); } #endif // after all its written in the stars - John Lennon