Jump to content
  • Advertisement
Sign in to follow this  

Text, DCs, and Offscreen DCs

This topic is 4905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

when i print text (using an HFONT object) straight to the screen DC, everything works except the text flickers when i try to print it to an offscreen dc, then bitblt it to the screen, it doesn't show anything if i set the flags in bitblt to WHITENESS or BLACKNESS, it works correctly, but when using SRCCOPY, nothing shows up i just want to print the text, as the rest of the graphics are handled by DirectX, but i could never get the directx font objects to work

Share this post

Link to post
Share on other sites
I think you actually BitBlt to the wrong DC,to be precise maybe pasting part of the code here might help.

1)Make sure the text was being drawn to the backbuffer's DC
2)Now bitblt is back to the actual DC
i.e: BitBlt(hdc,......,backbufferDC,SRCCOPY)

In DirectX did you crete the font like this
CD3DFont* Font;

need to include the #include "d3dfont.h" before you can do so.

Hopefully this helps...

Share this post

Link to post
Share on other sites
Ha. I had to post to fix this too - let me give you my solution, what I call a "BackBuffer" class.



#include <windows.h>


class BackBuffer
BackBuffer(HWND hwnd);

void Buffer(HWND hwnd);
HDC operator () ();
void Display(HWND hwnd);
void ClearBuffer(COLORREF color);

HBITMAP my_back_buffer;
RECT my_rc_client;
HDC my_memDC;
HDC my_hdc;



#include "backbuffer.h"

BackBuffer::BackBuffer(HWND hwnd)
HDC my_cDC;
my_cDC = GetDC(hwnd);
GetClientRect(hwnd, &my_rc_client);
my_back_buffer = CreateCompatibleBitmap(my_cDC, my_rc_client.right, my_rc_client.bottom);
ReleaseDC(hwnd, my_cDC);


void BackBuffer::Buffer(HWND hwnd)
my_hdc = GetDC(hwnd);
my_memDC = CreateCompatibleDC(my_hdc);
SelectObject(my_memDC, my_back_buffer);

HDC BackBuffer::operator () ()
return my_memDC;

void BackBuffer::Display(HWND hwnd)
BitBlt(my_hdc, 0, 0, my_rc_client.right, my_rc_client.bottom, my_memDC, 0, 0, SRCCOPY);
ReleaseDC(hwnd, my_hdc);

void BackBuffer::ClearBuffer(COLORREF color)
HPEN back_pen = CreatePen(PS_SOLID, 1, color);
HPEN old_pen = (HPEN)SelectObject(my_memDC, back_pen);
HBRUSH back_brush = CreateSolidBrush(color);
HBRUSH old_brush = (HBRUSH)SelectObject(my_memDC, back_brush);
Rectangle(my_memDC, 0, 0, my_rc_client.right, my_rc_client.bottom);
SelectObject(my_memDC, old_pen);
SelectObject(my_memDC, old_brush);

Here is how you use it...

Let's assume you have a "Render" function that handles drawing to the screen. First off, create a global BackBuffer that gets created and deleted once during the program - otherwise you have to create and destroy a large bitmap once/frame, which causes some real slowdown. Anyways, during the WinMain message pump, call


then call your Render function, which should be able to somehow access the HDC "memDC" stored in BackBuffer, either as an argument or globally through

BackBuffer (), which returns its memDC.

when you are ready to display everything to the screen, call


This will draw everything to the screen. Call as often as you like, and put

BackBuffer.Display(hwnd) with your CALLBACK routine's message handler, during WM_PAINT messages.

Good luck. Again, 3 functions:

Render(BackBuffer(), ...); //Use the HDC in the first argument to render everything!

[EDIT] one other thing - the ClearBuffer function will clear the memory DC to a certain color - like if you called

BackBuffer.ClearColor(RGB(0, 0, 0));

during your Render function, as the first rendering call, it would clear it to black, ensuring you have no leftovers from last frame. If what you are going to
display will cover the whole screen, there is no need to call this function.

Also, I made this for a 2D, pure win32 API game, so you might have different needs. Hope it helps.

Share this post

Link to post
Share on other sites
Respecto on silverphyre673.That was a good piece of work for Win32.But I somehow did a code search and found that if you google hard enough,you will find a backbuffer proggie for MFC as well.So it depends which one you like to use.Win32 or MFC

Good stuff there silverphyre673.I had an almost same backbuffered WIN32 codes as yours seem like yours was pretty good.!


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!