static void __stop(int unused) { #pragma ref unused static bool stopped = false; if (stopped) { return; } stopped = true; puts("Stopping morrigan."); if (input) { bdestroy(input); } net_stop(); game_stop(); server_stop(); if (l) { landscape_destroy(l); l = NULL; } }
static void game_handle_input(game_t *g) { while(SDL_PollEvent(&g->event)) { if(g->event.type == SDL_QUIT) game_stop(g); else if(g->event.type == SDL_KEYDOWN) { if(g->event.key.keysym.sym == SDLK_UP) g->state->player_move_up = 1; if(g->event.key.keysym.sym == SDLK_DOWN) g->state->player_move_down = 1; if(g->event.key.keysym.sym == SDLK_LEFT) g->state->player_move_left = 1; if(g->event.key.keysym.sym == SDLK_RIGHT) g->state->player_move_right = 1; if(g->event.key.keysym.sym == SDLK_SPACE) g->state->player_fire = 1; } else if(g->event.type == SDL_KEYUP) { if(g->event.key.keysym.sym == SDLK_UP) g->state->player_move_up = 0; if(g->event.key.keysym.sym == SDLK_DOWN) g->state->player_move_down = 0; if(g->event.key.keysym.sym == SDLK_LEFT) g->state->player_move_left = 0; if(g->event.key.keysym.sym == SDLK_RIGHT) g->state->player_move_right = 0; if(g->event.key.keysym.sym == SDLK_SPACE) g->state->player_fire = 0; } } }
void game_destroy() { game_stop(); pthread_mutex_destroy(&fd_mutex); close(pipe_input[0]); close(pipe_input[1]); close(pipe_output[0]); close(pipe_output[1]); return; }
void game_network_player_manager_stop(GameManager * g) { GameNetworkPlayerManager * manager; UiMain * ui_main = ui_main_get_instance(); manager = GAME_NETWORK_PLAYER_MANAGER(g); game_stop( GAME(PRIVATE(manager)->current_game)); g_signal_handlers_disconnect_matched( G_OBJECT( PRIVATE(manager)->current_game ), G_SIGNAL_MATCH_DATA,0,0,NULL,NULL,manager); g_object_unref( PRIVATE(manager)->current_game); PRIVATE(manager)->current_game = NULL; ui_main_set_game(ui_main,NULL); }
//退出游戏 void quit_game() { game_stop(); gtk_main_quit(); g_object_unref(pixmap); }
gboolean play_game(gpointer data) { int i; if (playing == FALSE) { //如果游戏还没开始,初始化游戏并开始 init_game(); set_label(); playing = TRUE; return TRUE; } snake_run(); switch (snake.direction) { case UP: snake.body[0].y = snake.body[0].y - 1; //判断是否有向自己身体走的情况,如果有就改变方向 //头部[0]和身体[2]的坐标(x或y)相同,就需要向反方向改变 if (snake.body[0].y == snake.body[2].y) { snake.direction = DOWN; snake.body[0].y = snake.body[0].y + 2; } break; case DOWN: snake.body[0].y = snake.body[0].y + 1; if (snake.body[0].y == snake.body[2].y) { snake.direction = UP; snake.body[0].y = snake.body[0].y - 2; } break; case LEFT: snake.body[0].x = snake.body[0].x - 1; if (snake.body[0].x == snake.body[2].x) { snake.direction = RIGHT; snake.body[0].x = snake.body[0].x + 2; } break; case RIGHT: snake.body[0].x = snake.body[0].x + 1; if (snake.body[0].x == snake.body[2].x) { snake.direction = LEFT; snake.body[0].x = snake.body[0].x - 2; } break; default: break; } //判断蛇的头部是否碰到了自己的身体,如果碰到了就去死! for (i = 4; i <= snake.length; i++) if (snake.body[0].x == snake.body[i].x && snake.body[0].y == snake.body[i].y) snake.live = FALSE; //判断蛇是否碰到边界,如果碰到了就去死! if (snake.body[0].x < 0 || snake.body[0].y < 0 || snake.body[0].x > 40 || snake.body[0].y > 40) { snake.live = FALSE; } //判断蛇是否死了 if (she.live == FALSE) { live = live - 1; if (live == 0) { //没命就GAME OVER speed = 200; level = 1; foods = 20; live = 3; game_stop(); gtk_label_set_text(GTK_LABEL(label), " - Game Over -"); return FALSE; } //还有命就继续 set_label(); init_game(); } //判断蛇是否吃到了蛋,如果吃到了就变长! if (snake.body[0].x == food.x && snake.body[0].y == food.y) { snake.length = snake.length + 1; snake.body[snake.length].x = snake.body[snake.length - 1].x; snake.body[snake.length].y = snake.body[snake.length - 1].y; //重新生成一个蛋 food.x = my_rand(0, 20); food.y = food.x; foods = foods - 1; //减少一个蛋 if (foods == 0) { //如果吃完全部的蛋,进入下一局,速度加快 level = level + 1; foods = 20; if (speed > 30) speed = speed - 20; game_stop(); game_start(); } set_label(); } draw(); return TRUE; }
//---------------------------------------------------------------------------------------------------------------------- // process_commands //---------------------------------------------------------------------------------------------------------------------- static void process_commands(voglperf_data_t &data) { static const char *s_commands[] = { "game start [steamid | filename]: Start game.", "game stop: Send SIGTERM signal to game.", "game set (steamid | filename): Set gameid to launch.", "game args: set game arguments.", "logfile start [seconds]: Start capturing frame time data to filename.", "logfile stop: Stop capturing frame time data.", "status: Print status and options.", "quit: Quit voglperfrun.", }; for (size_t i = 0; i < data.commands.size(); i++) { bool handled = false; std::string ws_reply; std::vector<std::string> args; std::string &command = data.commands[i]; printf("> %s\n", command.c_str()); string_split(args, command, " "); bool on = (args[1] == "on" || args[1] == "1"); bool off = (args[1] == "off" || args[1] == "0"); if (on && (data.run_data.pid != (uint64_t)-1) && (args[0] == "logfile")) { // Special case someone typing "logfile on" while the game is running. // Turn it into a "logfile start" command. args[1] = "start"; } else if (!args[1].size() || on || off) { unsigned int flags_orig = data.flags; for (size_t j = 0; j < sizeof(g_options) / sizeof(g_options[0]); j++) { if (args[0] == g_options[j].name) { if (on) data.flags |= g_options[j].flag; else if(off) data.flags &= ~g_options[j].flag; ws_reply += string_format("%s: %s\n", g_options[j].name, (data.flags & g_options[j].flag) ? "On" : "Off"); // This is a launch option and the game is already running - warn them. if (on && g_options[j].launch_setting && (data.run_data.pid != (uint64_t)-1)) ws_reply += " Option used with next game launch...\n"; handled = true; } } if (data.run_data.pid != (uint64_t)-1) { // If the verbose or fpsshow args have changed, send msg. if ((flags_orig ^ data.flags) & (F_VERBOSE | F_FPSSHOW)) { mbuf_options_t mbuf; mbuf.mtype = MSGTYPE_OPTIONS; mbuf.fpsshow = !!(data.flags & F_FPSSHOW); mbuf.verbose = !!(data.flags & F_VERBOSE); int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } } } } if (handled) { // Handled with g_options above... } else if (args[0] == "status") { ws_reply += get_vogl_status_str(data); handled = true; } else if (args[0] == "help") { ws_reply += "Commands:\n"; for (size_t j = 0; j < sizeof(s_commands) / sizeof(s_commands[0]); j++) ws_reply += string_format(" %s\n", s_commands[j]); for (size_t j = 0; j < sizeof(g_options) / sizeof(g_options[0]); j++) ws_reply += string_format(" %s [on | off]: %s\n", g_options[j].name, g_options[i].desc); handled = true; } else if (args[0] == "quit" || args[0] == "q" || args[0] == "exit") { data.flags |= F_QUIT; ws_reply += "Quitting...\n"; handled = true; } else if (args[0] == "game") { if (args[1] == "args") { size_t pos = command.find("args") + 5; data.game_args = command.substr(pos); handled = true; } else if ((args[1] == "set") && args[2].size()) { data.gameid = args[2]; ws_reply += "Gameid set to '" + data.gameid + "'"; handled = true; } else if (args[1] == "start") { if (args[2].size()) data.gameid = args[2]; game_start(data); handled = true; } else if (args[1] == "stop") { game_stop(data); handled = true; } } else if (args[0] == "logfile") { if (data.run_data.pid == (uint64_t)-1) { ws_reply += "ERROR: Game not running.\n"; handled = true; } else if (args[1] == "start") { mbuf_logfile_start_t mbuf; std::string logfile = get_logfile_name(data.run_data.game_name); mbuf.mtype = MSGTYPE_LOGFILE_START; strncpy(mbuf.logfile, logfile.c_str(), sizeof(mbuf.logfile)); mbuf.logfile[sizeof(mbuf.logfile) - 1] = 0; mbuf.time = (uint64_t)atoi(args[2].c_str()); int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } handled = true; } else if (args[1] == "stop") { mbuf_logfile_stop_t mbuf; mbuf.mtype = MSGTYPE_LOGFILE_STOP; mbuf.logfile[0] = 0; int ret = msgsnd(data.msqid, &mbuf, sizeof(mbuf) - sizeof(mbuf.mtype), IPC_NOWAIT); if (ret == -1) { ws_reply += string_format("ERROR: msgsnd failed: %s\n", strerror(errno)); } handled = true; } } if (!handled) { ws_reply += string_format("ERROR: Unknown command '%s'.\n", command.c_str()); } if (ws_reply.size()) { webby_ws_write_buffer(NULL, ws_reply.c_str(), ws_reply.size()); } } data.commands.clear(); }
void sigint_handler(int sig) { game_stop(g); }