area.pp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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: IPTCConsole;
  15. format: IPTCFormat;
  16. surface: IPTCSurface;
  17. pixels: PDWord;
  18. width, height: Integer;
  19. i: Integer;
  20. x, y, r, g, b: Integer;
  21. area: IPTCArea;
  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. { create console }
  30. console.open('Area example', format);
  31. { create surface half the size of the console }
  32. surface := TPTCSurfaceFactory.CreateNew(console.width div 2, console.height div 2, format);
  33. { setup destination area }
  34. x := console.width div 4;
  35. y := console.height div 4;
  36. area := TPTCAreaFactory.CreateNew(x, y, x + surface.width, y + surface.height);
  37. { loop until a key is pressed }
  38. while not console.KeyPressed do
  39. begin
  40. { lock surface }
  41. pixels := surface.lock;
  42. try
  43. { get surface dimensions }
  44. width := surface.width;
  45. height := surface.height;
  46. { draw random pixels }
  47. for i := 1 to 100 do
  48. begin
  49. { get random position }
  50. x := Random(width);
  51. y := Random(height);
  52. { get random color }
  53. r := Random(256);
  54. g := Random(256);
  55. b := Random(256);
  56. { draw color [r,g,b] at position [x,y] }
  57. pixels[x + y * width] := (r shl 16) + (g shl 8) + b;
  58. end;
  59. finally
  60. { unlock surface }
  61. surface.unlock;
  62. end;
  63. { copy surface to console destination area }
  64. surface.copy(console, surface.area, area);
  65. { update console area }
  66. console.update;
  67. end;
  68. finally
  69. if Assigned(console) then
  70. console.close;
  71. end;
  72. except
  73. on error: TPTCError do
  74. { report error }
  75. error.report;
  76. end;
  77. end.