Sign in to follow this  
GavRobbs

DirectInput8

Recommended Posts

I'm having problems with direct input.My code is as follows: WARNING: HEAVY CODE #include <windows.h> #include <d3d9.h> #include <dinput.h> #include <d3dx9.h> #include <memory.h> IDirect3D9 * g_pD3D = NULL; IDirect3DDevice9 * g_pD3DDevice = NULL; IDirect3DTexture9 * g_pD3DTexture = NULL; IDirectInputDevice8 * g_pDIDevice = NULL; IDirectInput8 * g_pDI = NULL; HINSTANCE instance_info = NULL; char g_vKeyboardState[256]; bool KeyPressed(int k) { return g_vKeyboardState[k] & 0x80; }; HWND hwnd; struct CUSTOMVERTEX { float x, y, z; // Position in 3d space float u,v; //UVdata }; float angle = 0.0f; CUSTOMVERTEX g_triangle[] = { {-2.0f, -2.0f, 5.0f,0.0f,1.0f}, // Bottom left vertex { 0.0f, 2.0f, 5.0f,0.0f,0.0f}, // Top vertex { 2.0f, -2.0f, 5.0f,1.0f,1.0f} // Bottom right vertex }; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow) { WNDCLASSEX wndclass; MSG message; memset(&wndclass,0,sizeof(WNDCLASSEX)); wndclass.cbSize = sizeof(WNDCLASSEX); wndclass.style = CS_VREDRAW | CS_HREDRAW; wndclass.lpfnWndProc = WndProc; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wndclass.lpszClassName = "My Direct X Window Class"; wndclass.lpszMenuName = NULL; wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); wndclass.hInstance = hInstance; instance_info = hInstance; RegisterClassEx(&wndclass); hwnd = CreateWindow("My Direct X Window Class","My DirectX window",WS_OVERLAPPEDWINDOW,0,0,640,480,NULL,NULL,hInstance,NULL); RECT rect; ShowWindow(hwnd,nCmdShow); UpdateWindow(hwnd); while(GetMessage(&message,NULL,0,0) > 0) { TranslateMessage(&message); DispatchMessage(&message); }; return message.wParam; }; LRESULT CALLBACK WndProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam) { switch(msg) { case WM_CREATE: { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; memset(&d3dpp,0,sizeof(D3DPRESENT_PARAMETERS)); d3dpp.BackBufferCount = 1; d3dpp.BackBufferHeight = 480; d3dpp.BackBufferWidth = 640; d3dpp.BackBufferFormat = D3DFMT_R5G6B5; d3dpp.EnableAutoDepthStencil = FALSE; d3dpp.AutoDepthStencilFormat = D3DFMT_R5G6B5; d3dpp.MultiSampleQuality = 0; d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; d3dpp.hDeviceWindow = hWnd; d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.Windowed = TRUE; g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&g_pD3DDevice); D3DVERTEXELEMENT9 vdesc[] = { {0,0,D3DDECLTYPE_FLOAT3,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_POSITION,0}, {0,12,D3DDECLTYPE_FLOAT2,D3DDECLMETHOD_DEFAULT,D3DDECLUSAGE_TEXCOORD,0}, D3DDECL_END() }; LPDIRECT3DVERTEXDECLARATION9 vdecl; g_pD3DDevice->CreateVertexDeclaration(vdesc,&vdecl); g_pD3DDevice->SetVertexDeclaration(vdecl); g_pD3DDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID); g_pD3DDevice->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD); g_pD3DDevice->SetRenderState(D3DRS_LIGHTING,FALSE); D3DXMATRIX identity; D3DXMatrixIdentity(&identity); g_pD3DDevice->SetTransform(D3DTS_VIEW,&identity); g_pD3DDevice->SetTransform(D3DTS_WORLD,&identity); float aspect_ratio = (float)800/(float)600; D3DXMATRIX projection; D3DXMatrixPerspectiveFovLH(&projection,D3DXToRadian(60.0f),aspect_ratio,1.0f,1000.0f); g_pD3DDevice->SetTransform(D3DTS_PROJECTION,&projection); vdecl->Release(); D3DXCreateTextureFromFile(g_pD3DDevice,"texture.bmp",&g_pD3DTexture); MessageBox(NULL,"Direct3D operational","Info",MB_OK); DirectInput8Create(instance_info,DIRECTINPUT_VERSION,IID_IDirectInput8,(LPVOID *)g_pDI,NULL); //Stops working about here g_pDI->CreateDevice(GUID_SysKeyboard,&g_pDIDevice,NULL); g_pDIDevice->SetDataFormat(&c_dfDIKeyboard); g_pDIDevice->SetCooperativeLevel(hWnd,DISCL_FOREGROUND|DISCL_NONEXCLUSIVE); g_pDIDevice->Acquire(); break; }; case WM_CLOSE: { g_pDIDevice->Release(); g_pDI->Release(); g_pD3DTexture->Release(); g_pD3DDevice->Release(); g_pD3D->Release(); DestroyWindow(hWnd); break; }; case WM_PAINT: { if(KeyPressed(VK_UP)) { MessageBox(NULL,"Up key pressed","Info",MB_OK); };//Draw Scene g_pDIDevice->GetDeviceState(256,(LPVOID)g_vKeyboardState); g_pD3DDevice->SetTexture(0,g_pD3DTexture); g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0); g_pD3DDevice->BeginScene(); g_pD3DDevice->DrawPrimitiveUP( D3DPT_TRIANGLESTRIP, 1, g_triangle,sizeof(CUSTOMVERTEX)); g_pD3DDevice->EndScene(); g_pD3DDevice->Present(NULL,NULL,NULL,NULL); break; }; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd,msg,wParam,lParam); }; return 0; }; The code compiles,but when I run it I seem to get a memory error(you know, the instruction XXXXXXX referenced memory at YYYYYY and the memory can't be read).Due to some huge MessageBoxing on my part,I isolated the error to the region where the directInput interface is being created.If I remove the directInput parts,it runs.Can someone please help me with my problem? Any modification suggestions, to get my code to run? Thanks in advance,and sorry for the messy code.

Share this post


Link to post
Share on other sites
1) Install the debug runtimes. Thatll tell you what goes wrong.
2) Check your return values. If something goes wrong, you just ignore it and carry on.
3) NEVER use DirectInput for keyboard or mouse input, it's a terrible, terrible idea.

Share this post


Link to post
Share on other sites

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