Sign in to follow this  
cool katz

Direct Draw Blitting

Recommended Posts

cool katz    122
Sorry for making yet another direct draw blitting thread but this problem has been bothering me for weeks. The problem is that i am not able to load bitmaps and display them properly. My bitmap loading function is int Load_Bob_File(BOB_PTR bob, int cx, int cy, int mode, char *filename) { BITMAPFILEHEADER bfh; BITMAPINFOHEADER bih; FILE *bitf = fopen(filename, "rb"); fread(&bfh, sizeof(BITMAPFILEHEADER), 1, bitf); fread(&bih, sizeof(BITMAPINFOHEADER), 1, bitf); fclose(bitf); bob->width = bih.biWidth; bob->height = bih.biHeight; bob->surface = DDLoadBitmap(lpdd,filename,bob-> width,bob->height); DDCOLORKEY c; c.dwColorSpaceHighValue = 0; c.dwColorSpaceLowValue = 0; bob->surface->SetColorKey(DDCKEY_SRCBLT,&c); return(1); } And my Drawing Function is int Draw_Bob(BOB_PTR bob, // bob to draw LPDIRECTDRAWSURFACE7 dest) // surface to draw the bob on { // draw a bob at the x,y defined in the BOB // on the destination surface defined in dest RECT dest_rect, // the destination rectangle source_rect; // the source rectangle // is this a valid bob if (!bob) return(0); // fill in the destination rect dest_rect.left = bob->x; dest_rect.top = bob->y; dest_rect.right = bob->x+bob->width-1; dest_rect.bottom = bob->y+bob->height-1; // fill in the source rect source_rect.left = 0; source_rect.top = 0; source_rect.right = bob->width-1; source_rect.bottom = bob->height-1; HRESULT hr = dest->BltFast(bob->x, bob->y, bob->surface, &dest_rect ,(DDBLT_WAIT)); // blt to destination surface if (FAILED(hr)) { Write_Error("\nError Blitting in Function 'Draw_Bob()'-> %d", hr); return(0); } // return success return(1); } // end Draw_BOB This program compiles fine but the bitmap dosent display and when i look at my error.log file it says "Error Blitting in Function 'Draw_Bob()'" Any help would be greatly appreciated.

Share this post


Link to post
Share on other sites
dist0rted    100
Ah, a fellow LaMothe reader.

You'll find that using two files will make your life a ton easier. ddutil.h and ddutil.cpp. You can find them in \DXSDK\Samples\C++\Common\Include and \DXSDK\Samples\C++\Common\Src. Put both in your project. Now to load a bitmap onto a LPDIRECTDRAWSURFACE7, use the DDLoadBitmap Function like this:

lpddsMyBitmap = DDLoadBitmap(lpdd, "bitmap.bmp", 0, 0);

You can set a color key easily like this:

DDSetColorKey(lpddsMyBitmap, CLR_INVALID); /* CLR_INVALID says just to make the pixel in the top left corner of the bitmap as the one to use as the color key */

Then to Blit it onto the backbuffer:

RECT rRect; // RECT used to setting what dimensions are to take out the file

rRect.top = 0;
rRect.left = 0;
rRect.right = 32;
rRect.bottom = 32;

// This says it's a 32 x 32 bitmap in the top left of the bitmap

lpddsBackBuffer->BltFast(x, y, lpddsMyBitmap, &rRect, DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

// x and y are two integers saying the position where you want it to show up on the screen

And make sure you flip the primary and back buffers so the bitmap appears on screen:

lpddsPrimaryBuffer->Flip(NULL, DDFLIP_WAIT);

Share this post


Link to post
Share on other sites
cool katz    122
Nope still dosent work

here are my functions

Loading bob:

int Load_Bob_File(BOB_PTR bob, int cx, int cy, int mode, char *filename)
{
DDSURFACEDESC2 ddsd;

bob->surface = DDLoadBitmap(lpdd, filename, 0,0);
bob->surface->GetSurfaceDesc(&ddsd);

bob->width = ddsd.dwWidth;
bob->height = ddsd.dwHeight;

DDSetColorKey(bob->surface, CLR_INVALID);


return(1);
}


Drawing Bob:

int Draw_Bob(BOB_PTR bob, // bob to draw
LPDIRECTDRAWSURFACE7 dest) // surface to draw the bob on
{
// draw a bob at the x,y defined in the BOB
// on the destination surface defined in dest

RECT rRect;

// is this a valid bob
if (!bob)
return(0);

rRect.top = 0;
rRect.left = 0;
rRect.right = bob->width;
rRect.bottom = bob->height;

HRESULT hr = dest->BltFast(bob->x, bob->y, bob->surface, &rRect ,DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

// blt to destination surface
if (FAILED(hr))
{
Write_Error("\nError Blitting in Function 'Draw_Bob()'-> %d", hr);
return(0);
}

// return success
return(1);
} // end Draw_BOB


It still says the same thing as before except the width and height values are screwed up...

Share this post


Link to post
Share on other sites
dist0rted    100
Don't even use those functions. Just do this in your Initiation function:

LPDIRECTDRAWSURFACE7 MyBitmap; /* Should be somewhere like after your #include's so it's global */

MyBitmap = DDLoadBitmap(lpdd, "mybitmap.bmp", 0, 0);
DDSetColorKey(MyBitmap, CLR_INVALID);

Then in your Main function:

RECT rRect;
SetRect(&rRect, 0, 0, 32, 32);

lpddsback->BltFast(25, 25, MyBitmap, &rRect, DDBLTFAST_SRCCOLORKEY | DDBLTFAST_WAIT);

lpddsprimary->Flip(NULL, DDFLIP_WAIT);

Then in your Shutdown function:

if (MyBitmap) {
MyBitmap->Release();
MyBitmap = NULL;
}

Share this post


Link to post
Share on other sites
evillive2    779
As the previous poster mentioned, you will find that ddutil.* makes your life much simpler. You will find that the CSurface and CDisplay classes can be wrapped up into a CGraphics class of your own to handle all aspects of the DDraw API along with adding your own helper functions. This will make it much easier for you to recreate almost any NES/SNES/SEGA game you have ever seen without much hassle from the graphics standpoint (3d algorithms not withstanding). Hope you enjoy it as much as I did for about 6 months :)

Share this post


Link to post
Share on other sites

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