void InicializaSound(){ /* Initialize SDL's video and audio subsystems. Video is necessary to receive events. */ if (SDL_Init(SDL_INIT_AUDIO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return; } /* Make sure SDL_Quit gets called when the program exits. */ atexit(SDL_Quit); /* We also need to call this before we exit. SDL_Quit does not properly close the audio device for us. */ atexit(SDL_CloseAudio); /* Open the audio device. The sound driver will try to give us the requested format, but it might not succeed. The 'obtained' structure will be filled in with the actual format data. */ desired.freq = 44100; /* desired output sample rate */ desired.format = AUDIO_S16; /* request signed 16-bit samples */ desired.samples = 4096; /* this is more or less discretionary */ desired.channels = 2; /* ask for stereo */ desired.callback = AudioCallback; desired.userdata = NULL; /* we don't need this */ if (SDL_OpenAudio(&desired, &obtained) < 0) { printf("Unable to open audio device: %s\n", SDL_GetError()); return; } /* Load our sound files and convert them to the sound card's format. */ if (LoadAndConvertSound("teste.wav", &obtained, &cannon) != 0) { printf("Unable to load sound.\n"); return; } if (LoadAndConvertSound("sw.wav", &obtained, &musica) != 0) { printf("Unable to load sound.\n"); return; } /* Clear the list of playing sounds. */ ClearPlayingSounds(); /* SDL's audio is initially paused. Start it. */ SDL_PauseAudio(0); PlaySound(&musica); }
void InitAudio() { /* init sdl audio */ if (SDL_Init(SDL_INIT_AUDIO) < 0) { printf("SDL init audio failed: %s\n", SDL_GetError()); exit(1); } atexit(SDL_CloseAudio); /* open audio device */ desired.freq = 44100; /* desired output sample rate */ desired.format = AUDIO_S16; /*request signed 16-bit samples*/ desired.samples = 4096; /* this is somewhat arbitrary */ desired.channels = 2; /* ask for stereo */ desired.callback = AudioCallback; desired.userdata = NULL; /* we don’t need this */ if (SDL_OpenAudio(&desired, &obtained) < 0) { printf("Unable to open audio device: %s\n", SDL_GetError()); exit(1); } /* Load our sound files and convert them to the sound card’s format. */ int i; for(i = 0; i < SND_NUM; ++ i){ if (LoadAndConvertSound(wav_files[i], &obtained, &wav_sounds[i]) != 0) { printf("Unable to load sound %s!\n", wav_files[i]); exit(1); } } /* Clear the list of playing sounds. */ ClearPlayingSounds(); /* SDL’s audio is initially paused. Start it. */ SDL_PauseAudio(0); }
int main() { SDL_Surface* background, *background2, *background3, *background4; SDL_Rect src, dest; int frames; Uint32 colorkey; SDL_AudioSpec desired, obtained; sound_t bus_sound; /*initialize video and sound subsystem*/ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0){ printf("Unable to initialize video: %s\n.", SDL_GetError()); return 1; } /*ensure all subsystems exit safely*/ atexit(SDL_Quit); atexit(SDL_CloseAudio); /*set video mode*/ screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF | SDL_HWSURFACE); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } /*load background images*/ background = SDL_LoadBMP("img/src.bmp"); if (background == NULL) { printf("Unable to load image."); return 1; } background2 = SDL_LoadBMP("img/background2.bmp"); if (background2 == NULL) { printf("Unable to load background2."); return 1; } background3 = SDL_LoadBMP("img/background3.bmp"); if (background3 == NULL) { printf("Unable to load background3."); return 1; } background4 = SDL_LoadBMP("img/background4.bmp"); if (background4 == NULL) { printf("Unable to load background4."); return 1; } /*load bus*/ bus = SDL_LoadBMP("img/bus.bmp"); if (bus == NULL) { printf("Unable to load image."); return 1; } colorkey = SDL_MapRGB(bus->format, 255, 255, 255); /*set color key*/ SDL_SetColorKey(bus, SDL_SRCCOLORKEY, colorkey); /*load man*/ man = SDL_LoadBMP("img/man.bmp"); if (man == NULL) { printf("Unable to load image"); return 1; } colorkey = SDL_MapRGB(man->format, 255, 255, 255); /*set color key*/ SDL_SetColorKey(man, SDL_SRCCOLORKEY, colorkey); man2 = SDL_LoadBMP("img/man2.bmp"); if (man2 == NULL) { printf("Unable to load image"); return 1; } colorkey = SDL_MapRGB(man2->format, 255, 255, 255); /*set color key*/ SDL_SetColorKey(man2, SDL_SRCCOLORKEY, colorkey); car1 = SDL_LoadBMP("img/car1.bmp"); if (car1 == NULL) { printf("Unable to load image"); return 1; } colorkey = SDL_MapRGB(car1->format, 255, 255, 255); /*set color key*/ SDL_SetColorKey(car1, SDL_SRCCOLORKEY, colorkey); car2 = SDL_LoadBMP("img/car2.bmp"); if (car2 == NULL) { printf("Unable to load image"); return 1; } colorkey = SDL_MapRGB(car2->format, 255, 255, 255); /*set color key*/ SDL_SetColorKey(car2, SDL_SRCCOLORKEY, colorkey); //open audio device desired.freq = 44100; desired.format = AUDIO_S16; desired.samples = 4096; desired.channels = 2; desired.callback = AudioCallback; desired.userdata = NULL; if (SDL_OpenAudio(&desired, &obtained) < 0) { printf("Unable to open audio device: %s\n", SDL_GetError()); return 1; } //load sound files and convert them to sound card's format if (LoadAndConvertSound("audio/bus-pass.wav", &obtained, &bus_sound) != 0) { printf("Unable to load sound.\n"); return 1; } ClearPlayingSounds(); SDL_PauseAudio(0); init_bus(); init_man(); int passenger_in = 0; PlaySound(&bus_sound); while (psv.x > 0) { src.x = 0; src.y = 0; src.w = background->w; src.h = background->h; dest = src; SDL_BlitSurface(background, &src, screen, &dest); draw_obj(&psv, bus); if (psv.x < screen->w/2 && !passenger_in) { /*pause bus for passenger to enter*/ SDL_PauseAudio(1); if (passenger.x > psv.x + 40){ /*check if passenger has got in*/ passenger_in = 1; SDL_PauseAudio(0); } draw_man(); SDL_Flip(screen); move_obj(&passenger); } else { SDL_Flip(screen); move_obj(&psv); } } psv.x = 639; psv.y = 320; init_cars(); PlaySound(&bus_sound); while (psv.x + bus->w/2 > 0) { SDL_BlitSurface(background2, &src, screen, &dest); draw_obj(&priv1, car1); draw_obj(&priv2, car2); draw_obj(&psv, bus); SDL_Flip(screen); move_obj(&psv); move_obj(&priv1); move_obj(&priv2); } psv.x = 639; psv.y = 350; PlaySound(&bus_sound); while (psv.x + bus->w/2 > 0) { SDL_BlitSurface(background3, &src, screen, &dest); draw_obj(&psv, bus); SDL_Flip(screen); move_obj(&psv); } psv.x = 639; psv.y = 267; passenger.y = 270; passenger_in = 1; int has_paused = 0; PlaySound(&bus_sound); while (psv.x + bus->w/2 > 0) { SDL_BlitSurface(background4, &src, screen, &dest); if (screen->w/2 > psv.x && passenger_in == 1) { SDL_PauseAudio(1); if (has_paused == 0) { SDL_Delay(1000); has_paused = 1;} if (passenger.x > 639) {passenger_in = 0; SDL_PauseAudio(0);} draw_obj(&psv, bus); draw_man(); SDL_Flip(screen); move_obj(&passenger); } else { draw_obj(&psv, bus); SDL_Flip(screen); move_obj(&psv); } } //pause and lock sound system SDL_PauseAudio(1); SDL_LockAudio(); free(bus_sound.samples); SDL_UnlockAudio(); SDL_FreeSurface(background4); SDL_FreeSurface(background3); SDL_FreeSurface(background2); SDL_FreeSurface(background); SDL_FreeSurface(man); SDL_FreeSurface(bus); return 0; }
/*========================================================================= // Name: main() // Desc: Entrypoint //=======================================================================*/ int main(int argc, char *argv[]) { printf("-----------------------------------------------------\n"); printf(" BlueCube (improved version v0.9 hosted on github) \n"); printf(" just another tetris clone, \n"); printf(" written by Sebastian Falbesoner <theStack> \n"); printf("-----------------------------------------------------\n"); /* First, check whether sound should be activated */ bSoundActivated = 1; if ((argc == 2) && (strcmp(argv[1], "--nosound") == 0)) { printf("No sound is used!\n"); bSoundActivated = 0; } /* Init randomizer */ printf("Initializing randomizer... "); srand(time(NULL)); printf("done.\n"); /* Let's init the graphics and sound system */ printf("Starting up SDL... "); InitSDLex(); printf("done.\n"); if (bSoundActivated) { printf("Initializing sound subsystem... "); InitSound(); printf("done.\n"); } /* Load font */ printf("Loading font files... "); font = LoadFontfile("font/font.dat", "font/widths.dat"); printf("done.\n"); /* Load soundfiles */ if (bSoundActivated) { printf("Loading sound files... "); LoadSound("sound/killline.snd", &sndLine); LoadSound("sound/nextlev.snd", &sndNextlevel); printf("done.\n"); } printf("=== Let the fun begin! ===\n"); /* Init star background */ InitStars(); if (bSoundActivated) { ClearPlayingSounds(); SDL_PauseAudio(0); } /* Start menu first, please... */ gamestate = STATE_MENU; /* Call main loop */ Mainloop(); /* Free sounds again */ if (bSoundActivated) { printf("Releasing memory for sound samples... "); SDL_FreeWAV(sndLine.samples); SDL_FreeWAV(sndNextlevel.samples); printf("done.\n"); } /* Free font again */ printf("Releasing memory for fonts... "); FreeFont(font); printf("done.\n"); return 0; }