Archived

This topic is now archived and is closed to further replies.

Help with this Direct X code

This topic is 5534 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Guys, Just ploughing through the beginnings of Direct X. I have a small problem. Can someone take a look at this code for me and tell me why I''m getting the "Can''t Create Primary Surface" error message. I''ve checked my code and I can''t see the fault.
  
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include <dinput.h>

#define WIN32_LEAN_AND_MEAN

int MakeWindow();

HWND g_hwnd;
HINSTANCE g_hInstance;




int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	
	MSG msg;
	g_hInstance = hInstance;
	LPDIRECTDRAW7 lpdd;
	LPDIRECTDRAWSURFACE7 ddsPrimary;
	LPDIRECTDRAWSURFACE7 ddsBack;
	DDSURFACEDESC2 ddsd;
	HRESULT r;
	
	MakeWindow();

	r = DirectDrawCreateEx(0, (void **)&lpdd, IID_IDirectDraw7,0);
	if (FAILED(r))
	{
		MessageBox(g_hwnd, "Direct Draw Create Failed", "System Error", MB_OK | MB_ICONEXCLAMATION);
	}

	r = lpdd->SetCooperativeLevel(g_hwnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT );
	if (FAILED(r))
	{
		MessageBox(g_hwnd, "Set Co-Op Level Failed", "System Error", MB_OK | MB_ICONEXCLAMATION);
	}

	r = lpdd->SetDisplayMode(640,480,8,0,0);
	if (FAILED(r))
	{
		MessageBox(g_hwnd, "Set Display Failed", "System Error", MB_OK | MB_ICONEXCLAMATION);
	}

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

	r = lpdd->CreateSurface(&ddsd, &ddsPrimary,0);
	if (FAILED(r))
	{
		MessageBox(g_hwnd, "Unable to Create Primary Surface", "System Error", MB_OK | MB_ICONEXCLAMATION);
	}

	ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;

	r = ddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &ddsBack);
	if (FAILED(r))
	{
		MessageBox(g_hwnd, "Unable to attach surface", "System Error", MB_OK | MB_ICONEXCLAMATION);
	}



	
	

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



LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	PAINTSTRUCT ps;
	HDC hdc;

	switch(msg)
	{
	case WM_CLOSE:
		{
			int answer;
			answer = MessageBox(hwnd, "Do you wish to exit", "System Message", MB_YESNO | MB_ICONQUESTION);
			if (answer == IDYES)
			{
				PostQuitMessage(0);
			}
			else
			{
				return 0;
			}
		}
		break;
	case WM_PAINT:
		{
			hdc = BeginPaint(hwnd, &ps);
			EndPaint(hwnd, &ps);
			return 0;
			break;
		}
	}
	return (DefWindowProc(hwnd, msg, wParam, lParam));
}


int MakeWindow()
{
	WNDCLASSEX winclass;
	
	winclass.cbSize = sizeof(WNDCLASSEX);
	winclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
	winclass.lpfnWndProc = WndProc;
	winclass.cbClsExtra = 0;
	winclass.cbWndExtra = 0;
	winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	winclass.hInstance = g_hInstance;
	winclass.lpszClassName = "Winclass1";
	winclass.lpszMenuName = NULL;
	winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

	RegisterClassEx(&winclass);

	g_hwnd = CreateWindowEx(NULL,
						  "Winclass1",
						  "First Direct X",
						  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
						  0,0,
						  800,600,
						  NULL,
						  NULL,
						  g_hInstance,
						  NULL);

	if (g_hwnd == NULL)
	{
		MessageBox(g_hwnd, "Unable to create window", "System Error", MB_OK);
		return 0;
	}

	return 0;
}
  

Share this post


Link to post
Share on other sites
Try adding

memset(&ddsd,0,sizeof(ddsd));

right before your

ddsd.dwSize = sizeof(ddsd);

you have to clear out structs before you use them, I had some problems with this when I started.

Hope this helps

=*=
If things seem bad, think that they can get a whole load worse, and they don't seem so bad anymore

=*=

[edited by - hammerstein_02 on October 22, 2002 10:58:28 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Same thing as

ZeroMemory(&dsdd);

If you read the tutorials. It zeros out the structure so no erroneous flags and values are processed.

Share this post


Link to post
Share on other sites
look at this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_memset.asp

or this http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_CRT_memset.asp

U have a lot to find in Msdn online :http://msdn.microsoft.com/

Hope it helps

Share this post


Link to post
Share on other sites