DirectX Error

I recently bought a DirectX book. I started copying the code and am now getting an error. Here is my code:
#include <Windows.h>
#include <tchar.h>

HINSTANCE hInst;
HWND wndHandle;

int width = 640;
int height = 480;

bool InitWindow (HINSTANCE hInstance, int width, int height);
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
									LPTSTR lpCmdLine, int nCMDShow)
{

	if (!InitWindow(hInstance, width, height))
	{

		return false;

	}

	MSG msg = {0};
	while (WM_QUIT != msg.message)
	{

		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
		{

			TranslateMessage(&msg);
			DispatchMessage(&msg);

		}

	}

	return (int) msg.wParam;

	bool InitWindow (HINSTANCE hInstance, int width, int height);
	{

	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style = CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc = (WNDPROC)WndProc;
	wcex.cbClsExtra = 0;
	wcex.cbWndExtra = 0;
	wcex.hInstance = hInstance;
	wcex.hIcon = 0;
	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
	wcex.lpszMenuName = NULL;
	wcex.lpszClassName = TEXT("DirectXExample");
	wcex.hIconSm = 0;
	RegisterClassEx(&wcex);

	RECT rect = {0, 0, width, height};
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

	wndHandle = CreateWindow(TEXT("DirectXExample"),
		TEXT("DirectXExample"),
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		rect.right - rect.left,
		rect.bottom - rect.top,
		NULL,
		NULL,
		hInstance,
		NULL);

	if (!wndHandle)
	{

		return false;

	}

	ShowWindow(wndHandle, SW_SHOW);
	UpdateWindow(wndHandle);

	return true;

}


}

I get an error in the
CreateWindow
saying "Expected an expression."

I have tried placing all of the InitWindow code under the existing
bool InitWindow (HINSTANCE hInstance, int width, int height);
alongside making a new line for it, as you see in my code. Please help.

