keyboard2.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. {
  2. Keyboard example for the PTCPas library
  3. This source code is in the public domain
  4. }
  5. program KeyboardExample2;
  6. {$MODE objfpc}
  7. uses
  8. ptc;
  9. var
  10. console: IPTCConsole;
  11. surface: IPTCSurface;
  12. format: IPTCFormat;
  13. color: IPTCColor;
  14. timer: IPTCTimer;
  15. key: IPTCKeyEvent;
  16. x, y, delta: Real;
  17. left, right, up, down: Boolean;
  18. size: Integer;
  19. Done: Boolean;
  20. begin
  21. left := False;
  22. right := False;
  23. up := False;
  24. down := False;
  25. try
  26. try
  27. { create console }
  28. console := TPTCConsoleFactory.CreateNew;
  29. { enable key release events }
  30. console.KeyReleaseEnabled := True;
  31. { create format }
  32. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  33. { open the console }
  34. console.open('Keyboard example 2', format);
  35. { create timer }
  36. timer := TPTCTimerFactory.CreateNew;
  37. { create surface matching console dimensions }
  38. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  39. { setup cursor data }
  40. x := surface.width div 2;
  41. y := surface.height div 2;
  42. size := surface.width div 10;
  43. color := TPTCColorFactory.CreateNew(1, 1, 1);
  44. { start timer }
  45. timer.start;
  46. { main loop }
  47. Done := False;
  48. repeat
  49. { check for key press/release }
  50. while console.KeyPressed do
  51. begin
  52. console.ReadKey(key);
  53. case key.code of
  54. PTCKEY_LEFT: left := key.press;
  55. PTCKEY_RIGHT: right := key.press;
  56. PTCKEY_UP: up := key.press;
  57. PTCKEY_DOWN: down := key.press;
  58. PTCKEY_ESCAPE: begin
  59. Done := True;
  60. Break;
  61. end;
  62. end;
  63. end;
  64. { move square }
  65. delta := timer.delta*100;
  66. if left then
  67. x := x - delta;
  68. if right then
  69. x := x + delta;
  70. if up then
  71. y := y - delta;
  72. if down then
  73. y := y + delta;
  74. { clear surface }
  75. surface.clear;
  76. { draw cursor as a quad }
  77. surface.clear(color, TPTCAreaFactory.CreateNew(Trunc(x) - size, Trunc(y) - size, Trunc(x) + size, Trunc(y) + size));
  78. { copy to console }
  79. surface.copy(console);
  80. { update console }
  81. console.update;
  82. until Done;
  83. finally
  84. if Assigned(console) then
  85. console.close;
  86. end;
  87. except
  88. on error: TPTCError do
  89. { report error }
  90. error.report;
  91. end;
  92. end.