clip.pp 2.3 KB

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