• Advertisement

Archived

This topic is now archived and is closed to further replies.

Direct Input

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

Does anybody have any examples for setting up DirectInput for 2 joysticks. I am using the Jedi Headers. I''ve tried the examples on their web site, but am still having problems. Can anyone help with a simple framework for initializing, setting up, and destroying the DirectInput objects? Thanks in advance, Supps

Share this post


Link to post
Share on other sites
Advertisement
Hey,

There are good comps in both DelphiX and the DGC for DirectInput. I''ve been playing around with the DelphiX ones as of late and they offer excellent assigning of multiple keys and joystick buttons to an action. For example...

DXInput.Keyboard.Keystates[isLeft,0]:=vk_left; // left key
DXInput.Keyboard.Keystates[isLeft,1]:=ord(''z''); // z key
DXInput.Keyboard.Enabled:=true; // keyboard on
DXInput.Joystick.Enabled:=true; // joystick on

...assigns the left key (as well as joystick left) to isLeft (the ''is'' is for input state) state. Then to find if a user pressed the left key *or* the ''z'' key *or* moved the joystick to the left you can call...

if (isLeft in DXInput.States) then y:=y-1; { go left }

...one nice easy call. You should be able to use the component without installing all of DelphiX, or easily move it over to the JEDI Headers.










[ turbo | turbo.gamedev.net ]

Share this post


Link to post
Share on other sites
Hi Supps,

Forget about those comps ... noooo wait, sorry but I just had to say that .

First of all get you a copy of DirectX7.0a (notice the "a") if don''t have it yet. As far as I know DirectX7.0 had problems with joysticks.
With that done and using the latest DirectX 7.0 Headers you should do the following. (Its untested as I don''t have a joystick). Its only to show you a way of doing it.

    
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, DirectInput;

type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
DirectInput : IDirectInput7;
DirectInputDevice_Joystick1, DirectInputDevice_Joystick2 : IDirectInputDevice7;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
function DIEnumDevicesCallback(var lpddi: TDIDeviceInstanceA; pvRef: Pointer): Integer; stdcall;
var DirectXResult : HResult;
begin
if Form1.DirectInputDevice_Joystick1 = nil then
begin
DirectXResult := IDirectInput7(pvRef).CreateDeviceEx(lpddi.guidInstance, IID_IDirectInputDevice7, Form1.DirectInputDevice_Joystick1, nil);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
DirectXResult := Form1.DirectInputDevice_Joystick1.SetDataFormat(c_dfDIJoystick);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
DirectXResult := Form1.DirectInputDevice_Joystick1.SetCooperativeLevel(Form1.Handle, DISCL_NONEXCLUSIVE or DISCL_FOREGROUND);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
DirectXResult := Form1.DirectInputDevice_Joystick1.Acquire;
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
Result := DIENUM_CONTINUE;
end else
begin
DirectXResult := IDirectInput7(pvRef).CreateDeviceEx(lpddi.guidInstance, IID_IDirectInputDevice7, Form1.DirectInputDevice_Joystick2, nil);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); end;
DirectXResult := Form1.DirectInputDevice_Joystick2.SetDataFormat(c_dfDIJoystick);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
DirectXResult := Form1.DirectInputDevice_Joystick2.SetCooperativeLevel(Form1.Handle, DISCL_NONEXCLUSIVE or DISCL_FOREGROUND);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
DirectXResult := Form1.DirectInputDevice_Joystick2.Acquire;
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;
Result := DIENUM_STOP;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var DirectXResult : HResult;
begin
DirectXResult := DirectInputCreateEx(hinstance, DIRECTINPUT_VERSION, IID_IDirectInput7, DirectInput, nil);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;

DirectXResult := DirectInput.EnumDevices(DIDEVTYPE_JOYSTICK, DIEnumDevicesCallback, @DirectInput, DIEDFL_ATTACHEDONLY);
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); exit; end;

//Now Use the Joysticks

end;

procedure TForm1.FormDestroy(Sender: TObject);
var DirectXResult : HResult;
begin
if DirectInputDevice_Joystick1 <> nil then
begin
DirectXResult := DirectInputDevice_Joystick1._Release;
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); end;
end;
if DirectInputDevice_Joystick2 <> nil then
begin
DirectXResult := DirectInputDevice_Joystick2._Release;
if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); end;
end;
if DirectInput <> nil then
begin
// DirectXResult := DirectInput._Release;

// if DirectXResult <> DI_OK then begin ShowMessage(DIErrorString(DirectXResult)); end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var DIJoyState : TDIJoyState;
begin
if DirectInputDevice_Joystick1 <> nil then
begin
DirectInputDevice_Joystick1.Poll;
DirectInputDevice_Joystick1.GetDeviceState(SizeOf(DIJoyState), @DIJoyState);
// use DIJoyState

end;
if DirectInputDevice_Joystick2 <> nil then
begin
DirectInputDevice_Joystick2.Poll;
DirectInputDevice_Joystick2.GetDeviceState(SizeOf(DIJoyState), @DIJoyState);
// use DIJoyState

end;
end;

end.




[ Ampaze | turbo.gamedev.net ]

Share this post


Link to post
Share on other sites

  • Advertisement