home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / DELPHIX.ZIP / Source / DXPlayFm.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-06  |  9.4 KB  |  348 lines

  1. unit DXPlayFm;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, ExtCtrls, DirectX, DXPlay, ActiveX, DXETable, DIB;
  8.  
  9. type
  10.   TDelphiXDXPlayForm = class(TForm)
  11.     Notebook: TNotebook;
  12.     NextButton: TButton;
  13.     BackButton: TButton;
  14.     CancelButton: TButton;
  15.     Bevel1: TBevel;
  16.     ProviderList: TListBox;
  17.     Label1: TLabel;
  18.     Bevel2: TBevel;
  19.     Label2: TLabel;
  20.     NewGame: TRadioButton;
  21.     JoinGame: TRadioButton;
  22.     Label3: TLabel;
  23.     Label4: TLabel;
  24.     Label5: TLabel;
  25.     NewGameSessionName: TEdit;
  26.     NewGamePlayerName: TEdit;
  27.     Label7: TLabel;
  28.     JoinGamePlayerName: TEdit;
  29.     Label8: TLabel;
  30.     JoinGameSessionList: TListBox;
  31.     DXPaintBox1: TDXPaintBox;
  32.     procedure NotebookPageChanged(Sender: TObject);
  33.     procedure BackButtonClick(Sender: TObject);
  34.     procedure NextButtonClick(Sender: TObject);
  35.     procedure CancelButtonClick(Sender: TObject);
  36.     procedure ProviderListClick(Sender: TObject);
  37.     procedure NewGameClick(Sender: TObject);
  38.     procedure FormDestroy(Sender: TObject);
  39.     procedure FormShow(Sender: TObject);
  40.     procedure EditKeyDown(Sender: TObject; var Key: Word;
  41.       Shift: TShiftState);
  42.     procedure NewGameSessionNameKeyDown(Sender: TObject; var Key: Word;
  43.       Shift: TShiftState);
  44.     procedure NewGamePlayerNameKeyDown(Sender: TObject; var Key: Word;
  45.       Shift: TShiftState);
  46.     procedure JoinGameSessionListKeyDown(Sender: TObject; var Key: Word;
  47.       Shift: TShiftState);
  48.     procedure JoinGamePlayerNameKeyDown(Sender: TObject; var Key: Word;
  49.       Shift: TShiftState);
  50.   private
  51.     FProviderGUID: TGUID;
  52.   public
  53.     DPlay: IDirectPlay3A;
  54.     DXPlay: TCustomDXPlay;
  55.     PlayerName: string;
  56.     ProviderName: string;
  57.     SessionName: string;
  58.   end;
  59.  
  60. var
  61.   DelphiXDXPlayForm: TDelphiXDXPlayForm;
  62.  
  63. implementation
  64.  
  65. uses DXConsts;
  66.  
  67. {$R *.DFM}
  68.  
  69. procedure TDelphiXDXPlayForm.FormShow(Sender: TObject);
  70. begin
  71.   ProviderList.Items := DXPlay.Providers;
  72.   NotebookPageChanged(nil);
  73. end;
  74.  
  75. procedure TDelphiXDXPlayForm.FormDestroy(Sender: TObject);
  76. var
  77.   i: Integer;
  78. begin
  79.   for i:=0 to JoinGameSessionList.Items.Count-1 do
  80.     Dispose(PGUID(JoinGameSessionList.Items.Objects[i]));
  81. end;
  82.  
  83. procedure TDelphiXDXPlayForm.BackButtonClick(Sender: TObject);
  84. begin
  85.   if Notebook.ActivePage='SessionNew' then
  86.   begin
  87.     DPlay := nil;
  88.     Notebook.ActivePage := 'SessionType'
  89.   end else if Notebook.ActivePage='SessionJoin' then
  90.   begin
  91.     DPlay := nil;
  92.     Notebook.ActivePage := 'SessionType'
  93.   end else
  94.     Notebook.PageIndex := Notebook.PageIndex - 1;
  95. end;
  96.  
  97. procedure TDelphiXDXPlayForm.NextButtonClick(Sender: TObject);
  98.  
  99.   procedure InitDirectPlay;
  100.   var
  101.     DPlay1: IDirectPlay;
  102.   begin
  103.     if DXDirectPlayCreate(FProviderGUID, DPlay1, nil)<>0 then
  104.       raise EDXPlayError.CreateFmt(SCannotInitialized, [SDirectPlay]);
  105.  
  106.     DPlay := DPlay1 as IDirectPlay3A;
  107.   end;
  108.  
  109.   function EnumSessionsCallback(const lpThisSD: DPSESSIONDESC2;
  110.       var lpdwTimeOut: DWORD; dwFlags: DWORD; lpContext: Pointer): BOOL; stdcall;
  111.   var
  112.     Guid: PGUID;
  113.   begin
  114.     if dwFlags and DPESC_TIMEDOUT<>0 then
  115.     begin
  116.       Result := False;
  117.       Exit;
  118.     end;
  119.  
  120.     Guid := New(PGUID);
  121.     Move(lpThisSD.guidInstance, Guid^, SizeOf(TGUID));
  122.     TDelphiXDXPlayForm(lpContext).JoinGameSessionList.Items.AddObject(lpThisSD.lpszSessionNameA, Pointer(Guid));
  123.  
  124.     Result := True;
  125.   end;
  126.  
  127. var
  128.   dpDesc: DPSESSIONDESC2;
  129.   i: Integer;
  130.   c: array[0..1023] of Char;
  131.   hr: HRESULT;
  132. begin
  133.   if Notebook.ActivePage='SelectProvider' then
  134.   begin
  135.     FProviderGUID := PGUID(ProviderList.Items.Objects[ProviderList.ItemIndex])^;
  136.  
  137.     InitDirectPlay;
  138.  
  139.     Notebook.ActivePage := 'SessionType';
  140.   end else
  141.   if Notebook.ActivePage='SessionType' then
  142.   begin
  143.     if DPlay=nil then InitDirectPlay;
  144.  
  145.     if NewGame.Checked then
  146.       Notebook.ActivePage := 'SessionNew'
  147.     else
  148.     begin
  149.       for i:=0 to JoinGameSessionList.Items.Count-1 do
  150.         Dispose(PGUID(JoinGameSessionList.Items.Objects[i]));
  151.       JoinGameSessionList.Items.Clear;
  152.  
  153.       FillChar(dpDesc, SizeOf(dpDesc), 0);
  154.       dpDesc.dwSize := SizeOf(dpDesc);
  155.       dpDesc.guidApplication := DXPlayStringToGUID(DXPlay.GUID);
  156.  
  157.       hr := DPlay.EnumSessions(dpDesc, 0, @EnumSessionsCallback, Self, DPENUMSESSIONS_AVAILABLE);
  158.       if hr=DPERR_USERCANCEL then Exit;
  159.       if hr<>0 then
  160.         raise EDXPlayError.Create(SDXPlaySessionListCannotBeAcquired);
  161.  
  162.       Notebook.ActivePage := 'SessionJoin';
  163.     end;
  164.   end else if Notebook.ActivePage='SessionNew' then
  165.   begin
  166.     if DPlay=nil then InitDirectPlay;
  167.  
  168.     {  Session making  }
  169.     StrLCopy(@c, PChar(NewGameSessionName.Text), SizeOf(c));
  170.  
  171.     FillChar(dpDesc, SizeOf(dpDesc), 0);
  172.     dpDesc.dwSize := SizeOf(dpDesc);
  173.     dpDesc.dwFlags := DPSESSION_MIGRATEHOST or DPSESSION_KEEPALIVE;
  174.     dpDesc.lpszSessionNameA := @c;
  175.     dpDesc.guidApplication := DXPlayStringToGUID(DXPlay.GUID);
  176.     dpDesc.dwMaxPlayers := DXPlay.MaxPlayers;
  177.  
  178.     hr := DPlay.Open(dpDesc, DPOPEN_CREATE);
  179.     if hr=DPERR_USERCANCEL then Exit;
  180.     if hr<>0 then
  181.     begin
  182.       DPlay := nil;
  183.       raise EDXPlayError.CreateFmt(SDXPlaySessionCannotOpened, [NewGameSessionName.Text]);
  184.     end;
  185.  
  186.     PlayerName := NewGamePlayerName.Text;
  187.     ProviderName := ProviderList.Items[ProviderList.ItemIndex];
  188.     SessionName := NewGameSessionName.Text;
  189.  
  190.     Tag := 1;
  191.     Close;
  192.   end else if Notebook.ActivePage='SessionJoin' then
  193.   begin
  194.     if DPlay=nil then InitDirectPlay;
  195.  
  196.     {  Session connection  }
  197.     FillChar(dpDesc, SizeOf(dpDesc), 0);
  198.     dpDesc.dwSize := SizeOf(dpDesc);
  199.     dpDesc.guidInstance := PGUID(JoinGameSessionList.Items.Objects[JoinGameSessionList.ItemIndex])^;
  200.     dpDesc.guidApplication := DXPlayStringToGUID(DXPlay.GUID);
  201.  
  202.     hr := DPlay.Open(dpDesc, DPOPEN_JOIN);
  203.     if hr=DPERR_USERCANCEL then Exit;
  204.     if hr<>0 then
  205.     begin
  206.       DPlay := nil;
  207.       raise EDXPlayError.CreateFmt(SDXPlaySessionCannotOpened, [NewGameSessionName.Text]);
  208.     end;
  209.  
  210.     PlayerName := JoinGamePlayerName.Text;
  211.     ProviderName := ProviderList.Items[ProviderList.ItemIndex];
  212.     SessionName := JoinGameSessionList.Items[JoinGameSessionList.ItemIndex];
  213.  
  214.     Tag := 1;
  215.     Close;
  216.   end else
  217.     Notebook.PageIndex := Notebook.PageIndex + 1;
  218. end;
  219.  
  220. procedure TDelphiXDXPlayForm.CancelButtonClick(Sender: TObject);
  221. begin
  222.   Close;
  223. end;
  224.  
  225. procedure TDelphiXDXPlayForm.NotebookPageChanged(Sender: TObject);
  226. begin
  227.   if Notebook.ActivePage='SelectProvider' then
  228.   begin
  229.     BackButton.Enabled := False;
  230.     NextButton.Enabled := ProviderList.ItemIndex<>-1;
  231.     NextButton.Caption := DXPlayFormNext;
  232.   end else if Notebook.ActivePage='SessionType' then
  233.   begin
  234.     BackButton.Enabled := True;
  235.     NextButton.Enabled := NewGame.Checked or JoinGame.Checked;
  236.     NextButton.Caption := DXPlayFormNext;
  237.   end else if Notebook.ActivePage='SessionNew' then
  238.   begin
  239.     BackButton.Enabled := True;
  240.     NextButton.Enabled := (NewGameSessionName.Text<>'') and (NewGamePlayerName.Text<>'');
  241.     NextButton.Caption := DXPlayFormComplete;
  242.   end else if Notebook.ActivePage='SessionJoin' then
  243.   begin
  244.     BackButton.Enabled := True;
  245.     NextButton.Enabled := (JoinGameSessionList.ItemIndex<>-1) and (JoinGamePlayerName.Text<>'');
  246.     NextButton.Caption := DXPlayFormComplete;
  247.   end;
  248. end;
  249.  
  250. procedure TDelphiXDXPlayForm.ProviderListClick(Sender: TObject);
  251. begin
  252.   NotebookPageChanged(nil);
  253. end;
  254.  
  255. procedure TDelphiXDXPlayForm.NewGameClick(Sender: TObject);
  256. begin
  257.   NotebookPageChanged(nil);
  258. end;
  259.  
  260. procedure TDelphiXDXPlayForm.EditKeyDown(Sender: TObject;
  261.   var Key: Word; Shift: TShiftState);
  262. begin
  263.   if (Key=VK_RETURN) and (NextButton.Enabled) then
  264.   begin
  265.     NextButtonClick(nil);
  266.     Key := 0;
  267.   end;
  268. end;
  269.  
  270. procedure TDelphiXDXPlayForm.NewGameSessionNameKeyDown(Sender: TObject;
  271.   var Key: Word; Shift: TShiftState);
  272. begin
  273.   if NewGameSessionName.Text='' then Exit;
  274.  
  275.   if Key=VK_RETURN then
  276.   begin
  277.     if NextButton.Enabled then
  278.     begin
  279.       NextButtonClick(nil);
  280.       Key := 0;
  281.     end else if NewGamePlayerName.Text='' then
  282.     begin
  283.       NewGamePlayerName.SetFocus;
  284.       Key := 0;
  285.     end;
  286.   end;
  287. end;
  288.  
  289. procedure TDelphiXDXPlayForm.NewGamePlayerNameKeyDown(Sender: TObject;
  290.   var Key: Word; Shift: TShiftState);
  291. begin
  292.   if NewGamePlayerName.Text='' then Exit;
  293.  
  294.   if Key=VK_RETURN then
  295.   begin
  296.     if NextButton.Enabled then
  297.     begin
  298.       NextButtonClick(nil);
  299.       Key := 0;
  300.     end else if NewGameSessionName.Text='' then
  301.     begin
  302.       NewGameSessionName.SetFocus;
  303.       Key := 0;
  304.     end;
  305.   end;
  306. end;
  307.  
  308. procedure TDelphiXDXPlayForm.JoinGameSessionListKeyDown(Sender: TObject;
  309.   var Key: Word; Shift: TShiftState);
  310. begin
  311.   if JoinGameSessionList.ItemIndex=-1 then Exit;
  312.  
  313.   if Key=VK_RETURN then
  314.   begin
  315.     if NextButton.Enabled then
  316.     begin
  317.       NextButtonClick(nil);
  318.       Key := 0;
  319.     end else if JoinGamePlayerName.Text='' then
  320.     begin
  321.       JoinGamePlayerName.SetFocus;
  322.       Key := 0;
  323.     end;
  324.   end;
  325. end;
  326.  
  327. procedure TDelphiXDXPlayForm.JoinGamePlayerNameKeyDown(Sender: TObject;
  328.   var Key: Word; Shift: TShiftState);
  329. begin
  330.   if JoinGamePlayerName.Text='' then Exit;
  331.  
  332.   if Key=VK_RETURN then
  333.   begin
  334.     if NextButton.Enabled then
  335.     begin
  336.       NextButtonClick(nil);
  337.       Key := 0;
  338.     end else if JoinGameSessionList.ItemIndex=-1 then
  339.     begin
  340.       JoinGameSessionList.SetFocus;
  341.       Key := 0;         
  342.     end;
  343.   end;
  344. end;
  345.  
  346. end.
  347.  
  348.