Thread: Windows GDI Flickering

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Windows GDI Flickering

    Hi I have just finished learning how to program simple windows and before I moved onto Direct X and all that. I thought it would be a good idea to make a simple game in windows. So I started and Im using the idea of, clearing an object on the screen by drawing a black rectangle around it. Then drawing the object again in a different position over the top of it.
    I know windows GDI is meant to be slow I was just wondering if there was a way that isn't too advanced of getting rid of the flickering of the objects in the game.
    Thanks

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    22
    I haven't done any games or anything like that myself but I think you should use offscreen drawing. There's a lot of information about it in MSDN. Basicly you draw your things and then BitBlt them to screen DC.
    Not that I know anything, I started winprogramming just this weekend myself:P.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Basically you create a HBITMAP and draw to it, then BitBlt it onto your window.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    that's exactly right. It's called back buffering. You basically always draw to a bitmap in the background (a memory HDC). And then you blit when it is done. DirectX and OpenGL both also do this.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Okay so this HBITMAP. I am guessing this is a handle to something. My question is how would your draw to this. Could you still use your pens brushes and the HDC or is there another way, and then how do you then switch it with whats on the screen?

    If anyone has an example of some code I can see it would be very appreciated thanks.

  6. #6
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I have a code snippet of how I created my back buffer and bound it to a DC.
    http://cboard.cprogramming.com/showt...threadid=43686
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  7. #7
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    The best solution is the double buffer solution that everyone is speaking about. It is how all real games are made today.

    But... Depending on your game, you can make it like they did in the old days - draw the object in its new location first (assuming no see-thru pixels in the sprite), and then delete the sprite from the old location, but only what was NOT covered by the drawing.

    Personally, I would just learn the double buffering method. Even though GDI is slow, its fast enough for a simple game with double buffering. And, you'll get used to how it is done, which will help when you eventually move on to DirectX.

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Could you still use your pens brushes and the HDC or is there another way<<

    Yes. Once you have created an off-screen 'memory' device context and selected a valid bitmap into it you can use any gdi drawing function you want. Same goes for gdi objects such as pens and brushes.

    >>how do you then switch it with whats on the screen?<<

    Use BitBlt - an example of its use is linked at the bottom of that page. Search this board and google for plenty more examples.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Alright im starting to get this and managed to get an example of this technique working. Just another question and hopefully I will be out of your hair. You see I have a project in a number of classes each in control of drawing their object. Should I have each class create a seperate bitmap and do each bit individually, or just have one big bitmap as a parameter to each of these functions and repaint the whole screen?

    Thanks again

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>Basically you create a HBITMAP and draw to it,

    No you create a compatible device context and bitmap. A HBITMAP is just a handle to a bitmap. It needs a device context to hold the rest of the GDI resources.

    >>create a seperate bitmap and do each bit individually, or just have one big bitmap as a parameter to each of these functions

    Up to you. I do both depending on the complexity. If you are moving an image around the screen I would suggest seperate bmp's. As this will allow you finer control. Assemble all the little pieces then draw them all to the back buffer.

    Then call for a paint.
    Do not do more than one BitBlt() in the paint handler.
    "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

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    Alright thanks for that. I will just have one bitmap. its seems more efficient somehow and then get each class to edit it how it sees fit and then in the end bitblt the final product to the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows GDI vs. Java Graphics
    By Perspective in forum Windows Programming
    Replies: 7
    Last Post: 05-07-2007, 10:05 AM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM