int main(int argc, char *argv[]) { video_init(0); audio_init(argc, argv); game_init(); double last = glfwGetTime(); while (running) { double current = glfwGetTime(); int delta = (current - last) * 1000; if (delta < MIN_DELTA) { glfwSleep((MIN_DELTA-delta)/1000.0); continue; } last = current; now += delta; if (delta > MAX_DELTA) continue; game_tick(delta); } audio_shutdown(); video_shutdown(); return EXIT_SUCCESS; }
int main(void) { // initialize stdlib things signal(SIGINT, _finish); srand(time(NULL)); atexit(_exit_cb); // initialize ncurses initscr(); keypad(stdscr, true); nonl(); cbreak(); // TODO use raw() and handle CTRL inputs manually (no signal) noecho(); curs_set(false); nodelay(stdscr, true); // TODO support no color mode (just investigate if attron works) if (!has_colors()) { FPRINTF(stderr, "ncurses colors not supported\n"); exit(EXIT_FAILURE); } start_color(); init_pair(COLOR__GRASS, COLOR_WHITE, COLOR_GREEN); init_pair(COLOR__HP_GOOD, COLOR_GREEN, COLOR_GREEN); init_pair(COLOR__HP_WARNING, COLOR_YELLOW, COLOR_YELLOW); init_pair(COLOR__HP_DANGER, COLOR_RED, COLOR_RED); names_init(); struct game game; audio_init(&game.audio); game_init(&game); // TODO clean this up struct map_chunk chunk; map_chunk_init(&chunk); game.chunk = &chunk; // TODO improve game loop // - "rendering" and update tick do not need to have same rate // - fix input triggering after letting go, etc. for (;;) { int c = getch(); game_handle_input(&game, c); game_tick(&game); game_render(&game); ++game.frame; usleep(100000); } // clean up audio audio_destroy(&game.audio); return EXIT_SUCCESS; }
int game_thread( void * _parms ) { GameThreadParms * parms = (GameThreadParms*)_parms; GameState gs; SharedRenderState rs; game_init( gs, rs ); gs.zmq_control_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR ); { int ret = zsocket_connect( gs.zmq_control_socket, "inproc://control_game" ); assert( ret == 0 ); } gs.zmq_render_socket = zsocket_new( parms->zmq_context, ZMQ_PAIR ); zsocket_bind( gs.zmq_render_socket, "inproc://game_render" ); gs.zmq_input_req = zsocket_new( parms->zmq_context, ZMQ_REQ ); { int ret = zsocket_connect( gs.zmq_input_req, "inproc://input" ); assert( ret == 0 ); } unsigned int baseline = SDL_GetTicks(); int framenum = 0; while ( true ) { unsigned int now = SDL_GetTicks(); unsigned int target_frame = ( now - baseline ) / GAME_DELAY; if ( framenum <= target_frame ) { framenum++; // NOTE: build the state of the world at t = framenum * GAME_DELAY, // under normal conditions that's a time in the future // (the exception to that is if we are catching up on ticking game frames) game_tick( now, gs, rs ); // notify the render thread that a new game state is ready. // on the next render frame, it will start interpolating between the previous state and this new one zstr_sendf( gs.zmq_render_socket, "%d %f %f %f %f %f %f %f %f %f", baseline + framenum * GAME_DELAY, rs.position.x, rs.position.y, rs.orientation.w, rs.orientation.x, rs.orientation.y, rs.orientation.z, rs.smoothed_angular.x, rs.smoothed_angular.y, rs.smoothed_angular.z ); } else { int ahead = framenum * GAME_DELAY - ( now - baseline ); assert( ahead > 0 ); printf( "game sleep %d ms\n", ahead ); SDL_Delay( ahead ); } char * cmd = zstr_recv_nowait( gs.zmq_control_socket ); if ( cmd != NULL ) { assert( strcmp( cmd, "stop" ) == 0 ); free( cmd ); break; } } return 0; }
int main(int argc, char *argv[]) { struct timeval start_time; gettimeofday(&start_time, NULL); Game *game = game_init(start_time); while(1) { game_tick(game); } game_destroy(game); return 0; }
int main() { srvInit(); aptInit(); hidInit(NULL); irrstInit(NULL); gfxInitDefault(); yeti_init( &yeti, (framebuffer_t*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), (framebuffer_t*)gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL), textures, palette, lua ); gfxSet3D(true); game_init(&yeti); while(aptMainLoop()) { int i; for(i=0;i<2;i++) { yeti.viewport.front = yeti.viewport.back; yeti.viewport.back = (framebuffer_t*)gfxGetFramebuffer(GFX_TOP, leftOrRight?GFX_LEFT:GFX_RIGHT, NULL, NULL); game_draw(&yeti); leftOrRight^=1; } yetiUpdateKeyboard(&yeti); game_tick(&yeti); if(hidKeysDown()&KEY_START)break; gfxFlushBuffers(); gfxSwapBuffers(); gspWaitForEvent(GSPEVENT_VBlank0, true); } gfxExit(); irrstExit(); hidExit(); aptExit(); srvExit(); return 0; }
void update(void) { All * a = global_a; #line 175 if (land_was_resized()) { #line 177 viewport_update(game->viewport, land_display_width(), land_display_height()); a->resize_in_ticks = 10; } if (a->resize_in_ticks > 0) { a->resize_in_ticks--; if (a->resize_in_ticks == 0) { reload_fonts(); } } #line 185 if (a->load_after_redraw) { if (a->load_after_redraw == 2) { #line 188 load_level(a->editor || game->record->is_replaying || game->record->is_recording); if (a->find_entrance) { a->find_entrance = 0; if (game->player) { player_find_entrance(& game->player->super); } } #line 193 if (game && game->player) { #line 195 record_load(game->record, game->level, game->player->super.x, game->player->super.y, game->player->super.z); } #line 196 a->load_after_redraw++; } #line 196 return ; } #line 199 config_check_controls(a); #line 201 if (a->title) { title_tick(); } #line 202 else { #line 204 game_tick(game); } }
int main() { SDL_Event event; SDL_Window* window; t_game* game; int cont; window = create_window(); game = game_init(window); for (cont = 1; cont;) { while (SDL_PollEvent(&event)) if (event.type == SDL_QUIT) cont = 0; else if (event.type == SDL_KEYDOWN) event_keydown(event, game); if (cont) game_tick(game); } game_quit(game); return 0; }
void GW_GameEngine_VTech::do_timer(int timerid) { #ifdef GW_DEBUG GWDBG_FVOUTPUT("GW_GameEngine_VTech::do_timer(timerid %i)\n", timerid); #endif switch (timerid) { case TMR_GAMESTART: data_starttimer(TMR_GAME); break; case TMR_GAME: game_tick(); break; case TMR_REPRISE: game_reprise(); canmove_=true; data_starttimer(TMR_GAME); break; case TMR_GAMEOVER: // game is over: play "Game Over" tune, then freeze for 30 seconds, and finally // change mode to "Time 1", if no button is pressed game_over(); data_starttimer(TMR_GAMEOVERSOUND); data_starttimer(TMR_GAMEOVERWAIT); break; case TMR_GAMEOVERSOUND: gameover_=true; break; case TMR_GAMEOVERWAIT: //gameover_=true; SetMode(MODE_TIME1); break; } }
void play_single_player() { time_stepms = 4; time_step = 0.004f; spbg = get_image("spbg.png"); for(;;) { Uint32 last_tick = SDL_GetTicks(); first_tick = last_tick; last_avalanche = last_tick; init_field(); SDL_BlitSurface(spbg, 0, screen, 0); while(state == GS_NEUTRAL) { SDL_Event event; now = SDL_GetTicks(); while(now >= last_tick + time_stepms) { /* Skip delays larger than 5s */ if(now - last_tick > 5000) last_tick = now - 5000; last_tick += time_stepms; game_tick(now - last_tick < time_stepms); } while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: exit(EXIT_SUCCESS); break; case SDL_JOYBUTTONDOWN: if (event.jbutton.button == 15 || event.jbutton.button == 0)/* Code same as SDLK_SPACE */ { if(state == GS_NEUTRAL) { if(level < sizeof(levels) / sizeof(levels[0])) { bonus += levels[level].shoot_bonus; shoot(&p, random_bubble(&p), (levels[level].mode == GM_INV_GRAVITY) ? 400 : -1); } else shoot(&p, random_bubble(&p), -1); } } if (event.jbutton.button == 13 || event.jbutton.button == 2)/* Code same as SDLK_ESCAPE */ { if(level == 255) level = saved_level; #if LINUX || DARWIN if(getenv("HOME")) { char confpath[4096]; strcpy(confpath, getenv("HOME")); strcat(confpath, "/.pengupoprc"); int fd = open(confpath, O_WRONLY | O_CREAT, 0600); if(fd != -1) { lseek(fd, 32, SEEK_SET); write(fd, &level, 1); close(fd); } } #elif defined(WIN32) { HKEY k_config; if(ERROR_SUCCESS == RegCreateKey(HKEY_CURRENT_USER, "Software\\Junoplay.com\\Pengupop\\Config", &k_config)) { char str[64]; snprintf(str, sizeof(str), "%d", (level ^ 0x7236143)); str[63] = 0; RegSetValueEx(k_config, "bananas", 0, REG_SZ, str, strlen(str)); } } #endif return; } break; case SDL_JOYAXISMOTION: if ((event.jaxis.value > -3200) || (event.jaxis.value < 3200)) { switch (event.jaxis.axis) { case 0: if (p.right == -1)/* Code same as SDL_KEYUP:SDLK_LEFT */ p.right = 0; if (p.right == 1)/* Code same as SDL_KEYUP:SDLK_RIGHT */ p.right = 0; break; } } if ((event.jaxis.value < -3200) || (event.jaxis.value > 3200)) { switch (event.jaxis.axis) { case 0: if (event.jaxis.value < -22000)/* Code same as SDL_KEYDOWN:SDLK_LEFT */ p.right = -1; if (event.jaxis.value > 22000)/* Code same as SDL_KEYDOWN:SDLK_RIGHT */ p.right = 1; break; } } break; case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_q: case SDLK_ESCAPE: if(level == 255) level = saved_level; #if LINUX || DARWIN if(getenv("HOME")) { char confpath[4096]; strcpy(confpath, getenv("HOME")); strcat(confpath, "/.pengupoprc"); int fd = open(confpath, O_WRONLY | O_CREAT, 0600); if(fd != -1) { lseek(fd, 32, SEEK_SET); write(fd, &level, 1); close(fd); } } #elif defined(WIN32) { HKEY k_config; if(ERROR_SUCCESS == RegCreateKey(HKEY_CURRENT_USER, "Software\\Junoplay.com\\Pengupop\\Config", &k_config)) { char str[64]; snprintf(str, sizeof(str), "%d", (level ^ 0x7236143)); str[63] = 0; RegSetValueEx(k_config, "bananas", 0, REG_SZ, str, strlen(str)); } } #endif return; case SDLK_x: case SDLK_RETURN: case SDLK_SPACE: case SDLK_UP: if(state == GS_NEUTRAL) { if(level < sizeof(levels) / sizeof(levels[0])) { bonus += levels[level].shoot_bonus; shoot(&p, random_bubble(&p), (levels[level].mode == GM_INV_GRAVITY) ? 400 : -1); } else shoot(&p, random_bubble(&p), -1); } break; case SDLK_LEFT: p.right = -1; break; case SDLK_RIGHT: p.right = 1; break; case 's': sound_enable = !sound_enable; break; case 'f': #ifndef WIN32 SDL_WM_ToggleFullScreen(screen); #else if(fullscreen) screen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE); else screen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_FULLSCREEN); SDL_BlitSurface(spbg, 0, screen, 0); p.dirty_minx = 0; p.dirty_miny = 0; p.dirty_maxx = max_field_width * 32; p.dirty_maxy = 440; #endif fullscreen = !fullscreen; break; default:; } break; case SDL_KEYUP: switch(event.key.keysym.sym) { case SDLK_LEFT: if(p.right == -1) p.right = 0; break; case SDLK_RIGHT: if(p.right == 1) p.right = 0; break; default:; } break; } } if(p.evil_bubble_count && !next_evil) { int i = 0; while(p.evil_bubble_count && i < sizeof(p.mbubbles) / sizeof(p.mbubbles[0])) { if(p.mbubbles[i].color != 0 || p.mbubbles[i].lastpaintx != INT_MIN) { ++i; continue; } float rand = sin(pow(p.evil_bubble_seed++, 4.5)) * 0.5 + 0.5; float angle = rand * 60 - 30; p.mbubbles[i].falling = 0; p.mbubbles[i].velx = sin(angle / 180 * M_PI) * bubble_speed; p.mbubbles[i].vely = -cos(angle / 180 * M_PI) * bubble_speed; p.mbubbles[i].x = 112.0f; p.mbubbles[i].y = 400.0f; p.mbubbles[i].color = p.evil_bubbles[0]; --p.evil_bubble_count; memmove(p.evil_bubbles, &p.evil_bubbles[1], p.evil_bubble_count); next_evil = 10; break; } } if(next_evil) --next_evil; SDL_UpdateRect(screen, 0, 0, 0, 0); } Uint32 message_until = now + 2000; p.right = 0; for(;;) { now = SDL_GetTicks(); SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_RETURN) message_until = now; } } while(now >= last_tick + time_stepms) { /* Skip delays larger than 5s */ if(now - last_tick > 5000) last_tick = now - 5000; last_tick += time_stepms; game_tick(now - last_tick < time_stepms); } if(now > message_until) break; SDL_UpdateRect(screen, 0, 0, 0, 0); } if(state == GS_SHINE_GET) ++level; if(level == sizeof(levels) / sizeof(levels[0])) { level = 0; for(;;) { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_KEYDOWN) { if(event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_RETURN) return; } } SDL_BlitSurface(logo, 0, screen, 0); print_string(0, 320, 250, L"Congratulations!", 1); print_string(0, 320, 290, L"That was all", 1); SDL_UpdateRect(screen, 0, 0, 0, 0); } return; } } }
int main(){ //setup log file fpLog = fopen("log.txt", "w"); if(fpLog == NULL){ printf("Failed to open log file\n"); } //seed random srand(time(NULL)); //set up frame limiting clock_t s_cycle, e_cycle; s_cycle = clock(); //get amount of time to init game fprintf(fpLog, "Initializing...\n"); //log allegro_init_install(); //allegro inits Game game; game_init(&game); //initialize game struct TMX_map map; load_map(&game, &map); //load first map Sprite player, car; player_init(&game, &player, &map); //initialize player struct car_init(&game, &car, &map, 32, 32, 5); //initialize car struct gen_npcs(&game, &map); //generate and initialize npcs e_cycle = clock(); //get length of initialization fprintf(fpLog, "Initialization done in %f seconds.\n", (double)(e_cycle - s_cycle) / 1000); //show instructions on how to play display_instructions(&game); //set active sprite game.active_sprite = &player; fprintf(fpLog, "Beginning main loop.\n"); while(game.running){ //to limit frames we have a set refresh rate //to maintain this refresh rate we subtract the //time elapsed over the entire cycle //to do this we get the time at the start of the cycle //and the end to get the difference s_cycle = clock(); //exit sequence exit_sequence(&game); //check if we enter the car enter_car(&game, &player, &car, &map); //toggle debugging if(key[KEY_F1] && !game.debug_cd){ game.debug = !game.debug; printf(game.debug ? "DEBUGGING ENABLED\n" : "DEBUGGING DISABLED\n"); game.debug_cd = 10; }else{ game.debug_cd -= game.debug_cd ? 1 : 0; } //resets game if(key[KEY_F5]){ game_init(&game); load_map(&game, &map); player_init(&game, &player, &map); //initialize player struct car_init(&game, &car, &map, 32, 32, 5); gen_npcs(&game, &map); } //toggle music if(key[KEY_M]){ game.music ? stop_sample(game.bgMusic) : play_sample(game.bgMusic, 255, 128, 1000, 1); } //clear buffer clear_to_color(game.buffer, makecol(0, 0, 0)); //show map masked_blit(map.map, game.buffer, game.cam_x, game.cam_y, 0, 0, game.screen_w, game.screen_h); //pan camera pan_camera(&game); //do mouse click mouse_click(&game, game.active_sprite); npc_sequence(&game, &map); //display the player if we're not in the car if (!game.in_car){ sprite_sequence(&game, &player, &map, !game.in_car); if (key[KEY_SPACE] && !player.bullet_cd){ sprite_shoot(&game, &player); player.bullet_cd = 10; }else{ player.bullet_cd -= player.bullet_cd ? 1 : 0; } }else{ free(player.bullets); player.bullets = NULL; player.bullet_count = 0; } //show car sprite_sequence(&game, &car, &map, game.in_car); //display all overlays blit_overlays(&game); //show hud show_hud(&game, &player); //display buffer to screen stretch_blit(game.buffer, screen, 0, 0, game.buffer->w, game.buffer->h, 0, 0, game.screen_w * STRETCH_FACTOR, game.screen_h * STRETCH_FACTOR); //countdown timer game_tick(&game, &map); e_cycle = clock(); /*maintain consistent framerate by calculating delay of processing, using max because we don't want negative rest*/ printf("\rIteration took %f%20s", (double) (e_cycle - s_cycle), " "); rest(max(game.delay - (double) (e_cycle - s_cycle), 0)); } fprintf(fpLog, "Running exit scripts...\n"); //destroy all bitmaps destroy_bitmaps(&game, &player, &map, &car); fprintf(fpLog, "Exiting...\n"); return 0; }END_OF_MAIN();
static void enemy_tag_loop() { update_caption = tag_update_caption; tag_enemy_current_route = 0; tag_enemy_current_shot = 0; tag_enemy_spawnline = map_spawn_line; tag_enemy_y = 0; tag_enemy_upscrolled = 0; reset_tag_enemy(); tag_enemy_id = init_enemy(&tag_enemy); SDL_Event sdl_event; while(1) { if(!obj_slot_used[tag_enemy_id]) update_tag_enemy(0); //tag_enemy_id = init_enemy(&tag_enemy); unsigned need_redraw = 1; while (SDL_PollEvent(&sdl_event)) { need_redraw = 0; int dir = -1; switch (sdl_event.type) { case SDL_KEYUP: switch(sdl_event.key.keysym.sym) { case SDLK_e: dump_enemy(); return; case SDLK_g: toggle_gun(); break; case SDLK_d: enter_direction(); break; case SDLK_i: insert_steps(); break; case SDLK_s: insert_shot(); break; case SDLK_p: do_pause(); break; case SDLK_c: clear_screen(); video_update(); need_redraw = 1; break; case SDLK_SPACE: update_tag_enemy(1); break; case SDLK_PAGEUP: scrollup(); break; case SDLK_KP_PLUS: dir = 1; case SDLK_KP_MINUS: if((sdl_event.key.keysym.mod & KMOD_RSHIFT) || (sdl_event.key.keysym.mod & KMOD_LSHIFT)) toggle_shape(dir); else if((sdl_event.key.keysym.mod & KMOD_RALT) || (sdl_event.key.keysym.mod & KMOD_LALT)) toggle_route(dir); else if((sdl_event.key.keysym.mod & KMOD_RCTRL) || (sdl_event.key.keysym.mod & KMOD_LCTRL)) toggle_shot(dir); else toggle_vel(dir); break; default: break; } case SDL_KEYDOWN: switch(sdl_event.key.keysym.sym) { case SDLK_RIGHT: dir = 1; case SDLK_LEFT: if((sdl_event.key.keysym.mod & KMOD_RSHIFT) || (sdl_event.key.keysym.mod & KMOD_LSHIFT)) dir *= 4; tag_enemy.x += dir; update_tag_enemy(1); break; case SDLK_DOWN: dir = 1; case SDLK_UP: if((sdl_event.key.keysym.mod & KMOD_RSHIFT) || (sdl_event.key.keysym.mod & KMOD_LSHIFT)) dir *= 4; tag_enemy_y += dir; update_tag_enemy(1); break; default: ; } break; default: ; } } game_tick(need_redraw); } }