Sign in to follow this  

dinput8 wont work

This topic is 4014 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

im making a program that combines directx of d3d dinput and dsound, but it won't work. i use msvs2005 pro, but it still can't find the bug, because no symbols are loaded for any call or stack frame, and i got no idea why that happens either. here is the code, it is in 2 files. does anyone know how to fix this???
//directx.cpp
#include "Lib.h" 

const char g_szClassName [ ] = "DirectX 8 Multimedia"; 

//Global variables
//DirectX 8 objects
IDirect3D8 *g_pD3D8 = NULL; 
IDirect3DDevice8 *g_pD3DDevice = NULL; 
ID3DXFont *g_pFont = NULL; 
IDirectInput8 *g_pDI = NULL; 
IDirectInputDevice8 *g_pDIMouse = NULL; 
IDirectInputDevice8 *g_pDIKeyboard = NULL; 
IDirectSound8 *g_pDS = NULL; 
IDirectSoundBuffer8 *g_pDSBuffer = NULL; 
LOGFONT lfFont; 
RECT fontArea; 
char keys[256]; 
DIMOUSESTATE mouse; 
//---------------------------------------------------------- 

BOOL InitD3D(HWND hwnd, HINSTANCE hInst)
 {
	 D3DDISPLAYMODE d3ddm; 
	 D3DPRESENT_PARAMETERS d3dpp; 

	 if ((g_pD3D8 = Direct3DCreate8(D3D_SDK_VERSION)) == NULL)
		 return FALSE; 

	 if (FAILED(g_pD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
		 return FALSE; 

	 ZeroMemory(&d3dpp, sizeof(d3dpp)); 
	 d3dpp.Windowed = TRUE; 
	 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
	 d3dpp.BackBufferFormat = d3ddm.Format; 

	 if (FAILED(g_pD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, 
		 D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pD3DDevice)))
	 {
		 MessageBoxA(hwnd, "Could not create the Direct3D device.", "Error", MB_ICONSTOP); 
		 return FALSE; 
	 }

	 return TRUE; 
 }

BOOL CreateFonts()
 {
	 assert(g_pD3DDevice != NULL); 

	 //Set up the font formats
	 strcpy((char *) lfFont.lfFaceName, "Tahoma"); 
	 lfFont.lfWidth = 25; 
	 lfFont.lfHeight = 60;

	 ID3DXFont *pFont = NULL; 
	 if (FAILED(D3DXCreateFontIndirect(g_pD3DDevice, &lfFont, &g_pFont)))
		 return FALSE; 

	 return TRUE; 
 } 

 BOOL InitDI(HWND hwnd, HINSTANCE hInst)
 {
	 if (FAILED(DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, 
		 (void **) &g_pDI, NULL)))
		 return FALSE;

	 return TRUE; 
 }

 BOOL CreateInputDevices(HWND hwnd, HINSTANCE hInst)
 {
	 assert(g_pDI != NULL); 

			 //Create a mouse
			 if (FAILED(g_pDI->CreateDevice(GUID_SysMouse, &g_pDIMouse, NULL)))
			 {
				 MessageBoxA(hwnd, "Could not create the mouse device.", "Error", MB_ICONSTOP); 
				 return FALSE; 
			 }

			 if (FAILED(g_pDIMouse->SetDataFormat(&c_dfDIMouse)))
				 return FALSE; 

			 if (FAILED(g_pDIMouse->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
				return FALSE;	  

			 //Create a keyboard
			 if (FAILED(g_pDI->CreateDevice(GUID_SysKeyboard, &g_pDIKeyboard, NULL)))
			 {
				 MessageBoxA(hwnd, "Could not create the keyboard device.", "Error", MB_ICONSTOP); 
				 return FALSE; 
			 }

			 if (FAILED(g_pDIKeyboard->SetDataFormat(&c_dfDIKeyboard)))
				 return FALSE; 

			 if (FAILED(g_pDIKeyboard->SetCooperativeLevel(hwnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND)))
				 return FALSE; 

	 //Acquire the devices
	 if (FAILED(g_pDIMouse->Acquire()))
	 {
		 MessageBoxA(hwnd, "Could not acquire the mouse input device.", "Error", MB_ICONSTOP); 
		 return FALSE; 
	 }

	 if (FAILED(g_pDIKeyboard->Acquire()))
	 {
		 MessageBoxA(hwnd, "Could not acquire the keyboard input device.", "Error", MB_ICONSTOP); 
		 return FALSE; 
	 }

	 return TRUE; 
 }

BOOL GetInput(long size, void *buffer, IDirectInputDevice8 *pDIDevice)
{  
	assert(pDIDevice != NULL); 

	while (true)
	{
		pDIDevice->Poll(); 

		if (SUCCEEDED(pDIDevice->GetDeviceState(size, &buffer)))
			break;

		//Reacquire the device
		if (FAILED(pDIDevice->Acquire()))
			return FALSE;  
	}

	return TRUE; 
}
		
 BOOL InitDS(HWND hwnd, HINSTANCE hInst, IDirectSound8 *pDS, IDirectSoundBuffer8 *pDSBuffer)
 {
	 DSBUFFERDESC dsbd; 
	 WAVEFORMATEX wf; 

	 if (FAILED(DirectSoundCreate8(NULL, &g_pDS, NULL)))
		 return FALSE; 

	 if (FAILED(g_pDS->SetCooperativeLevel(hwnd, DSSCL_EXCLUSIVE)))
		return FALSE; 

	 //Set the buffer details
	 ZeroMemory(&dsbd, sizeof(DSBUFFERDESC)); 
	 dsbd.dwSize = sizeof(DSBUFFERDESC);  
	 dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER | DSBCAPS_GLOBALFOCUS | DSBCAPS_CTRLVOLUME; 
	 dsbd.dwBufferBytes = 0; 
	 dsbd.lpwfxFormat = NULL;

	 if (FAILED(g_pDS->CreateSoundBuffer(&dsbd, (LPDIRECTSOUNDBUFFER *) &g_pDSBuffer, NULL)))
		 return FALSE; 

	 //Set the wave format
	 ZeroMemory(&wf, sizeof(WAVEFORMATEX)) ;
	 wf.wFormatTag = WAVE_FORMAT_PCM; 
	 wf.nChannels = 2; 
	 wf.nSamplesPerSec = 22050; 
	 wf.wBitsPerSample = 16; 
	 wf.nBlockAlign = (wf.wBitsPerSample / 8) * wf.nChannels; 
	 wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign; 
	 
	 if (FAILED(g_pDSBuffer->SetFormat(&wf)))
		 return FALSE; 

	 return TRUE; 
 }											  
//--------------------------------------------------------
//Main functions
BOOL InitDX(HWND hwnd, HINSTANCE hInst)
{
	//Init COM
	CoInitialize(NULL);		 

	//Init Direct3D
	if (!InitD3D(hwnd, hInst))
		return FALSE; 

	if (!CreateFonts()) 
		return FALSE; 

	//Init DirectInput
	if (!InitDI(hwnd, hInst)) 
		return FALSE; 

	if (!CreateInputDevices(hwnd, hInst))
		return FALSE; 

	//Init DirectSound
	//if (!InitDS(hwnd, hInst, g_pDS, g_pDSBuffer))
		//return FALSE;  

	//Set up the font areas
	fontArea.top = 50; 
	fontArea.left = 50;  

	//Error checking
	assert(g_pD3D8 != NULL); 
	assert(g_pD3DDevice != NULL); 
	assert(g_pFont != NULL); 
	assert(g_pDI != NULL); 
	assert(g_pDIMouse != NULL); 
	assert(g_pDIKeyboard != NULL);
	//assert(g_pDS != NULL); 
	//assert(g_pDSBuffer != NULL); 

	return TRUE; 
}

BOOL DoFrame()
{	
	if (FAILED(g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 197, 
		218, 255), 1.0f, 0)))
	{
		ERR("Could not clear the backbuffer!!!"); 
		return FALSE; 
	}

	//Read in the input from the mouse and keyboard
	if (!GetInput(sizeof(DIMOUSESTATE), (void *) &mouse, g_pDIMouse))
	{
		ERR("Could not get the mouse input."); 
		return FALSE; 
	} 

	if (!GetInput(256, (void *) &keys, g_pDIKeyboard))
	{
		ERR("Could not get the keyboard input."); 
		return FALSE; 
	}
	
	//Begin drawing
	if (SUCCEEDED(g_pD3DDevice->BeginScene()))
	{
		//Draw the text
		if (FAILED(g_pFont->DrawTextA("DirectX 8 Multimedia", -1, &fontArea, 0, 
			D3DCOLOR_RGBA(0, 134, 215, 255))))
			return FALSE; 

		//Flip the backbuffers
		if (FAILED(g_pD3DDevice->Present(NULL, NULL, NULL, NULL)))
			return FALSE; 

		g_pD3DDevice->EndScene(); 
	}

	//Determine key states
	if (KEY(DIK_ESCAPE))       
	{
		//Exit
		CleanUp(GetModuleHandle(NULL)); 
		exit(0); 
	}

	return TRUE; 
}

