timer.pp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Timer example for OpenPTC 1.0 C++ Implementation
  6. Copyright (c) Glenn Fiedler ([email protected])
  7. This source code is in the public domain
  8. }
  9. Program TimerExample;
  10. {$MODE objfpc}
  11. Uses
  12. ptc;
  13. Var
  14. console : TPTCConsole;
  15. format : TPTCFormat;
  16. surface : TPTCSurface;
  17. timer : TPTCTimer;
  18. time, t : Double;
  19. pixels : PDWord;
  20. width, height : Integer;
  21. repeats, center, magnitude, intensity, sx : Single;
  22. x, y : Integer;
  23. Begin
  24. timer := Nil;
  25. format := Nil;
  26. surface := Nil;
  27. console := Nil;
  28. Try
  29. Try
  30. { create console }
  31. console := TPTCConsole.Create;
  32. { create format }
  33. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  34. { open the console }
  35. console.open('Timer example', format);
  36. { create surface matching console dimensions }
  37. surface := TPTCSurface.Create(console.width, console.height, format);
  38. { create timer }
  39. timer := TPTCTimer.Create;
  40. { start timer }
  41. timer.start;
  42. { loop until a key is pressed }
  43. While Not console.KeyPressed Do
  44. Begin
  45. { get current time from timer }
  46. time := timer.time;
  47. { clear surface }
  48. surface.clear;
  49. { lock surface }
  50. pixels := surface.lock;
  51. Try
  52. { get surface dimensions }
  53. width := surface.width;
  54. height := surface.height;
  55. { sine curve parameters }
  56. repeats := 2;
  57. center := height / 2;
  58. magnitude := height / 3;
  59. { render a sine curve }
  60. For x := 0 To width - 1 Do
  61. Begin
  62. { rescale 'x' in the range [0,2*pi] }
  63. sx := x / width * 2 * pi;
  64. { calculate time at current position }
  65. t := time + sx * repeats;
  66. { lookup sine intensity at time 't' }
  67. intensity := sin(t);
  68. { convert intensity to a y position on the surface }
  69. y := Trunc(center + intensity * magnitude);
  70. { plot pixel on sine curve }
  71. pixels[x + y * width] := $000000FF;
  72. End;
  73. Finally
  74. { unlock surface }
  75. surface.unlock;
  76. End;
  77. { copy to console }
  78. surface.copy(console);
  79. { update console }
  80. console.update;
  81. End;
  82. Finally
  83. timer.Free;
  84. surface.Free;
  85. console.close;
  86. console.Free;
  87. format.Free;
  88. End;
  89. Except
  90. On error : TPTCError Do
  91. { report error }
  92. error.report;
  93. End;
  94. End.