mouse.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. {
  2. Mouse example for the PTCPas library
  3. This source code is in the public domain
  4. }
  5. program MouseExample;
  6. {$MODE objfpc}
  7. uses
  8. ptc;
  9. var
  10. console: TPTCConsole = nil;
  11. surface: TPTCSurface = nil;
  12. format: TPTCFormat = nil;
  13. event: TPTCEvent = nil;
  14. pixels: PUint32;
  15. color: Uint32;
  16. width, height: Integer;
  17. I: Integer;
  18. X, Y: Integer;
  19. button: Boolean;
  20. Done: Boolean = False;
  21. begin
  22. try
  23. try
  24. { create console }
  25. console := TPTCConsole.Create;
  26. { create format }
  27. format := TPTCFormat.Create(32, $FF0000, $FF00, $FF);
  28. { open the console }
  29. console.open('Mouse example', format);
  30. { we're going to draw our own cursor, so disable the default cursor }
  31. console.option('hide cursor');
  32. { create surface matching console dimensions }
  33. surface := TPTCSurface.Create(console.width, console.height, format);
  34. { initialization }
  35. X := 0;
  36. Y := 0;
  37. repeat
  38. { wait for events }
  39. console.NextEvent(event, True, PTCAnyEvent);
  40. { handle mouse events }
  41. if event is TPTCMouseEvent then
  42. begin
  43. { if there's more than one mouse event, process them all... }
  44. repeat
  45. X := (event as TPTCMouseEvent).X;
  46. Y := (event as TPTCMouseEvent).Y;
  47. button := PTCMouseButton1 in (event as TPTCMouseEvent).ButtonState;
  48. until not console.NextEvent(event, False, [PTCMouseEvent]);
  49. end;
  50. { handle keyboard events }
  51. if (event is TPTCKeyEvent) and (event as TPTCKeyEvent).Press then
  52. begin
  53. case (event as TPTCKeyEvent).Code of
  54. PTCKEY_G: console.Option('grab mouse');
  55. PTCKEY_U: console.Option('ungrab mouse');
  56. PTCKEY_ESCAPE: Done := True;
  57. end;
  58. end;
  59. { clear surface }
  60. surface.clear;
  61. { lock surface }
  62. pixels := surface.lock;
  63. try
  64. { get surface dimensions }
  65. width := surface.width;
  66. height := surface.height;
  67. if button then
  68. color := $00FF00 { green cursor, if button 1 is pressed }
  69. else
  70. color := $FFFFFF; { white cursor if button 1 is not pressed }
  71. { draw a small cross for a cursor }
  72. for I := 2 to 10 do
  73. begin
  74. if (X - I) >= 0 then
  75. pixels[X - I + Y * width] := color;
  76. if (X + I) < width then
  77. pixels[X + I + Y * width] := color;
  78. if (Y - I) >= 0 then
  79. pixels[X + (Y - I) * width] := color;
  80. if (Y + I) < height then
  81. pixels[X + (Y + I) * width] := color;
  82. end;
  83. finally
  84. { unlock surface }
  85. surface.unlock;
  86. end;
  87. { copy to console }
  88. surface.copy(console);
  89. { update console }
  90. console.update;
  91. until Done;
  92. finally
  93. console.close;
  94. console.Free;
  95. surface.Free;
  96. format.Free;
  97. event.Free;
  98. end;
  99. except
  100. on error: TPTCError do
  101. { report error }
  102. error.report;
  103. end;
  104. end.