Double Buffering

Anything to do with programming games...
Darren
Posts: 13
Joined: Fri Jul 08, 2005 11:49 pm

Double Buffering

Post by Darren » Fri Jul 08, 2005 11:54 pm

I can't get my apps to stop flickering. From my understanding double buffering is just painting to offscreen memory and then blting it to the screen. Here is the method I am currently trying to use, anyone know what I am not doing correctly?

Code: Select all

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow){
   static TCHAR szAppName[] = "app_name";
   HWND hwnd;
   MSG msg;
   WNDCLASS wndclass;
   wndclass.style = CS_HREDRAW | CS_VREDRAW;
   wndclass.lpfnWndProc = WndProc;
   wndclass.cbClsExtra = 0;
   wndclass.cbWndExtra = 0;
   wndclass.hInstance = hInstance;
   wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
   wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
   wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
   wndclass.lpszMenuName = NULL;
   wndclass.lpszClassName = szAppName;
   if(!RegisterClass(&wndclass))   {
      MessageBox(NULL, "Class registration failed!", szAppName, MB_ICONERROR);
      return 0;
   }
   hwnd = CreateWindow(szAppName, "caption", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 700, 700,
   NULL, NULL,   hInstance, NULL);
   ShowWindow(hwnd, iCmdShow);
   UpdateWindow(hwnd);

   while(GetMessage(&msg, NULL, 0, 0))   {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
   }
   return (int)msg.wParam;
}
HDC buffer;
HBITMAP bitmap_buffer;
#define TIMER 1
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
   HDC hdc;
   PAINTSTRUCT ps;
   RECT rect;
   switch(message){
      case WM_TIMER:
         InvalidateRect(hwnd, NULL, true);
      return 0;
      case WM_CREATE:
         hdc = GetDC(hwnd);
         SetTimer(hwnd, TIMER, 100, 0);
         bitmap_buffer = CreateCompatibleBitmap(hdc, 700, 700);
         buffer = CreateCompatibleDC(hdc);
         SelectObject(buffer, bitmap_buffer);
         ReleaseDC(hwnd, hdc);
      return 0;
      case WM_PAINT:
         hdc = BeginPaint(hwnd, &ps);
         GetClientRect(hwnd, &rect);
         FillRect(buffer, &rect, (HBRUSH)GetStockObject(BLACK_BRUSH));
         TextOut(buffer, 100, 100, "this shouldn't flicker", strlen("this shouldn't flicker"));
         BitBlt(hdc, 0, 0, 700, 700, buffer, 0, 0, SRCCOPY);
         EndPaint(hwnd, &ps);
      return 0;
      case WM_CLOSE:
         PostQuitMessage(0);
      return 0;
   }
   return DefWindowProc(hwnd, message, wParam, lParam);
}


thanks

User avatar
qwas
Posts: 350
Joined: Thu Apr 14, 2005 6:00 pm
Location: London, UK
Contact:

Post by qwas » Sat Jul 09, 2005 6:23 am

????????????????????????????? CANT PROGRAM LOL
Proud member of Seans forum since 14th April 2005. 1 year and still counting.
Now to spam those 30 messages to get into 3rd place in post count. :D

I'd like to buy your soul please.
http://www.mindistortion.net/iwantyours ... lex_Rider2

User avatar
Sean OConnor
Posts: 1299
Joined: Fri Sep 03, 2004 7:47 am
Location: Cambridge, UK
Contact:

Post by Sean OConnor » Mon Jul 11, 2005 6:05 am

Yes, I can see what you are doing wrong!

In this line you are telling your application that the background is painted with a white brush:

wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);

and in this line:

InvalidateRect(hwnd, NULL, true);

you are telling the computer to redraw the entire screen ("NULL") but first repaint the screen with a white brush ("true"). That's why it will keep flickering white each time you redraw a frame.

I always do:

wndclass.hbrBackground = NULL;

and

InvalidateRect(hwnd, NULL, FALSE);

If you want to have a white background just fill the offscreen bitmap with a white brush before drawing everything to it and then when you BLT that to the screen everything will look OK.

Darren
Posts: 13
Joined: Fri Jul 08, 2005 11:49 pm

Post by Darren » Mon Jul 11, 2005 4:29 pm

thanks, that solved the problem. everything looks great :)

User avatar
Fighter_Ace
Posts: 409
Joined: Mon Sep 06, 2004 4:36 am
Location: Sacramento, CA, United States of America - Also can be found somewhere in the land of 1's and 0's
Contact:

Post by Fighter_Ace » Mon Jul 11, 2005 8:02 pm

Sweet! I just gotta get good at programming! It it utterly too amazing!
My thanks and best regards to all my former submitters.

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Tue Jul 12, 2005 1:43 am

