Jump to content
  • Advertisement
Sign in to follow this  

Problems setting cooperation levels with DirectInput.

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

Hello folks! I'm still working my way through "Programming Role Playing Games with DirectX 2nd Ed." by Jim Adams, and have once again fallen into a few problems. Im going through his examples on DirectInput, but when I compile any of his code, the programmes crash upon running. I've stepped through the programme, and the following call seems to fail with all devices: pDIDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))); I've not much of a clue why really, as his book follows the same method. If anyone could help me out it would be greatly appreciated. If the error is caused by something further up the code, or full source code would help, I'd be more than happy to repost with it. Thanks for everything!

Share this post


Link to post
Share on other sites
Advertisement
Its kindof hard to tell with one line of code... can you check and make sure pDIDevice and hWnd are actuially assigned?

Also, I notcied in that one line of code you have one begin brace '(' and 3 end brances ')))', is this not the entire line of code? If not, you need to paste the entire thing.

Share this post


Link to post
Share on other sites
Thanks for the reply Dreq. As far as I can tell everything is assigned correctly, and the three brackets was due to my sloppy copy and paste job.

The full source code in his example is as follows:

/**************************************************
WinMain.cpp
Chapter 3 Keyboard Device Demo

Programming Role-Playing Games with DirectX, 2nd Edition
by Jim Adams (Jan 2004)

Required libraries:
DINPUT8.LIB and DXGUID.LIB
**************************************************/

#include <windows.h>
#include <stdio.h>

#include <dinput.h>
#include "resource.h"

// Application variables ////////////////////////
HWND g_hWnd; // Window handle
char g_szClass[] = "KeyboardDemo"; // Class name

IDirectInput8 *g_pDI; // DirectInput component
IDirectInputDevice8 *g_pDIDevice; // Keyboard device

// Application prototypes ///////////////////////
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

IDirectInputDevice8 *InitKeyboard(HWND hWnd, IDirectInput8 *pDI);
BOOL ReadDevice(IDirectInputDevice8 *pDIDevice, void *DataBuffer, long BufferSize);

// Application //////////////////////////////////
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int nCmdShow)
{
WNDCLASS wc;
MSG Msg;
char KeyStateBuffer[256];

// Register window class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hInstance = hInst;
wc.hIcon = LoadIcon(hInst, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClass;
RegisterClass(&wc);

// Create the dialog box window and show it
g_hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_KEYBOARD), 0, NULL);
UpdateWindow(g_hWnd);
ShowWindow(g_hWnd, nCmdShow);

// Initialize DirectInput and get keyboard device
DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_pDI, NULL);
g_pDIDevice = InitKeyboard(g_hWnd, g_pDI);

// Loop until ESCAPE pressed
ZeroMemory(&Msg, sizeof(MSG));
while(Msg.message != WM_QUIT) {

// Handle Windows messages (if any)
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

// Read in keyboard and break if ESCAPE pressed
ReadDevice(g_pDIDevice, (void*)KeyStateBuffer, 256);
if(KeyStateBuffer[DIK_ESCAPE] & 0x80)
break;
}

// Release DirectInput objects
g_pDIDevice->Unacquire();
g_pDIDevice->Release();
g_pDI->Release();

// Clean up
UnregisterClass(g_szClass, hInst);

return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
case WM_DESTROY:
PostQuitMessage(0);
break;

default: return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

return 0;
}

IDirectInputDevice8 *InitKeyboard(HWND hWnd, IDirectInput8 *pDI)
{
IDirectInputDevice8 *pDIDevice;

// Create the device object
if(FAILED(pDI->CreateDevice(GUID_SysKeyboard, &pDIDevice, NULL)))
return NULL;

// Set the data format
if(FAILED(pDIDevice->SetDataFormat(&c_dfDIKeyboard))) {
pDIDevice->Release();
return NULL;
}

// Set the cooperative mode
if(FAILED(pDIDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE))) {
pDIDevice->Release();
return NULL;
}

// Acquire the device for use
if(FAILED(pDIDevice->Acquire())) {
pDIDevice->Release();
return NULL;
}

// Everything was a success, return the pointer
return pDIDevice;
}

BOOL ReadDevice(IDirectInputDevice8 *pDIDevice, void *DataBuffer, long BufferSize)
{
HRESULT hr;

while(1) {
// Poll device
g_pDIDevice->Poll();

// Read in state
if(SUCCEEDED(hr = g_pDIDevice->GetDeviceState(BufferSize,(LPVOID)DataBuffer)))
break;

// Return on an unknown error
if(hr != DIERR_INPUTLOST && hr != DIERR_NOTACQUIRED)
return FALSE;

// Reacquire and try again
if(FAILED(g_pDIDevice->Acquire()))
return FALSE;
}

// Return a success
return TRUE;
}


The programme compiles fine, but fails when running on the line of code mentioned.

Share this post


Link to post
Share on other sites
In the call to SetCoorperativeLevel use the DISCL_BACKGROUND flag instead of DISCL_FOREGROUND. Hope that helps.

-AJ

Share this post


Link to post
Share on other sites
Thankyou indeed u235, that did fix the problem!

However, a new one has presented itself. The window which is supposed to be displayed (as shown in his ready compiled example on the CD) doesn't show. The programme still runs however - I've checked using task manager.

Any idea whats going wrong here?

Share this post


Link to post
Share on other sites
Bah! I should pay more attention to the code. It seems he hasn't inserted a section to deal with a messagebox, which is probably a good reason why one isn't displaying :P

I wish the source code once compiled and the precompiled code did the same things. Perhaps he's just trying to catch out the lazy ones such as myself, hehe.

Thanks for everything folk. You've been a fantastic help :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!