clip.pp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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: TPTCConsole = nil;
  15. surface: TPTCSurface = nil;
  16. format: TPTCFormat = nil;
  17. area: TPTCArea;
  18. x1, y1, x2, y2: Integer;
  19. pixels: PUint32;
  20. width, height: Integer;
  21. i: Integer;
  22. x, y, r, g, b: Integer;
  23. begin
  24. try
  25. try
  26. { create console }
  27. console := TPTCConsole.Create;
  28. { create format }
  29. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  30. { open the console }
  31. console.open('Clip example', format);
  32. { create surface matching console dimensions }
  33. surface := TPTCSurface.Create(console.width, console.height, format);
  34. { calculate clip coordinates }
  35. x1 := console.width div 4;
  36. y1 := console.height div 4;
  37. x2 := console.width - x1;
  38. y2 := console.height - y1;
  39. { setup clip area }
  40. area := TPTCArea.Create(x1, y1, x2, y2);
  41. try
  42. { set clip area }
  43. console.clip(area);
  44. finally
  45. area.Free;
  46. end;
  47. { loop until a key is pressed }
  48. while not console.KeyPressed do
  49. begin
  50. { lock surface }
  51. pixels := surface.lock;
  52. try
  53. { get surface dimensions }
  54. width := surface.width;
  55. height := surface.height;
  56. { draw random pixels }
  57. for i := 1 to 100 do
  58. begin
  59. { get random position }
  60. x := Random(width);
  61. y := Random(height);
  62. { get random color }
  63. r := Random(256);
  64. g := Random(256);
  65. b := Random(256);
  66. { draw color [r,g,b] at position [x,y] }
  67. pixels[x + y * width] := (r shl 16) + (g shl 8) + b;
  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. console.close;
  80. console.Free;
  81. surface.Free;
  82. format.Free;
  83. end;
  84. except
  85. on error: TPTCError do
  86. { report error }
  87. error.report;
  88. end;
  89. end.