// DUiGraphic.cpp: implementation of the CDUiGraphic class.
//
//////////////////////////////////////////////////////////////////////

#include "DUiMan.h"
#include "DirectXHelpers.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDUiGraphic::CDUiGraphic()
{
    SetObjectType(DUI_GRAPHIC);

    m_pDDS = NULL;
    m_pDirectDraw = NULL;

}

CDUiGraphic::~CDUiGraphic()
{
    if (m_pDDS)
        m_pDDS->Release();
}

DWORD CDUiGraphic::Display(BOOL bRefresh)
{
    DWORD dwRetVal = 0;
    HRESULT hr;
    
    RECT rDest = {m_xPos, m_yPos, m_xPos + m_dwWidth, m_yPos + m_dwHeight};

    if (m_pDDS)
    {
        hr = m_pddsMain->Blt(&rDest, m_pDDS, NULL, DDBLT_WAIT, NULL);
        if (hr != S_OK)
        {
            g_pDUiManager->DisplayDebugErrorf(L"Graphic blit failure %08x m_pDDS %d",hr,m_pDDS);
        }
    }
  
    return dwRetVal;
}

DWORD CDUiGraphic::DisplayText(BOOL bRefresh)
{
    DWORD dwRetVal = 0;

    return dwRetVal;
}

DWORD CDUiGraphic::Initialize()
{
    DWORD               dwRetVal = 0;
    HBITMAP             hbitmap;
    BITMAP              bitmap;
    DDSURFACEDESC       ddsd;
    HDC                 hdcBitmap;
    HDC                 hdcSurface;
    HRESULT             hr;

    if (m_pDirectDraw)
    {
#ifdef _WIN32_WCE
        hbitmap = (HBITMAP)LoadImage (g_pDUiManager->GetAppHInstance(), m_wszGraphicName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | 0x0010);
        if (!hbitmap)
            hbitmap = LoadBitmap(g_pDUiManager->GetAppHInstance(), m_wszGraphicName);
#else
        hbitmap = (HBITMAP)LoadImage (g_pDUiManager->GetAppHInstance(), m_wszGraphicName, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
#endif
        if (hbitmap== NULL)
        {
            g_pDUiManager->DisplayDebugErrorf(L"CNLI %d %d %s",g_pDUiManager->GetAppHInstance(),GetLastError(),m_wszGraphicName);
            return NULL;
        }
        
        GetObject (hbitmap, sizeof(BITMAP), &bitmap);
        
        memset(&ddsd, 0, sizeof(ddsd));
        ddsd.dwSize = sizeof(DDSURFACEDESC);
    
        m_pddsMain->GetSurfaceDesc(&ddsd);
        
        ddsd.dwSize         = sizeof(DDSURFACEDESC);
        ddsd.dwFlags        = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
        ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
        
        if (!m_dwWidth)
            m_dwWidth = bitmap.bmWidth;
        if (!m_dwHeight)
            m_dwHeight = bitmap.bmHeight;

        ddsd.dwWidth        = bitmap.bmWidth;
        ddsd.dwHeight       = bitmap.bmHeight;
        hr = m_pDirectDraw->CreateSurface(&ddsd, &m_pDDS, NULL);
        
        hdcBitmap = CreateCompatibleDC (NULL);
        SelectObject (hdcBitmap, hbitmap);
        
        hr = m_pDDS->GetDC (&hdcSurface);
        
        BitBlt (hdcSurface, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
        
        m_pDDS->ReleaseDC (hdcSurface);

        DeleteDC (hdcBitmap);
        DeleteObject (hbitmap);
    }
    return dwRetVal;
}
