Example #1
0
/* 
 * main
 *   DESCRIPTION: Play the adventure game.
 *   INPUTS: none (command line arguments are ignored)
 *   OUTPUTS: none
 *   RETURN VALUE: 0 on success, 3 in panic situations
 */
int
main ()
{
    game_condition_t game;  /* outcome of playing */

    /* Randomize for more fun (remove for deterministic layout). */
    srand (time (NULL));

    /* Provide some protection against fatal errors. */
    clean_on_signals ();

    if (!build_world ()) {PANIC ("can't build world");}
    init_game ();

    /* Perform sanity checks. */
    if (0 != sanity_check ()) {
	PANIC ("failed sanity checks");
    }

    /* Create status message thread. */
    if (0 != pthread_create (&status_thread_id, NULL, status_thread, NULL)) {
        PANIC ("failed to create status thread");
    }
    push_cleanup (cancel_status_thread, NULL); {

	/* Start mode X. */
	if (0 != set_mode_X (fill_horiz_buffer, fill_vert_buffer)) {
	    PANIC ("cannot initialize mode X");
	}
	push_cleanup ((cleanup_fn_t)clear_mode_X, NULL); {

	    /* Initialize the keyboard and/or Tux controller. */
	    if (0 != init_input ()) {
		PANIC ("cannot initialize input");
	    }
	    push_cleanup ((cleanup_fn_t)shutdown_input, NULL); {

		game = game_loop ();

	    } pop_cleanup (1);

	} pop_cleanup (1);

    } pop_cleanup (1);

    /* Print a message about the outcome. */
    switch (game) {
	case GAME_WON: printf ("You win the game!  CONGRATULATIONS!\n"); break;
	case GAME_QUIT: printf ("Quitter!\n"); break;
    }

    /* Return success. */
    return 0;
}
Example #2
0
/*
 * main
 *   DESCRIPTION: Initializes and runs the two threads
 *   INPUTS: none
 *   OUTPUTS: none
 *   RETURN VALUE: 0 on success, -1 on failure
 *   SIDE EFFECTS: none
 */
int main()
{
	int ret;
   	struct termios tio_new;
	unsigned long update_rate = 32; /* in Hz */

	pthread_t tid1;
	pthread_t tid2;
	pthread_t tid3;

	// Initialize RTC
	fd = open("/dev/rtc", O_RDONLY, 0);
	// Enable RTC periodic interrupts at update_rate Hz
	// Default max is 64...must change in /proc/sys/dev/rtc/max-user-freq
	ret = ioctl(fd, RTC_IRQP_SET, update_rate);	
	ret = ioctl(fd, RTC_PIE_ON, 0);
	
	//Initialize Tux control
	tux_fd=open("/dev/ttyS0", O_RDWR | O_NOCTTY);
	int ldsic_num = N_MOUSE;
	ioctl(tux_fd, TIOCSETD, &ldsic_num);
	ioctl(tux_fd, TUX_INIT);
	

	// Initialize Keyboard
	// Turn on non-blocking mode
    	if (fcntl (fileno (stdin), F_SETFL, O_NONBLOCK) != 0) 
	{
        	perror ("fcntl to make stdin non-blocking");
		return -1;
    	}
	
	// Save current terminal attributes for stdin.
    	if (tcgetattr (fileno (stdin), &tio_orig) != 0) 
	{
		perror ("tcgetattr to read stdin terminal settings");
		return -1;
	}
	
	// Turn off canonical (line-buffered) mode and echoing of keystrokes
 	// Set minimal character and timing parameters so as
        tio_new = tio_orig;
    	tio_new.c_lflag &= ~(ICANON | ECHO);
    	tio_new.c_cc[VMIN] = 1;
    	tio_new.c_cc[VTIME] = 0;
    	if (tcsetattr (fileno (stdin), TCSANOW, &tio_new) != 0) 
	{
		perror ("tcsetattr to set stdin terminal settings");
		return -1;
	}


	// Perform Sanity Checks and then initialize input and display
	if ((sanity_check () != 0) || (set_mode_X (fill_horiz_buffer, fill_vert_buffer) != 0))
	{
		return 3;
	}

	// Create the threads
	pthread_create(&tid1, NULL, rtc_thread, NULL);
	pthread_create(&tid2, NULL, keyboard_thread, NULL);
	pthread_create(&tid3, NULL, tux_thread, NULL);
	
	// Wait for all the threads to end
	pthread_join(tid1, NULL);
	pthread_join(tid2, NULL);
	pthread_join(tid3, NULL);

	// Shutdown Display
	clear_mode_X();
	
	// Close Keyboard
	(void)tcsetattr (fileno (stdin), TCSANOW, &tio_orig);
		
	// Close RTC
	close(fd);
	
	//Close Tux Control
	close(tux_fd);

	// Print outcome of the game
	if (winner == 1)
	{	
		printf ("You win the game!  CONGRATULATIONS!\n");
	}
	else if (quit_flag == 1)
	{
		printf ("Quitter!\n");
	}
	else
	{
		printf ("Sorry, you lose...\n");
	}

	// Return success
	return 0;
}