Comments

  • You missed the dwStyle parameter in the CreateWindow call. It should be the third parameter, just after the window name. A quick google shows the book uses WS_OVERLAPPEDWINDOW
  • Ah. Thank you. I probably just skipped over it by accident. And I have just one more question. Should I being making a new
    bool InitWindow (HINSTANCE hInstance, int width, int height);
    
    to put the InitWindow code under, or should I put the InitWindow code under the existing
    bool InitWindow (HINSTANCE hInstance, int width, int height);
    
    ?
  • This code:
    bool InitWindow (HINSTANCE hInstance, int width, int height);
    

    Is a declaration of a function is it telling the compiler what parameters the function takes and what it returns.

    You need to put your InitWindow code in a function definition. This looks like the declaration above, but without the semicolon and with the actual function code inside braces. It also needs to be outside any other function.
    bool InitWindow (HINSTANCE hInstance, int width, int height)
    {
       // Put your code here.
    }
    
  • Ok. Thanks. Now I added the
    LRESULT CALLBACK WndProc (WHND hWnd, UINT message, WPARAM wParam, LPARAM lParam
    
    function. The first bracket after this gets an error saying "expected a ';'" Here is all of my code now, for reference:
    #include <Windows.h>
    #include <tchar.h>
    
    HINSTANCE hInst;
    HWND wndHandle;
    
    int width = 640;
    int height = 480;
    
    bool InitWindow (HINSTANCE hInstance, int width, int height);
    LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int APIENTRY_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    									LPTSTR lpCmdLine, int nCMDShow)
    {
    
    	if (!InitWindow(hInstance, width, height))
    	{
    
    		return false;
    
    	}
    
    	MSG msg = {0};
    	while (WM_QUIT != msg.message)
    	{
    
    		while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
    		{
    
    			TranslateMessage(&msg);
    			DispatchMessage(&msg);
    
    		}
    
    	}
    
    	return (int) msg.wParam;
    
    	bool InitWindow (HINSTANCE hInstance, int width, int height);
    	{
    
    	WNDCLASSEX wcex;
    
    	wcex.cbSize = sizeof(WNDCLASSEX);
    	wcex.style = CS_HREDRAW | CS_VREDRAW;
    	wcex.lpfnWndProc = (WNDPROC)WndProc;
    	wcex.cbClsExtra = 0;
    	wcex.cbWndExtra = 0;
    	wcex.hInstance = hInstance;
    	wcex.hIcon = 0;
    	wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    	wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    	wcex.lpszMenuName = NULL;
    	wcex.lpszClassName = TEXT("DirectXExample");
    	wcex.hIconSm = 0;
    	RegisterClassEx(&wcex);
    
    	RECT rect = {0, 0, width, height};
    	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
    
    	wndHandle = CreateWindow(TEXT("DirectXExample"),
    		TEXT("DirectXExample"),
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT,
    		CW_USEDEFAULT,
    		rect.right - rect.left,
    		rect.bottom - rect.top,
    		NULL,
    		NULL,
    		hInstance,
    		NULL);
    
    	if (!wndHandle)
    	{
    
    		return false;
    
    	}
    
    	ShowWindow(wndHandle, SW_SHOW);
    	UpdateWindow(wndHandle);
    
    	return true;
    
    }
    
    	LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    	{
    
    		switch (message)
    		{
    
    		case WM_KEYDOWN:
    			switch (wParam)
    			{
    
    			case VK_ESCAPE:
    				PostQuitMessage(0);
    			break;
    
    			}
    
    			break;
    			
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    
    		}
    }
    
    1. You still have InitWindow defined inside _tWinMain, add a } after return (int) msg.wParam; to finish _tWinMain
    2. You still have a semicolon at the end of your InitWindow definition
    3. You need to return a value from WndProc.
      return (int) msg.wParam;
    [COLOR="Red"]} <-- Add this brace[/COLOR]
    
    bool InitWindow (HINSTANCE hInstance, int width, int height)[COLOR="Red"]; <-- Remove this semicolon[/COLOR]
      {
    
  • I fixed those errors. Now when I try to build the solution, I get these errors:

    [ICODE]Error 1 error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup C:\Users\Michael\documents\visual studio 2012\Projects\DirectX\MSVCRTD.lib(crtexew.obj) DirectX
    Error 2 error LNK1120: 1 unresolved externals C:\Users\Michael\documents\visual studio 2012\Projects\DirectX\Debug\DirectX.exe 1 1 DirectX
    [/ICODE]
  • That is saying it can't find your winmain function. The reason for this is that you don't have a space between APIENTRY and _tWinMain in this line
    int APIENTRY_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance
                ^ here
    
  • This fixed it. Thanks for the help!
  • I have another question now. The book I am learning from has code for a swap chain, but I am unsure as to where I should be putting this. No matter where I put
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    

    I get an error saying

    [ICODE]DXGI_SWAP_CHAIN_DESC is undefined[/ICODE]

    Where should I be putting the swap chain?
  • That error indicates the compiler doesn't know what DXGI_SWAP_CHAIN_DESC is. If you look at the msdn page for DXGI_SWAP_CHAIN_DESC, it says that it is declared in DXGH.h, so you will need to #include that header file.
  • DXGI.h fixed it. Thanks! And I'm guessing you just made a typo?
  • Oops. yes typo - sorry about that.
  • I am now trying to change the width and height. Here is that:
    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    	swapChainDesc.BufferDesc.Width = 640;
    

    And here are the errors:
    Error	1	error C2143: syntax error : missing ';' before '.'	c:\users\michael\documents\visual studio 2012\projects\directx\winmain.cpp	122	1	DirectX
    Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\michael\documents\visual studio 2012\projects\directx\winmain.cpp	122	1	DirectX
    Error	3	error C2371: 'swapChainDesc' : redefinition; different basic types	c:\users\michael\documents\visual studio 2012\projects\directx\winmain.cpp	122	1	DirectX
    	4	IntelliSense: this declaration has no storage class or type specifier	c:\Users\Michael\Documents\Visual Studio 2012\Projects\DirectX\winmain.cpp	122	2	DirectX
    	5	IntelliSense: expected a ';'	c:\Users\Michael\Documents\Visual Studio 2012\Projects\DirectX\winmain.cpp	122	15	DirectX
    

    What is my problem this time?
  • I can't tell exactly without more of the program, but errors like that usually happen when you put code where you are meant to be putting declarations. Is this inside a function or right at the top of your program?
    If you are declaring a global variable, you will have to move the initializing code to inside your winmain.
  • I had it below all of my code. I moved it inside my winmain function but after everything else. Thanks for your help.
Sign In or Register to comment.