コード例 #1
0
//Logic for States
void
state_logic ()
{
  if (state_terminated)
    {
      if (state_terminated > 0)
	{
	  trans_stop ();
	  state_quit ();
	  game_shutdown ();
	  state_terminated = -1;	//One Time Quit.
	}
      return;
    }

  if (state_switched)
    {
      return;
    }

  handlers[state].proc_logic ();
  {
    dt = al_get_time () - prev_time;
    while (dt >= GUI_DT)
      {
	gui_update ();
	dt -= GUI_DT;
	prev_time = al_get_time ();
      }
    gui_update ();
  }
}
コード例 #2
0
ファイル: main.cpp プロジェクト: Luminoth/eblocks
int main(int argc, char* argv[])
{
    const int width=450;
    const int height=560;
    const int bpp=16;

    Video video;
    if(!video.is_good()) {
        std::cerr << "Video initialization failed: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    Audio audio;
    if(!audio.is_good()) {
        std::cerr << "Audio initialization failed: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    struct State game_state;
    if(!game_init(&video, &audio, &game_state, width, height, bpp)) {
        std::cerr << "Game initialization failed: " << SDL_GetError() << std::endl;
        SDL_Quit();
        return 1;
    }

    process_arguments(argc, argv, &game_state);

#if defined DEBUG
    std::cout << "Block size: " << Block::width << "x" << Block::height << std::endl;
    std::cout << "Tetrion dimensions: " << Tetrion::NUM_ROWS << "x" << Tetrion::NUM_COLS << std::endl;
#endif

    game_run(&video, &audio, &game_state);

    game_shutdown(&audio, &game_state);
    return 0;
}
コード例 #3
0
ファイル: game.c プロジェクト: shiver/vectir
// the main game loop which will run until the game is done
void game_loop() {
	SDL_Event event, e;
	SDL_TimerID timer;
	int last_game_update = 0;
	int last_particle_update = 0;
	int last_render = 0;
	int previous_level = 0;

	debug_print("Loading media...");
	load_media();

	// check if we want to show score increments
	game.show_score_increments = atoi(config_getValue("show_score_increments"));

	// setup input
	input_init();

	// create & show menu
	ui_menuInit();
	ui_toggleMenuVisible();
	SDL_SetEventFilter(ui_handleEvents);

	// loop forever
	debug_print("Entering main game loop...");
	while (1) {
		// see if its time to trigger an update (make sure we're not paused either)
		if (!game.paused && 
					SDL_GetTicks() - last_game_update > game_getGameUpdateFreq()) {
			last_game_update = SDL_GetTicks();		// remember time of last update
			game_update();
		}

		if (game.show_score_increments) { // at the moment we just have the one particle set
			// see if its time to trigger a particle update
			if (SDL_GetTicks() - last_particle_update > PARTICLE_UPDATE_INTERVAL) {
				last_particle_update = SDL_GetTicks();
				particle_update();
			}
		}

		// check for any events waiting
		while (SDL_PollEvent(&event)) {
			switch(event.type) {
				// key presses are handled in input.c
				case SDL_KEYDOWN:
					input_onKeyDown(event.key.keysym.sym);
					break;
				case LEFT_KEY:
					tetromino_moveLeft(current_tetromino);
					if (grid_checkCollision(grid, current_tetromino)) {
						tetromino_moveRight(current_tetromino);
					}
					break;
        case RIGHT_KEY:
          tetromino_moveRight(current_tetromino);
          if (grid_checkCollision(grid, current_tetromino)) {
            tetromino_moveLeft(current_tetromino);
          }
          break;
        case DOWN_KEY:
					// uses the key repeat interval to accelerate the tetromino down
          tetromino_moveDown(current_tetromino);
          if (grid_checkCollision(grid, current_tetromino)) {
            tetromino_moveUp(current_tetromino);
					}
					break;
        case UP_KEY:
          // rotate to a new position
          tetromino_setNextPosition(current_tetromino);
          tetromino_setShape(current_tetromino, current_tetromino->type,
            current_tetromino->position);

          // make sure the new position doesn't cause any collisions
          // if it does, reverse the rotation
          if (grid_checkCollision(grid, current_tetromino)) {
            tetromino_setPrevPosition(current_tetromino);
            tetromino_setShape(current_tetromino, current_tetromino->type,
            current_tetromino->position);
          }
          break;
				case SPACE_KEY:
					tetromino_moveDown(current_tetromino);

					// move the tetromino down until it causes a collision
					while (!grid_checkCollision(grid, current_tetromino)) {
						tetromino_moveDown(current_tetromino);
					}
					// once we have a collision, move it back into place
					tetromino_moveUp(current_tetromino);
					break;
				case PAUSE_KEY:
					debug_print("Pausing game");
					game.paused = !game.paused;

					// stop the game timer
					if (game.paused) {
						SDL_RemoveTimer(timer);
						timer = NULL;
					}
					else { // start it again
						timer = SDL_AddTimer(1000, game_updateTime, NULL);
					}
					break;
				case ESCAPE_KEY:
					// pause game updates
					debug_print("Escape key pressed.");
					
					// toggle paused game state if we're in game
					if (grid && current_tetromino) {
						game.paused = !game.paused;
						if (game.paused) { // stop couting time played
							SDL_RemoveTimer(timer);
							timer = NULL;
						}
						// starting timer again, only if we're still in a game
						else if (grid && current_tetromino) { 
							timer = SDL_AddTimer(1000, game_updateTime, NULL);
						}
					}

					// show or hide the menu
					if (grid && current_tetromino) {
						ui_toggleMenuVisible();
					}
					
					// enable ui message pump if its visible
					if (ui_isMenuVisible()) {
						// if we're in game, show in-game menu
						if (grid && current_tetromino) { 
							ui_menuPageSetCurrentById(MENU_IN_GAME);
						}	
						// otherwise show main menu
						else {
							ui_menuPageSetCurrentById(MENU_MAIN);
						}
						SDL_SetEventFilter(ui_handleEvents);
					}
					break;
				case GAME_START_NEW:
					// set some game variables
					game.level = 0;
					game.score = 0;
					game.lines = 0;
					game.paused = 0;

				  // time variables
					game.hours = 0;
					game.minutes = 0;
					game.seconds = 0;

					// create the grid
					grid = grid_createNew(GRID_WIDTH, GRID_HEIGHT);

				  // create the first tetromino
				  current_tetromino = tetromino_createNew();
				  current_tetromino->x = 0;
  				current_tetromino->y = 0;
  				
					// update time
				  SDL_Init(SDL_INIT_TIMER);
				  
					if (timer) {
						SDL_RemoveTimer(timer);
						timer = NULL;
					}

					ui_toggleMenuVisible();
					timer = SDL_AddTimer(1000, game_updateTime, NULL);
					game.paused = 0;
					break;
				case GAME_END: // called by either the menu or game over scenario
					// destroy timer, grid and tetromino
					SDL_RemoveTimer(timer);
					timer = NULL;

					grid_destroy(grid);
					grid = NULL;
					tetromino_destroy(current_tetromino);
					current_tetromino = NULL;

					// show menu if it isn't already visible
					ui_menuPageSetCurrentById(MENU_MAIN);
					if (!ui_isMenuVisible()) {
						SDL_SetEventFilter(ui_handleEvents);
						ui_toggleMenuVisible();
					}
					break;
				case TETROMINO_CREATE:
					// assumes that the old one has already been discarded
					current_tetromino = tetromino_createNew();
			    current_tetromino->x = 0;
			    current_tetromino->y = 0;

					// check if we have an immediate collision (game over)
					if (grid_checkCollision(grid, current_tetromino)) {
						SDL_RemoveTimer(timer);
						timer = NULL;
						
						e.type = GAME_END;
						SDL_PushEvent(&e);
					}
					break;
        case GRID_REMOVE_LINE:
					if (game.show_score_increments) {
          	// animated score increment
						game_showScoreIncrement(event.user.code, (game.level + 1) * 10);
					}

          grid_removeLine(grid, event.user.code);

          // increment number of complete lines
          game.lines += 1;
          // +10 per block and x10 per level
          game.score += (game.level + 1) * 10 * GRID_WIDTH;

          // increment the game level every 10 lines
          previous_level = game.level;
					game.level = game.lines / 10;
					if (previous_level != game.level) {
						game_showLevelIncrement();
					}

          break;
        case GAME_QUIT:
          SDL_RemoveTimer(timer); // stop gameplay timer
          timer = NULL;
					game_shutdown();
            break;
        // unhandled events are ignored
  			default:
  				break;
  		}
    }

		// update the display
		// without this delay gfx card tries to commit suicide by melting
		if (SDL_GetTicks() - last_render > 3) {
			display_update();
			last_render= SDL_GetTicks();
		}
	}
}
コード例 #4
0
 //------------------------------------------------------------
 // La función principal main()
 //------------------------------------------------------------
 int main(int argc, char **argv)
 {
     // Iniciar Allegro (y añadidos)
     al_init();
     al_init_image_addon();
     al_init_primitives_addon();
  
     // Instalar el mouse, teclado, etc.
     al_install_keyboard();
     al_install_mouse();
  
     if (FULLSCREEN)
         al_set_new_display_flags(ALLEGRO_FULLSCREEN);
  
     // Crear el "display"
     display = al_create_display(SCREEN_W, SCREEN_H);
  
     if (!SHOW_CURSOR)
         al_hide_mouse_cursor(display);
  
     // Poner el título de la ventana
     al_set_window_title(display, WINDOW_TITLE);
  
     // Creamos el timer (controlador de FPS)
     timer = al_create_timer(1.0 / FRAMERATE);
  
     // Creamos la 'pila' de eventos
     event_queue = al_create_event_queue();
  
     // Preparamos el juego
     game_setup();
  
     // Los eventos que usaremos
     al_register_event_source(event_queue, al_get_keyboard_event_source());
     al_register_event_source(event_queue, al_get_mouse_event_source());
     al_register_event_source(event_queue, al_get_display_event_source(display));
     al_register_event_source(event_queue, al_get_timer_event_source(timer));
  
     al_start_timer(timer);
  
     // Si esta variable se pone a 0, el juego terminará de inmediato
     game_is_running = 1;
  
     ALLEGRO_EVENT event;
  
     // El 'loop' principal del juego
     while (game_is_running)
     {
         al_wait_for_event(event_queue, &event);
  
         // Si el botón para cerrar fue presionado...
         if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE)
             break;
  
         // Actualizamos las teclas
         if (event.type == ALLEGRO_EVENT_KEY_DOWN)
         {
             // Al presionar <Esc> el juego terminará
             if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE)
                 game_is_running = 0;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_LEFT)
                 key[KEY_LEFT] = 1;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT)
                 key[KEY_RIGHT] = 1;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_UP)
                 key[KEY_UP] = 1;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_DOWN)
                 key[KEY_DOWN] = 1;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_X)
                 key[KEY_X] = 1;
         }
  
         // Actualizamos las teclas
         if (event.type == ALLEGRO_EVENT_KEY_UP)
         {
             if (event.keyboard.keycode == ALLEGRO_KEY_LEFT)
                 key[KEY_LEFT] = 0;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT)
                 key[KEY_RIGHT] = 0;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_UP)
                 key[KEY_UP] = 0;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_DOWN)
                 key[KEY_DOWN] = 0;
  
             if (event.keyboard.keycode == ALLEGRO_KEY_X)
                 key[KEY_X] = 0;
         }
  
         if (event.type == ALLEGRO_EVENT_TIMER)
         {
             game_update();
             redraw = 1;
         }
  
         if (redraw)
         {
             redraw = 0;
  
             al_clear_to_color(al_map_rgb(0, 0, 0));
  
             game_render();
  
             al_flip_display();
         }
     }
  
     al_destroy_display(display);
     al_destroy_timer(timer);
     al_destroy_event_queue(event_queue);
  
     game_shutdown();
  
     return 0;
 }
コード例 #5
0
ファイル: androidmain.cpp プロジェクト: netguy204/giggle
JNIEXPORT void JNICALL Java_com_fiftyply_mosaic_GameSurfaceView_nativePause(JNIEnv* env, jobject obj) {
  LOGI("nativePause");
  game_shutdown();
}