console.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. {$MACRO ON}
  2. {$DEFINE DEFAULT_WIDTH:=320}
  3. {$DEFINE DEFAULT_HEIGHT:=200}
  4. {$DEFINE DEFAULT_FORMAT:=TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF)}
  5. {$ASMMODE intel}
  6. Constructor VGAConsole.Create;
  7. Var
  8. { I, J : Integer;
  9. r, g, b, a : DWord;
  10. tmpbpp : Integer;}
  11. tmp : TPTCFormat;
  12. Begin
  13. m_area := Nil;
  14. m_clip := Nil;
  15. m_keyboard := Nil;
  16. m_copy := Nil;
  17. m_palette := Nil;
  18. m_default_format := Nil;
  19. m_open := False;
  20. m_locked := False;
  21. m_title[0] := #0;
  22. m_information[0] := #0;
  23. m_default_width := DEFAULT_WIDTH;
  24. m_default_height := DEFAULT_HEIGHT;
  25. m_default_format := DEFAULT_FORMAT;
  26. { InitVESA;}
  27. m_primary := Nil;
  28. { m_modes[0].Create;}
  29. m_area := TPTCArea.Create;
  30. m_clip := TPTCArea.Create;
  31. m_copy := TPTCCopy.Create;
  32. m_palette := TPTCPalette.Create;
  33. tmp := TPTCFormat.Create(8);
  34. m_modes[0] := TPTCMode.Create(320, 200, tmp);
  35. tmp.Destroy;
  36. tmp := TPTCFormat.Create(8, $E0, $1C, $03);
  37. m_modes[1] := TPTCMode.Create(320, 200, tmp);
  38. tmp.Destroy;
  39. tmp := TPTCFormat.Create(16, $F800, $7E0, $1F);
  40. m_modes[2] := TPTCMode.Create(320, 200, tmp);
  41. tmp.Destroy;
  42. m_modes[3] := TPTCMode.Create;
  43. m_faketype := FAKEMODE2A;
  44. configure('ptc.cfg');
  45. End;
  46. Destructor VGAConsole.Destroy;
  47. Begin
  48. close;
  49. internal_clear_mode_list;
  50. If m_keyboard <> Nil Then
  51. m_keyboard.Destroy;
  52. If m_copy <> Nil Then
  53. m_copy.Destroy;
  54. If m_default_format <> Nil Then
  55. m_default_format.Destroy;
  56. If m_palette <> Nil Then
  57. m_palette.Destroy;
  58. If m_clip <> Nil Then
  59. m_clip.Destroy;
  60. If m_area <> Nil Then
  61. m_area.Destroy;
  62. Inherited Destroy;
  63. End;
  64. Procedure VGAConsole.configure(Const _file : String);
  65. Var
  66. F : Text;
  67. S : String;
  68. Begin
  69. ASSign(F, _file);
  70. Try
  71. Reset(F);
  72. Except
  73. Exit;
  74. End;
  75. Try
  76. While Not EoF(F) Do
  77. Begin
  78. Readln(F, S);
  79. option(S);
  80. End;
  81. Finally
  82. CloseFile(F);
  83. End;
  84. End;
  85. Function VGAConsole.option(Const _option : String) : Boolean;
  86. Begin
  87. {...}
  88. If (System.Copy(_option, 1, 8) = 'FAKEMODE') And (Length(_option) = 10) And
  89. (_option[9] >= '1') And (_option[9] <= '3') And
  90. (_option[10] >= 'A') And (_option[10] <= 'C') Then
  91. Begin
  92. Case _option[9] Of
  93. '1' : Case _option[10] Of
  94. 'A' : m_faketype := FAKEMODE1A;
  95. 'B' : m_faketype := FAKEMODE1B;
  96. 'C' : m_faketype := FAKEMODE1C;
  97. End;
  98. '2' : Case _option[10] Of
  99. 'A' : m_faketype := FAKEMODE2A;
  100. 'B' : m_faketype := FAKEMODE2B;
  101. 'C' : m_faketype := FAKEMODE2C;
  102. End;
  103. '3' : Case _option[10] Of
  104. 'A' : m_faketype := FAKEMODE3A;
  105. 'B' : m_faketype := FAKEMODE3B;
  106. 'C' : m_faketype := FAKEMODE3C;
  107. End;
  108. End;
  109. option := True;
  110. Exit;
  111. End;
  112. option := m_copy.option(_option);
  113. End;
  114. Procedure VGAConsole.internal_clear_mode_list;
  115. Var
  116. I : Integer;
  117. Done : Boolean;
  118. Begin
  119. I := 0;
  120. Done := False;
  121. Repeat
  122. Done := Not m_modes[I].valid;
  123. m_modes[I].Destroy;
  124. Inc(I);
  125. Until Done;
  126. End;
  127. Function VGAConsole.modes : PPTCMode;
  128. Begin
  129. { internal_clear_mode_list;}
  130. modes := m_modes;
  131. End;
  132. Procedure VGAConsole.open(Const _title : String; _pages : Integer); Overload;
  133. Begin
  134. open(_title, m_default_format, _pages);
  135. End;
  136. Procedure VGAConsole.open(Const _title : String; Const _format : TPTCFormat;
  137. _pages : Integer); Overload;
  138. Begin
  139. open(_title, m_default_width, m_default_height, _format, _pages);
  140. End;
  141. Procedure VGAConsole.open(Const _title : String; _width, _height : Integer;
  142. Const _format : TPTCFormat; _pages : Integer); Overload;
  143. Var
  144. m : TPTCMode;
  145. Begin
  146. m := TPTCMode.Create(_width, _height, _format);
  147. Try
  148. open(_title, m, _pages);
  149. Finally
  150. m.Destroy;
  151. End;
  152. End;
  153. Procedure VGAConsole.open(Const _title : String; Const _mode : TPTCMode;
  154. _pages : Integer); Overload;
  155. Var
  156. { _width, _height : Integer;
  157. _format : TPTCFormat;}
  158. I : Integer;
  159. { modefound : Integer;}
  160. modetype : Integer;
  161. Begin
  162. If Not _mode.valid Then
  163. Raise TPTCError.Create('invalid mode');
  164. If _mode.format.indexed Then
  165. modetype := INDEX8
  166. Else
  167. If _mode.format.bits = 8 Then
  168. modetype := RGB332
  169. Else
  170. modetype := FAKEMODE;
  171. internal_pre_open_setup(_title);
  172. internal_open_fullscreen_start;
  173. internal_open_fullscreen(modetype);
  174. internal_open_fullscreen_finish(_pages);
  175. internal_post_open_setup;
  176. End;
  177. Procedure VGAConsole.close;
  178. Begin
  179. If m_open Then
  180. Begin
  181. If m_locked Then
  182. Raise TPTCError.Create('console is still locked');
  183. { flush all key presses }
  184. While KeyPressed Do ReadKey;
  185. internal_close;
  186. m_open := False;
  187. End;
  188. End;
  189. Procedure VGAConsole.flush;
  190. Begin
  191. check_open;
  192. check_unlocked;
  193. End;
  194. Procedure VGAConsole.finish;
  195. Begin
  196. check_open;
  197. check_unlocked;
  198. End;
  199. Procedure VGAConsole.vga_load(data : Pointer); ASSembler;
  200. Asm
  201. push es
  202. mov ax, fs
  203. mov es, ax
  204. mov ecx, 64000/4
  205. mov esi, [data]
  206. mov edi, 0A0000h
  207. cld
  208. rep movsd
  209. pop es
  210. End;
  211. Procedure VGAConsole.update;
  212. Var
  213. framebuffer : PInteger;
  214. Begin
  215. check_open;
  216. check_unlocked;
  217. Case m_CurrentMode Of
  218. 0, 1 : Begin
  219. While (inportb($3DA) And 8) <> 0 Do;
  220. While (inportb($3DA) And 8) = 0 Do;
  221. vga_load(m_primary);
  222. End;
  223. 2 : fakemode_load(m_primary, True);
  224. End;
  225. { WriteToVideoMemory(m_primary, 0, m_pitch * m_height);}
  226. End;
  227. Procedure VGAConsole.update(Const _area : TPTCArea);
  228. Begin
  229. update;
  230. End;
  231. Procedure VGAConsole.internal_ReadKey(k : TPTCKey);
  232. Begin
  233. check_open;
  234. m_keyboard.internal_ReadKey(k);
  235. End;
  236. Function VGAConsole.internal_PeekKey(k : TPTCKey) : Boolean;
  237. Begin
  238. check_open;
  239. Result := m_keyboard.internal_PeekKey(k);
  240. End;
  241. Procedure VGAConsole.copy(Var surface : TPTCBaseSurface);
  242. Var
  243. pixels : Pointer;
  244. Begin
  245. check_open;
  246. check_unlocked;
  247. pixels := lock;
  248. Try
  249. surface.load(pixels, width, height, pitch, format, palette);
  250. unlock;
  251. Except
  252. On error : TPTCError Do
  253. Begin
  254. unlock;
  255. Raise TPTCError.Create('failed to copy console to surface', error);
  256. End;
  257. End;
  258. End;
  259. Procedure VGAConsole.copy(Var surface : TPTCBaseSurface;
  260. Const source, destination : TPTCArea);
  261. Var
  262. pixels : Pointer;
  263. Begin
  264. check_open;
  265. check_unlocked;
  266. pixels := lock;
  267. Try
  268. surface.load(pixels, width, height, pitch, format, palette, source, destination);
  269. unlock;
  270. Except
  271. On error : TPTCError Do
  272. Begin
  273. unlock;
  274. Raise TPTCError.Create('failed to copy console to surface', error);
  275. End;
  276. End;
  277. End;
  278. Function VGAConsole.lock : Pointer;
  279. Var
  280. pixels : Pointer;
  281. Begin
  282. check_open;
  283. If m_locked Then
  284. Raise TPTCError.Create('console is already locked');
  285. pixels := m_primary;
  286. m_locked := True;
  287. lock := pixels;
  288. End;
  289. Procedure VGAConsole.unlock;
  290. Begin
  291. check_open;
  292. If Not m_locked Then
  293. Raise TPTCError.Create('console is not locked');
  294. m_locked := False;
  295. End;
  296. Procedure VGAConsole.load(Const pixels : Pointer;
  297. _width, _height, _pitch : Integer;
  298. Const _format : TPTCFormat;
  299. Const _palette : TPTCPalette);
  300. Var
  301. Area_ : TPTCArea;
  302. console_pixels : Pointer;
  303. Begin
  304. check_open;
  305. check_unlocked;
  306. If clip.Equals(area) Then
  307. Begin
  308. console_pixels := lock;
  309. Try
  310. Try
  311. m_copy.request(_format, format);
  312. m_copy.palette(_palette, palette);
  313. m_copy.copy(pixels, 0, 0, _width, _height, _pitch, console_pixels, 0, 0,
  314. width, height, pitch);
  315. Except
  316. On error : TPTCError Do
  317. Begin
  318. Raise TPTCError.Create('failed to load pixels to console', error);
  319. End;
  320. End;
  321. Finally
  322. unlock;
  323. End;
  324. End
  325. Else
  326. Begin
  327. Area_ := TPTCArea.Create(0, 0, width, height);
  328. Try
  329. load(pixels, _width, _height, _pitch, _format, _palette, Area_, area);
  330. Finally
  331. Area_.Destroy;
  332. End;
  333. End;
  334. End;
  335. Procedure VGAConsole.load(Const pixels : Pointer;
  336. _width, _height, _pitch : Integer;
  337. Const _format : TPTCFormat;
  338. Const _palette : TPTCPalette;
  339. Const source, destination : TPTCArea);
  340. Var
  341. console_pixels : Pointer;
  342. clipped_source, clipped_destination : TPTCArea;
  343. tmp : TPTCArea;
  344. Begin
  345. check_open;
  346. check_unlocked;
  347. clipped_destination := Nil;
  348. clipped_source := TPTCArea.Create;
  349. Try
  350. clipped_destination := TPTCArea.Create;
  351. console_pixels := lock;
  352. Try
  353. Try
  354. tmp := TPTCArea.Create(0, 0, _width, _height);
  355. Try
  356. TPTCClipper.clip(source, tmp, clipped_source, destination, clip, clipped_destination);
  357. Finally
  358. tmp.Destroy;
  359. End;
  360. m_copy.request(_format, format);
  361. m_copy.palette(_palette, palette);
  362. m_copy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
  363. console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, pitch);
  364. Except
  365. On error:TPTCError Do
  366. Begin
  367. Raise TPTCError.Create('failed to load pixels to console area', error);
  368. End;
  369. End;
  370. Finally
  371. unlock;
  372. End;
  373. Finally
  374. clipped_source.Destroy;
  375. If clipped_destination <> Nil Then
  376. clipped_destination.Destroy;
  377. End;
  378. End;
  379. Procedure VGAConsole.save(pixels : Pointer;
  380. _width, _height, _pitch : Integer;
  381. Const _format : TPTCFormat;
  382. Const _palette : TPTCPalette);
  383. Var
  384. Area_ : TPTCArea;
  385. console_pixels : Pointer;
  386. Begin
  387. check_open;
  388. check_unlocked;
  389. If clip.Equals(area) Then
  390. Begin
  391. console_pixels := lock;
  392. Try
  393. Try
  394. m_copy.request(format, _format);
  395. m_copy.palette(palette, _palette);
  396. m_copy.copy(console_pixels, 0, 0, width, height, pitch, pixels, 0, 0,
  397. _width, _height, _pitch);
  398. Except
  399. On error : TPTCError Do
  400. Begin
  401. Raise TPTCError.Create('failed to save console pixels', error);
  402. End;
  403. End;
  404. Finally
  405. unlock;
  406. End;
  407. End
  408. Else
  409. Begin
  410. Area_ := TPTCArea.Create(0, 0, width, height);
  411. Try
  412. save(pixels, _width, _height, _pitch, _format, _palette, area, Area_);
  413. Finally
  414. Area_.Destroy;
  415. End;
  416. End;
  417. End;
  418. Procedure VGAConsole.save(pixels : Pointer;
  419. _width, _height, _pitch : Integer;
  420. Const _format : TPTCFormat;
  421. Const _palette : TPTCPalette;
  422. Const source, destination : TPTCArea);
  423. Var
  424. console_pixels : Pointer;
  425. clipped_source, clipped_destination : TPTCArea;
  426. tmp : TPTCArea;
  427. Begin
  428. check_open;
  429. check_unlocked;
  430. clipped_destination := Nil;
  431. clipped_source := TPTCArea.Create;
  432. Try
  433. clipped_destination := TPTCArea.Create;
  434. console_pixels := lock;
  435. Try
  436. Try
  437. tmp := TPTCArea.Create(0, 0, _width, _height);
  438. Try
  439. TPTCClipper.clip(source, clip, clipped_source, destination, tmp, clipped_destination);
  440. Finally
  441. tmp.Destroy;
  442. End;
  443. m_copy.request(format, _format);
  444. m_copy.palette(palette, _palette);
  445. m_copy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
  446. pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
  447. Except
  448. On error:TPTCError Do
  449. Begin
  450. Raise TPTCError.Create('failed to save console area pixels', error);
  451. End;
  452. End;
  453. Finally
  454. unlock;
  455. End;
  456. Finally
  457. clipped_source.Destroy;
  458. If clipped_destination <> Nil Then
  459. clipped_destination.Destroy;
  460. End;
  461. End;
  462. Procedure VGAConsole.clear;
  463. Var
  464. tmp : TPTCColor;
  465. Begin
  466. check_open;
  467. check_unlocked;
  468. If format.direct Then
  469. tmp := TPTCColor.Create(0, 0, 0, 0)
  470. Else
  471. tmp := TPTCColor.Create(0);
  472. Try
  473. clear(tmp);
  474. Finally
  475. tmp.Destroy;
  476. End;
  477. End;
  478. Procedure VGAConsole.clear(Const color : TPTCColor);
  479. Var
  480. tmp : TPTCArea;
  481. Begin
  482. check_open;
  483. check_unlocked;
  484. tmp := TPTCArea.Create;
  485. Try
  486. clear(color, tmp);
  487. Finally
  488. tmp.Destroy;
  489. End;
  490. End;
  491. Procedure VGAConsole.clear(Const color : TPTCColor;
  492. Const _area : TPTCArea);
  493. Begin
  494. {...}
  495. End;
  496. Procedure VGAConsole.palette(Const _palette : TPTCPalette);
  497. Begin
  498. check_open;
  499. If format.indexed Then
  500. Begin
  501. m_palette.load(_palette.data);
  502. internal_SetPalette(_palette.data);
  503. End;
  504. End;
  505. Function VGAConsole.palette : TPTCPalette;
  506. Begin
  507. check_open;
  508. palette := m_palette;
  509. End;
  510. Procedure VGAConsole.clip(Const _area : TPTCArea);
  511. Var
  512. tmp : TPTCArea;
  513. Begin
  514. check_open;
  515. tmp := TPTCClipper.clip(_area, m_area);
  516. Try
  517. m_clip.ASSign(tmp);
  518. Finally
  519. tmp.Destroy;
  520. End;
  521. End;
  522. Function VGAConsole.width : Integer;
  523. Begin
  524. check_open;
  525. width := m_width;
  526. End;
  527. Function VGAConsole.height : Integer;
  528. Begin
  529. check_open;
  530. height := m_height;
  531. End;
  532. Function VGAConsole.pitch : Integer;
  533. Begin
  534. check_open;
  535. pitch := m_pitch;
  536. End;
  537. Function VGAConsole.pages : Integer;
  538. Begin
  539. check_open;
  540. pages := 2;{m_primary.pages;}
  541. End;
  542. Function VGAConsole.area : TPTCArea;
  543. Begin
  544. check_open;
  545. area := m_area;
  546. End;
  547. Function VGAConsole.clip : TPTCArea;
  548. Begin
  549. check_open;
  550. clip := m_clip;
  551. End;
  552. Function VGAConsole.format : TPTCFormat;
  553. Begin
  554. check_open;
  555. format := m_modes[m_CurrentMode].format;
  556. End;
  557. Function VGAConsole.name : String;
  558. Begin
  559. name := 'VGA';
  560. End;
  561. Function VGAConsole.title : String;
  562. Begin
  563. End;
  564. Function VGAConsole.information : String;
  565. Begin
  566. End;
  567. Procedure VGAConsole.internal_pre_open_setup(Const _title : String);
  568. Begin
  569. End;
  570. Procedure VGAConsole.internal_open_fullscreen_start;
  571. {Var
  572. f : TPTCFormat;}
  573. Begin
  574. { f := TPTCFormat.Create(32, $0000FF, $00FF00, $FF0000);}
  575. { m_160x100buffer := TPTCSurface.Create(160, 100, f);}
  576. { f.Destroy;}
  577. { set80x50;}
  578. End;
  579. Procedure VGAConsole.internal_open_fullscreen(ModeType : Integer);
  580. Var
  581. tmp : TPTCArea;
  582. Begin
  583. VGASetMode(320, 200, ModeType, m_faketype);
  584. Case ModeType Of
  585. INDEX8 : Begin
  586. m_CurrentMode := 0;
  587. m_pitch := 320;
  588. End;
  589. RGB332 : Begin
  590. m_CurrentMode := 1;
  591. m_pitch := 320;
  592. End;
  593. FAKEMODE : Begin
  594. m_CurrentMode := 2;
  595. m_pitch := 640;
  596. End;
  597. End;
  598. m_width := 320;
  599. m_height := 200;
  600. tmp := TPTCArea.Create(0, 0, width, height);
  601. Try
  602. m_area.ASSign(tmp);
  603. m_clip.ASSign(tmp);
  604. Finally
  605. tmp.Destroy;
  606. End;
  607. End;
  608. Procedure VGAConsole.internal_open_fullscreen_finish(_pages : Integer);
  609. Begin
  610. If m_primary <> Nil Then
  611. FreeMem(m_primary);
  612. m_primary := GetMem(m_height * m_pitch);
  613. End;
  614. Procedure VGAConsole.internal_post_open_setup;
  615. Begin
  616. If m_keyboard <> Nil Then
  617. m_keyboard.Destroy;
  618. m_keyboard := TDosKeyboard.Create;
  619. { temporary platform dependent information fudge }
  620. {sprintf(m_information,"dos version x.xx.x\nvesa version x.xx\nvesa driver name xxxxx\ndisplay driver vendor xxxxx\ncertified driver? x\n");}
  621. { set open flag }
  622. m_open := True;
  623. End;
  624. Procedure VGAConsole.internal_reset;
  625. Begin
  626. If m_primary <> Nil Then
  627. FreeMem(m_primary);
  628. m_primary := Nil;
  629. If m_keyboard <> Nil Then
  630. m_keyboard.Destroy;
  631. m_keyboard := Nil;
  632. { m_primary.Destroy;}
  633. { m_keyboard.Destroy;}
  634. { m_primary := Nil;}
  635. { m_keyboard := Nil;}
  636. End;
  637. Procedure VGAConsole.internal_close;
  638. Begin
  639. If m_primary <> Nil Then
  640. Begin
  641. FreeMem(m_primary);
  642. m_primary := Nil;
  643. End;
  644. RestoreTextMode;
  645. End;
  646. Procedure VGAConsole.internal_SetPalette(data : Pint32);
  647. Var
  648. i : Integer;
  649. c : DWord;
  650. Begin
  651. outportb($3C8, 0);
  652. For i := 0 To 255 Do
  653. Begin
  654. c := (data^ Shr 2) And $003F3F3F;
  655. outportb($3C9, c Shr 16);
  656. outportb($3C9, c Shr 8);
  657. outportb($3C9, c);
  658. Inc(data);
  659. End;
  660. End;
  661. Procedure VGAConsole.check_open;
  662. Begin
  663. {$IFDEF DEBUG}
  664. If Not m_open Then
  665. Raise TPTCError.Create('console is not open');
  666. {$ELSE}
  667. {$ENDIF}
  668. End;
  669. Procedure VGAConsole.check_unlocked;
  670. Begin
  671. {$IFDEF DEBUG}
  672. If m_locked Then
  673. Raise TPTCError.Create('console is not unlocked');
  674. {$ELSE}
  675. {$ENDIF}
  676. End;