+ Reply to Thread
Results 1 to 10 of 10

Thread: Getting a Device Context to a Bunch O' Bits.

  1. #1
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default Getting a Device Context to a Bunch O' Bits.

    Hello Windows API Gurus!

    I have a bunch of bits in RGBA format-- 32 bit. Rather than write my own 32-bit code, 16-bit code, and everything in between code, I'd like to use BitBlt to dump these guys onto a DirectX surface (there's no alpha involved in this, so I can use bitblt).

    So, what I want to ask is... how can I get a device context to what is essentially a char* array? Obivously I need to build something up, possibly a bitmap, but I literally don't know where to begin.

    Thanks!
    --John

  2. #2
    Administrator
    Join Date
    Jul 2004
    Posts
    3,401

    Default

    I'm not really sure I get your question but it kind of sounds like you need the function... "setdibitstodevice" -- you should be able to pass in the format you need and then blt the dc to the surface.

    - S
    Steve Verreault - Twilight Games
    http://www.twilightgames.com --- http://www.indiegamer.com

    "Do you really think it is weakness that yields to temptation? I tell you that there are terrible temptations which it requires strength, strength and courage to yield to.” - Oscar Wilde

  3. #3
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default

    Well, here, let me take it a little further with some code I just tried to write from Google...

    I have my array of bits, which is declared as "char *mData".

    To get a DC, I tried to do this, by making a bitmap temporarily, and trying to select the bitmap into the DC:

    Code:
    mDC=::CreateCompatibleDC(NULL);
    
    BITMAPINFO aBMPInfo;
    ZeroMemory(&aBMPInfo,sizeof(aBMPInfo));
    aBMPInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
    aBMPInfo.bmiHeader.biWidth=mWidth;
    aBMPInfo.bmiHeader.biHeight=mHeight;
    aBMPInfo.bmiHeader.biSizeImage=(((mWidth*32+31)&~31)>>3)*mHeight;
    aBMPInfo.bmiHeader.biCompression=BI_RGB;
    aBMPInfo.bmiHeader.biXPelsPerMeter=0;
    aBMPInfo.bmiHeader.biYPelsPerMeter=0;
    aBMPInfo.bmiHeader.biClrImportant=0;
    aBMPInfo.bmiHeader.biClrUsed=0;
    aBMPInfo.bmiHeader.biPlanes=1;
    aBMPInfo.bmiHeader.biBitCount=32; 
    
    mBitmap=::CreateDIBSection(mDC,&aBMPInfo,DIB_RGB_COLORS,(void**)mData,NULL,0);
    
    ::SelectObject(mDC,mBitmap);
    ...Later, I try to bitblt "mDC" onto my DirectX Surface's DC... (I know the DX Surface DC is working because I am able to do fillrects and whatnot on it):

    Code:
    BitBlt(aSurfaceDC,0,0,mWidth,mHeight,mDC,0,0,SRCCOPY);
    ...what I get, though is total blackness. So I assume I'm screwing up that CreateDIBSection call... but I can't figure out how (I haven't used the WinAPI extensively since 1996).

  4. #4
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default

    One more note:

    I tried implementing your idea thus:

    SetDIBitsToDevice(mDC,0,0,mWidth,mHeight,0,0,0,mHe ight,mData,&aBMPInfo,DIB_RGB_COLORS);

    ...but still I get the blackness.


    Furthermore, if I try to do a simple fillrect to that new DC that I created, I get nothing, too-- I'd expect that fillrect to show up on the DC I BitBlt the new DC to. Is there some special hoop you need to jump through when you create a DC?
    Last edited by Raptisoft; 11-01-2004 at 03:10 AM.

  5. #5
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default

    Okay, taking it ONE step further, using this line to create the DC:

    Code:
    mDC=::CreateDC("DISPLAY",NULL,NULL,NULL);
    ...works. But then it blts my stuff on the desktop too. What do you use to create a whole new DC that you can use more or less randomly?

  6. #6
    Senior Member
    Join Date
    Jul 2004
    Location
    Calgary, AB
    Posts
    522

    Default GetDC instead?

    Hey dude,

    Have you tried using GetDC(HWND hWnd) rather than using CreateDC?

    I know from my OpenGL days on the PC, you had to do this to grab the DC context to make it GL-"friendly" and be able to manipulate it..(either to create a windowed or fullscreen display)

    Hopefully this link still works to the MSDN...

    GetDC(HWND hWnd)

  7. #7
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default

    Hi Wazoo...
    I would, but the location of this piece of code... well, I'd prefer that it be completely standalone, no window references, completely portable if I ever want to do something else with it.

    I might not understand the WinAPI... is a DC always referring to something else, like a pointer to display memory? Or is it possible to create a whole page out in the great nowhere to use?

    For instance, can I make a DIB... then get an DC pointing at the DIB? I don't seem to see anything like this in the help file... but, and I say again... MS help files are only useful if you already know exactly what you're looking for and just want to check format.

  8. #8
    Senior Member
    Join Date
    Jul 2004
    Location
    Croatia, Europe
    Posts
    125

    Default

    Quote Originally Posted by Raptisoft
    Hello Windows API Gurus!
    I have a bunch of bits in RGBA format-- 32 bit. Rather than write my own 32-bit code, 16-bit code, and everything in between code, I'd like to use BitBlt to dump these guys onto a DirectX surface (there's no alpha involved in this, so I can use bitblt).
    If all you want is arbitrary pixel format conversion with no alpha, perhaps this would help? It's undocumented and it's not fast, but it works. You'll need to change a few includes to compile it (take out the memmanager stuff and change path to "util_macros.h" to where you put it.

    BTW, mask[3] are bltk_pixformat_t are bit masks for RGB bits.

  9. #9
    Senior Member
    Join Date
    Jul 2004
    Posts
    116

    Default

    Quote Originally Posted by Raptisoft
    Code:
    mBitmap=::CreateDIBSection(mDC,&aBMPInfo,DIB_RGB_COLORS,(void**)mData,NULL,0);
    
    ::SelectObject(mDC,mBitmap);
    
    BitBlt(aSurfaceDC,0,0,mWidth,mHeight,mDC,0,0,SRCCOPY);
    ...what I get, though is total blackness. So I assume I'm screwing up that CreateDIBSection call... but I can't figure out how (I haven't used the WinAPI extensively since 1996).
    Assuming that mData is the pointer to your image data, then what's happening is that you are never copying the data to the DIB. The 4th parameter to CreateDIBSection is an out parameter that receives a pointer the the DIB's bits, not an in parameter pointer to the image you want to load.

    To get your image data to the DIB, you need to copy it scanline by scanline to the DIB array after the call to CreateDIBSection. Then you can call SelectObject and BitBlt as you have it now. I believe gametutorials.com has some examples of loading images via CreateDIBSection. So does Petzold

  10. #10
    Senior Member
    Join Date
    Jul 2004
    Posts
    799

    Default

    Ah, thank you Aldacron. That did the trick, finally. Dumb mistake, huh?

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts