console.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. {$MACRO ON}
  2. {$DEFINE DEFAULT_WIDTH:=320}
  3. {$DEFINE DEFAULT_HEIGHT:=200}
  4. {$DEFINE DEFAULT_FORMAT:=TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF)}
  5. Constructor CGAConsole.Create;
  6. Var
  7. I : Integer;
  8. Begin
  9. { m_160x100buffer := Nil;}
  10. m_primary := Nil;
  11. m_keyboard := Nil;
  12. m_copy := Nil;
  13. m_default_format := Nil;
  14. m_open := False;
  15. m_locked := False;
  16. FillChar(m_modes, SizeOf(m_modes), 0);
  17. m_title[0] := #0;
  18. m_information[0] := #0;
  19. m_default_width := DEFAULT_WIDTH;
  20. m_default_height := DEFAULT_HEIGHT;
  21. m_default_format := DEFAULT_FORMAT;
  22. For I := 0 To 255 Do
  23. m_modes[I] := TPTCMode.Create;
  24. calcpal := @calcpal_colorbase;
  25. use_charset := @charset_b7asc;
  26. build_colormap(0);
  27. m_copy := TPTCCopy.Create;
  28. configure('ptc.cfg');
  29. End;
  30. Destructor CGAConsole.Destroy;
  31. Var
  32. I : Integer;
  33. Begin
  34. close;
  35. For I := 0 To 255 Do
  36. If m_modes[I] <> Nil Then
  37. m_modes[I].Destroy;
  38. If m_keyboard <> Nil Then
  39. m_keyboard.Destroy;
  40. If m_copy <> Nil Then
  41. m_copy.Destroy;
  42. If m_default_format <> Nil Then
  43. m_default_format.Destroy;
  44. Inherited Destroy;
  45. End;
  46. Procedure CGAConsole.configure(Const _file : String);
  47. Var
  48. F : Text;
  49. S : String;
  50. Begin
  51. ASSign(F, _file);
  52. Try
  53. Reset(F);
  54. Except
  55. Exit;
  56. End;
  57. Try
  58. While Not EoF(F) Do
  59. Begin
  60. Readln(F, S);
  61. option(S);
  62. End;
  63. Finally
  64. CloseFile(F);
  65. End;
  66. End;
  67. Function CGAConsole.option(Const _option : String) : Boolean;
  68. Begin
  69. {...}
  70. option := m_copy.option(_option);
  71. End;
  72. Function CGAConsole.modes : PPTCMode;
  73. Begin
  74. {todo...}
  75. modes := @m_modes;
  76. End;
  77. Procedure CGAConsole.open(Const _title : String; _pages : Integer); Overload;
  78. Begin
  79. open(_title, m_default_format, _pages);
  80. End;
  81. Procedure CGAConsole.open(Const _title : String; Const _format : TPTCFormat;
  82. _pages : Integer); Overload;
  83. Begin
  84. open(_title, m_default_width, m_default_height, _format, _pages);
  85. End;
  86. Procedure CGAConsole.open(Const _title : String; _width, _height : Integer;
  87. Const _format : TPTCFormat; _pages : Integer); Overload;
  88. Var
  89. m : TPTCMode;
  90. Begin
  91. m := TPTCMode.Create(_width, _height, _format);
  92. open(_title, m, _pages);
  93. m.Destroy;
  94. End;
  95. Procedure CGAConsole.open(Const _title : String; Const _mode : TPTCMode;
  96. _pages : Integer); Overload;
  97. Var
  98. _width, _height : Integer;
  99. _format : TPTCFormat;
  100. Begin
  101. If Not _mode.valid Then
  102. Raise TPTCError.Create('invalid mode');
  103. _width := _mode.width;
  104. _height := _mode.height;
  105. _format := _mode.format;
  106. internal_pre_open_setup(_title);
  107. internal_open_fullscreen_start;
  108. internal_open_fullscreen(_width, _height, _format);
  109. internal_open_fullscreen_finish(_pages);
  110. internal_post_open_setup;
  111. End;
  112. Procedure CGAConsole.close;
  113. Begin
  114. If m_open Then
  115. Begin
  116. If m_locked Then
  117. Raise TPTCError.Create('console is still locked');
  118. {flush all key presses}
  119. While KeyPressed Do ReadKey;
  120. internal_close;
  121. m_open := False;
  122. End;
  123. End;
  124. Procedure CGAConsole.flush;
  125. Begin
  126. check_open;
  127. check_unlocked;
  128. End;
  129. Procedure CGAConsole.finish;
  130. Begin
  131. check_open;
  132. check_unlocked;
  133. End;
  134. Procedure CGAConsole.update;
  135. Var
  136. framebuffer : PByte;
  137. Begin
  138. check_open;
  139. check_unlocked;
  140. framebuffer := m_primary.lock;
  141. { vrc;}
  142. CGADump(framebuffer);
  143. m_primary.unlock;
  144. End;
  145. Procedure CGAConsole.update(Const _area : TPTCArea);
  146. Begin
  147. update;
  148. End;
  149. Procedure CGAConsole.internal_ReadKey(k : TPTCKey);
  150. Begin
  151. check_open;
  152. m_keyboard.internal_ReadKey(k);
  153. End;
  154. Function CGAConsole.internal_PeekKey(k : TPTCKey) : Boolean;
  155. Begin
  156. check_open;
  157. Result := m_keyboard.internal_PeekKey(k);
  158. End;
  159. Procedure CGAConsole.copy(Var surface : TPTCBaseSurface);
  160. Var
  161. pixels : Pointer;
  162. Begin
  163. check_open;
  164. check_unlocked;
  165. pixels := lock;
  166. Try
  167. surface.load(pixels, width, height, pitch, format, palette);
  168. unlock;
  169. Except
  170. On error : TPTCError Do
  171. Begin
  172. unlock;
  173. Raise TPTCError.Create('failed to copy console to surface', error);
  174. End;
  175. End;
  176. End;
  177. Procedure CGAConsole.copy(Var surface : TPTCBaseSurface;
  178. Const source, destination : TPTCArea);
  179. Begin
  180. End;
  181. Function CGAConsole.lock : Pointer;
  182. Var
  183. pixels : Pointer;
  184. Begin
  185. check_open;
  186. If m_locked Then
  187. Raise TPTCError.Create('console is already locked');
  188. pixels := m_primary.lock;
  189. m_locked := True;
  190. lock := pixels;
  191. End;
  192. Procedure CGAConsole.unlock;
  193. Begin
  194. check_open;
  195. If Not m_locked Then
  196. Raise TPTCError.Create('console is not locked');
  197. m_primary.unlock;
  198. m_locked := False;
  199. End;
  200. Procedure CGAConsole.load(Const pixels : Pointer;
  201. _width, _height, _pitch : Integer;
  202. Const _format : TPTCFormat;
  203. Const _palette : TPTCPalette);
  204. Var
  205. Area_ : TPTCArea;
  206. console_pixels : Pointer;
  207. c, a : TPTCArea;
  208. Begin
  209. c := clip; a := area;
  210. If (c.left = a.left) And
  211. (c.top = a.top) And
  212. (c.right = a.right) And
  213. (c.bottom = a.bottom) Then
  214. Begin
  215. check_open;
  216. check_unlocked;
  217. console_pixels := lock;
  218. Try
  219. m_copy.request(_format, format);
  220. m_copy.palette(_palette, palette);
  221. m_copy.copy(pixels, 0, 0, _width, _height, _pitch, console_pixels, 0, 0,
  222. width, height, pitch);
  223. unlock;
  224. Except
  225. On error : TPTCError Do
  226. Begin
  227. unlock;
  228. Raise TPTCError.Create('failed to load pixels to console', error);
  229. End;
  230. End;
  231. End
  232. Else
  233. Begin
  234. Area_ := TPTCArea.Create(0, 0, width, height);
  235. load(pixels, _width, _height, _pitch, _format, _palette, Area_, area);
  236. Area_.Destroy;
  237. End;
  238. End;
  239. Procedure CGAConsole.load(Const pixels : Pointer;
  240. _width, _height, _pitch : Integer;
  241. Const _format : TPTCFormat;
  242. Const _palette : TPTCPalette;
  243. Const source, destination : TPTCArea);
  244. Var
  245. console_pixels : Pointer;
  246. clipped_source, clipped_destination : TPTCArea;
  247. tmp : TPTCArea;
  248. Begin
  249. check_open;
  250. check_unlocked;
  251. console_pixels := lock;
  252. clipped_source := TPTCArea.Create;
  253. clipped_destination := TPTCArea.Create;
  254. Try
  255. tmp := TPTCArea.Create(0, 0, _width, _height);
  256. TPTCClipper.clip(source, tmp, clipped_source, destination, clip, clipped_destination);
  257. tmp.Destroy;
  258. m_copy.request(_format, format);
  259. m_copy.palette(_palette, palette);
  260. m_copy.copy(pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, _pitch,
  261. console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, pitch);
  262. unlock;
  263. Except
  264. On error:TPTCError Do
  265. Begin
  266. clipped_source.Destroy;
  267. clipped_destination.Destroy;
  268. unlock;
  269. Raise TPTCError.Create('failed to load pixels to console area', error);
  270. End;
  271. End;
  272. clipped_source.Destroy;
  273. clipped_destination.Destroy;
  274. End;
  275. Procedure CGAConsole.save(pixels : Pointer;
  276. _width, _height, _pitch : Integer;
  277. Const _format : TPTCFormat;
  278. Const _palette : TPTCPalette);
  279. Var
  280. Area_ : TPTCArea;
  281. console_pixels : Pointer;
  282. c, a : TPTCArea;
  283. Begin
  284. c := clip; a := area;
  285. If (c.left = a.left) And
  286. (c.top = a.top) And
  287. (c.right = a.right) And
  288. (c.bottom = a.bottom) Then
  289. Begin
  290. check_open;
  291. check_unlocked;
  292. console_pixels := lock;
  293. Try
  294. m_copy.request(format, _format);
  295. m_copy.palette(palette, _palette);
  296. m_copy.copy(console_pixels, 0, 0, width, height, pitch, pixels, 0, 0,
  297. _width, _height, _pitch);
  298. unlock;
  299. Except
  300. On error : TPTCError Do
  301. Begin
  302. unlock;
  303. Raise TPTCError.Create('failed to save console pixels', error);
  304. End;
  305. End;
  306. End
  307. Else
  308. Begin
  309. Area_ := TPTCArea.Create(0, 0, width, height);
  310. save(pixels, _width, _height, _pitch, _format, _palette, area, Area_);
  311. Area_.Destroy;
  312. End;
  313. End;
  314. Procedure CGAConsole.save(pixels : Pointer;
  315. _width, _height, _pitch : Integer;
  316. Const _format : TPTCFormat;
  317. Const _palette : TPTCPalette;
  318. Const source, destination : TPTCArea);
  319. Var
  320. console_pixels : Pointer;
  321. clipped_source, clipped_destination : TPTCArea;
  322. tmp : TPTCArea;
  323. Begin
  324. check_open;
  325. check_unlocked;
  326. console_pixels := lock;
  327. clipped_source := TPTCArea.Create;
  328. clipped_destination := TPTCArea.Create;
  329. Try
  330. tmp := TPTCArea.Create(0, 0, _width, _height);
  331. TPTCClipper.clip(source, clip, clipped_source, destination, tmp, clipped_destination);
  332. tmp.Destroy;
  333. m_copy.request(format, _format);
  334. m_copy.palette(palette, _palette);
  335. m_copy.copy(console_pixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, pitch,
  336. pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, _pitch);
  337. unlock;
  338. Except
  339. On error:TPTCError Do
  340. Begin
  341. clipped_source.Destroy;
  342. clipped_destination.Destroy;
  343. unlock;
  344. Raise TPTCError.Create('failed to save console area pixels', error);
  345. End;
  346. End;
  347. clipped_source.Destroy;
  348. clipped_destination.Destroy;
  349. End;
  350. Procedure CGAConsole.clear;
  351. Begin
  352. End;
  353. Procedure CGAConsole.clear(Const color : TPTCColor);
  354. Begin
  355. End;
  356. Procedure CGAConsole.clear(Const color : TPTCColor;
  357. Const _area : TPTCArea);
  358. Begin
  359. End;
  360. Procedure CGAConsole.palette(Const _palette : TPTCPalette);
  361. Begin
  362. check_open;
  363. m_primary.palette(_palette);
  364. End;
  365. Function CGAConsole.palette : TPTCPalette;
  366. Begin
  367. check_open;
  368. palette := m_primary.palette;
  369. End;
  370. Procedure CGAConsole.clip(Const _area : TPTCArea);
  371. Begin
  372. check_open;
  373. m_primary.clip(_area);
  374. End;
  375. Function CGAConsole.width : Integer;
  376. Begin
  377. check_open;
  378. width := m_primary.width;
  379. End;
  380. Function CGAConsole.height : Integer;
  381. Begin
  382. check_open;
  383. height := m_primary.height;
  384. End;
  385. Function CGAConsole.pitch : Integer;
  386. Begin
  387. check_open;
  388. pitch := m_primary.pitch;
  389. End;
  390. Function CGAConsole.pages : Integer;
  391. Begin
  392. check_open;
  393. pages := 1;{m_primary.pages;}
  394. End;
  395. Function CGAConsole.area : TPTCArea;
  396. Begin
  397. check_open;
  398. area := m_primary.area;
  399. End;
  400. Function CGAConsole.clip : TPTCArea;
  401. Begin
  402. check_open;
  403. clip := m_primary.clip;
  404. End;
  405. Function CGAConsole.format : TPTCFormat;
  406. Begin
  407. check_open;
  408. format := m_primary.format;
  409. End;
  410. Function CGAConsole.name : String;
  411. Begin
  412. End;
  413. Function CGAConsole.title : String;
  414. Begin
  415. End;
  416. Function CGAConsole.information : String;
  417. Begin
  418. End;
  419. Procedure CGAConsole.internal_pre_open_setup(Const _title : String);
  420. Begin
  421. End;
  422. Procedure CGAConsole.internal_open_fullscreen_start;
  423. Var
  424. f : TPTCFormat;
  425. Begin
  426. CGAPrecalc;
  427. f := TPTCFormat.Create(32, $FF0000, $00FF00, $0000FF);
  428. m_primary := TPTCSurface.Create(320, 200, f);
  429. f.Destroy;
  430. { set80x50;}
  431. CGA320;
  432. End;
  433. Procedure CGAConsole.internal_open_fullscreen(_width, _height : Integer; Const _format : TPTCFormat);
  434. Begin
  435. { m_primary := TPTCSurface.Create(_width, _height, _format);}
  436. End;
  437. Procedure CGAConsole.internal_open_fullscreen_finish(_pages : Integer);
  438. Begin
  439. End;
  440. Procedure CGAConsole.internal_post_open_setup;
  441. Begin
  442. If m_keyboard <> Nil Then
  443. m_keyboard.Destroy;
  444. m_keyboard := TDosKeyboard.Create;
  445. { create win32 keyboard
  446. m_keyboard = new DosKeyboard();//m_window->handle(),m_window->thread(),false);}
  447. { temporary platform dependent information fudge }
  448. {sprintf(m_information,"dos version x.xx.x\nvesa version x.xx\nvesa driver name xxxxx\ndisplay driver vendor xxxxx\ncertified driver? x\n");}
  449. { set open flag }
  450. m_open := True;
  451. End;
  452. Procedure CGAConsole.internal_reset;
  453. Begin
  454. If m_primary <> Nil Then
  455. m_primary.Destroy;
  456. { m_keyboard.Destroy;}
  457. m_primary := Nil;
  458. { m_keyboard := Nil;}
  459. End;
  460. Procedure CGAConsole.internal_close;
  461. Begin
  462. If m_primary <> Nil Then
  463. m_primary.Destroy;
  464. m_primary := Nil;
  465. { If m_160x100buffer <> Nil Then
  466. m_160x100buffer.Destroy;
  467. m_160x100buffer := Nil;}
  468. CGAText;
  469. { m_keyboard.Destroy;
  470. m_keyboard := Nil;}
  471. End;
  472. Procedure CGAConsole.check_open;
  473. Begin
  474. {$IFDEF DEBUG}
  475. If Not m_open Then
  476. Raise TPTCError.Create('console is not open');
  477. {$ENDIF}
  478. End;
  479. Procedure CGAConsole.check_unlocked;
  480. Begin
  481. {$IFDEF DEBUG}
  482. If m_locked Then
  483. Raise TPTCError.Create('console is not unlocked');
  484. {$ENDIF}
  485. End;