That's GDI right Sean?
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

User avatar
Sean OConnor
Posts: 1299
Joined: Fri Sep 03, 2004 7:47 am
Location: Cambridge, UK
Contact:

Post by Sean OConnor » Tue Jul 12, 2005 7:10 am

Yes, this is GDI

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Tue Jul 12, 2005 2:22 pm

I thought so. GDI is MFC right? Last night I learned to control bitmaps with regular buttons using GDI. I'm getting better at a bunch of languages and tools. This is great :)
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

User avatar
Sean OConnor
Posts: 1299
Joined: Fri Sep 03, 2004 7:47 am
Location: Cambridge, UK
Contact:

Post by Sean OConnor » Tue Jul 12, 2005 3:47 pm

No, GDI is just the basic graphics calls like BitBlt. You can use MFC if you want but that's all C++ and object orientated stuff and I use plain C. Either way though you are using the GDI functions.

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Tue Jul 12, 2005 4:00 pm

ah I thought CBitmap was in MFC. That's what I used, you prolly did it dif. I've got the bitmap control down now so it's time for writing the game loop :D
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

User avatar
Sean OConnor
Posts: 1299
Joined: Fri Sep 03, 2004 7:47 am
Location: Cambridge, UK
Contact:

Post by Sean OConnor » Tue Jul 12, 2005 8:40 pm

Yes, CBitmap sounds like an MFC thing. It's probably just some sort of wrapper thing for the HBITMAP that I would use myself.

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Tue Jul 12, 2005 9:15 pm

yep i looked, it has a bunch of functions within the class. this is good stuff!
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Tue Jul 12, 2005 9:16 pm

i wish directx was this easy hehe
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

User avatar
Fighter_Ace
Posts: 409
Joined: Mon Sep 06, 2004 4:36 am
Location: Sacramento, CA, United States of America - Also can be found somewhere in the land of 1's and 0's
Contact:

Post by Fighter_Ace » Wed Jul 13, 2005 7:50 pm

OK OK OK! I am a totally new at this. But what do GDI and MFC stand for? Also, frawsty, could you send me a sample of your work maybe? It sounds as if you are doing very well in it work and I thought you could possibly give me a heads-up :wink: . Keep me updated 8) ... I like this programming stuff a little too much :twisted: ! Heh heh!

Oh, and when you said,

frawsty wrote:I thought so. GDI is MFC right? Last night I learned to control bitmaps with regular buttons using GDI. I'm getting better at a bunch of languages and tools. This is great :)


What other languages ore you exactly learning?

P.S. I almost have a full game out on GM with no tutorials used what-so-ever! Yeah, I'm new to this stuff, and I am taking the easy way... for now, but still have hopes to improve!
My thanks and best regards to all my former submitters.

User avatar
frawsty
Posts: 77
Joined: Sat Jun 04, 2005 3:40 pm
Location: So Cal, USA
Contact:

Post by frawsty » Wed Jul 13, 2005 9:28 pm

Fighter_Ace wrote:OK OK OK! I am a totally new at this. But what do GDI and MFC stand for?

GDI = Graphical Device Interface (graphics handling in Windows apps)
MFC = Microsoft Foundation Classes (M$'s c++ classes for Windows programmers)

Fighter_Ace wrote:Also, frawsty, could you send me a sample of your work maybe? It sounds as if you are doing very well in it work and I thought you could possibly give me a heads-up :wink: . Keep me updated 8) ... I like this programming stuff a little too much :twisted: ! Heh heh!


I'm doing noob stuff in Visual C++ you can look at but if you don't know VC++ you're gonna be like wtf?? I haven't done much.

I'm not really messing with GM anymore. I want to focus on my Windows programming skills so I can use directX like the big boys :)

Fighter_Ace wrote:
frawsty wrote:I thought so. GDI is MFC right? Last night I learned to control bitmaps with regular buttons using GDI. I'm getting better at a bunch of languages and tools. This is great :)


What other languages ore you exactly learning?


I know C++ but I'm very rusty. I was learning Actionscript for Flash games but the performance is terrible so no more of that (but it's basically c/c++.) GML for a little bit but I don't want to get dependent on GM now that I've gotten the dust brushed off my C++ :)

GM is a great tool for you to bridge the ++ part of C to C++. Objects, Instances, Relationships etc.

Turn on your YIM or AIM if you want to chat. I always have it on.

P.S. I almost have a full game out on GM with no tutorials used what-so-ever! Yeah, I'm new to this stuff, and I am taking the easy way... for now, but still have hopes to improve!
Johnny
--------
http://www.funkyfreshdesigns.com
Web Dev, Game Dev, Computer Support, Networking and more!

Post Reply