Пример #1
0
int main(int argc, char *argv[])
{
    enum { GAME_COMPUTER, GAME_NETWORK, GAME_UNKNOWN } game_type = GAME_UNKNOWN;
    char *remote_address;
    int i;
	
    if (argc < 2) {
	printf("Penguin Warrior\n");
	printf("Usage: pw [ mode ] [ option ... ]\n");
	printf("  Game modes (choose one):\n");
	printf("    --computer\n");
	printf("    --net <remote ip address>\n");
	printf("  Display options, for tweaking and experimenting:\n");
	printf("    --fullscreen\n");
	printf("    --hwsurface  (use an SDL hardware surface if possible)\n");
	printf("    --doublebuf  (use SDL double buffering if possible)\n");
	printf("  The display options default to a windowed, software buffer.\n");
	exit(EXIT_FAILURE);
    }

    /* Process command line arguments. There are plenty of libraries for command
       line processing, but our needs are simple in this case. */
    for (i = 0; i < argc; i++) {

	/* Networked or scripted opponent? */
	if (!strcmp(argv[i], "--computer")) {
	    game_type = GAME_COMPUTER;
	    continue;
	} else if (!strcmp(argv[i], "--net")) {
	    game_type = GAME_NETWORK;
	    if (++i >= argc) {
		printf("You must supply an IP address for --net.\n");
		exit(EXIT_FAILURE);
	    }
	    remote_address = argv[i];
	    continue;

	    /* Display-related options */
	} else if (!strcmp(argv[i], "--hwsurface")) {
	    hwsurface = 1;
	} else if (!strcmp(argv[i], "--doublebuf")) {
	    doublebuf = 1;
	} else if (!strcmp(argv[i], "--fullscreen")) {
	    fullscreen = 1;
	}
    }

    switch (game_type) {
    case GAME_COMPUTER:
	opponent_type = OPP_COMPUTER;
	printf("Playing against the computer.\n");
	break; 
			
    case GAME_NETWORK:
	printf("Sorry, network play is not implemented... yet!\n");
	exit(EXIT_FAILURE);
			
    default:
	printf("You must select a game type.\n");
	exit(EXIT_FAILURE);
    }
	
    /* Initialize our random number generator. */
    initrandom();

    /* Fire up SDL. */
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
	printf("Unable to initialize SDL: %s\n", SDL_GetError());
	exit(EXIT_FAILURE);
    }
    atexit(SDL_Quit);
	
    /* Set an appropriate 16-bit double buffered video mode. */
    if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
			 (hwsurface ? SDL_HWSURFACE : SDL_SWSURFACE) |
			 (doublebuf ? SDL_DOUBLEBUF : 0) |
			 (fullscreen ? SDL_FULLSCREEN : 0)) == NULL) {
	printf("Unable to set video mode: %s\n", SDL_GetError());
	exit(EXIT_FAILURE);
    }
	
    /* Save the screen pointer for later use. */
    screen = SDL_GetVideoSurface();
	
    /* Set the window caption to the name of the game. */
    SDL_WM_SetCaption("Penguin Warrior", "Penguin Warrior");
	
    /* Hide the mouse pointer. */
    SDL_ShowCursor(0);

    /* Start the OpenAL-based audio system. */
    InitAudio();

    /* Initialize music and give the music system a file. */
    InitMusic();
    if (LoadMusic("reflux.ogg") != 0) {
	/* If that failed, don't worry about it. */
	printf("Unable to load reflux.ogg.\n");
    }
		
    /* Load the game's data into globals. We can only do this after the
       video and audio subsystems are ready for action, since the loading
       routines interact with SDL and OpenAL. */
    LoadGameData();

    /* Initialize the background starfield. */
    InitBackground();
	
    /* Play! */
    InitPlayer();
    InitOpponent();
    PlayGame();

    /* Unhide the mouse pointer. */
    SDL_ShowCursor(1);
	
    /* Unload data. */
    UnloadGameData();

    /* Shut down audio. */
    CleanupMusic();
    CleanupAudio();

    return 0;
}
Пример #2
0
int main(int argc, char *argv[])
{
    enum { GAME_COMPUTER, GAME_NETWORK, GAME_UNKNOWN } game_type = GAME_UNKNOWN;
    char *remote_address = NULL;
    int remote_port;
    int i;
	
    if (argc < 2) {
	printf("Penguin Warrior\n");
	printf("Usage: pw [ mode ] [ option ... ]\n");
	printf("  Game modes (choose one):\n");
	printf("    --computer\n");
	printf("    --client <remote ip address>\n");
	printf("    --server <port number>\n");
	printf("  Display options, for tweaking and experimenting:\n");
	printf("    --fullscreen\n");
	printf("    --hwsurface  (use an SDL hardware surface if possible)\n");
	printf("    --doublebuf  (use SDL double buffering if possible)\n");
	printf("  The display options default to a windowed, software buffer.\n");
	exit(EXIT_FAILURE);
    }

    /* Process command line arguments. There are plenty of libraries for command
       line processing, but our needs are simple in this case. */
    for (i = 0; i < argc; i++) {

	/* Networked or scripted opponent? */
	if (!strcmp(argv[i], "--computer")) {
	    game_type = GAME_COMPUTER;
	    continue;
	} else if (!strcmp(argv[i], "--client")) {
	    game_type = GAME_NETWORK;
	    if (i + 2 >= argc) {
		printf("You must supply an IP address "\
		       "and port number for network games.\n");
		exit(EXIT_FAILURE);
	    }
	    remote_address = argv[i+1];
	    remote_port = atoi(argv[i+2]);
	    i++;
	    continue;

	} else if (!strcmp(argv[i], "--server")) {
	    game_type = GAME_NETWORK;
	    if (++i >= argc) {
		printf("You must supply a port number "\
		       "for --server.\n");
		exit(EXIT_FAILURE);
	    }
	    remote_port = atoi(argv[i]);
	    continue;
			
	    /* Display-related options */
	} else if (!strcmp(argv[i], "--hwsurface")) {
	    hwsurface = 1;
	} else if (!strcmp(argv[i], "--doublebuf")) {
	    doublebuf = 1;
	} else if (!strcmp(argv[i], "--fullscreen")) {
	    fullscreen = 1;
	}
    }

    /* If this is a network game, make a connection. */
    if (game_type == GAME_NETWORK) {

	/* If there's no remote address, the user selected
	   server mode. */
	if (remote_address == NULL) {
	    if (WaitNetgameConnection(remote_port,
				      &netlink) != 0) {
		printf("Unable to receive connection.\n");
		exit(EXIT_FAILURE);
	    }
	} else {
	    if (ConnectToNetgame(remote_address, remote_port,
				 &netlink) != 0) {
		printf("Unable to connect.\n");
		exit(EXIT_FAILURE);
	    }
	}

	opponent_type = OPP_NETWORK;
	printf("Playing in network game against %s.\n", netlink.dotted_ip);

    } else if (game_type == GAME_COMPUTER) {

	opponent_type = OPP_COMPUTER;
	printf("Playing against the computer.\n");

    } else {

	printf("No game type selected.\n");
	exit(EXIT_FAILURE);

    }
	
    /* Initialize our random number generator. */
    initrandom();

    /* Create a mutex to protect the player data. */
    player_mutex = SDL_CreateMutex();
    if (player_mutex == NULL) {
	fprintf(stderr, "Unable to create mutex: %s\n",
		SDL_GetError());
    }

    /* Start our scripting engine. */
    InitScripting();
    if (LoadGameScript("pw.tcl") != 0) {
	fprintf(stderr, "Exiting due to script error.\n");
	exit(EXIT_FAILURE);
    }

    /* Fire up SDL. */
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
	printf("Unable to initialize SDL: %s\n", SDL_GetError());
	exit(EXIT_FAILURE);
    }
    atexit(SDL_Quit);
	
    /* Set an appropriate 16-bit video mode. */
    if (SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 16,
			 (hwsurface ? SDL_HWSURFACE : SDL_SWSURFACE) |
			 (doublebuf ? SDL_DOUBLEBUF : 0) |
			 (fullscreen ? SDL_FULLSCREEN : 0)) == NULL) {
	printf("Unable to set video mode: %s\n", SDL_GetError());
	exit(EXIT_FAILURE);
    }
	
    /* Save the screen pointer for later use. */
    screen = SDL_GetVideoSurface();
	
    /* Set the window caption to the name of the game. */
    SDL_WM_SetCaption("Penguin Warrior", "Penguin Warrior");
	
    /* Hide the mouse pointer. */
    SDL_ShowCursor(0);

    /* Initialize the status display. */
    if (InitStatusDisplay() < 0) {
	printf("Unable to initialize status display.\n");
	exit(EXIT_FAILURE);
    }
		
    /* Start the OpenAL-based audio system. */
    InitAudio();

    /* Initialize music and give the music system a file. */
    InitMusic();
    if (LoadMusic("reflux.ogg") < 0) {
	/* If that failed, don't worry about it. */
	printf("Unable to load reflux.ogg.\n");
    }

    /* Load the game's data into globals. */
    LoadGameData();

    /* Initialize the background starfield. */
    InitBackground();

    /* Start the network thread. */
    if (game_type == GAME_NETWORK) {
	network_thread = SDL_CreateThread(NetworkThread, NULL);
	if (network_thread == NULL) {
	    printf("Unable to start network thread: %s\n",
		   SDL_GetError());
	    exit(EXIT_FAILURE);
	}
    }
	
    /* Play! */
    InitPlayer(&player);
    InitPlayer(&opponent);
    PlayGame();

    /* Kill the network thread. */
    if (game_type == GAME_NETWORK) {
	SDL_KillThread(network_thread);
    }

    /* Close the network connection. */
    if (game_type == GAME_NETWORK)
	CloseNetgameLink(&netlink);

    /* Unhide the mouse pointer. */
    SDL_ShowCursor(1);
	
    /* Clean up the status display. */
    CleanupStatusDisplay();

    /* Unload data. */
    UnloadGameData();
	
    /* Shut down our scripting engine. */
    CleanupScripting();

    /* Get rid of the mutex. */
    SDL_DestroyMutex(player_mutex);

    /* Shut down audio. */
    CleanupMusic();
    CleanupAudio();

    return 0;
}
Пример #3
0
void UpdateMusic()
{
    /* ALsizei size,freq; */
    /* ALenum  format_new; */
    /* ALvoid  *data; */
    /* ALboolean  loop; /// important */
    /* int error; */
    /* printf ("before load wave\n"); */
    /* alutLoadWAVFile("reflux.wav", &format_new, &data, &size, &freq, &loop); */
    /* if(alGetError() != AL_NO_ERROR) */
    /* { */
    /*     printf("- Error creating 1 !!\n"); */
    /*     exit(2); */
    /* } */
    /* else */
    /* { */
    /*     printf("init - no errors after 1\n"); */
    /* } */
    /* printf ("%d %d %d\n",format_new, size, freq); */
    /* alBufferData(music_buffer, format_new, data, size, freq); */

    /* if ((error = alGetError()) != AL_NO_ERROR) */
    /* { */
    /*     printf("alBufferData buffer 0 : %d\n", error); */
    /*     // Delete buffers */
    /*     return 0; */
    /* } */

    /* alutUnloadWAV(format_new, data, size, freq); */
    /* if(alGetError() != AL_NO_ERROR) */
    /* { */
    /*     printf("- Error creating 3 !!\n"); */
    /*     exit(2); */
    /* } */
    /* else */
    /* { */
    /*     printf("init - no errors after 3\n"); */
    /* } */
    
    /* alSourcei(music_source, AL_BUFFER, music_buffer); */
    
    /* alSourcePlay(music_source); */
    /* if(alGetError() != AL_NO_ERROR) */
    /* { */
    /*     printf("- Error creating 4 !!\n"); */
    /*     exit(2); */
    /* } */
    /* else */
    /* { */
    /*     printf("init - no errors after 4\n"); */
    /* } */
    /* return; */
    
    int written;
    int format;
	
    if (music_enabled && music_file_loaded)
    {
        /* Do we need to fetch more data? */
        if (buf_pos == -1)
        {
            buf_count = 0;
            buf_pos = 0;
			
            if (music_playing)
            {
                /* libvorbisfile does not always return the full amount of
                   data requested, so loop until we have a full block. */
                while (music_playing && buf_count < MUSIC_BUF_SIZE)
                {
                    int amt;
                    amt = ov_read(&music_file,
                                  (char *)&buf[buf_count],
                                  (MUSIC_BUF_SIZE - buf_count) * 2,
                                  0, 2, 1, &music_section) / 2;

                    buf_count += amt;

                    /* End of the stream? */
                    if (amt == 0)
                    {
                        printf("End of music stream.\n");
                        music_playing = 0;
                        break;
                    }
                    
                    /* Slow down the loop a bit. Otherwise Vorbis decoding
                       will take huge spikes of CPU and cause noticeable jolts
                       in the main loop, even though this is running in a
                       separate thread. usleep won't waste time; it'll give
                       control back to the kernel briefly. */
                    usleep(10);
                }

            }
            else
            {
                /* No more music, so fill the buffer with zeroes. */
                buf_count = MUSIC_BUF_SIZE;
                memset(buf, 0, MUSIC_BUF_SIZE*2);
            }

        }

        /* Determine the correct format. This can change at any time.
           (it probably won't, but Vorbis allows for this) */
        if (music_info->channels == 1)
            format = AL_FORMAT_MONO16;
        else
            format = AL_FORMAT_STEREO16;

        /* If we have a buffer of data, append it to the playback buffer.
           alBufferAppendWriteData_LOKI is similar to the well-documented
           alBufferAppendData, but it allows us to specify the internal storage
           format for the data. This prevents OpenAL from converting stereo data
           to mono. (With this function, we should get stereo playback.) */
        if (buf_count != 0)
        {

            /* written = alBufferAppendWriteData_LOKI(music_buffer, */
            /* 				   format, */
            /* 				   &buf[buf_pos], */
            /* 				   MUSIC_BUF_SIZE-buf_pos, */
            /* 				   music_info->rate, */
            /* 				   format); */

            alBufferData(music_buffer,
                         (ALenum) format,
                         (ALvoid *) buf,
                         (ALsizei) MUSIC_BUF_SIZE,
                         (ALsizei) music_info->rate);

            /* Check for (unlikely) errors. If something went wrong,
               disable music. */
            /* if (written < 0 || alGetError() != AL_NO_ERROR) { */
            /* printf("OpenAL error, disabling music.\n"); */
            /* CleanupMusic(); */
            /* } */

            if (alGetError() != AL_NO_ERROR)
            {
                printf("OpenAL error, disabling music.\n");
                CleanupMusic();
            }
            else
            {
                /// written = MUSIC_BUF_SIZE-buf_pos;
            }
        
            /* Update the buffer position based on how much data we wrote.
               If we've played the entire buffer, set the position to -1 so
               that the next call to UpdateMusic will refill the buffer. */

            /* buf_pos += written; */

            /* if (buf_pos >= buf_count) */
            /*     buf_pos = -1; */
            alSourcei(music_source, AL_BUFFER, music_buffer);
            alSourcePlay(music_source);
        }
    }
}