keyboard2.pp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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: TPTCConsole = nil;
  11. surface: TPTCSurface = nil;
  12. format: TPTCFormat = nil;
  13. color: TPTCColor = nil;
  14. timer: TPTCTimer = nil;
  15. key: TPTCKeyEvent = nil;
  16. area: TPTCArea;
  17. x, y, delta: Real;
  18. left, right, up, down: Boolean;
  19. size: Integer;
  20. Done: Boolean;
  21. begin
  22. left := False;
  23. right := False;
  24. up := False;
  25. down := False;
  26. try
  27. try
  28. { create key }
  29. key := TPTCKeyEvent.Create;
  30. { create console }
  31. console := TPTCConsole.Create;
  32. { enable key release events }
  33. console.KeyReleaseEnabled := True;
  34. { create format }
  35. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  36. { open the console }
  37. console.open('Keyboard example 2', format);
  38. { create timer }
  39. timer := TPTCTimer.Create;
  40. { create surface matching console dimensions }
  41. surface := TPTCSurface.Create(console.width, console.height, format);
  42. { setup cursor data }
  43. x := surface.width div 2;
  44. y := surface.height div 2;
  45. size := surface.width div 10;
  46. color := TPTCColor.Create(1, 1, 1);
  47. { start timer }
  48. timer.start;
  49. { main loop }
  50. Done := False;
  51. repeat
  52. { check for key press/release }
  53. while console.KeyPressed do
  54. begin
  55. console.ReadKey(key);
  56. case key.code of
  57. PTCKEY_LEFT: left := key.press;
  58. PTCKEY_RIGHT: right := key.press;
  59. PTCKEY_UP: up := key.press;
  60. PTCKEY_DOWN: down := key.press;
  61. PTCKEY_ESCAPE: begin
  62. Done := True;
  63. Break;
  64. end;
  65. end;
  66. end;
  67. { move square }
  68. delta := timer.delta*100;
  69. if left then
  70. x := x - delta;
  71. if right then
  72. x := x + delta;
  73. if up then
  74. y := y - delta;
  75. if down then
  76. y := y + delta;
  77. { clear surface }
  78. surface.clear;
  79. { setup cursor area }
  80. area := TPTCArea.Create(Trunc(x) - size, Trunc(y) - size, Trunc(x) + size, Trunc(y) + size);
  81. try
  82. { draw cursor as a quad }
  83. surface.clear(color, area);
  84. finally
  85. area.Free;
  86. end;
  87. { copy to console }
  88. surface.copy(console);
  89. { update console }
  90. console.update;
  91. until Done;
  92. finally
  93. color.Free;
  94. console.close;
  95. console.Free;
  96. surface.Free;
  97. key.Free;
  98. timer.Free;
  99. format.Free;
  100. end;
  101. except
  102. on error: TPTCError do
  103. { report error }
  104. error.report;
  105. end;
  106. end.