Hey everyone, I've been trying (slowly, unsuccessfully) to learn DirectDraw, but it hasn't been working for me. As far as I can tell, my code which loads a bitmap is working fine, since earlier I succeeded in making a ship appear on the screen (discoloured, but whole). However, now that I've changed some stuff, (i.e. moved away from keeping an array here and a bit of stuff there to having a struct containing the bitmap data), the ship won't show up! Could somebody take a look at my code and point out what's wrong?
PHP Code:
struct BITMAP_FILE
{
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
PALETTEENTRY palette[256];
unsigned char* data;
};
struct BITMAP_OBJECT
{
int state;
int attr;
int x, y;
int xv, yv;
int width, height;
LPDIRECTDRAWSURFACE image;
};
bool CreateBitmapObj(BITMAP_OBJECT& bo, BITMAP_FILE& bf, int flags, int cx, int cy, int mode)
{
//Create
DDSURFACEDESC ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
ddsd.dwWidth = bo.width = bf.bih.biWidth;
ddsd.dwHeight = bo.height = bf.bih.biHeight;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | flags;
memset(&bo, 0, sizeof(bo));
bo.state = BOBJ_STATE_ALIVE;
bo.image = NULL;
if(ddraw->CreateSurface(&ddsd, &bo.image, NULL) != DD_OK)
return false;
DDCOLORKEY key;
key.dwColorSpaceHighValue = 0;
key.dwColorSpaceLowValue = 0;
bo.image->SetColorKey(DDCKEY_SRCBLT, &key);
//Load
if(mode == BOBJ_MODE_CELL)
{
x *= bo.width + 1;
y *= bo.height + 1;
}
unsigned char* src;
unsigned char* dest;
src = bf.data + (y * bf.bih.biWidth) + x;
memset(&ddsd, 0, sizeof(ddsd));
bo.image->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL);
dest = (unsigned char*)ddsd.lpSurface;
for(int y = 0; y < bo.height; ++y)
{
memcpy(dest, src, bo.width);
dest += bo.width;
src += bf.bih.biWidth;
}
bo.image->Unlock(ddsd.lpSurface);
bo.state |= BOBJ_STATE_LOADED;
return true;
}
void DrawBitmapObj(BITMAP_OBJECT& bo, LPDIRECTDRAWSURFACE dest)
{
RECT src, dst;
src.left = 0;
src.top = 0;
src.right = bo.width - 1;
src.bottom = bo.height - 1;
dst.left = bo.x;
dst.top = bo.y;
dst.right = bo.x + bo.width - 1;
dst.bottom = bo.y + bo.height - 1;
dest->Blt(&dst, bo.image, &src, DDBLT_WAIT | DDBLT_KEYSRC, NULL);
}