void CleanUp(HINSTANCE hInst)
{
	//Unacquire the input devices
	if (g_pDIMouse) g_pDIMouse->Unacquire(); 
	if (g_pDIKeyboard) g_pDIKeyboard->Unacquire(); 

	//Disable COM
	CoUninitialize(); 

	//Clean up the COM objects
	RELEASE(g_pD3D8); 
	RELEASE(g_pD3DDevice); 
	RELEASE(g_pFont); 
	RELEASE(g_pDI); 
	RELEASE(g_pDIMouse); 
	RELEASE(g_pDIKeyboard); 
	RELEASE(g_pDS); 
	RELEASE(g_pDSBuffer); 
}
//------------------------------

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CLOSE:
		DestroyWindow(hwnd); 
		break;

	case WM_DESTROY:
		PostQuitMessage(0); 
		break; 

	default:
		return DefWindowProc(hwnd, msg, wParam, lParam); 
	}

	return 0;	
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEXA wc; 
	HWND hwnd; 
	MSG msg; 
    
	wc.cbSize = sizeof(WNDCLASSEX); 
	wc.style = CS_CLASSDC; 
	wc.lpfnWndProc = WndProc; 
	wc.cbClsExtra = 0; 
	wc.cbWndExtra = 0; 
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 
	wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
	wc.hInstance = hInst; 
	wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 3);  
	wc.lpszClassName = g_szClassName; 
	wc.lpszMenuName = NULL; 

	if (!RegisterClassExA(&wc))
		return 1; 

	hwnd = CreateWindowExA (
		0, 
		g_szClassName, 
		"",  
		WS_BORDER | WS_SYSMENU | WS_CAPTION, 
		200, 
		200, 
		600, 
		400, 
		NULL, NULL, hInst, NULL); 

	if (hwnd == NULL)
		return 1; 

	ShowWindow(hwnd, nCmdShow); 
	UpdateWindow(hwnd); 

	//Initialize DirectX
	if (!InitDX(hwnd, hInst))
	{
		MessageBoxA(hwnd, "Could not initialize DirectX 8", "Error", MB_ICONSTOP); 
		return 1; 
	}

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

		else 
		{
			if (!DoFrame())
			{
				MessageBoxA(hwnd, "Could not perform frame actions.", "Error", MB_ICONSTOP); 
				return 1; 
			} 
		}
	}

	//Clean up
	//CleanUp(hInst); 

	return 0; 
}



//lib.h 
#define _CRT_SECURE_NO_DEPRECATE

#include <windows.h> 
#include <assert.h> 
#include <d3d8.h> 
#include <d3dx8.h> 
#include <dinput.h> 
#include <dsound.h> 
#include <dmusici.h> 

#define RELEASE(x) { if (x) (x)->Release(); }
#define KEY(x) ((keys[x] & 0x80) ? TRUE : FALSE) 
#define ERR(x) { MessageBoxA(NULL, x, "Error", MB_ICONSTOP); }

void CleanUp(HINSTANCE); 

Share this post


Link to post
Share on other sites
Whilst I appreciate you've tried some debugging, you might well need to read through Introduction to Debugging - it might be of use to you.

You've posted a lot of code without any indication of what line(s) cause the error? Which line, which return codes, what application state causes the error?

HELP US TO HELP YOU

Solving the call-stack and symbols thing isn't hard, search these forums - regular Simon O'Connor (aka 'S1CA') has answered this on several occasions and I know for fact it appears as one of the top results [wink]

hth
Jack

Share this post


Link to post
Share on other sites

This topic is 4014 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this