//----------------------------------------------------------------------------//
CEGuiD3D11BaseApplication::CEGuiD3D11BaseApplication() :
    pimpl(new CEGuiBaseApplication11Impl),
    d_lastFrameTime(GetTickCount())
{
    if (pimpl->d_window = Win32AppHelper::createApplicationWindow(s_defaultWindowWidth, s_defaultWindowHeight))
    {
        if (initialiseDirect3D(s_defaultWindowWidth, s_defaultWindowHeight, true))
        {
            // set the swap chain ptr into window data so we can get access
            // later.  This is a bit of a hack, but saved us redesigning the
            // entire framework just for this.
            SetWindowLongPtr(pimpl->d_window,
                             GWLP_USERDATA,
                             (LONG_PTR)pimpl->d_swapChain);

            if (Win32AppHelper::initialiseDirectInput(pimpl->d_window,
                                                      pimpl->d_directInput))
            {
                d_renderer = &CEGUI::Direct3D11Renderer::create(pimpl->d_device,
                                                                pimpl->d_context);
                return;
            }

            cleanupDirect3D();
        }

        DestroyWindow(pimpl->d_window);
    }
    else
        MessageBox(0, Win32AppHelper::CREATE_WINDOW_ERROR,
                   Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);

    CEGUI_THROW(std::runtime_error(
        "Windows Direct3D 11 application failed to initialise."));
}
//----------------------------------------------------------------------------//
CEGuiD3D11BaseApplication::~CEGuiD3D11BaseApplication()
{
    Win32AppHelper::mouseLeaves();

    CEGUI::Direct3D11Renderer::destroy(
        *static_cast<CEGUI::Direct3D11Renderer*>(d_renderer));

    Win32AppHelper::cleanupDirectInput(pimpl->d_directInput);

    cleanupDirect3D();

    DestroyWindow(pimpl->d_window);

    delete pimpl;
}
//----------------------------------------------------------------------------//
CEGuiD3D10BaseApplication::~CEGuiD3D10BaseApplication()
{
    Win32AppHelper::mouseLeaves();

    // cleanup gui system
    delete CEGUI::System::getSingletonPtr();
    delete pimpl->d_renderer;

    Win32AppHelper::cleanupDirectInput(pimpl->d_directInput);

    cleanupDirect3D();
   
    DestroyWindow(pimpl->d_window);

    delete pimpl;
}
//----------------------------------------------------------------------------//
CEGuiD3D10BaseApplication::CEGuiD3D10BaseApplication() :
    pimpl(new CEGuiBaseApplicationImpl),
    d_lastTime(GetTickCount()),
    d_frames(0),
    d_FPS(0)
{
    if (pimpl->d_window = Win32AppHelper::createApplicationWindow(800, 600))
    {
        if (initialiseDirect3D(800, 600, true))
        {
            // set the swap chain ptr into window data so we can get access
            // later.  This is a bit of a hack, but saved us redesigning the
            // entire framework just for this.
            SetWindowLongPtr(pimpl->d_window,
                             GWLP_USERDATA,
                             (LONG_PTR)pimpl->d_swapChain);

            if (Win32AppHelper::initialiseDirectInput(pimpl->d_window,
                                                      pimpl->d_directInput))
            {
                pimpl->d_renderer =
                    new CEGUI::DirectX10Renderer(pimpl->d_device, 0);

                // initialise the gui system
                new CEGUI::System(pimpl->d_renderer);

                // initialise the required dirs for the DefaultResourceProvider
                CEGUI::DefaultResourceProvider* rp =
                    static_cast<CEGUI::DefaultResourceProvider*>
                        (CEGUI::System::getSingleton().getResourceProvider());

                const char* dataPathPrefix = getDataPathPrefix();
                char resourcePath[PATH_MAX];

                // for each resource type, set a resource group directory
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "schemes/");
                rp->setResourceGroupDirectory("schemes", resourcePath);
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "imagesets/");
                rp->setResourceGroupDirectory("imagesets", resourcePath);
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "fonts/");
                rp->setResourceGroupDirectory("fonts", resourcePath);
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "layouts/");
                rp->setResourceGroupDirectory("layouts", resourcePath);
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "looknfeel/");
                rp->setResourceGroupDirectory("looknfeels", resourcePath);
                sprintf(resourcePath, "%s/%s", dataPathPrefix, "lua_scripts/");
                rp->setResourceGroupDirectory("lua_scripts", resourcePath);
                #if defined(CEGUI_WITH_XERCES) && (CEGUI_DEFAULT_XMLPARSER == XercesParser)
                    sprintf(resourcePath, "%s/%s", dataPathPrefix, "XMLRefSchema/");
                    rp->setResourceGroupDirectory("schemas", resourcePath);
                #endif

                CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Informative);

                return;
            }

            cleanupDirect3D();
        }

        DestroyWindow(pimpl->d_window);
    }
    else
    {
        MessageBox(0, Win32AppHelper::CREATE_WINDOW_ERROR,
                   Win32AppHelper::APPLICATION_NAME, MB_ICONERROR|MB_OK);
    }

    throw std::runtime_error("Windows Direct3D 10 application failed to "
                             "initialise.");
}