random.pp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Random 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 RandomExample;
  10. {$MODE objfpc}
  11. uses
  12. ptc;
  13. var
  14. console: IPTCConsole;
  15. surface: IPTCSurface;
  16. format: IPTCFormat;
  17. pixels: PUint32;
  18. width, height: Integer;
  19. i: Integer;
  20. x, y, r, g, b: Integer;
  21. begin
  22. try
  23. try
  24. { create console }
  25. console := TPTCConsoleFactory.CreateNew;
  26. { create format }
  27. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  28. { open the console }
  29. console.open('Random example', format);
  30. { create surface matching console dimensions }
  31. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  32. { loop until a key is pressed }
  33. while not console.KeyPressed do
  34. begin
  35. { lock surface }
  36. pixels := surface.lock;
  37. try
  38. { get surface dimensions }
  39. width := surface.width;
  40. height := surface.height;
  41. { draw random pixels }
  42. for i := 1 to 100 do
  43. begin
  44. { get random position }
  45. x := Random(width);
  46. y := Random(height);
  47. { get random color }
  48. r := Random(256);
  49. g := Random(256);
  50. b := Random(256);
  51. { draw color [r,g,b] at position [x,y] }
  52. pixels[x + y * width] := (r shl 16) + (g shl 8) + b;
  53. end;
  54. finally
  55. { unlock surface }
  56. surface.unlock;
  57. end;
  58. { copy to console }
  59. surface.copy(console);
  60. { update console }
  61. console.update;
  62. end;
  63. finally
  64. if Assigned(console) then
  65. console.close;
  66. end;
  67. except
  68. on error: TPTCError do
  69. { report error }
  70. error.report;
  71. end;
  72. end.