gdiconsolei.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. Constructor TGDIConsole.Create;
  2. Begin
  3. Inherited Create;
  4. FDefaultWidth := 320;
  5. FDefaultHeight := 200;
  6. FDefaultFormat := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  7. FCopy := TPTCCopy.Create;
  8. FClear := TPTCClear.Create;
  9. FArea := TPTCArea.Create;
  10. FClip := TPTCArea.Create;
  11. FPalette := TPTCPalette.Create;
  12. FOpen := False;
  13. { configure console }
  14. Configure('ptcpas.cfg');
  15. End;
  16. Destructor TGDIConsole.Destroy;
  17. Begin
  18. Close;
  19. {...}
  20. FWin32DIB.Free;
  21. FWindow.Free;
  22. FPalette.Free;
  23. FEventQueue.Free;
  24. FCopy.Free;
  25. FClear.Free;
  26. FArea.Free;
  27. FClip.Free;
  28. FDefaultFormat.Free;
  29. Inherited Destroy;
  30. End;
  31. Procedure TGDIConsole.Open(Const ATitle : String; APages : Integer = 0);
  32. Begin
  33. Open(ATitle, FDefaultFormat, APages);
  34. End;
  35. Procedure TGDIConsole.Open(Const ATitle : String; Const AFormat : TPTCFormat;
  36. APages : Integer = 0);
  37. Begin
  38. Open(ATitle, FDefaultWidth, FDefaultHeight, AFormat, APages);
  39. End;
  40. Procedure TGDIConsole.Open(Const ATitle : String; Const AMode : TPTCMode;
  41. APages : Integer = 0);
  42. Begin
  43. Open(ATitle, AMode.Width, AMode.Height, AMode.Format, APages);
  44. End;
  45. Procedure TGDIConsole.Open(Const ATitle : String; AWidth, AHeight : Integer;
  46. Const AFormat : TPTCFormat; APages : Integer = 0);
  47. Var
  48. tmp : TPTCArea;
  49. Begin
  50. If FOpen Then
  51. Close;
  52. (* FWindow := TWin32Window.Create('PTC_GDI_FULLSCREEN',
  53. ATitle,
  54. WS_EX_TOPMOST,
  55. DWord(WS_POPUP Or WS_SYSMENU Or WS_VISIBLE), // fpc windows RTL bug - WS_POPUP should be a DWord!!!
  56. SW_NORMAL,
  57. 0, 0,
  58. GetSystemMetrics(SM_CXSCREEN),
  59. GetSystemMetrics(SM_CYSCREEN),
  60. False, False);*)
  61. FWindow := TWin32Window.Create('PTC_GDI_WINDOWED_FIXED',
  62. ATitle,
  63. 0,
  64. WS_VISIBLE Or WS_SYSMENU Or WS_CAPTION Or WS_MINIMIZEBOX,
  65. SW_NORMAL,
  66. CW_USEDEFAULT, CW_USEDEFAULT,
  67. AWidth, AHeight,
  68. {m_center_window}False,
  69. False);
  70. (* FWindow := TWin32Window.Create('PTC_GDI_WINDOWED_RESIZABLE',
  71. ATitle,
  72. 0,
  73. WS_OVERLAPPEDWINDOW Or WS_VISIBLE,
  74. SW_NORMAL,
  75. CW_USEDEFAULT, CW_USEDEFAULT,
  76. AWidth, AHeight,
  77. {m_center_window}False,
  78. False);*)
  79. FWin32DIB := TWin32DIB.Create(AWidth, AHeight);
  80. FreeAndNil(FKeyboard);
  81. FreeAndNil(FMouse);
  82. FreeAndNil(FEventQueue);
  83. FEventQueue := TEventQueue.Create;
  84. FKeyboard := TWin32Keyboard.Create(FWindow.Handle, FWindow.Thread, False, FEventQueue);
  85. FMouse := TWin32Mouse.Create(FWindow.Handle, FWindow.Thread, False, FEventQueue, {FFullScreen}False, AWidth, AHeight);
  86. tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
  87. Try
  88. FArea.Assign(tmp);
  89. FClip.Assign(tmp);
  90. Finally
  91. tmp.Free;
  92. End;
  93. FWindow.Update;
  94. FTitle := ATitle;
  95. FOpen := True;
  96. End;
  97. Procedure TGDIConsole.Close;
  98. Begin
  99. If Not FOpen Then
  100. Exit;
  101. {...}
  102. FreeAndNil(FKeyboard);
  103. FreeAndNil(FMouse);
  104. FreeAndNil(FWin32DIB);
  105. FreeAndNil(FWindow);
  106. FreeAndNil(FEventQueue);
  107. FTitle := '';
  108. FOpen := False;
  109. End;
  110. Procedure TGDIConsole.Copy(Var ASurface : TPTCBaseSurface);
  111. Begin
  112. // todo...
  113. End;
  114. Procedure TGDIConsole.Copy(Var ASurface : TPTCBaseSurface;
  115. Const ASource, ADestination : TPTCArea);
  116. Begin
  117. // todo...
  118. End;
  119. Procedure TGDIConsole.Load(Const APixels : Pointer;
  120. AWidth, AHeight, APitch : Integer;
  121. Const AFormat : TPTCFormat;
  122. Const APalette : TPTCPalette);
  123. Var
  124. Area_ : TPTCArea;
  125. console_pixels : Pointer;
  126. Begin
  127. CheckOpen( 'TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
  128. CheckUnlocked('TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette)');
  129. If Clip.Equals(Area) Then
  130. Begin
  131. Try
  132. console_pixels := Lock;
  133. Try
  134. FCopy.Request(AFormat, Format);
  135. FCopy.Palette(APalette, Palette);
  136. FCopy.Copy(APixels, 0, 0, AWidth, AHeight, APitch, console_pixels, 0, 0,
  137. Width, Height, Pitch);
  138. Finally
  139. Unlock;
  140. End;
  141. Except
  142. On error : TPTCError Do
  143. Raise TPTCError.Create('failed to load pixels to console', error);
  144. End;
  145. End
  146. Else
  147. Begin
  148. Area_ := TPTCArea.Create(0, 0, width, height);
  149. Try
  150. Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, Area_, Area);
  151. Finally
  152. Area_.Free;
  153. End;
  154. End;
  155. End;
  156. Procedure TGDIConsole.Load(Const APixels : Pointer;
  157. AWidth, AHeight, APitch : Integer;
  158. Const AFormat : TPTCFormat;
  159. Const APalette : TPTCPalette;
  160. Const ASource, ADestination : TPTCArea);
  161. Var
  162. console_pixels : Pointer;
  163. clipped_source, clipped_destination : TPTCArea;
  164. tmp : TPTCArea;
  165. Begin
  166. CheckOpen( 'TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
  167. CheckUnlocked('TGDIConsole.Load(APixels, AWidth, AHeight, APitch, AFormat, APalette, ASource, ADestination)');
  168. clipped_source := Nil;
  169. clipped_destination := Nil;
  170. Try
  171. console_pixels := Lock;
  172. Try
  173. clipped_source := TPTCArea.Create;
  174. clipped_destination := TPTCArea.Create;
  175. tmp := TPTCArea.Create(0, 0, AWidth, AHeight);
  176. Try
  177. TPTCClipper.Clip(ASource, tmp, clipped_source, ADestination, Clip, clipped_destination);
  178. Finally
  179. tmp.Free;
  180. End;
  181. FCopy.request(AFormat, Format);
  182. FCopy.palette(APalette, Palette);
  183. FCopy.copy(APixels, clipped_source.left, clipped_source.top, clipped_source.width, clipped_source.height, APitch,
  184. console_pixels, clipped_destination.left, clipped_destination.top, clipped_destination.width, clipped_destination.height, Pitch);
  185. Finally
  186. Unlock;
  187. clipped_source.Free;
  188. clipped_destination.Free;
  189. End;
  190. Except
  191. On error : TPTCError Do
  192. Raise TPTCError.Create('failed to load pixels to console area', error);
  193. End;
  194. End;
  195. Procedure TGDIConsole.Save(APixels : Pointer;
  196. AWidth, AHeight, APitch : Integer;
  197. Const AFormat : TPTCFormat;
  198. Const APalette : TPTCPalette);
  199. Begin
  200. // todo...
  201. End;
  202. Procedure TGDIConsole.Save(APixels : Pointer;
  203. AWidth, AHeight, APitch : Integer;
  204. Const AFormat : TPTCFormat;
  205. Const APalette : TPTCPalette;
  206. Const ASource, ADestination : TPTCArea);
  207. Begin
  208. // todo...
  209. End;
  210. Function TGDIConsole.Lock : Pointer;
  211. Begin
  212. Result := FWin32DIB.Pixels; // todo...
  213. FLocked := True;
  214. End;
  215. Procedure TGDIConsole.Unlock;
  216. Begin
  217. FLocked := False;
  218. End;
  219. Procedure TGDIConsole.Clear;
  220. Begin
  221. // todo...
  222. End;
  223. Procedure TGDIConsole.Clear(Const AColor : TPTCColor);
  224. Begin
  225. // todo...
  226. End;
  227. Procedure TGDIConsole.Clear(Const AColor : TPTCColor;
  228. Const AArea : TPTCArea);
  229. Begin
  230. // todo...
  231. End;
  232. Procedure TGDIConsole.Configure(Const AFileName : String);
  233. Var
  234. F : Text;
  235. S : String;
  236. Begin
  237. AssignFile(F, AFileName);
  238. {$I-}
  239. Reset(F);
  240. {$I+}
  241. If IOResult <> 0 Then
  242. Exit;
  243. While Not EoF(F) Do
  244. Begin
  245. {$I-}
  246. Readln(F, S);
  247. {$I+}
  248. If IOResult <> 0 Then
  249. Break;
  250. Option(S);
  251. End;
  252. CloseFile(F);
  253. End;
  254. Function TGDIConsole.Option(Const AOption : String) : Boolean;
  255. Begin
  256. // todo...
  257. Result := FCopy.Option(AOption);
  258. End;
  259. Procedure TGDIConsole.Palette(Const APalette : TPTCPalette);
  260. Begin
  261. // todo...
  262. End;
  263. Procedure TGDIConsole.Clip(Const AArea : TPTCArea);
  264. Var
  265. tmp : TPTCArea;
  266. Begin
  267. CheckOpen('TGDIConsole.Clip(AArea)');
  268. tmp := TPTCClipper.Clip(AArea, FArea);
  269. Try
  270. FClip.Assign(tmp);
  271. Finally
  272. tmp.Free;
  273. End;
  274. End;
  275. Function TGDIConsole.Clip : TPTCArea;
  276. Begin
  277. CheckOpen('TGDIConsole.Clip');
  278. Result := FClip;
  279. End;
  280. Function TGDIConsole.Palette : TPTCPalette;
  281. Begin
  282. CheckOpen('TGDIConsole.Palette');
  283. Result := FPalette;
  284. End;
  285. Function TGDIConsole.Modes : PPTCMode;
  286. Begin
  287. // todo...
  288. Result := Nil;
  289. End;
  290. Procedure TGDIConsole.Flush;
  291. Begin
  292. CheckOpen( 'TGDIConsole.Flush');
  293. CheckUnlocked('TGDIConsole.Flush');
  294. // todo...
  295. End;
  296. Procedure TGDIConsole.Finish;
  297. Begin
  298. CheckOpen( 'TGDIConsole.Finish');
  299. CheckUnlocked('TGDIConsole.Finish');
  300. // todo...
  301. End;
  302. Procedure TGDIConsole.Update;
  303. Var
  304. ClientRect : RECT;
  305. DeviceContext : HDC;
  306. Begin
  307. CheckOpen( 'TGDIConsole.Update');
  308. CheckUnlocked('TGDIConsole.Update');
  309. FWindow.Update;
  310. DeviceContext := GetDC(FWindow.m_window);
  311. If DeviceContext <> 0 Then
  312. Begin
  313. If GetClientRect(FWindow.m_window, @ClientRect) Then
  314. Begin
  315. StretchDIBits(DeviceContext,
  316. 0, 0, ClientRect.right, ClientRect.bottom,
  317. 0, 0, FWin32DIB.Width, FWin32DIB.Height,
  318. FWin32DIB.Pixels,
  319. FWin32DIB.BMI^,
  320. DIB_RGB_COLORS,
  321. SRCCOPY);
  322. End;
  323. ReleaseDC(FWindow.m_window, DeviceContext);
  324. End;
  325. End;
  326. Procedure TGDIConsole.Update(Const AArea : TPTCArea);
  327. Begin
  328. Update;
  329. End;
  330. Function TGDIConsole.NextEvent(Var AEvent : TPTCEvent; AWait : Boolean; Const AEventMask : TPTCEventMask) : Boolean;
  331. Begin
  332. CheckOpen('TGDIConsole.NextEvent');
  333. // CheckUnlocked('TGDIConsole.NextEvent');
  334. FreeAndNil(AEvent);
  335. Repeat
  336. { update window }
  337. FWindow.Update;
  338. { try to find an event that matches the EventMask }
  339. AEvent := FEventQueue.NextEvent(AEventMask);
  340. Until (Not AWait) Or (AEvent <> Nil);
  341. Result := AEvent <> Nil;
  342. End;
  343. Function TGDIConsole.PeekEvent(AWait : Boolean; Const AEventMask : TPTCEventMask) : TPTCEvent;
  344. Begin
  345. CheckOpen('TGDIConsole.PeekEvent');
  346. // CheckUnlocked('TGDIConsole.PeekEvent');
  347. Repeat
  348. { update window }
  349. FWindow.Update;
  350. { try to find an event that matches the EventMask }
  351. Result := FEventQueue.PeekEvent(AEventMask);
  352. Until (Not AWait) Or (Result <> Nil);
  353. End;
  354. Function TGDIConsole.GetWidth : Integer;
  355. Begin
  356. CheckOpen('TGDIConsole.GetWidth');
  357. Result := FWin32DIB.Width;
  358. End;
  359. Function TGDIConsole.GetHeight : Integer;
  360. Begin
  361. CheckOpen('TGDIConsole.GetHeight');
  362. Result := FWin32DIB.Height;
  363. End;
  364. Function TGDIConsole.GetPitch : Integer;
  365. Begin
  366. CheckOpen('TGDIConsole.GetPitch');
  367. Result := FWin32DIB.Pitch;
  368. End;
  369. Function TGDIConsole.GetArea : TPTCArea;
  370. Begin
  371. CheckOpen('TGDIConsole.GetArea');
  372. Result := FArea;
  373. End;
  374. Function TGDIConsole.GetFormat : TPTCFormat;
  375. Begin
  376. CheckOpen('TGDIConsole.GetFormat');
  377. Result := FWin32DIB.Format;
  378. End;
  379. Function TGDIConsole.GetPages : Integer;
  380. Begin
  381. CheckOpen('TGDIConsole.GetPages');
  382. Result := 2;
  383. End;
  384. Function TGDIConsole.GetName : String;
  385. Begin
  386. Result := 'GDI';
  387. End;
  388. Function TGDIConsole.GetTitle : String;
  389. Begin
  390. CheckOpen('TGDIConsole.GetTitle');
  391. Result := FTitle;
  392. End;
  393. Function TGDIConsole.GetInformation : String;
  394. Begin
  395. CheckOpen('TGDIConsole.GetInformation');
  396. Result := ''; // todo...
  397. End;
  398. Procedure TGDIConsole.CheckOpen(AMessage : String);
  399. Begin
  400. If Not FOpen Then
  401. Try
  402. Raise TPTCError.Create('console is not open');
  403. Except
  404. On error : TPTCError Do
  405. Raise TPTCError.Create(AMessage, error);
  406. End;
  407. End;
  408. Procedure TGDIConsole.CheckUnlocked(AMessage : String);
  409. Begin
  410. If FLocked Then
  411. Try
  412. Raise TPTCError.Create('console is locked');
  413. Except
  414. On error : TPTCError Do
  415. Raise TPTCError.Create(AMessage, error);
  416. End;
  417. End;