Direct Input

Post those lines of code you feel like sharing or find what you require for your project here; or simply use them as tutorials.
Post Reply
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Direct Input

Post by belfegor »

#include <windows.h>
#include <windowsx.h>

#include <dinput.h>

#pragma comment (lib, "dinput.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

LPDIRECTINPUT8 din; //direct input interface
LPDIRECTINPUTDEVICE8 dinkeyboard; //keyboard
LPDIRECTINPUTDEVICE8 dinmouse; //mouse
DIDEVICEOBJECTDATA data;

#include <irrlicht.h>
#pragma comment(lib, "Irrlicht.lib")

HWND hWnd;

LRESULT CALLBACK WindowsProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void initDirectInput(HINSTANCE hInstance, HWND hWnd);
void detect_keys();
void detect_mousepos();
void cleanDirectInput();

irr::IrrlichtDevice* device;
irr::gui::IGUIStaticText* text;

class MER : public irr::IEventReceiver
{
bool OnEvent(irr::SEvent event)
{
if(event.EventType == irr::EET_KEY_INPUT_EVENT
&& event.KeyInput.Key == irr::KEY_ESCAPE
&& !event.KeyInput.PressedDown)
{
cleanDirectInput();
device->closeDevice();
return true;
}
return false;
}
};


int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WindowsProcedure;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = DLGWINDOWEXTRA;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
wcex.lpszMenuName = 0;
wcex.lpszClassName = L"lpszClassName";
wcex.hIconSm = 0;

RegisterClassEx(&wcex);
hWnd = CreateWindowEx(NULL, NULL, NULL, WS_OVERLAPPEDWINDOW,
NULL, NULL, NULL, NULL, NULL, NULL, hInstance, NULL);

MER receiever;
irr::SIrrlichtCreationParameters param;
param.WindowId = reinterpret_cast<irr::s32>(hWnd);
param.DriverType = irr::video::EDT_DIRECT3D9;
param.WindowSize.Width = 640;
param.WindowSize.Height = 480;
param.Fullscreen = false;
param.EventReceiver = &receiever;

device = irr::createDeviceEx(param);
device->setWindowCaption(L"WIN CAPTION");
irr::video::IVideoDriver* driver = device->getVideoDriver();
irr::scene::ISceneManager* smgr = device->getSceneManager();
irr::gui::IGUIEnvironment* gui = device->getGUIEnvironment();

irr::scene::ISceneNode* cube = smgr->addCubeSceneNode(25);
cube->setMaterialFlag(irr::video::EMF_LIGHTING, false);
cube->setMaterialTexture(0, driver->getTexture("rockwall.bmp"));
cube->setPosition(irr::core::vector3df(0.0f, 0.0f, 0.0f));

irr::scene::ICameraSceneNode* cam = smgr->addCameraSceneNodeFPS();
cam->setPosition(irr::core::vector3df(100.0f, 100.0f, 30.0f));
cam->setTarget(irr::core::vector3df(0.0f, 0.0f, 0.0f));

text = gui->addStaticText(L"", irr::core::rect<irr::s32>(20, 20, 200, 40), true);
text->setOverrideFont(gui->getBuiltInFont());
text->setOverrideColor(irr::video::SColor(255, 255, 255, 255));

ShowWindow(hWnd , SW_SHOW);

initDirectInput(hInstance, hWnd);

UpdateWindow(hWnd);

while(device->run())
{
driver->beginScene(true, true, irr::video::SColor(0));
smgr->drawAll();
gui->drawAll();
detect_keys();
detect_mousepos();
driver->endScene();
}
device->drop();
}

void initDirectInput(HINSTANCE hInstance, HWND hWnd)
{
DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8,
(void**)&din, NULL);

din->CreateDevice(GUID_SysKeyboard, &dinkeyboard, NULL);
din->CreateDevice(GUID_SysMouse, &dinmouse, NULL);

dinkeyboard->SetDataFormat(&c_dfDIKeyboard);
dinmouse->SetDataFormat(&c_dfDIMouse2);

dinkeyboard->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);
dinmouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);

dinmouse->Acquire();
return;
}

