Advertisement

CSprite not working (long beware)

Started by May 19, 2002 10:00 AM
0 comments, last by Laroche 22 years, 7 months ago
I''m not sure whether this should go in the DirectX forum or Begginers, but I''m sure quite a few people should be able to fix it.. I Made a CSprite Class, which im going to paste below:
  
#include "CSprite.h"

CSprite::CSprite()
{
	lpDD7 = NULL;
}

CSprite::~CSprite()
{
	lpddsTileset->Release();
	lpddsTileset = NULL;

	lpDD7->Release();
	lpDD7 = NULL;
}

CSprite::DrawHBitmap(HBITMAP hBitmap, int x, int y, int width, int height)
{
	HDC           hdcImage;
	HDC           hdc;
	BITMAP        bm;

	if (lpddsTileset == NULL || hBitmap == NULL)
	   return (FALSE);
	lpddsTileset->Restore();

	if (!(hdcImage = CreateCompatibleDC(NULL)))
		return (FALSE);
	if (!(SelectObject(hdcImage, hBitmap)))
		return (FALSE);

	if (!(GetObject(hBitmap, sizeof(bm), &bm)))
		return (FALSE);
	width = width == 0 ? bm.bmWidth  : width;
	height = height == 0 ? bm.bmHeight : height;

	if (!(lpddsTileset->GetDC(&hdc)))
		return (FALSE);
	if (!(BitBlt(hdc, x, y, width, height, hdcImage, 0, 0, SRCCOPY)))
		return (FALSE);
	lpddsTileset->ReleaseDC(hdc);
	DeleteDC(hdcImage);
	return (TRUE);
}

CSprite::CreateSurface(int xs, int ys)
{
	DDSURFACEDESC2 ddsd;
	ZeroMemory(&ddsd, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
	ddsd.dwWidth = xs;
	ddsd.dwHeight = ys;
	ddsd.ddckCKSrcBlt.dwColorSpaceLowValue  = 0;
	ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = 0; 
	if (FAILED(lpDD7->CreateSurface(&ddsd, &lpddsTileset, NULL)))
	{
		MessageBox(NULL, "CreateSurface Failed", "Error", MB_OK);
		return (FALSE);
	}
	return (TRUE);
}

CSprite::CreateBitmapSurface(char *fname, int xs, int ys)
{
	HBITMAP hBitmap;
	CreateSurface(xs, ys);
	hBitmap = (HBITMAP)LoadImage(NULL, fname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	DrawHBitmap(hBitmap, 0, 0, xs, ys);
	DeleteObject(hBitmap);
	return (TRUE);
}

CSprite::Draw(int X, int Y)
{
	return (1);
}
  
The Declaration is:
  
#ifndef CSprite_
#define CSprite_
#include <ddraw.h>

class CSprite
{
public:
	CSprite();
	~CSprite();

	// Accessor Methods

	LPDIRECTDRAWSURFACE7 GetSurface() { return lpddsTileset; }
	LPDIRECTDRAW7 GetlpDD7() { return lpDD7; }

	// Public Methods

	int CreateBitmapSurface(char *fname, int xs, int ys);
	void SetlpDD7 (LPDIRECTDRAW7 lpdd7) { lpDD7 = lpdd7; }
	int Draw(int X, int Y);
private:
	// Members

	LPDIRECTDRAWSURFACE7 lpddsTileset;
	LPDIRECTDRAW7 lpDD7;

	// Private Methods

	int CreateSurface(int xs, int ys);
	int DrawHBitmap(HBITMAP hBitmap, int x, int y, int width, int height);
};
#endif
  
Now what happens is that when everything is setup, a bitmap should be loaded and put in the lpddsTileset member. I have directX set up with another class, and now i want to perform a blt().. this is what i use:
  
// Initialize DirectDraw 7

	CDDW *pDirectDraw = new CDDW(hWnd);
	pDirectDraw->CDDW_SetCooperativeLevel();
	pDirectDraw->CDDW_SetVideoMode(640,480,16);
	pDirectDraw->CDDW_CreateNormalSurfaces();
	pDirectDraw->CDDW_AttachClipper(0,0,640,480);

	// Intialize Test Sprite

	CSprite *pCool = new CSprite();
	pCool->SetlpDD7(pDirectDraw->CDDW_GetlpDD7());
	if (!(pCool->CreateBitmapSurface("Cool.bmp",63,63)))
	{
		MessageBox(NULL, "uh oh!", "DAMN", MB_OK);
	}

	RECT dest, src;
	SetRect(&src, 0, 0, 63, 63);   // the coordinates of the tile

	SetRect(&dest, 0, 0, 63, 63);  // where you want it to end up on the back buffer


	
	while (TRUE)
	{
		// Process All Pending Window Messages

      if (PeekMessage( &Msg, NULL, 0, 0, PM_NOREMOVE ))
	  {
		  TranslateMessage(&Msg);
		  DispatchMessage(&Msg);
	  }
      
		if (KEYSTATE(VK_ESCAPE) == TRUE)
		{
			break;
		}
		
		pDirectDraw->CDDW_GetlpDDSBack()->Blt(&dest,pCool->GetSurface(),&src,DDBLT_WAIT,NULL);
		pDirectDraw->CDDW_GetlpDDSPrimary()->Flip(NULL,DDFLIP_WAIT);
	}
	
	delete pDirectDraw;
	delete pCool;
	return (Msg.wParam);
}
  
I''ve done millions of error-checking things but nothing seems to work..The cooperative level is set and everything compiles fine (no warnings) but i just get a black screen...Is it my CSprite bitmap loading that is wrong or my DirectDraw Wrapper?
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content
None knows how to fix this? or should i have posted this in the DirectX forum? sorry for replying to my own post, feel like a jerk, but its a gigantic bug that i can''t seem to fix..
Check out my music at: http://zed.cbc.ca/go.ZeD?user_id=41947&user=Laroche&page=content

This topic is closed to new replies.

Advertisement