IDirectDraw7

Started by
1 comment, last by Pipo DeClown 21 years, 9 months ago
I get no error in my compiler but I do get the g_hWnd = NULL error. Can some one help me?
  #include <windows.h>
#include <ddraw.h>
#define MsgB(x) MessageBox(NULL,x,"Error!",MB_OK);
#define MsgB2(x) MessageBox(NULL,x,"Yeah!",MB_OK);
#define SR(x) if(x){x->Release(); x=NULL;}

const char* ClassName = "TheClassName";
const char* Title = "TheTitle";

bool bInit;
HWND g_hWnd;
HINSTANCE g_hInst;

LPDIRECTDRAW7 lpDD = NULL;
LPDIRECTDRAWSURFACE7 lpS= NULL;
LPDIRECTDRAWSURFACE7 lpS2= NULL;
LPDIRECTDRAWCLIPPER lpClip = NULL;
int CleanUp(){
	SR(lpS2)
	SR(lpS)
	SR(lpClip)
	SR(lpDD)
	return (true);
}

static bool Init(){
	HRESULT ddrval;

	ddrval = DirectDrawCreateEx(NULL,(void**)&lpDD,IID_IDirectDraw7,NULL);
	if(ddrval != DD_OK){
		MsgB("DDO not created")
			return 0;
	}
	ddrval = lpDD->SetCooperativeLevel(g_hWnd,
	//	DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT

		DDSCL_NORMAL);
	if(ddrval != DD_OK){
		MsgB("SCL failed")
			CleanUp();
		return 0;
	}

	ddrval = lpDD->SetDisplayMode(1024,768,32,0,0);
	if(ddrval != DD_OK){
		MsgB("SDM failed")
			CleanUp();
		return 0;
	}
	ddrval = lpDD->CreateClipper(NULL,&lpClip,NULL);
	if(ddrval != DD_OK){
		MsgB("CClip failed")
			CleanUp();
		return 0;
	}
	
	lpClip->SetHWnd(0,g_hWnd);

	DDSURFACEDESC2 ddsd;
	DDSCAPS2 ddscaps;

	ZeroMemory(&ddsd,sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags= DDSD_CAPS | DDSD_BACKBUFFERCOUNT ;
	ddsd.dwBackBufferCount = 1;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP;
	ddrval  = lpDD->CreateSurface(&ddsd,&lpS,0);
	if(ddrval != DD_OK){
		MsgB("Create Surf fail")
			CleanUp();
		return 0;
	}

	ZeroMemory(&ddscaps,sizeof(ddscaps));
	ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
	ddrval = lpS->GetAttachedSurface(&ddscaps,&lpS2);
	if(ddrval != DD_OK){
		MsgB("GetAttach fail")
			CleanUp();
		return 0;
	}

	bInit = true;

	return (true);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	if(!bInit){
		CleanUp();
		return 0;
	}
    switch(msg){
	case WM_CREATE: {return 0;}break;
	case WM_PAINT: {return 0;}break;
	case WM_DESTROY:{
		CleanUp();
		PostQuitMessage(0);
		return 0;
					}break;
	default: return DefWindowProc(hWnd,msg,wParam,lParam);
	}
	return 0L;
}

int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE PrevInst, LPSTR lpCmdLine, int nCmdShow){

     static TCHAR szAppName[] = TEXT ("HelloWin") ;

     MSG          msg ;
     WNDCLASS     wndclass ;

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = (WNDPROC)WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof(DWORD) ;
     wndclass.hInstance     = hInst;
     wndclass.hIcon         = NULL;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;

     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
	 HWND hWnd;
     hWnd =( CreateWindow (szAppName,                  // window class name

                          TEXT ("The Hello Program"), // window caption

                          WS_POPUP | WS_VISIBLE,        // window style

                          CW_USEDEFAULT,              // initial x position

                          CW_USEDEFAULT,              // initial y position

                          1024,              // initial x size

                          768,              // initial y size

                          NULL,                       // parent window handle

                          NULL,                       // window menu handle

                          hInst,                  // program instance handle

                          NULL)  );                    // creation parameters

	 g_hWnd = hWnd;
    if(!g_hWnd){
		MsgB("g_hWnd == NULL")
			return 0;
	}

	ShowWindow (g_hWnd, nCmdShow) ;
	UpdateWindow(g_hWnd);


	g_hInst = hInst;

	if(Init()){
			MsgB2("Yeah! DD init!")
				}
	else {
		MsgB("Damn! DD not init!")
			CleanUp();
		return 0;
	}

	while( msg.message != WM_QUIT )
	{
		if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{

		}
	}
	return msg.wParam;
}  
I really need your help! I''m compiling in wXP and MSVC++ 6.0!
Advertisement
WNDCLASSes can seem to be a bit crazy even when they appear to be registered properly - this is a long shot, but try adding:


  wndcls.hIcon = LoadIcon( NULL, IDI_APPLICATION );  


and adding the CS_CLASSDC flag to the style. I always used to find a missing hIcon or hCursor stopped my windows from being created.

Tom L
refrain_from_stupidity( &me );
Do not pass window caption as TEXT("The Hello Program") do it as "The Hello Program".

This topic is closed to new replies.

Advertisement