buffer.pp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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: TPTCConsole = nil;
  15. format: TPTCFormat = nil;
  16. palette: TPTCPalette = nil;
  17. width, height: Integer;
  18. pixels: PUint32 = nil;
  19. x, y, r, g, b: Integer;
  20. i: Integer;
  21. begin
  22. try
  23. try
  24. { create console }
  25. console := TPTCConsole.Create;
  26. { create format }
  27. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  28. { open the console }
  29. console.open('Buffer example', format);
  30. { get console dimensions }
  31. width := console.width;
  32. height := console.height;
  33. { allocate a buffer of pixels }
  34. pixels := GetMem(width * height * SizeOf(Uint32));
  35. FillChar(pixels^, width * height * SizeOf(Uint32), 0);
  36. palette := TPTCPalette.Create;
  37. { loop until a key is pressed }
  38. while not console.KeyPressed do
  39. begin
  40. { draw random pixels }
  41. for i := 1 to 100 do
  42. begin
  43. { get random position }
  44. x := Random(width);
  45. y := Random(height);
  46. { get random color }
  47. r := Random(256);
  48. g := Random(256);
  49. b := Random(256);
  50. { draw color [r,g,b] at position [x,y] }
  51. pixels[x + y * width] := (r shl 16) or (g shl 8) or b;
  52. end;
  53. { load pixels to console }
  54. console.load(pixels, width, height, width * 4, format, palette);
  55. { update console }
  56. console.update;
  57. end;
  58. finally
  59. { free pixels buffer }
  60. FreeMem(pixels);
  61. console.close;
  62. palette.Free;
  63. format.Free;
  64. console.Free;
  65. end;
  66. except
  67. on error: TPTCError do
  68. { report error }
  69. error.report;
  70. end;
  71. end.