buffer.pp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Buffer 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 BufferExample;
  10. {$MODE objfpc}
  11. uses
  12. ptc;
  13. var
  14. console: IPTCConsole;
  15. format: IPTCFormat;
  16. width, height: Integer;
  17. pixels: PUint32 = nil;
  18. x, y, r, g, b: Integer;
  19. i: Integer;
  20. begin
  21. try
  22. try
  23. { create console }
  24. console := TPTCConsoleFactory.CreateNew;
  25. { create format }
  26. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  27. { open the console }
  28. console.Open('Buffer example', format);
  29. { get console dimensions }
  30. width := console.Width;
  31. height := console.Height;
  32. { allocate a buffer of pixels }
  33. pixels := GetMem(width * height * SizeOf(Uint32));
  34. FillChar(pixels^, width * height * SizeOf(Uint32), 0);
  35. { loop until a key is pressed }
  36. while not console.KeyPressed do
  37. begin
  38. { draw random pixels }
  39. for i := 1 to 100 do
  40. begin
  41. { get random position }
  42. x := Random(width);
  43. y := Random(height);
  44. { get random color }
  45. r := Random(256);
  46. g := Random(256);
  47. b := Random(256);
  48. { draw color [r,g,b] at position [x,y] }
  49. pixels[x + y * width] := (r shl 16) or (g shl 8) or b;
  50. end;
  51. { load pixels to console }
  52. console.Load(pixels, width, height, width * 4, format, TPTCPaletteFactory.CreateNew);
  53. { update console }
  54. console.Update;
  55. end;
  56. finally
  57. { free pixels buffer }
  58. FreeMem(pixels);
  59. if Assigned(console) then
  60. console.close;
  61. end;
  62. except
  63. on error: TPTCError do
  64. { report error }
  65. error.report;
  66. end;
  67. end.