示例#1
0
文件: main.c 项目: Sachs27/pong
int main(int argc, char *argv[])
{
    pong_t pong;
    int quit;
    unsigned long last_ticks, delta_ticks;
    SDL_Event e;

    log_enter_func(__func__);
    SDL_Init(SDL_INIT_EVERYTHING);
    Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);

    pong.screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    if (!pong.screen) {
        log_put_msg_in_func("SDL_SetVideoMode FAILED\n");
        goto EXIT;
    }
    SDL_WM_SetCaption("Pong", 0);
    pong.rect.x = 0;
    pong.rect.y = 0;
    pong.rect.w = 320;
    pong.rect.h = 480;

    if (pong_create_ball(&pong) != 0) {
        log_put_msg_in_func("pong_create_ball FAILED\n");
        goto EXIT;
    }

    if (pong_create_player(&pong) != 0) {
        log_put_msg_in_func("pong_create_player FAILED\n");
        goto EXIT;
    }
    
    quit = 0;
    last_ticks = SDL_GetTicks();
    while (!quit) {
        while (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT) {
                quit = !quit;
            } else if (e.type == SDL_KEYDOWN) {
                if (e.key.keysym.sym == SDLK_ESCAPE) {
                    quit = !quit;
                }
            }
        }
        if ((delta_ticks = SDL_GetTicks() - last_ticks) < 33) {
            SDL_Delay(3);
        } else {
            pong_update(&pong, delta_ticks);
            pong_render(&pong);
            last_ticks += 33;
        }
    }
EXIT:
    pong_destroy_ball(&pong);
    pong_destroy_player(&pong);
    Mix_CloseAudio();
    SDL_Quit();
    log_leave_func(__func__);
    return 0;
}
示例#2
0
文件: pong.c 项目: TimWhiting/pong
void pong_main(void)
{
    Pong game;
    SDL_Window *window = NULL;

    //initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fprintf(stderr, "SDL not initialized: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    //create the window
    window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
    if (!window)
    {
        fprintf(stderr, "SDL could not create window: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }

    //create a hardware accelerated renderer for the window
    game.renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE);

    //create the font texture
    SDL_RWops *s = SDL_RWFromMem(assets_font, assets_font_size);
    if (!s)
    {
        fprintf(stderr, "Unable to load font data: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Surface *font = SDL_LoadBMP_RW(s, 1); //we close the stream automatically
    game.font = SDL_CreateTextureFromSurface(game.renderer, font);
    if (!game.font)
    {
        fprintf(stderr, "Unable to create font texture: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_FreeSurface(font);

    //create the sprites texture
    s = SDL_RWFromMem(assets_sprites, assets_sprites_size);
    if (!s)
    {
        fprintf(stderr, "Unable to load sprite data: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_Surface *sprites = SDL_LoadBMP_RW(s, 1); //we close the stream automatically
    game.sprites = SDL_CreateTextureFromSurface(game.renderer, sprites);
    if (!game.sprites)
    {
        fprintf(stderr, "Unable to create sprites texture: %s\n", SDL_GetError());
        exit(EXIT_FAILURE);
    }
    SDL_FreeSurface(sprites);

    //initial game settings
    game.player1Score = 0;
    game.player2Score = 0;
    game.ball.x = WINDOW_WIDTH / 2;
    game.ball.y = WINDOW_HEIGHT / 2;
    game.ballDir = BALL_DEFAULT_DIR;
    paddle_init(&game.player1, &game.player2);

    //main game loop
    while(1)
    {
        if (!handle_events())
        {
            break;
        }

        //perform the game logic
        pong_tick(&game);

        //render the game
        pong_render(&game);
    }

    //destroy our window and associated resources
    SDL_DestroyTexture(game.font);
    SDL_DestroyRenderer(game.renderer);
    SDL_DestroyWindow(window);

    //all done!
    SDL_Quit();
    exit(EXIT_SUCCESS);
}