area.pp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Area 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 AreaExample;
  10. {$MODE objfpc}
  11. Uses
  12. ptc;
  13. Var
  14. console : TPTCConsole;
  15. format : TPTCFormat;
  16. surface : TPTCSurface;
  17. pixels : PDWord;
  18. width, height : Integer;
  19. i : Integer;
  20. x, y, r, g, b : Integer;
  21. area : TPTCArea;
  22. Begin
  23. area := Nil;
  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. { create console }
  34. console.open('Area example', format);
  35. { create surface half the size of the console }
  36. surface := TPTCSurface.Create(console.width Div 2, console.height Div 2, format);
  37. { setup destination area }
  38. x := console.width Div 4;
  39. y := console.height Div 4;
  40. area := TPTCArea.Create(x, y, x + surface.width, y + surface.height);
  41. { loop until a key is pressed }
  42. While Not console.KeyPressed Do
  43. Begin
  44. { lock surface }
  45. pixels := surface.lock;
  46. Try
  47. { get surface dimensions }
  48. width := surface.width;
  49. height := surface.height;
  50. { draw random pixels }
  51. For i := 1 To 100 Do
  52. Begin
  53. { get random position }
  54. x := Random(width);
  55. y := Random(height);
  56. { get random color }
  57. r := Random(256);
  58. g := Random(256);
  59. b := Random(256);
  60. { draw color [r,g,b] at position [x,y] }
  61. pixels[x + y * width] := (r Shl 16) + (g Shl 8) + b;
  62. End;
  63. Finally
  64. { unlock surface }
  65. surface.unlock;
  66. End;
  67. { copy surface to console destination area }
  68. surface.copy(console, surface.area, area);
  69. { update console area }
  70. console.update;
  71. End;
  72. Finally
  73. console.close;
  74. console.Free;
  75. surface.Free;
  76. format.Free;
  77. area.Free;
  78. End;
  79. Except
  80. On error : TPTCError Do
  81. { report error }
  82. error.report;
  83. End;
  84. End.