int pal_or_ntsc_selection() { unsigned short padbuf; int x; game_drawenv.draw_on_display = 1; x = game_drawenv.y; game_drawenv.y = game_dispenv.y; GsSetDrawEnv(&game_drawenv); game_rect.x = 0; game_rect.y = 0; game_rect.w = 320; game_rect.h = 240; game_rect.r = 0; game_rect.g = 0; game_rect.b = 0; game_rect.attribute = 0; GsSortRectangle(&game_rect); game_print("PAL/NTSC SELECTION", 128, 64); game_print("X - PAL", 128, 80); game_print("O - NTSC", 128, 88); GsDrawList(); while(GsIsDrawing()); game_drawenv.draw_on_display = 0; game_drawenv.y = x; GsSetDrawEnv(&game_drawenv); while(1) { PSX_ReadPad(&padbuf, NULL); if((padbuf & PAD_CROSS) && !cross_pressed) return VMODE_PAL; // PAL if((padbuf & PAD_CIRCLE) && !circle_pressed) { //printf("circle_pressed = %d\n", circle_pressed); return VMODE_NTSC; // NTSC } if(!(padbuf & PAD_CROSS)) cross_pressed = 0; if(!(padbuf & PAD_CIRCLE)) circle_pressed = 0; } }
void* cl_recv(void* args){ printf("Pret a recevoir...\n"); char* ch = (char*)malloc(100); char* buff = (char*)malloc(1024); int i,r; Params* p = (Params*)args; int sockfd = p->sock; while(1){ r = recv(sockfd,ch,100,0); if(r < 0){return NULL;} i = 0; while(i<r && ch[i] != '\0'){ if(ch[i] != '\n'){ sprintf(buff,"%s%c",buff,ch[i]); } else{ struct Command c = split_command(buff); puts("\nReception : "); pthread_mutex_lock(&mutex_stock); command_treatment(p->p_game,&c); pthread_mutex_unlock(&mutex_stock); int j = 0; while(j < 1024){ buff[j++] = '\0'; } game_print(p->p_game); } i++; } } pthread_cancel(pth_display); pthread_cancel(pth_recv); return NULL; }
void game_center_print(char *string, int x, int y) { int l = strlen(string); l<<=2; game_print(string, x - l, y); }
/* This function ask a moves, save it, apply it and return 0 if the game is finish (won or tie) */ int next_move(struct game *myGame){ struct move currMove; /* this is need for game_state_transtiction */ // struct state s1; printf("Player: %c\n", myGame->players[myGame->current_player]); /* continue to ask until the move is correct */ while(!move_is_valid(myGame, &currMove)); game_update(myGame, &currMove); // game_state_transition(&myGame->state, &currMove, &s1, myGame); /* update the moves history */ // TODO this can be used as history replay mode // if(update_moves(&currMove) !=0) // return 1; /* print the table */ game_print(myGame); /* check if there is a winner or it is tie */ return is_finish(myGame, &currMove); }
int main(int argc, char **argv) { FILE *game_file; Game *game; int width, height, allow_lines_after_overflow, n, piece_index; char *piece_file_name; if (argc != 2) { fprintf(stderr, "Usage: %s game_file_name\n", argv[0]); exit(1); } initialize_random_generator(time(NULL)); game_file = open_data_file(argv[1], "r"); FREAD(&width, sizeof(int), 1, game_file); FREAD(&height, sizeof(int), 1, game_file); FREAD(&allow_lines_after_overflow, sizeof(int), 1, game_file); FREAD(&n, sizeof(int), 1, game_file); MALLOCN(piece_file_name, char, n); FREAD(piece_file_name, sizeof(char), n, game_file); game = new_game(0, width, height, allow_lines_after_overflow, piece_file_name, NULL); do { FREAD(&game->score, sizeof(int), 1, game_file); FREAD(&piece_index, sizeof(int), 1, game_file); game_set_current_piece_index(game, piece_index); FREAD(game->board->rows, sizeof(int), height, game_file); game_print(stdout, game); } while (!feof(game_file)); exit_random_generator(); return 0; }
/* This function will ALWAYS return 1 if the move is invalid */ int move_is_valid(struct game *myGame, struct move *currMove){ /* we NEED a string since we can read "show" */ char *currMoveString; currMoveString = malloc((myGame->max_input > strlen(SHOW_TABLE) ? myGame->max_input : strlen(SHOW_TABLE)) * sizeof(char)); puts(KINDLY); if(scanf("%s",currMoveString) != 1){ /* if we read EOF, free and bye bye */ free_all(myGame); /* actually i don't like this forced exit(0) */ exit(0); }; /* check if the user inputed 'show' */ if(strcmp(currMoveString, SHOW_TABLE) == 0){ game_print(myGame); /* we should ask again after showed the table */ return 0; }; /* the move input is valid, now we check the numerical values*/ return store_move_in_table(myGame, currMoveString,currMove); }
void game_run() { int x, y, a, b, c; unsigned short padbuf; while(speed_counter > 0) { scc++; if(vibration_cntdown > 0) { printf("vibration = %d\n", vibration_cntdown); pad_enable_vibration(0); pad_set_vibration(0, 0xFF, 0x80); vibration_cntdown--; if(vibration_cntdown == 0) pad_set_vibration(0, 0, 0); } PSX_ReadPad(&padbuf, NULL); if(scc == 5 && !game_over) { if(snake_dir <= SNAKE_DIR_RIGHT) { if(padbuf & PAD_UP) snake_dir = SNAKE_DIR_UP; if(padbuf & PAD_DOWN) snake_dir = SNAKE_DIR_DOWN; } else { if(padbuf & PAD_LEFT) snake_dir = SNAKE_DIR_LEFT; if(padbuf & PAD_RIGHT) snake_dir = SNAKE_DIR_RIGHT; } for(y = 0; y < 29; y++) for(x = 0; x < 40; x++) { if(snake_array[y][x] == snake_size) { switch(snake_dir) { case SNAKE_DIR_LEFT: b = y; a = x-1; break; case SNAKE_DIR_RIGHT: b = y; a = x+1; break; case SNAKE_DIR_UP: b = y-1; a = x; break; case SNAKE_DIR_DOWN: b = y+1; a = x; break; } c = check_snake_collision(a,b); if(c) { snake_array[b][a] = snake_size+1; if(c==2) { snake_size++; score+=100; //printf("%d\n", score); SsKeyOn(2); new_apple(); } } else { vibration_cntdown = 10; game_over = 1; SsKeyOff(0); SsKeyOn(1); scc = 0; } if(snake_array[y][x] == 1 && c!=2) snake_array[y][x] = 0; goto out_of_collision_checking; } } out_of_collision_checking: for(y = 0; y < 29; y++) for(x = 0; x < 40; x++) if(snake_array[y][x]&&snake_array[y][x]<0x80&&c!=2) snake_array[y][x]--; scc = 0; } else if(game_over) { scc++; if(scc >= 510) { if((padbuf & PAD_CROSS) && !cross_pressed) { game_setup(); cross_pressed = 1; } else if((padbuf & PAD_CIRCLE) && !circle_pressed) { circle_pressed = 1; GsSetVideoMode(320, 240, pal_or_ntsc_selection()); game_setup(); } if(!(padbuf & PAD_CROSS)) cross_pressed = 0; if(!(padbuf & PAD_CIRCLE)) circle_pressed = 0; } } speed_counter--; } if(screen_old) { game_rect.x = 0; game_rect.y = 0; game_rect.w = 320; game_rect.h = 240; game_rect.r = 0; game_rect.g = 0; game_rect.b = 0; game_rect.attribute = 0; GsSortRectangle(&game_rect); game_sprite.x = 0; game_sprite.y = 0; game_sprite.w = 256; game_sprite.h = 240; game_sprite.u = 0; game_sprite.v = 0; game_sprite.r = NORMAL_LUMINOSITY; game_sprite.g = NORMAL_LUMINOSITY; game_sprite.b = NORMAL_LUMINOSITY; game_sprite.tpage = 5; game_sprite.attribute = COLORMODE(COLORMODE_16BPP); GsSortSimpleSprite(&game_sprite); game_sprite.x += 256; game_sprite.w = 64; game_sprite.tpage = 9; GsSortSimpleSprite(&game_sprite); game_rect.w = 8; game_rect.h = 8; game_rect.attribute = ENABLE_TRANS | TRANS_MODE(0); for(y = 0; y < 29; y++) { for(x = 0; x < 40; x++) { game_rect.x = x * 8; game_rect.y = y * 8; if(snake_array[y][x] >= 1 && snake_array[y][x] <= 0x7F) { game_rect.r = 0; game_rect.g = 255; game_rect.b = 0; GsSortRectangle(&game_rect); } else if(snake_array[y][x] == 0x80) { game_rect.r = 255; game_rect.g = 0; game_rect.b = 255; GsSortRectangle(&game_rect); } else if(snake_array[y][x] == 0x81) { game_rect.r = 0; game_rect.g = 0; game_rect.b = 128; GsSortRectangle(&game_rect); } } } sprintf(string_buf, "SCORE: %d", score); game_print(string_buf, 0, 232); if(game_over) { game_rect.w = 320; game_rect.h = 240; game_rect.x = 0; game_rect.y = 0; if(scc<=255)x=scc;else x=255; game_rect.r = x; game_rect.g = x; game_rect.b = x; game_rect.attribute = ENABLE_TRANS|TRANS_MODE(2); GsSortRectangle(&game_rect); if(scc>=300) { game_center_print("GAME OVER!", 160, 120); } if(scc>=420) game_center_print("WHAT DO YOU WANT TO DO NOW?", 160, 136); if(scc>=450) game_center_print("PRESS X TO RESTART THE GAME.",160, 152); if(scc>=480) game_center_print("PRESS O FOR PAL/NTSC SELECTION SCREEN.",160,168); if(scc>=510) game_center_print("MADE WITH PSXSDK BY GIUSEPPE GATTA, 2010", 160, 200); } GsDrawList(); // While the graphic synthesizer (video card) is drawing // just sleep. while(GsIsDrawing()); // Swap drawing and display Y position, and swap list array // to use. In this way we achieve a double buffer. if(game_dispenv.y == 0) { game_dispenv.y = 256; game_drawenv.y = 0; } else { game_dispenv.y = 0; game_drawenv.y = 256; } GsSetDispEnv(&game_dispenv); GsSetDrawEnv(&game_drawenv); screen_old = 0; } }