Skip to main content
gamedev.net
🔒 Locked

Review

Started by sakky 3 replies 350+ views
Original Post
sakky
sakky
Hey, I'm starting to get back into C++ again. I'v had a long enought break. I was playing SspiderMan and beat it in 2 day. Any ways, I need some one to check over my code real quike. Its just a graphics class to initailize DirectDraw7 and setup a few offscreen surfaceses. Well, heres what I got so far.
    
// Header 


#ifndef C_GRAPHICS2D_H
#define C_GRAPHICS2D_H


#include <afxwin.h>


#include <ddraw.h>


class CGraphics2D
{
	IDirectDraw7			*lpDD;
	IDirectDrawSurface7		*lpDDSPrimary;
	IDirectDrawSurface7		*lpDDSBack;
	DDSURFACEDESC2			ddsd;
	DDSCAPS2				ddsCaps;

	public:

		CGraphics2D();
		~CGraphics2D();

		BOOL Init(HWND);
		void Blit(IDirectDrawSurface7 *,RECT,RECT);
		IDirectDrawSurface7 *CreateBitmapSurface(LPSTR,COLORREF);
		void Flip(void);
		void Clear(void);
		void Destroy(void);
};


#endif // C_GRAPHICS2D_H


// Source File



#include <afxwin.h>


#include <ddraw.h>


#include "CGraphics2D.h"


CGraphics2D::CGraphics2D()
{
	lpDD = NULL;
	lpDDSPrimary = NULL;
	lpDDSBack = NULL;
}



CGraphics2D::~CGraphics2D()
{
	Destroy();
}



BOOL CGraphics2D::Init(HWND hWnd)
{
	if(FAILED(DirectDrawCreateEx(0,(LPVOID*)&lpDD,IID_IDirectDraw7,0)))
		return(FALSE);

	if(FAILED(lpDD->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN | DDSCL_ALLOWREBOOT)))
		return(FALSE);

	if(FAILED(lpDD->SetDisplayMode(640,480,32,0,0)))
	{
		if(FAILED(lpDD->SetDisplayMode(640,480,16,0,0)))
			return(FALSE);
	}

	::ZeroMemory(&ddsd,sizeof(ddsd));
		ddsd.dwSize = sizeof(ddsd);
		ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
		ddsd.ddsCaps.dwCaps = DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_PRIMARYSURFACE;
		ddsd.dwBackBufferCount = 1;

	if(FAILED(lpDD->CreateSurface(&ddsd,&lpDDSPrimary,0)))
		return(FALSE);

		ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

	if(FAILED(lpDDSPrimary->GetAttachedSurface(&ddsCaps,&lpDDSBack)))
		return(FALSE);

	return(TRUE);
}



void CGraphics2D::Clear()
{
	DDBLTFX		clear;
	long		hr = 0;

	::ZeroMemory(&clear,sizeof(clear));
	clear.dwSize = sizeof(clear);
	clear.dwFillColor = 0;

	while(TRUE)
	{
		hr = lpDDSBack-> Blt(NULL,NULL,NULL,DDBLT_COLORFILL,&clear);
		if(hr == DD_OK) 
			break;

		if(hr == DDERR_SURFACELOST)
			lpDDSPrimary->Restore();
			
		if(hr == DDERR_WASSTILLDRAWING)
			return;
	}
	if(hr != DD_OK) 
		return;
}



void CGraphics2D::Flip()
{
	long		hr = 0;

	while(TRUE)
	{
		hr = lpDDSPrimary->Flip(NULL,DDFLIP_WAIT);
		if(hr == DD_OK) 
			break;

		if(hr == DDERR_SURFACELOST)
			lpDDSPrimary->Restore();
			
		if(hr == DDERR_WASSTILLDRAWING)
			return;
	}
	if(hr != DD_OK) 
		return;
}



