Sign in to follow this  

Double buffering with a scrolling background...

This topic is 4840 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

I'm wondering what is the fastest way to implement this? I have a vertically scrolling background and a bitmap that gets drawn on top of it. The background image is tiled to fill up the window, and then scrolled (using the "formula" found at gametutorials.com). I'm wondering if anyone knows of a faster way of doing this (using only the windows gdi, since this is a small project before I start learning directx). I think the best way to do this would be to separate the scrolling background into a different "layer" than everything else, but I don't know how to go about doing that! Or is there a better way? - Stephen

Share this post


Link to post
Share on other sites
Seems to me you are thinking of an offscreen dc. There must be a lot of examples out there already, since this has been (one of) the standard methods for ages. But if you like, I can dig up some old code and post it as an example.

Share this post


Link to post
Share on other sites
Well, there we go then..

Bare in mind that you just copy-pasted it, with some small modifications, so it's far from perfect. But I'll hope it can be of some use. Note: I used a memdc-structure to keep everything a bit more organized, and it was easier to create arrays of 'em that way.. I even recall I made a class that took care of a lot of issues that come up in your situation, be I can't find it right know.



// declaring stuff

struct MEMDC
{
HDC hDC;
HBITMAP hBmp;
HBITMAP hBmpOld;
int dwWidth;
int dwHeight;
};

MEMDC g_MemDC = {NULL, NULL, NULL, 0, 0};

// creating and destroying memory device context

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
static HDC hdc;

switch(msg)
{
case WM_CREATE:

//create memdc
hdc = GetDC(hWnd);
g_MemDC.hBmp = LoadBitmap(g_hInstance, MAKEINTRESOURCE(WHATEVER_RESOURCE_ID));
/*
you can also load it directly from file, obviously:
(HBITMAP)LoadImage(NULL, "ontheheap.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE);
*/

g_MemDC.hDC = CreateCompatibleDC(hdc);
g_MemDC.hBmpOld = (HBITMAP)SelectObject(g_MemDC.hDC, g_MemDC.hBmp);

BITMAP bm;

GetObject(g_MemDC.hBmp, sizeof(bm), &bm);
g_MemDC.dwWidth = bm.bmWidth;
g_MemDC.dwHeight = bm.bmHeight;

ReleaseDC(hWnd, hdc);

break;

case WM_DESTROY:

//destroy memdc
if (g_MemDC.hDC)
{
DeleteObject(SelectObject(g_MemDC.hDC, g_MemDC.hBmpOld));
DeleteDC(g_MemDC.hDC);
}
}

etc...

}

// using it

VOID PaintStuff(CONST HWND hWnd)
{

PAINTSTRUCT ps;
HDC hdc;

InvalidateRect(hWnd, NULL, TRUE);

hdc = BeginPaint(hWnd, &ps);

BitBlt(hdc, 10, 20, 100, 200, g_MemDC.hDC, 50, 0, SRCCOPY);

EndPaint(hWnd, &ps);
}



Share this post


Link to post
Share on other sites
If you want GDI then check out GCL (GDI Console Layer)
It provide with a linear char buffer like in Mode13h and uses backbuffering.It's pretty fast.You can see some stuff it can do on the page screenshots.

Everything in GCL is nicely wrapped up into a compact class.By making some members public you can use the windos API funtions to load bitmaps and write to the backbuffer.Feel free to contact me in case of doubts ;).

Share this post


Link to post
Share on other sites

This topic is 4840 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this