gameunit.pp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. {
  2. $Id$
  3. A simple unit with some common used routines for FPCGames (FpcTris and
  4. SameGame)
  5. Contains
  6. - Highscore routines "developped" for FPCTris, but now also used by SameGame
  7. - "Dummy" mouse routines which either shell to API units or to MSMouse.
  8. See the file COPYING.FPC, included in this distribution,
  9. for details about the copyright.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. **********************************************************************}
  14. UNIT GameUnit;
  15. INTERFACE
  16. {MouseAPI defined : unit unes API mouse units, which requires that package,
  17. but also works under Linux
  18. MouseAPI undef : RTL unit MsMouse. API not required, but doesn't work under
  19. Linux }
  20. {$UNDEF MouseAPI}
  21. TYPE CHARSET=SET OF CHAR;
  22. {---- Unified Mouse procedures. ---- }
  23. FUNCTION MousePresent : BOOLEAN;
  24. PROCEDURE HideMouse;
  25. PROCEDURE ShowMouse;
  26. PROCEDURE GetMouseState(VAR MX,MY,MState : LONGINT);
  27. PROCEDURE DoneMouse;
  28. PROCEDURE InitMouse;
  29. Const LButton = 1; {left button}
  30. RButton = 2; {right button}
  31. MButton = 4; {middle button}
  32. {---- Standard Highscore procedures ----}
  33. TYPE HighScoreType = Packed RECORD
  34. Name : String[12];
  35. Score: LONGINT;
  36. END;
  37. HighScoreArr = ARRAY[0..9] OF HighScoreType;
  38. VAR HighScore : HighScoreArr;
  39. ScorePath : String;
  40. HighX,HighY : LONGINT;
  41. PROCEDURE LoadHighScore(FileName:STRING);
  42. PROCEDURE SaveHighScore;
  43. PROCEDURE ShowHighScore;
  44. FUNCTION SlipInScore(Score:LONGINT):LONGINT;
  45. {---- Keyboard routines ----}
  46. CONST {Constants for GetKey}
  47. ArrU = $04800; ArrL = $04B00; ArrR = $04D00; BS = $08; (* Backspace *)
  48. ArrD = $05000; CR = $0D; ESC = $1B; KDelete= $05300;
  49. KInsert= $05200; Home = $04700; KEnd = $04F00; CtrlY = $19;
  50. CtrlT = $14;
  51. CONST FieldSpace : CHAR = #177;
  52. AlfaBeta : CHARSET= [' '..'z'];
  53. FUNCTION GetKey:LONGINT;
  54. {Generic string input routine}
  55. FUNCTION InputStr(VAR S:String;X,Y,Len:LONGINT;TextIn:BOOLEAN;CharAllow:CHARSET):BOOLEAN;
  56. {---- Misc ----}
  57. PROCEDURE SetDefaultColor; {Restore the attribs saved on startup}
  58. IMPLEMENTATION
  59. {$IFDEF MouseAPI}
  60. Uses Mouse,Dos,Crt;
  61. {$ELSE}
  62. Uses MsMouse,Dos,Crt;
  63. {$ENDIF}
  64. VAR DefColor : BYTE; {Backup of startup colors}
  65. CONST
  66. {The initial names. If people feel they are missing, I first checked the Alias,
  67. and then filled with names of the FPC-Devel list, and arranged them alfabetically}
  68. InitNames : ARRAY[0..9] OF String[12] = ('Carl','Daniel','Florian','Jonas','Lee','Marco','Michael (3x)',
  69. 'Peter','Pierre','Thomas' );
  70. {$IFDEF MouseAPI}
  71. VAR MouseBuffer : LONGINT;
  72. {$ENDIF}
  73. FUNCTION MousePresent : BOOLEAN;
  74. BEGIN
  75. {$IFDEF MouseAPI}
  76. MousePresent:=DetectMouse<>0;
  77. {$ELSE}
  78. MousePresent:=MouseFound;
  79. {$ENDIF}
  80. END;
  81. PROCEDURE ShowMouse;
  82. BEGIN
  83. {$IFDEF MouseAPI}
  84. Mouse.ShowMouse;
  85. {$ELSE}
  86. MsMouse.ShowMouse;
  87. {$ENDIF}
  88. END;
  89. PROCEDURE HideMouse;
  90. BEGIN
  91. {$IFDEF MouseAPI}
  92. Mouse.HideMouse;
  93. {$ELSE}
  94. MsMouse.HideMouse;
  95. {$ENDIF}
  96. END;
  97. PROCEDURE InitMouse;
  98. BEGIN
  99. {$IFDEF MouseAPI}
  100. Mouse.InitMouse;
  101. {$ELSE}
  102. MsMouse.InitMouse;
  103. {$ENDIF}
  104. END;
  105. PROCEDURE DoneMouse;
  106. BEGIN
  107. {$IFDEF MouseAPI}
  108. Mouse.DoneMouse;
  109. {$ENDIF}
  110. END;
  111. PROCEDURE GetMouseState(VAR MX,MY,MState : LONGINT);
  112. {$IFDEF MouseAPI}
  113. VAR MouseEvent : TMouseEvent;
  114. {$ENDIF}
  115. BEGIN
  116. {$IFDEF MouseAPI}
  117. GetMouseEvent(MouseEvent);
  118. MX:=MouseEvent.X SHL 3;
  119. MY:=MouseEvent.Y SHL 3;
  120. MState:=MouseEvent.Buttons;
  121. {$ELSE}
  122. MsMouse.GetMouseState(MX,MY,MState);
  123. {$ENDIF}
  124. END;
  125. Procedure LoadHighScore(FileName:STRING);
  126. var
  127. F: File;
  128. I : LONGINT;
  129. BEGIN
  130. {$I-}
  131. Assign(F, FileName);
  132. FileMode := 0; {Set file access to read only }
  133. Reset(F);
  134. Close(F);
  135. {$I+}
  136. IF IOResult=0 THEN
  137. ScorePath:=FileName
  138. ELSE
  139. ScorePath:=FSearch(FileName,GetEnv('PATH'));
  140. IF ScorePath='' THEN
  141. BEGIN
  142. FOR I:=0 TO 9 DO
  143. BEGIN
  144. HighScore[I].Name:=InitNames[I];
  145. HighScore[I].Score:=(I+1)*750;
  146. END;
  147. ScorePath:=FileName;
  148. END
  149. ELSE
  150. BEGIN
  151. Assign(F,ScorePath);
  152. Reset(F,1);
  153. BlockRead(F,HighScore,SIZEOF(HighScoreArr));
  154. Close(F);
  155. END;
  156. END;
  157. Procedure SaveHighScore;
  158. var
  159. F: File;
  160. BEGIN
  161. Assign(F,ScorePath);
  162. Rewrite(F,1);
  163. BlockWrite(F,HighScore,SIZEOF(HighScoreArr));
  164. Close(F);
  165. END;
  166. FUNCTION SlipInScore(Score:LONGINT):LONGINT;
  167. VAR I,J : LONGINT;
  168. BEGIN
  169. I:=0;
  170. WHILE (Score>HighScore[I].Score) AND (I<10) DO
  171. INC(I);
  172. IF I<>0 THEN
  173. BEGIN
  174. IF I>1 THEN
  175. FOR J:=0 TO I-2 DO
  176. HighScore[J]:=HighScore[J+1];
  177. HighScore[I-1].Score:=Score;
  178. HighScore[I-1].Name:='';
  179. END;
  180. SlipInScore:=I;
  181. END;
  182. PROCEDURE ShowHighScore;
  183. VAR I : LONGINT;
  184. {HighX=40 HighY=9}
  185. BEGIN
  186. GotoXY(HighX+5,9); Write('The Highscores');
  187. FOR I:=0 TO 9 DO
  188. BEGIN
  189. GotoXY(HighX,HighY+11-I);
  190. Write(HighScore[I].Name,' ':(13-Length(HighScore[I].Name)),' ',HighScore[I].Score:5);
  191. END;
  192. END;
  193. FUNCTION GetKey:LONGINT;
  194. VAR InKey: LONGINT;
  195. BEGIN
  196. InKey:=ORD(ReadKey);
  197. IF InKey=0 THEN InKey:=ORD(ReadKey) SHL 8;
  198. GetKey:=InKey;
  199. END;
  200. FUNCTION InputStr(VAR S:String;X,Y,Len:LONGINT;TextIn:BOOLEAN;CharAllow:CHARSET):BOOLEAN;
  201. {
  202. Input a string from keyboard, in a nice way,
  203. allowed characters are in CHARSET CharAllow, but several editting
  204. keys are always allowed, see CASE loop.
  205. Parameters:
  206. X,Y Coordinates field
  207. Len Length field
  208. TextIn S already filled?}
  209. VAR
  210. InGev : LONGINT; { No. of chars inputted }
  211. Posi : LONGINT; { Cursorposition}
  212. Ins : BOOLEAN; { Insert yes/no}
  213. Key : LONGINT; { Last key as ELib.GetKey
  214. code <255 if normal key,
  215. >256 if special/function
  216. key. See keys.inc}
  217. Uitg : String; {The inputted string}
  218. Full : BOOLEAN; { Is the string full? }
  219. EndVal : WORD;
  220. PROCEDURE ReWr; { Rewrite the field, using Uitg}
  221. VAR I : LONGINT; { Temporary variabele }
  222. BEGIN
  223. IF Length(Uitg)>Len THEN
  224. Uitg[0]:=CHR(Len);
  225. IF Length(Uitg)>0 THEN
  226. FOR I:= 1 TO Length(Uitg) DO
  227. BEGIN
  228. GotoXY(X+I-1,Y);
  229. IF Uitg[I]=CHR(32) THEN
  230. Write(FieldSpace)
  231. ELSE
  232. Write(Uitg[I]);
  233. END;
  234. IF Len<>Length(Uitg) THEN
  235. BEGIN
  236. GotoXY(X+Length(Uitg),Y);
  237. FOR I:= Length(Uitg) TO Len-1 DO
  238. Write(FieldSpace);
  239. END;
  240. END;
  241. PROCEDURE DoCursor; { Put Cursor in/out insert-mode }
  242. BEGIN
  243. {$IFNDEF Linux}
  244. { IF Ins THEN
  245. SetCursorSize($11E)
  246. ELSE
  247. SetCursorSize($71E); }
  248. {$ENDIF}
  249. END;
  250. BEGIN
  251. { Init }
  252. InGev :=0; { 0 chars untill now }
  253. Posi :=1; { Cursorposition 0 }
  254. Ins :=TRUE; { Insert according to parameters }
  255. DoCursor; { Set cursor accordingly }
  256. Key :=0;
  257. { put ±±± padded field on screen }
  258. FillChar(Uitg,Len+1,FieldSpace);
  259. Uitg[0]:=CHR(Len);
  260. ReWr;
  261. GotoXY(X,Y);
  262. FillChar(Uitg,Len,32);
  263. UitG[0]:=#0;
  264. IF TextIn THEN
  265. BEGIN
  266. Uitg:=S;
  267. Posi:=Length(Uitg)+1; { Put a predefined }
  268. ReWr; { String on screen if specified }
  269. END;
  270. EndVal:=0;
  271. WHILE EndVal=0 DO
  272. BEGIN
  273. Full:=FALSE;
  274. IF ((Posi)>=Len) THEN
  275. BEGIN
  276. Full:=TRUE;
  277. Posi:=Len;
  278. END;
  279. GotoXY(X+Posi-1,Y);
  280. {$IFNDEF Linux}
  281. {$IFDEF FPC}
  282. CursorOn;
  283. {$ENDIF}
  284. DoCursor;
  285. {$ENDIF}
  286. Key:=GetKey;
  287. {$IFNDEF Linux}
  288. {$IFDEF FPC}
  289. CursorOff;
  290. {$ENDIF}
  291. {$ENDIF}
  292. CASE Key OF
  293. CR : BEGIN
  294. EndVal:=1;
  295. S:=UitG;
  296. END;
  297. ESC : EndVal:=2;
  298. BS : IF Posi>1 THEN { BackSpace }
  299. BEGIN
  300. DEC(Posi);
  301. Delete(Uitg,Posi,1);
  302. DEC(InGev);
  303. ReWr;
  304. END;
  305. KDelete : BEGIN
  306. Delete(Uitg,Posi,1);
  307. DEC(InGev);
  308. ReWr;
  309. END;
  310. ArrR : IF (NOT Full) AND ((Posi-1)<InGev) THEN
  311. BEGIN
  312. INC (Posi);
  313. GotoXY(X+Posi-1,Y);
  314. END;
  315. KInsert : BEGIN
  316. Ins:= NOT Ins;
  317. DoCursor;
  318. END;
  319. ArrL : IF (NOT (Posi=1)) THEN
  320. BEGIN
  321. DEC (Posi);
  322. GotoXY(X+Posi-1,Y);
  323. END;
  324. Home : Posi:=1;
  325. KEnd : Posi:=InGev-1;
  326. CtrlY : BEGIN
  327. Delete(Uitg,Posi,Length(Uitg)-Posi);
  328. ReWr;
  329. END;
  330. CtrlT : BEGIN
  331. Uitg[0]:=#0; Posi:=1; ReWr;
  332. END;
  333. END; {Case}
  334. IF EndVal=0 THEN
  335. BEGIN
  336. IF (CHR(Key) IN CharAllow) THEN
  337. BEGIN
  338. IF Posi>Len THEN
  339. Posi:=Len;
  340. IF (Ins=FALSE) OR Full THEN
  341. BEGIN
  342. IF (ORD(Uitg[0])<Posi) THEN
  343. Uitg[0]:=CHR(Posi);
  344. Uitg[Posi]:=CHR(Key);
  345. END
  346. ELSE
  347. BEGIN
  348. Insert(CHR(Key),Uitg,Posi);
  349. { InsertC(uitg,CHR(Key),Posi);}
  350. END;
  351. ReWr;
  352. INC(Posi);
  353. END;
  354. END;
  355. InGev:=Length(Uitg);
  356. END;
  357. InputStr:=Endval=1;
  358. END;
  359. PROCEDURE SetDefaultColor;
  360. BEGIN
  361. TextColor(DefColor AND 15);
  362. TextBackground(DefColor SHR 4);
  363. END;
  364. BEGIN
  365. {$IFDEF MouseAPI}
  366. MouseBuffer:=0;
  367. {$ENDIF}
  368. DefColor:=TextAttr; { Save the current attributes, to restore}
  369. END.
  370. {
  371. $Log$
  372. Revision 1.1 1999-06-01 19:24:33 peter
  373. * updates from marco
  374. }