random.pp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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: TPTCConsole = nil;
  15. surface: TPTCSurface = nil;
  16. format: TPTCFormat = nil;
  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 := TPTCConsole.Create;
  26. { create format }
  27. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  28. { open the console }
  29. console.open('Random example', format);
  30. { create surface matching console dimensions }
  31. surface := TPTCSurface.Create(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. console.close;
  65. console.Free;
  66. surface.Free;
  67. format.Free;
  68. end;
  69. except
  70. on error: TPTCError do
  71. { report error }
  72. error.report;
  73. end;
  74. end.