Program pre ovládanie joystiku v pascale

Delphi & Pascal (česká wiki)
Přejít na: navigace, hledání
Kategorija: Programy zos Pascalu

Program: Joydemo.pas
Subor exe: Joydemo.exe
Mušiš mac: Joystick.pas

Program názorne ukáže všetky možnosti ako ovládať joystick. V 4 demách ukáže pohyb pákového ovládača, stlačenie buttonov, rekalibraciu, ale aj ovládanie dvoch joystickov naraz.
{ JOYDEMO.PAS                                                       }
{ Tento program nazorne ukaze vsetky moznosti ako ovladat joystick. }
{ V 4 demach ukaze pohyb, stlacenie buttonov, rekalibraciu, ale aj  }
{ ovladanie dvoch joystickov naraz.                                 }
{                                                                   }
{ Datum:03.08.1997                             http://www.trsek.com }
 
uses dos,crt,joystick;
 
const header='Ukazkovy priklad pre JOYSTICK';
      footer='          Stlac button na joystiku pre ukoncenie dema.';
      DemoChar=#22;
 
type CursorType=(none,normal,fat);
 
var InitX,InitY : word;
    NumberOfSticks : byte;
    Reg : Registers;
    CursorStartLine, CursorEndLine : byte;
 
 
{ Zisti poziciu kurzora }
procedure GetCursorData;
begin
 Reg.ah:=$0f;
 intr($10,Reg);
 Reg.ah:=$03;
 intr($10,Reg);
 CursorStartLine:=Reg.ch;
 CursorEndLine:=Reg.cl;
end;
 
 
{ Zobrazi poziciu kurzora }
procedure SetCursor ( c:CursorType );
begin
 case c of
  none:   Reg.ch:=$20;
  normal: Reg.ch:=CursorStartLine;
  fat:	  Reg.ch:=0;
 end;
 Reg.cl:=CursorEndLine;
 Reg.ah:=1;
 intr($10,Reg);
end;
 
 
{ Vykresli okno v ktorom sa pohybuje kurzor }
procedure DrawBox( x1,y1,x2,y2 : integer );
var i:integer;
begin
 gotoxy(x1,y1);
 write(#218);
 for i:=x1+1 to x2-1 do write(#196);
 write(#191);
 for i:=y1+1 to y2-1 do begin
   gotoxy(x1,i);write(#179);
   gotoxy(x2,i);write(#179);
  end;
 gotoxy(x1,y2);write(#192);
 for i:=x1+1 to x2-1 do write(#196);
 write(#217);
end;
 
 
{ Demo1 }
procedure Demo1;
var ch:char;
begin
 clrscr;
 writeln(header,'1');
 gotoxy(1,7);
 writeln('Toto je test funkcnosti Joystiku spolu zo jeho zobrazenim.');
 writeln('Preto predpokladam ze mas pripojeny aspon jeden joystick.');
 writeln;
 if JoystickPressent then writeln('Mas jeden joystick.')
 		     else writeln('Nemas pripojeny joystick.');
 writeln;
 writeln;
 gotoxy(14,23);
 write('Kolko mas nainstalovanych joystikov ?');
 repeat
  ch:=readkey;
 until (ch in ['0'..'2']);
 NumberOfSticks:=ord(ch)-48;
end;
 
 
{ Demo2 }
procedure Demo2;
 
const linela='JOYSTICK A';
      linelb='JOYSTICK B';
      line2a='X-Axis Y-Axis Button 1 Button 2';
      line2b='       X-Axis Y-Axis Button 1 Button 2';
 
type StatusStringType=string[3];
 
var JoyAX,JoyAY,JoyBX,JoyBY : word;
    line : string;
    ch : char;
 
{ Lokalna funkcia pre proceduru Demo2 }
function ButtonStatusSt ( b:boolean ):StatusStringType;
begin
 if b then ButtonStatusSt:='IN'
      else ButtonStatusSt:='OUT';
end;
 
begin
 clrscr;
 SetCursor(none);
 writeln(header,'2');
 writeln;
 writeln('(Vstup Joysticku zobrazeny ako raw data)');
 if (NumberOfSticks=2) then line:=linela+linelb
		       else line:=linela;
 gotoxy(40-(length(line) div 2),7);
 writeln(line);
 if (NumberOfSticks=2) then line:=line2a+line2b
 		       else line:=line2a;
 gotoxy(40-(length(line) div 2),8);
 write(line);
 gotoxy(24,23);
 write('Prechod na dalsie demo stlac button.');
 
 repeat
  ReadJoyA(JoyAX,JoyAY);
  if (NumberOfSticks=2) then gotoxy(1,9)
  		        else gotoxy(22,9);
  write(JoyAX:5,JoyAY:8,ButtonStatusSt(ButtonA1):9,ButtonStatusSt(ButtonA2):10);
  if (NumberOfSticks=2) then begin
     ReadJoyB(JoyBX,JoyBY);
     write(JoyBX:16,JoyBY:8,ButtonStatusSt(ButtonB1):9,ButtonStatusSt(ButtonB2):10);
     end;
 until keypressed;
 
 while keypressed do ch:=readkey;
end;	{ of demo2 }
 
 
{ Demo3 }
procedure Demo3;
var JoyX,JoyY:word;
    ScreenX,ScreenY:byte;
begin
 clrscr;
 SetCursor(none);
 writeln(header,'3');
 writeln;
 writeln('(Joystick ako indikator smeru)');
 gotoxy(1,25);
 write(footer);
 DrawBox(1,5,80,24);
 ScreenX:=40;
 ScreenY:=14;
 
 repeat
  gotoxy(ScreenX,ScreenY);
  write(' ');
  ReadJoyA(JoyX,JoyY);
  if JoyX>InitX+(InitX div 5) then inc(ScreenX)
  			      else if JoyX<InitX-(InitX div 5) then dec(ScreenX);
  if JoyY>InitY+(InitY div 5) then inc(ScreenY)
                              else if JoyY<InitY-(InitY div 5) then dec(ScreenY);
  if ScreenX>79 then ScreenX:=79;
  if ScreenX<2  then ScreenX:=2;
  if ScreenY>23 then ScreenY:=23;
  if ScreenY<6  then ScreenY:=6;
  gotoxy(ScreenX,Screeny);
  write(DemoChar);
  delay(30);
 until (ButtonA1 or ButtonA2);
 
 repeat until not(ButtonA1 or ButtonA2);
end;
 
 
{ Demo4 }
procedure Demo4;
var LowX, LowY, HighX, HighY, JoyX, JoyY : word;
    ScreenX,ScreenY,OldScreenX,OldScreenY : byte;
begin
 clrscr;
 writeln(header,'4');
 writeln;
 writeln('(Joystick ako indikator pozicie)');
 gotoxy(1,7);
 writeln('Pre spustenie tohoto dema musis odistit joystick.');
 gotoxy(1,9);
 repeat
  writeln('Posun joystick hore-vlavo a stlac jeden z butonov.');
  repeat until (ButtonA1 or ButtonA2);
  ReadJoyA(LowX,LowY);
  repeat until not(ButtonA1 or ButtonA2);
  writeln('Posun joystick dole-vpravo a stlac jeden z butonov.');
  repeat until (ButtonA1 or ButtonA2);
  ReadJoyA(HighX,HighY);
  repeat until not(ButtonA1 or ButtonA2);
 
  if (LowX >= HighX) or (LowY >= HighY) then begin
     writeln;
     writeln('Kalibracia neprebehla spravne. Skus znova rekalibraciu.');
     writeln;
    end;
 until ( (LowX < HighX) and (LowY < HighY) );
 
 clrscr;
 SetCursor(none);
 DrawBox(1,1,80,24);
 gotoxy(1,25);
 write(footer);
 OldScreenX:=100;
 OldScreenY:=100;
 
 repeat
  ReadJoyA(JoyX,JoyY);
  if JoyX<LowX  then JoyX:=LowX;
  if JoyX>HighX then JoyX:=HighX;
  if JoyY<LowY  then JoyY:=LowY;
  if JoyY<HighY then JoyY:=HighY;
  ScreenX:=(((JoyX-LowX+1)*77) div (HighX-LowX))+2;
  ScreenY:=(((JoyY-LowY+1)*21) div (HighY-LowY))+2;
  if (OldScreenX <> ScreenX) or (OldScreenY <> ScreenY) then begin
     gotoxy(OldScreenX,OldScreenY);write(' ');
     gotoxy(ScreenX,ScreenY);write(DemoChar);
     OldScreenX:=ScreenX;OldScreenY:=ScreenY;
    end;
  gotoxy(1,1);
 until (ButtonA1 or ButtonA2);
end;
 
 
BEGIN
 ReadJoyA(InitX,InitY);
 
 GetCursorData;
 Demo1;
 if (NumberOfSticks=0) then halt;
 Demo2;
 Demo3;
 Demo4;
 clrscr;
 SetCursor(normal);
END.