Thread: SetPixel(), then moving window

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    31

    SetPixel(), then moving window

    I have a really basic program that's only job is to fill a window with pixels of random color in random places. I do this with SetPixel() placed inside my windows procedure (just to make sure this happens a lot, probably not the best way, I know). Things seem to work fine, but when I move the window or resize it, etc. the entire screen clears and I don't understand why. Also, why is DirectX faster than GDI? What does DirectX use that makes it run faster and how can I access it?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>when I move the window or resize it, etc. the entire screen clears and I don't understand why.

    Because the hdc you have drawn to has lost scope or been cleared by the OS.

    Have a look at the code I posted here. Should give you a good start.

    http://cboard.cprogramming.com/showthread.php?t=64333

    If you create a single buffer then it should be fast enough. SetPixel() is slow and could be avoided.

    >>why is DirectX faster than GDI?

    AFAIK directx calls the graphics card to do some processing. GDI is called, then the graphics card.

    GDI is the tool to use for your app (IMO)
    "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

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    So why does moving it and resizing it make the DC out of its scope? How exactly is the appearance of the window stored?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>How exactly is the appearance of the window stored?

    Thats the point, its not stored.........(Apart from in your resource script or winclass background brush)

    Unless you hold a copy in a buffer DC yourself and copy it back when a paint msg is received.
    "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

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Aw, I think I gotcha. Should I store everything in a separate bitmap and only use a DC when I'm actually drawing it to the window? I've heard bad things happen when you have too many DC's open.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    That's the idea. It's called back-buffering. You set the pixels in a bitmap and "blit" it to the screen in one GDI call. This is very fast. No need for DirectX for most applications.

    Where DirectX comes in to play is when you want to use hardware accelleration for certain common graphical operations such as matrix math, 3d texture mapping, etc.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    as far as having too many DCs open. You don't really need any open until the moment of the WM_PAINT message. One method is to just get the paintDC and SetDIBits into it.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Cool, thanks guys. One more question: I tried to set the brush color of one of my DCs using SetDCBrushColor() and for whatever reason I got a compiler error saying that it was undeclared. This struck me as odd seeing as on MSDN it said that it was part of Wingdi.h which is included in Windows.h . I checked this out, and sure enough, there it was. I wasn't able to use this, and ended up doing a SelectObject() instead, which worked fine. Anybody have an idea as to why this error occurred?

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    31
    Oh, also, is there a way to modify a bitmap without creating a DC for it? I've read in tutorials that this is the only way, but you just said that you only really need it when WM_PAINT is sent

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Quote Originally Posted by SgtMuffles
    Oh, also, is there a way to modify a bitmap without creating a DC for it? I've read in tutorials that this is the only way, but you just said that you only really need it when WM_PAINT is sent
    allocate a buffer big enough for the DIB. You'll have to look up the format. It's basically a bmp file without BITMAPFILEHEADER. That is what windows refers to as a DIB. Then just set the bytes. No DC or GDI calls necessary. On the WM_PAINT, SetDibits()
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM