Thread: Windows Pixel Blocks... PLEASE READ!

  1. #1
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Question Windows Pixel Blocks... PLEASE READ!

    In dos I have an adress I can 'write' to and I cna ptu pixels up to the screen, with memcpy I can effectively set up a backbuffer and software renderer. Well in windows I need to BitBlt from one context to another... how do I create a context that isnt on the screan, then write my pixels to it, and BitBlt it over... At leaste I think thats how I would do it... anywayz I need to copy over an array of pixels from memory to the screen somehow in Windows... this is all that stands in my way.

    Preferably the array would be of "COLORREF" values...

    Thanks in advance for all help! SPH

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You need to draw to a memory DC, here's a link to one of the threads where it has already been discussed -

    http://www.cprogramming.com/cboard/s...ghlight=BitBlt

  3. #3
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Exclamation Problem...

    Well I have sevral problems with this:

    1.) It won't work
    2.) It doesnt do the original purpose: copying an array of COLORREF values to an HDC quikly.

    Here is my code..:

    #include <windows.h>

    HWND hwnd = NULL;
    bool jah = false;

    HDC p_Primary = NULL;
    HDC p_Secondary = NULL;

    LRESULT CALLBACK mainproc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    switch(message)
    {
    case WM_CLOSE:
    {
    jah = false;
    return 0;
    }
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }

    int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
    {
    WNDCLASSEX mainclass;
    ZeroMemory((void*)&mainclass,sizeof(WNDCLASSEX));
    mainclass.cbSize = sizeof(WNDCLASSEX);
    mainclass.hbrBackground = (HBRUSH)COLOR_WINDOW;
    mainclass.hCursor = LoadCursor(hInstance,IDC_ARROW);
    mainclass.hIcon = LoadIcon(hInstance,IDI_APPLICATION);
    mainclass.hIconSm = mainclass.hIcon;
    mainclass.hInstance = hInstance;
    mainclass.lpfnWndProc = mainproc;
    mainclass.lpszClassName = "wingltest";

    if(!RegisterClassEx(&mainclass))return 0;

    hwnd = CreateWindowEx(WS_OVERLAPPEDWINDOW,"wingltest","wi ngltest",
    WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstanc e,NULL);

    ShowWindow(hwnd,SW_SHOW);
    UpdateWindow(hwnd);

    jah = true;

    MSG *thread = new MSG;

    p_Primary = GetDC(hwnd);
    p_Secondary = CreateCompatibleDC(p_Primary);

    for( unsigned int sloopy = 0 ; sloopy < 640 ; sloopy++ )
    {
    for( unsigned int sloopx = 0 ; sloopx < 480 ; sloopx++ )
    {
    SetPixel(p_Secondary,sloopx,sloopy,RGB(45,45,30));
    }
    }

    BitBlt(p_Primary,0,0,640,480,p_Secondary,0,0,SRCCO PY);

    while(jah)
    {
    PeekMessage(thread,NULL,NULL,NULL,PM_REMOVE);
    TranslateMessage(thread);
    DispatchMessage(thread);
    }

    DeleteDC(p_Secondary);
    ReleaseDC(hwnd,p_Primary);

    return 1;
    }

    SPH

    P.S. I still need to know that array copyign thing!!!

  4. #4
    Unregistered
    Guest
    It looks like you are expecting too much from gdi graphics. Since you have called your wnd "wingltest" I presume it is your intention to test openGL in windows.

    If so you should head over to http://nehe.gamedev.net/ for a major boost in that direction.

    If not then you might consider writing your own 'SetPixel' fn for gdi bitmaps. For this go to msdn and look up BITMAPINFO and BITMAPINFOHEADER structures to begin with.

    You might want to take a look at CreateDIBSection too as it's a swift way of retaining a ptr to the bitmap bits themselves.

  5. #5
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Thumbs up Explaining...

    Its called that becouse im porting something I made for dos over to windows, I use OpenGL ALL THE TIME!!!! lolz, OpenGL = Open Graphics Language, wingl = windows graphics language. I am porting a software renderer, and all I need is an interface with the gdi. I originally wanted to just make an HBITMAP with the correct COLORREF's and then somehow putting it to the HDC, I dont know how to use bitmaps, what I need to do:

    1.) Put an array of COLORREF's onto an HDC.

    Thats it, I would REALLY appriciate any code for this...

    SPH

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >1.) It won't work

    I haven't looked at all of your code, but when you're creating a memory DC, you have to select a bitmap into it, otherwise you're not drawing on anything. Your code doesn't appear to be doing that.

  7. #7
    Unregistered
    Guest
    Thanks Sorensen, for clarifying that - I didn't look too closely at that part (oops!).

    CreateDIBSection is a better way to go than trying to use SetPixel - unless you are happy to go make a cup of coffee, drink it, break wind a couple of times...

    Use BitBlt with the handle returned from CreateDIBSection - as Sorensen has described. I think there are explicit examples on msdn. I'd go look up one of my own but I have a massive headache and am having to wear shades to look at my monitor

  8. #8
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb Allright...

    I'm not incredibly expierienced with gdi... could I have a sample peice of code like this:

    void BltArrayContext(HDC dest,COLORREF *Pixels,unsigned int x,unsigned int y);

    This would copy the array of Pixels to the destination Context... that would be GREAT!!!!

    SPH

  9. #9
    Unregistered
    Guest
    Sure...

    in your WM_PAINT handler:

    PAINTSTRUCT ps;
    BeginPaint(hwnd,&ps);
    //create a tmp dc
    HDC hTmpDC=CreateCompatibleDC(ps.hdc);
    //create a 'source' bitmap for demo purposes
    HBITMAP hSource=CreateCompatibleBitmap(ps.hdc,640,480);
    //load demo bmp into dc but save dc 'contents' first
    int nDC=SaveDC(hTmpDC);
    SelectObject(hTmpDC,hSource);
    //now that the bmp (which has no info in it yet) can be drawn to
    //so first set up a RECT for bmp dimension
    RECT rc;
    SetRect(&rc,0,0,640,480);
    //now make a blue brush to paint it with
    HBRUSH hbr=CreateSolidBrush(RGB(0,0,255));
    //and paint the bitmap with it
    FillRect(hTmpDC,&rc,hbr);
    //gdi objects are system resource hungry so free 'em asap
    DeleteObject(hbr);
    //now transfer or 'bit-block transfer' the blue rectangle source
    //bitmap to the wnd surface
    BitBlt(ps.hdc,0,0,640,480,hTmpDC,0,0,SRCCOPY);
    //now remeber to clean up all those gdi objects to stop horrible
    //things from happening
    RestoreDC(hTmpDC);
    DeleteObject(hSource);
    DeleteDC(hTmpDC);
    EndPaint(hwnd,&ps);

    You should have a glaringly blue 640x480 rectangle displayed in your wnd.

    To stop flicker on re-draw during resizing of the wnd ( and if 640x480 are the client dimensions of your wnd) then handle the WM_ERASBKGND msg and return TRUE. This stops the background from being erased and redrawn.

    Does that help?

  10. #10
    Unregistered
    Guest
    oops....

    where I have :

    RestoreDC(hTmpDC);

    replace with:

    RestoreDC(hTmpDC,nDC);

  11. #11
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Lightbulb thanks!

    That worked well when I modified it to my purposes, exept one thing:

    It seems to be running in 256 Color mode, when I use the RGB() macro, it only uses the r paramiter, and that acts like 256 colors...

    Can anyone help ?

    Thanks, SPH

  12. #12
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    1. You are doing a lot in our paint function where this should be a limited to what is vital. All the creats should be done on init and cleaned up on exit. SelectObject() returns the previous item in a HDC and can be used to catch and return the GDI objects.

    2. You are creating a hdc compatible with the update area in the paint. What is the update area, (could be a ctrl)? Better to create it with 'NULL' as this will allow you to create one compatible with the display. Again this should not be done each time the app tries to repaint.

    3. Ensure you have set the mode in the HDC

    SetStretchBltMode(hdc,HALFTONE);

    4. This can also be helpful but must be called before the BeginPaint() as BeginPaint() validates the update region. It can allow multi HDC's to be selectively draw and smaller areas to speed up the paint.

    Code:
    GetUpdateRect(hWnd, &theRect, 0);
    if (IsRectEmpty(&theRect))
    {
        GetClientRect(hWnd,&theRect);
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  13. #13
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    hmm

    If you want good functionality with a back buffer and such, i think you should load up direct draw from directx 7.0

    You can get a pointer to the back buffer just like in dos as you want it. Then you can tell directx to flip the buffers.

    Directx is pretty difficult to setup though. But you would only be setting up direct draw and in the end it would probably minimize the extra code you have to write. It should be much faster than GDI as well.

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>2. You are creating a hdc compatible with the update area in the paint. What is the update area, (could be a ctrl)? Better to create it with 'NULL' as this will allow you to create one compatible with the display. Again this should not be done in repaint.

    Sorry I meant

    GetDC(NULL);//gets display hdc

    and create compatible to this.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    Registered User minime6696's Avatar
    Join Date
    Aug 2001
    Posts
    267

    Arrow ...

    That desktop HDC thing didnt do anything... and thats true mostly becosue the windows are created on the same paramiters as the desktop...

    SPH

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  2. Read Excel File from Windows Program
    By baburajv in forum Windows Programming
    Replies: 2
    Last Post: 11-07-2006, 01:42 PM
  3. Dialog Box Problems
    By Morgul in forum Windows Programming
    Replies: 21
    Last Post: 05-31-2005, 05:48 PM
  4. dual boot Win XP, win 2000
    By Micko in forum Tech Board
    Replies: 6
    Last Post: 05-30-2005, 02:55 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM