timer.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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: IPTCConsole;
  15. format: IPTCFormat;
  16. surface: IPTCSurface;
  17. timer: IPTCTimer;
  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. try
  25. try
  26. { create console }
  27. console := TPTCConsoleFactory.CreateNew;
  28. { create format }
  29. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  30. { open the console }
  31. console.open('Timer example', format);
  32. { create surface matching console dimensions }
  33. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  34. { create timer }
  35. timer := TPTCTimerFactory.CreateNew;
  36. { start timer }
  37. timer.start;
  38. { loop until a key is pressed }
  39. while not console.KeyPressed do
  40. begin
  41. { get current time from timer }
  42. time := timer.time;
  43. { clear surface }
  44. surface.clear;
  45. { lock surface }
  46. pixels := surface.lock;
  47. try
  48. { get surface dimensions }
  49. width := surface.width;
  50. height := surface.height;
  51. { sine curve parameters }
  52. repeats := 2;
  53. center := height / 2;
  54. magnitude := height / 3;
  55. { render a sine curve }
  56. for x := 0 to width - 1 do
  57. begin
  58. { rescale 'x' in the range [0,2*pi] }
  59. sx := x / width * 2 * pi;
  60. { calculate time at current position }
  61. t := time + sx * repeats;
  62. { lookup sine intensity at time 't' }
  63. intensity := sin(t);
  64. { convert intensity to a y position on the surface }
  65. y := Trunc(center + intensity * magnitude);
  66. { plot pixel on sine curve }
  67. pixels[x + y * width] := $000000FF;
  68. end;
  69. finally
  70. { unlock surface }
  71. surface.unlock;
  72. end;
  73. { copy to console }
  74. surface.copy(console);
  75. { update console }
  76. console.update;
  77. end;
  78. finally
  79. if Assigned(console) then
  80. console.close;
  81. end;
  82. except
  83. on error: TPTCError do
  84. { report error }
  85. error.report;
  86. end;
  87. end.