random.pp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  15. surface : TPTCSurface;
  16. format : TPTCFormat;
  17. pixels : Pint32;
  18. width, height : Integer;
  19. i : Integer;
  20. x, y, r, g, b : Integer;
  21. Begin
  22. format := Nil;
  23. surface := Nil;
  24. console := Nil;
  25. Try
  26. Try
  27. { create console }
  28. console := TPTCConsole.Create;
  29. { create format }
  30. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  31. { open the console }
  32. console.open('Random example', format);
  33. { create surface matching console dimensions }
  34. surface := TPTCSurface.Create(console.width, console.height, format);
  35. { loop until a key is pressed }
  36. While Not console.KeyPressed Do
  37. Begin
  38. { lock surface }
  39. pixels := surface.lock;
  40. Try
  41. { get surface dimensions }
  42. width := surface.width;
  43. height := surface.height;
  44. { draw random pixels }
  45. For i := 1 To 100 Do
  46. Begin
  47. { get random position }
  48. x := Random(width);
  49. y := Random(height);
  50. { get random color }
  51. r := Random(256);
  52. g := Random(256);
  53. b := Random(256);
  54. { draw color [r,g,b] at position [x,y] }
  55. pixels[x + y * width] := (r Shl 16) + (g Shl 8) + b;
  56. End;
  57. Finally
  58. { unlock surface }
  59. surface.unlock;
  60. End;
  61. { copy to console }
  62. surface.copy(console);
  63. { update console }
  64. console.update;
  65. End;
  66. Finally
  67. console.close;
  68. console.Free;
  69. surface.Free;
  70. format.Free;
  71. End;
  72. Except
  73. On error : TPTCError Do
  74. { report error }
  75. error.report;
  76. End;
  77. End.