Beispiel #1
0
void SoundSystem::playMusic (int times)
{
  if (!audio) return;
  if (!music) return;
  if (volumemusic == 0) return;
#ifdef HAVE_SDL_MIXER
  musicplaying = true;
  Mix_PlayMusic (music1, times);
  setVolumeMusic ();
#endif
}
bool Game::CreateAndInit()
{
    // If an instance already exists, we'll just keep it as is.
    if (pInstance != NULL)
    {
        return true;
    }

#ifdef GAME_EXECUTABLE
    LoadCompletedCases();
    PopulateCaseSignatureMap();
#endif

    gScreenWidth = 960;
    gScreenHeight = 540;
    gFramerate = 60.0; // FPS (0 for unlimited)

#ifdef GAME_EXECUTABLE
    gIsFullscreen = gEnableFullscreen;

    // Seed the pseudo-random number generator.
    srand((unsigned int)time(NULL));
#endif

    // Turn on ALL THE THINGS.
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        return false;
    }

    atexit(SDL_Quit);

#ifdef GAME_EXECUTABLE
    SDL_StopTextInput();

    // Register all codecs.
    av_register_all();

    gUiThreadId = SDL_ThreadID();
#else
    // Initialize networking for the purposes of checking for updates.
    curl_global_init(CURL_GLOBAL_ALL);
    gpCurlHandle = curl_easy_init();
#endif

#ifdef GAME_EXECUTABLE
    SDL_DisplayMode mode;
    SDL_GetDesktopDisplayMode(0, &mode);

    #ifdef MLI_DEBUG
        #ifdef WIN32
            // Force stdout to console, not text files.
            freopen("CON", "a", stdout);
            freopen("CON", "a", stderr);
        #endif
    #endif

    #ifdef MLI_DEBUG
    cout << gTitle << endl;
    #endif

    SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "0");

    double nativeAspectRatio = (double)mode.w / mode.h;
    double gameAspectRatio = (double)gScreenWidth / gScreenHeight;
    bool constraintIsVertical = nativeAspectRatio > gameAspectRatio;

    gScreenScale = constraintIsVertical ? (double)mode.h / gScreenHeight : (double)mode.w / gScreenWidth;

    if (constraintIsVertical)
    {
        gHorizontalOffset = (Uint16)floor(0.5 + (mode.w - gScreenWidth * gScreenScale) / 2);
    }
    else
    {
        gVerticalOffset = (Uint16)floor(0.5 + (mode.h - gScreenHeight * gScreenScale) / 2);
    }
#endif

    SDL_WindowFlags flags = SDL_WINDOW_SHOWN;

#ifdef GAME_EXECUTABLE
    if (gIsFullscreen)
    {
        flags = (SDL_WindowFlags)(flags | SDL_WINDOW_FULLSCREEN_DESKTOP);
    }
#endif

    gpWindow = SDL_CreateWindow(gTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, gScreenWidth, gScreenHeight, flags);

    // If we couldn't get a window, we're pretty screwed.
    if (gpWindow == NULL)
    {
        return false;
    }

#ifdef __WINDOWS
#ifndef LAUNCHER
    // Load and set the window icon - this is needed on Windows,
    // since just having the icon in the .exe doesn't set it for things like
    // the Alt+Tab menu.
    struct SDL_SysWMinfo info;
    SDL_VERSION(&info.version);

    if (SDL_GetWindowWMInfo(gpWindow, &info) && info.subsystem == SDL_SYSWM_WINDOWS)
    {
        HWND hwnd = info.info.win.window;

        const HANDLE hBigIcon =
            LoadImage(
                GetModuleHandle(NULL),
                MAKEINTRESOURCE(IDI_ICON),
                IMAGE_ICON,
                GetSystemMetrics(SM_CXICON),
                GetSystemMetrics(SM_CYICON),
                0);

        if (hBigIcon != NULL)
        {
            SendMessage(hwnd, WM_SETICON, ICON_BIG, reinterpret_cast<WPARAM>(hBigIcon));
        }
    }
#endif
#endif

    gpRenderer = SDL_CreateRenderer(gpWindow, -1, 0);

    // Ditto for the renderer.
    if (gpRenderer == NULL)
    {
        return false;
    }

    // And for the renderer info, too.
    SDL_RendererInfo rendererInfo;

    if (SDL_GetRendererInfo(gpRenderer, &rendererInfo) < 0)
    {
        return false;
    }

    gMaxTextureWidth = rendererInfo.max_texture_width;
    gMaxTextureHeight = rendererInfo.max_texture_height;

#ifdef GAME_EXECUTABLE
    // Initialize audio subsystems.
    initAudio();

    // Set initial volume levels.
    setVolumeMusic(gBackgroundMusicVolume);
    setVolumeSound(gSoundEffectsVolume);
    setVolumeAmbiance(gSoundEffectsVolume);
    setVolumeDialog(gVoiceVolume);

    for (unsigned int i = 0; i < builtInBgmCount; i++)
    {
        ResourceLoader::GetInstance()->PreloadMusic(bgmIdList[i], "BGM/" + bgmIdList[i]);
    }

    for (unsigned int i = 0; i < builtInSfxCount; i++)
    {
        ResourceLoader::GetInstance()->PreloadSound(sfxIdList[i], "SFX/" + sfxIdList[i]);
    }

    TTF_Init();

    MouseHelper::Init();
    KeyboardHelper::Init();
    TextInputHelper::Init();
    EventProviders::Init();

    // We'll be drawing a custom cursor, so let's hide the system cursor.
    SDL_ShowCursor(SDL_DISABLE);
#endif

    pInstance = new Game();
    pInstance->Init();

    return true;
}