void shutdown() { if (!_sdlInitialized) return; // Shutdown happens in reverse order. Sound_Quit(); SDL_Quit(); _sdlInitialized = false; }
void Sound_release(void) { if (sound_enabled) { Sound_Quit(); Mix_CloseAudio(); Mix_Quit(); } /* if */ sound_enabled=false; } /* Sound_release */
int main(int argc, char **argv) { int i; if (!Sound_Init()) /* this calls SDL_Init(SDL_INIT_AUDIO) ... */ { fprintf(stderr, "Sound_Init() failed: %s.\n", Sound_GetError()); SDL_Quit(); return(42); } /* if */ for (i = 1; i < argc; i++) /* each arg is an audio file to play. */ playOneSoundFile(argv[i]); /* Shutdown the libraries... */ Sound_Quit(); SDL_Quit(); return(0); } /* main */
/* ** Audio stream callback. ** ** Called by the SDL background thread each time the audio buffer is ready ** to receive more data. The function decodes PCM samples from the sound ** stream and copies them to the buffer for playback. When an end-of-stream ** is reached, closes the audio stream and exits cleanly. */ static void stream_callback (void *userdata, Uint8 *buffer, int length) { Sound_Sample *stream = (Sound_Sample *) userdata; Uint32 bytes_decoded; /* Decode a chunk of PCM samples from the sound stream. */ Sound_SetBufferSize (stream, length); bytes_decoded = Sound_Decode (stream); if (bytes_decoded == 0) { /* End-of-stream reached; exit cleanly. */ Sound_FreeSample (stream); Sound_Quit (); SDL_Quit (); exit (0); } /* Copy the decoded samples to the audio buffer. */ memcpy (buffer, stream->buffer, length); }
void Audio_Quit(void) { SDL_PauseAudio(1); Sound_Quit(); SDL_CloseAudio(); }
int main(int argc, char *argv[]) { SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); // SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); /* initialize SDL first */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { showInitError(std::string("Error initializing SDL: ") + SDL_GetError()); return 0; } if (!EventThread::allocUserEvents()) { showInitError("Error allocating SDL user events"); return 0; } #ifndef WORKDIR_CURRENT /* set working directory */ char *dataDir = SDL_GetBasePath(); if (dataDir) { int result = chdir(dataDir); (void)result; SDL_free(dataDir); } #endif /* now we load the config */ Config conf; conf.read(argc, argv); if (!conf.gameFolder.empty()) if (chdir(conf.gameFolder.c_str()) != 0) { showInitError(std::string("Unable to switch into gameFolder ") + conf.gameFolder); return 0; } conf.readGameINI(); if (conf.windowTitle.empty()) conf.windowTitle = conf.game.title; assert(conf.rgssVersion >= 1 && conf.rgssVersion <= 3); printRgssVersion(conf.rgssVersion); int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG; if (IMG_Init(imgFlags) != imgFlags) { showInitError(std::string("Error initializing SDL_image: ") + SDL_GetError()); SDL_Quit(); return 0; } if (TTF_Init() < 0) { showInitError(std::string("Error initializing SDL_ttf: ") + SDL_GetError()); IMG_Quit(); SDL_Quit(); return 0; } if (Sound_Init() == 0) { showInitError(std::string("Error initializing SDL_sound: ") + Sound_GetError()); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } SDL_Window *win; Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS; if (conf.winResizable) winFlags |= SDL_WINDOW_RESIZABLE; if (conf.fullscreen) winFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; win = SDL_CreateWindow(conf.windowTitle.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, conf.defScreenW, conf.defScreenH, winFlags); if (!win) { showInitError(std::string("Error creating window: ") + SDL_GetError()); return 0; } /* OSX and Windows have their own native ways of * dealing with icons; don't interfere with them */ #ifdef __LINUX__ setupWindowIcon(conf, win); #else (void) setupWindowIcon; #endif ALCdevice *alcDev = alcOpenDevice(0); if (!alcDev) { showInitError("Error opening OpenAL device"); SDL_DestroyWindow(win); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } SDL_DisplayMode mode; SDL_GetDisplayMode(0, 0, &mode); /* Can't sync to display refresh rate if its value is unknown */ if (!mode.refresh_rate) conf.syncToRefreshrate = false; EventThread eventThread; RGSSThreadData rtData(&eventThread, argv[0], win, alcDev, mode.refresh_rate, conf); int winW, winH; SDL_GetWindowSize(win, &winW, &winH); rtData.windowSizeMsg.post(Vec2i(winW, winH)); /* Load and post key bindings */ rtData.bindingUpdateMsg.post(loadBindings(conf)); /* Start RGSS thread */ SDL_Thread *rgssThread = SDL_CreateThread(rgssThreadFun, "rgss", &rtData); /* Start event processing */ eventThread.process(rtData); /* Request RGSS thread to stop */ rtData.rqTerm.set(); /* Wait for RGSS thread response */ for (int i = 0; i < 1000; ++i) { /* We can stop waiting when the request was ack'd */ if (rtData.rqTermAck) { Debug() << "RGSS thread ack'd request after" << i*10 << "ms"; break; } /* Give RGSS thread some time to respond */ SDL_Delay(10); } /* If RGSS thread ack'd request, wait for it to shutdown, * otherwise abandon hope and just end the process as is. */ if (rtData.rqTermAck) SDL_WaitThread(rgssThread, 0); else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.windowTitle.c_str(), "The RGSS script seems to be stuck and mkxp will now force quit", win); if (!rtData.rgssErrorMsg.empty()) { Debug() << rtData.rgssErrorMsg; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.windowTitle.c_str(), rtData.rgssErrorMsg.c_str(), win); } /* Clean up any remainin events */ eventThread.cleanup(); Debug() << "Shutting down."; alcCloseDevice(alcDev); SDL_DestroyWindow(win); Sound_Quit(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; }
int main(int argc, char *argv[]) { /* initialize SDL first */ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { Debug() << "Error initializing SDL:" << SDL_GetError(); return 0; } if (!EventThread::allocUserEvents()) { Debug() << "Error allocating SDL user events"; return 0; } #ifndef WORKDIR_CURRENT /* set working directory */ char *dataDir = SDL_GetBasePath(); if (dataDir) { int result = chdir(dataDir); (void)result; SDL_free(dataDir); } #endif /* now we load the config */ Config conf; conf.read(argc, argv); conf.readGameINI(); assert(conf.rgssVersion >= 1 && conf.rgssVersion <= 3); printRgssVersion(conf.rgssVersion); int imgFlags = IMG_INIT_PNG | IMG_INIT_JPG; if (IMG_Init(imgFlags) != imgFlags) { Debug() << "Error initializing SDL_image:" << SDL_GetError(); SDL_Quit(); return 0; } if (TTF_Init() < 0) { Debug() << "Error initializing SDL_ttf:" << SDL_GetError(); IMG_Quit(); SDL_Quit(); return 0; } if (Sound_Init() == 0) { Debug() << "Error initializing SDL_sound:" << Sound_GetError(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } SDL_SetHint("SDL_VIDEO_MINIMIZE_ON_FOCUS_LOSS", "0"); SDL_Window *win; Uint32 winFlags = SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS; if (conf.winResizable) winFlags |= SDL_WINDOW_RESIZABLE; if (conf.fullscreen) winFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP; win = SDL_CreateWindow(conf.game.title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, conf.defScreenW, conf.defScreenH, winFlags); if (!win) { Debug() << "Error creating window:" << SDL_GetError(); return 0; } if (!conf.iconPath.empty()) { SDL_Surface *iconImg = IMG_Load(conf.iconPath.c_str()); if (iconImg) { SDL_SetWindowIcon(win, iconImg); SDL_FreeSurface(iconImg); } } EventThread eventThread; RGSSThreadData rtData(&eventThread, argv[0], win, conf); /* Load and post key bindings */ rtData.bindingUpdateMsg.post(loadBindings(conf)); /* Start RGSS thread */ SDL_Thread *rgssThread = SDL_CreateThread(rgssThreadFun, "rgss", &rtData); /* Start event processing */ eventThread.process(rtData); /* Request RGSS thread to stop */ rtData.rqTerm.set(); /* Wait for RGSS thread response */ for (int i = 0; i < 1000; ++i) { /* We can stop waiting when the request was ack'd */ if (rtData.rqTermAck) { Debug() << "RGSS thread ack'd request after" << i*10 << "ms"; break; } /* Give RGSS thread some time to respond */ SDL_Delay(10); } /* If RGSS thread ack'd request, wait for it to shutdown, * otherwise abandon hope and just end the process as is. */ if (rtData.rqTermAck) SDL_WaitThread(rgssThread, 0); else SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), "The RGSS script seems to be stuck and mkxp will now force quit", win); if (!rtData.rgssErrorMsg.empty()) { Debug() << rtData.rgssErrorMsg; SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, conf.game.title.c_str(), rtData.rgssErrorMsg.c_str(), win); } /* Clean up any remainin events */ eventThread.cleanup(); /* Store key bindings */ BDescVec keyBinds; rtData.bindingUpdateMsg.get(keyBinds); storeBindings(keyBinds, rtData.config); Debug() << "Shutting down."; SDL_DestroyWindow(win); Sound_Quit(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; }