clip.pp 2.1 KB

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