keybrd2.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Program KeyboardExample2;
  2. {$MODE objfpc}
  3. Uses
  4. ptc;
  5. Var
  6. console : TPTCConsole;
  7. surface : TPTCSurface;
  8. format : TPTCFormat;
  9. color : TPTCColor;
  10. timer : TPTCTimer;
  11. key : TPTCKey;
  12. area : TPTCArea;
  13. x, y, delta : Real;
  14. left, right, up, down : Boolean;
  15. size : Integer;
  16. Done : Boolean;
  17. Begin
  18. left := False;
  19. right := False;
  20. up := False;
  21. down := False;
  22. Try
  23. Try
  24. { create key }
  25. key := TPTCKey.Create;
  26. { create console }
  27. console := TPTCConsole.Create;
  28. { enable key release events }
  29. console.KeyReleaseEnabled := True;
  30. { create format }
  31. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  32. { open the console }
  33. console.open('Keyboard example 2', format);
  34. { create timer }
  35. timer := TPTCTimer.Create;
  36. { create surface matching console dimensions }
  37. surface := TPTCSurface.Create(console.width, console.height, format);
  38. { setup cursor data }
  39. x := surface.width Div 2;
  40. y := surface.height Div 2;
  41. size := surface.width Div 10;
  42. color := TPTCColor.Create(1, 1, 1);
  43. { start timer }
  44. timer.start;
  45. { main loop }
  46. Done := False;
  47. Repeat
  48. { check for key press/release }
  49. While console.KeyPressed Do
  50. Begin
  51. console.ReadKey(key);
  52. Case key.code Of
  53. PTCKEY_LEFT : left := key.press;
  54. PTCKEY_RIGHT : right := key.press;
  55. PTCKEY_UP : up := key.press;
  56. PTCKEY_DOWN : down := key.press;
  57. PTCKEY_ESCAPE : Begin
  58. Done := True;
  59. Break;
  60. End;
  61. End;
  62. End;
  63. { move square }
  64. delta := timer.delta*100;
  65. If left Then
  66. x -= delta;
  67. If right Then
  68. x += delta;
  69. If up Then
  70. y -= delta;
  71. If down Then
  72. y += delta;
  73. { clear surface }
  74. surface.clear;
  75. { setup cursor area }
  76. area := TPTCArea.Create(Trunc(x) - size, Trunc(y) - size, Trunc(x) + size, Trunc(y) + size);
  77. Try
  78. { draw cursor as a quad }
  79. surface.clear(color, area);
  80. Finally
  81. area.Free;
  82. End;
  83. { copy to console }
  84. surface.copy(console);
  85. { update console }
  86. console.update;
  87. Until Done;
  88. Finally
  89. color.Free;
  90. console.close;
  91. console.Free;
  92. surface.Free;
  93. key.Free;
  94. timer.Free;
  95. format.Free;
  96. End;
  97. Except
  98. On error : TPTCError Do
  99. { report error }
  100. error.report;
  101. End;
  102. End.