2016-07-07 4 views
2

먼저 UINT를 UINT** framebuffer에 색으로 쓴 다음 CreateDIBSection으로 BITMAPINFO를 만듭니다. 그러나 프로그램을 실행 한 후에는 내가 설정 한 색 대신 검정색으로 표시됩니다. 무엇이 잘못 되었습니까?CreateDIBSection을 사용하여 BITMAPINFO에서 색상 데이터를 쓰는 방법은 무엇입니까?

PAINTSTRUCT ps; 
     HDC hdc; 

    static int s_widthClient, s_heightClient; 
    static BITMAPINFO s_bitmapInfo; 
    static HDC s_hdcBackbuffer;  
    static HBITMAP s_hBitmap; 
    static HBITMAP s_hOldBitmap; 
    static void* s_pData;   

    switch (message) 
    { 
    case WM_CREATE: 
    { 

     RECT rc; 
     GetClientRect(hWnd, &rc); 
     s_widthClient = rc.right - rc.left; 
     s_heightClient = rc.bottom - rc.top; 

     Tiny3DDevice pDevice(s_widthClient, s_heightClient, s_pData); 
     pDevice.Test(); 

     BITMAPINFOHEADER bmphdr = { 0 }; 
     bmphdr.biSize = sizeof(BITMAPINFOHEADER); 
     bmphdr.biWidth = s_widthClient; 
     bmphdr.biHeight = -s_heightClient; 
     bmphdr.biPlanes = 1; 
     bmphdr.biBitCount = 32; 
     bmphdr.biSizeImage = s_heightClient * s_widthClient * 4; 

     s_hdcBackbuffer = CreateCompatibleDC(nullptr); 

     HDC hdc = GetDC(hWnd); 
     //s_hBitmap = CreateCompatibleBitmap(hdc, s_widthClient, s_heightClient); 
     s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS, 
      reinterpret_cast<void**>(&pDevice.m_pFramebuffer), nullptr, 0); 

     s_hOldBitmap = (HBITMAP)SelectObject(s_hdcBackbuffer, s_hBitmap); 
     ReleaseDC(hWnd, hdc); 
    } 
    break; 
    case WM_PAINT: 
    { 
     hdc = BeginPaint(hWnd, &ps); 
     //BitBlt(s_hdcBackbuffer, 0, 0, s_widthClient, s_heightClient, nullptr, 0, 0, WHITENESS); 
     ////draw text 
     //SetTextColor(s_hdcBackbuffer, RGB(0, 0, 0)); 
     //SetBkMode(s_hdcBackbuffer, TRANSPARENT); 
     //TextOut(s_hdcBackbuffer, 0, 5, text.c_str(), text.size()); 

     BitBlt(ps.hdc, 0, 0, s_widthClient, s_heightClient, s_hdcBackbuffer, 0, 0, SRCCOPY); 
     EndPaint(hWnd, &ps); 
    } 
     break; 

과 Tiny3DDevice :

class Tiny3DDevice 
    { 
    public: 
     Tiny3DDevice(int width, int height, void *fb); 
     ~Tiny3DDevice(); 
    public: 
     void Test();  
    public: 
     int m_width; 
     int m_height; 
     UINT** m_pFramebuffer; 
    }; 
Tiny3DDevice::Tiny3DDevice(int width, int height, void *fb) 
{ 
    m_width = width; 
    m_height = height; 
    m_pFramebuffer = new UINT*[width]; 
    for (int i = 0; i < width; ++i) 
    { 
     m_pFramebuffer[i] = new UINT[height]; 
    } 
} 

void Tiny3DDevice::Test() 
{ 
    ZCFLOAT3 color(0.5f, 0.5f, 0.5f); 
    for (int i = 0; i < m_width; ++i) 
     for (int j = 0; j < m_height; ++j) 
     { 
      //m_pFramebuffer[i][j] = MathUtil::ColorToUINT(color); 
      m_pFramebuffer[i][j] = 0x3fbcefff; 
     } 
} 

무슨 일입니까? m_framebuffer?에 어떻게 데이터를 써야합니까? 이 CreateDIBSection에 할당하기 때문에

+0

'Create_CompatibleDC'와'SelectObject (memdc, s_hBitmap);'를'WM_CREATE'에 넣지 마십시오. 대신에 그것을 'WM_PAINT'에 넣고 마지막에 리소스를 해제하십시오. 당신이 가지고있는 방법은 몇 마이크로 초 더 빠르지 만 그것은 틀립니다. –

답변

2

당신은, 당신이 (당신의 코드에서 pDevice.m_pFramebuffer를) 태양 광 발전을 할당 할 필요가 없습니다이

PVOID pv; 
if (s_hBitmap = CreateDIBSection(nullptr, (PBITMAPINFO)&bmphdr, DIB_RGB_COLORS, &pv, 0, 0)) 
{ 
    RtlFillMemoryUlong((PULONG)pv, bmphdr.biSizeImage, 0x3fbcefff); 
} 

같은 코드가 필요합니다. 당신은 그것을 채울 필요가 있습니다. Tiny3DDevice의 코드가 완전히 잘못되었거나 무의미합니다. s_hdcBackbuffer에 대한 사용 정적 바르 등 - 악몽

BITMAPINFOHEADER bmphdr = { 0 }; 

아무도 시도의 사용이 :)?

BITMAPINFOHEADER bmphdr = { }; 
+0

하지만 'SetPixel'처럼 m_pFramebuffer를 변경하여 색상을 변경하고 싶습니다. 어떻게해야합니까? – zhangbaochong

+0

하지만 어떤 문제가 있습니까 ?? 또는 나중에 동적 변경을 위해 pv (m_pFramebuffer)를 채우거나이 포인터를 저장하십시오. 알의 대답을 이해하고 있니? – RbMm

+0

네, 괜찮습니다, 고마워요! 나는 m_pFramebuffer를 할당하는 코드를 삭제하고 다음과 같은 코드를 변경 :'경우 (s_hBitmap = CreateDIBSection (nullptr, (PBITMAPINFO) bmphdr, DIB_RGB_COLORS, \t \t \t reinterpret_cast (pDevice.m_pFramebuffer), nullptr, 0)) \t \t { \t \t \tpDevice.Test(); \t \t} – zhangbaochong