void CGraphics2D::Destroy()
{
	if(lpDD != NULL)
	{
		if(lpDDSPrimary != NULL)
		{
			lpDDSPrimary->Release();
			lpDDSPrimary = NULL;
		}

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



void CGraphics2D::Blit(IDirectDrawSurface7 *lpDDS,RECT rcDest,RECT rcSrc)
{
	long hr = 0;

	while(TRUE)
	{
		hr = lpDDSBack-> Blt(&rcDest,lpDDS,&rcSrc,DDBLT_KEYSRC,NULL);
		if(hr == DD_OK) 
			break;

		if(hr == DDERR_SURFACELOST)
			lpDDSPrimary->Restore();
			
		if(hr == DDERR_WASSTILLDRAWING)
			return;
	}
	if(hr != DD_OK) 
		return;
}



IDirectDrawSurface7 *CGraphics2D::CreateBitmapSurface(LPSTR szBitmap,COLORREF SrcKey)
{
	DDCOLORKEY				DDColorKey;
	HBITMAP                 hbm = NULL;
    BITMAP                  bm;
    HDC						hDC = NULL;
	HDC						hImageDC = NULL;
	DDSURFACEDESC2          DDsd;
    IDirectDrawSurface7     *pdds = NULL;

	hbm = (HBITMAP) ::LoadImage(GetModuleHandle(NULL),szBitmap,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION);
    
	if(hbm == NULL)
        hbm = (HBITMAP) ::LoadImage(NULL,szBitmap,IMAGE_BITMAP,0,0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    
	if(hbm == NULL)
        return NULL;
   
	::GetObject(hbm, sizeof(bm), &bm);
    
	::ZeroMemory(&ddsd, sizeof(ddsd));
    DDsd.dwSize = sizeof(ddsd);
    DDsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_CKSRCBLT;
    DDsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    DDsd.dwWidth = bm.bmWidth;
    DDsd.dwHeight = bm.bmHeight;

	DDColorKey.dwColorSpaceLowValue = (DWORD) SrcKey;
	DDColorKey.dwColorSpaceHighValue = (DWORD) SrcKey;
	
	DDsd.ddckCKSrcBlt = DDColorKey;
    
	if(FAILED(lpDD->CreateSurface(&DDsd,&pdds,NULL)))
        return(NULL);
    
	pdds->GetDC(&hDC);
	
	hDC = ::CreateCompatibleDC(hImageDC);
	::SelectObject(hImageDC,hbm);
	::BitBlt(hDC,0,0,bm.bmWidth,bm.bmHeight,hImageDC,0,0,SRCCOPY);

	pdds->ReleaseDC(hDC);
	
	::DeleteDC(hDC);
	::DeleteDC(hImageDC);
	::DeleteObject(hbm);

	return(pdds);
}


  
I haven't included any of the error handeling, like MessageBoxes and log files yet. How would I get a MessageBox to show up while DirectDraw is running ?? I think I might of asked that before, but I'm not sure. Thanx for any advice thats helpfull in advance. [edited by - Sakky on August 1, 2002 2:26:54 PM]
Take back the internet with the most awsome browser around, FireFox
Evil Bill
Evil Bill
Dunno if its any help, but theres an exaple in the DX8 SDK - "FullscreenDialog"

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
Evil Bill
Evil Bill
What that sample does is calls CreateDialog() to create a modless dialog (Whereas a MessageBox() is a modal dialog), and then processes messages for it in the main apps message pump.
If you don''t have it, post here and someone (including myself) should be able to send you the source.

You might need to create a simple modeless dialog box with a text string that can be changed (basically re-write the MessageBox() function), but i don''t know if theres an easier way to do it.

HTH, Steve

Steve
DirectX Programmer
Soon to be the new Bill Gates
Member of the Unban Mindwipe Society (UMWS)
sakky
sakky
Whats this about dialogs? /The sample I seen was a function called InitFail. I guess I could look in the samples then. I think it was in the sampkle with DirectX 7 and the threee red donuts were on the screen rotating. I think I can remember that function was used there. It had a va list or something? The function used it in some werid way and then used a regular MessageBox. You would pass in the HRESULT and then the text for the MessageBox to display.
Take back the internet with the most awsome browser around, FireFox
sakky
sakky
This is what I ment. For all those that wanted to know how to get a MessageBox to display while DirectDraw is going, there it is.


[/source]
#include <stdarg.h>



HRESULT
InitFail (HWND hWnd, HRESULT hRet, LPCTSTR szError, ...)
{
char szBuff[128];
va_list vl;

va_start (vl, szError);
vsprintf (szBuff, szError, vl);
ReleaseAllObjects (hWnd);
MessageBox (hWnd, szBuff, TITLE, MB_OK);
DestroyWindow (hWnd);
va_end (vl);
return hRet;
}
[/source]
Take back the internet with the most awsome browser around, FireFox

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.