gameunit.pp 10 KB

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