void detect_keys()
{
static BYTE keystate[256];
dinkeyboard->Acquire();
dinkeyboard->GetDeviceState(256, (LPVOID)keystate);

if(keystate[DIK_F10] & 0x80)
text->setText(L"F10 pressed");
else if(keystate[DIK_LALT] & 0x80)
text->setText(L"LEFT ALT pressed");
else if(keystate[DIK_RALT] & 0x80)
text->setText(L"RIGHT ALT pressed");
else if(keystate[DIK_RETURN] & 0x80)
text->setText(L"RETURN pressed");
else if(keystate[DIK_NUMPADENTER] & 0x80)
text->setText(L"NUMPAD_ENTER pressed");
else if(keystate[DIK_RCONTROL] & 0x80)
text->setText(L"RIGHT CONTROL pressed");
else if(keystate[DIK_LCONTROL] & 0x80)
text->setText(L"LEFT CONTROL pressed");
else if(keystate[DIK_LSHIFT] & 0x80)
text->setText(L"LEFT SHIFT pressed");
else if(keystate[DIK_RSHIFT] & 0x80)
text->setText(L"RIGHT SHIFT pressed");

return;
}

void cleanDirectInput()
{
dinkeyboard->Unacquire();
dinmouse->Unacquire();
din->Release();
return;
}
void detect_mousepos(void)
{
static DIMOUSESTATE2 mousestate;
dinmouse->Acquire();
dinmouse->GetDeviceState(sizeof(DIMOUSESTATE2), (LPVOID)&mousestate);
if(mousestate.rgbButtons[0] & 0x80)
text->setText(L"MOUSE_01");
else if(mousestate.rgbButtons[1] & 0x80)
text->setText(L"MOUSE_02");
else if(mousestate.rgbButtons[2] & 0x80)
text->setText(L"MOUSE_03");
else if(mousestate.rgbButtons[3] & 0x80)
text->setText(L"MOUSE_04");
else if(mousestate.rgbButtons[4] & 0x80)
text->setText(L"MOUSE_05");
else if(mousestate.lZ < 0)
text->setText(L"MOUSE_WHEEL_UP");
else if(mousestate.lZ > 0)
text->setText(L"MOUSE_WHEEL_DOWN");
return;
}

LRESULT CALLBACK WindowsProcedure(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
HWND hwndCtl = (HWND)lParam;
int code = HIWORD(wParam);

if (hwndCtl == hWnd)
{
DestroyWindow(hWnd);
PostQuitMessage(0);
return 0;
}
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
Sorry for formating.
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
BlindSide
Admin
Posts: 2821
Joined: Thu Dec 08, 2005 9:09 am
Location: NZ!

Post by BlindSide »

I see you only have keyboard and mouse, is it easy to add joystick support using this?
Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

wow thats some impressive code.

reminds me why I ONLY program with irrlicht.

can anyone explain why windows code is so messy looking?

hmm yeah an os is on my list of things to do but somehow getting laid and dieing will come first. besides as much as everyone wants to deny microsofts quality there is probably a perfectly good explination... like accidental reinitialization of variables.

where the hell did I ever learn to talk like this?
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

I see you only have keyboard and mouse, is it easy to add joystick support using this?
My knowledge of DX is weak.
I just folowed some tutorial on it and after sucess i feeled
i should post it here (someone might whant to use it).Didnt tried
joystick because i havent got one, but i guess it is similar code as for
mouse and keyboard.

@midnight
You are the king.
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
dhenton9000
Posts: 395
Joined: Fri Apr 08, 2005 8:46 pm

Post by dhenton9000 »

Midnight
Posts: 1772
Joined: Fri Jul 02, 2004 2:37 pm
Location: Wonderland

Post by Midnight »

belfegor wrote: @midnight
You are the king.
wow I am? cool want to be my general?

First order of business is to nuke the world and bring me 50 virgins to repopulate the earth with.

anyone else that is smart can have the whores.
to prevent gene degradation only.
if you want the daughters you'll have to kill me.

wheres my jester xrhit I'm bored. hehe :P
belfegor
Posts: 383
Joined: Mon Sep 18, 2006 7:22 pm
Location: Serbia

Post by belfegor »

dhenton9000 wrote:I've got a joystick sample:
Thanks.I gonna look at it.
midnight wrote:First order of business is to nuke the world...
It gonna hapen soon anyway.But i am afraid that there is not going to be
planet earth to populate with after kataklysm.

"..only the dead trees are growing here..." MAYHEM :twisted:
Small FPS demo made using Irrlicht&NewtonDEMO
InfoHERE
Its at very early stage but i think im crazy enough to finish it all alone.
Post Reply