help with a simple directx prog

Started by
3 comments, last by OoMMMoO 23 years, 10 months ago
Im new to directx program and Im getting two errors when creating a window and creating direct x her is my code
    
#include<windows.h>
#include<ddraw.h>
#include<string.h>

LRESULT CALLBACK WndProc(HWND hwnd,UINT imessage,WPARAM wparam,LPARAM lparam)
{
	switch(imessage)
	{
	case WM_CLOSE:
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hwnd,imessage,wparam,lparam);
	}
	return 0;
}

const char *Cname="Tutorial",*Wname="Tutorial";

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE,LPSTR,int nCmdShow)
{
	LPDIRECTDRAW lpdd;
	HRESULT ddrval;
	HWND hwnd;
	MSG message;
	WNDCLASS wndclass;

	wndclass.cbClsExtra=0;
	wndclass.cbWndExtra=0;
	wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
	wndclass.hIcon=LoadIcon(hinstance,NULL);
	wndclass.lpfnWndProc=WndProc;
	wndclass.style=CS_HREDRAW/CS_VREDRAW;

	if(!RegisterClass(&wndclass))return false;

	hwnd=CreateWindow(Cname,Wname,WS_OVERLAPPEDWINDOW,0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hinstance,NULL);
	
	ShowWindow(hwnd,nCmdShow);
	while(GetMessage(&message,hwnd,0,0))
	{
		TranslateMessage(&message);
		DispatchMessage(&message);
	}
	
	ddrval=DirectDrawCreate(NULL,&lpdd,NULL);
	if(ddrval!=DD_OK)
		MessageBox(NULL,"Error creating DX!","ERROR",MB_OK);

	return message.wParam;

}
[/source]

Ther errors are 
[source]
ddr.obj : error LNK2001: unresolved external symbol _DirectDrawCreate@12
Debug/dd.exe : fatal error LNK1120: 1 unresolved externals
    
Di I do this right? if not can someone show me the right way?
Advertisement
You must link ddraw.lib to your project in order to compile it.




Prosper / LOADED corporation
You aren''t including the libraries in your project.
I looooove programming! Even at my young age.
Two problems: the window portion of your program doesn''t create a HWND hWnd that is needed to create the DirectDraw object, and you''ve not included ddraw.lib and dxguid.lib in "Project/Settings/Link" (if you''re using the Visual C++ compiler). I''m new at this too (consider the blind leading the blind lol), but many web tutorial''s are available to help you get started and one book is very good, "Sams Teach Yourself DirectX 7 in 24 Hours" by Robert Dunlop. Try the code listed below. All it will do is create a fullscreen window, that''s it

// Link with; kernel32.lib user32.lib gdi32.lib ddraw.lib dxguid.lib
//------------------------------------------------------------------
#define INITGUID
#define WIN32_LEAN_AND_MEAN // Reduces size of win32 header files
#include // Master include file for Windows apps
#include

// Global Variables
HWND hWnd; // Handle to a window
HINSTANCE ddrval; // Handle to an instance.
HRESULT hRes; // Used to describe an error or warning.

// Global interface pointers
LPDIRECTDRAW7 lpDD=NULL; // DirectDraw Object

// Window Class and Caption Names
static char szClass[]="WinClass";
static char szCaption[]="Your App";

// Error Return String
const char *ErrStr=NULL;

// Error Messages
const char Err_DirectDrawCreate[]="DirectDrawCreate FAILED";

// Called when the program exits to release all objects and surfaces.
void Quit(void)
{
if( lpDD != NULL )
{
lpDD->Release();
lpDD = NULL;
}
}

// Windows Message Handler
LRESULT CALLBACK WinProc(HWND hWnd, unsigned uMsg,
WPARAM wParam,LPARAM lParam)
{
switch(uMsg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case VK_ESCAPE:Quit();DestroyWindow(hWnd); break;
}
break;
case WM_PAINT:
PAINTSTRUCT ps;
BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
break;
case WM_DESTROY:
Quit();
PostQuitMessage(0);
break;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
DestroyWindow(hWnd);
break;
}
default: return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
return 0L;
}

// Register the Window Class
bool Init(HINSTANCE ddrval, int CmdShow)
{
WNDCLASS wc;
wc.style = CS_HREDRAW / CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WinProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = sizeof(DWORD);
wc.hInstance = ddrval;
wc.hIcon = NULL;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = szClass;

if(!RegisterClass(&wc)) return FALSE;
hWnd= CreateWindowEx(WS_EX_TOPMOST,szClass,szCaption,
WS_POPUP,0,0,
GetSystemMetri(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,NULL,ddrval,NULL);
if(!hWnd) return FALSE;

// Hide the mouse cursor
ShowCursor(0);

ShowWindow(hWnd, CmdShow);
UpdateWindow(hWnd);

// Setup DirectDraw
hRes=DirectDrawCreateEx(NULL,
(void**)&lpDD, IID_IDirectDraw7, NULL);
if(hRes != DD_OK) { ErrStr=Err_DirectDrawCreate;
return FALSE; }

return TRUE;
}

// Winmain function
int PASCAL WinMain(HINSTANCE ddrval,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int CmdShow)
{
MSG msg; // Windows message structure
BOOL GotMsg; // Holds the value of the peekmessage structure

// Initialize the application
if(!Init(ddrval, CmdShow))
{ return FALSE; } // Exit the application if fail

PeekMessage( &msg, NULL, 0U, 0U, PM_NOREMOVE );

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

return (msg.wParam);
}

Have you downloaded the SDK from microsoft''s website? You should get it. They have something like a "D3D Sample App Framework", with source code, that they use for all their samples. You can use this to start your own project -- I did.

This topic is closed to new replies.

Advertisement