buffer.pp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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;
  15. format : TPTCFormat;
  16. palette : TPTCPalette;
  17. width, height : Integer;
  18. pixels : Pint32;
  19. x, y, r, g, b : Integer;
  20. i : Integer;
  21. Begin
  22. pixels := Nil;
  23. format := Nil;
  24. palette := Nil;
  25. console := Nil;
  26. Try
  27. Try
  28. { create console }
  29. console := TPTCConsole.Create;
  30. { create format }
  31. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  32. { open the console }
  33. console.open('Buffer example', format);
  34. { get console dimensions }
  35. width := console.width;
  36. height := console.height;
  37. { allocate a buffer of pixels }
  38. pixels := GetMem(width * height * SizeOf(int32));
  39. palette := TPTCPalette.Create;
  40. { loop until a key is pressed }
  41. While Not console.KeyPressed Do
  42. Begin
  43. { draw random pixels }
  44. For i := 1 To 100 Do
  45. Begin
  46. { get random position }
  47. x := Random(width);
  48. y := Random(height);
  49. { get random color }
  50. r := Random(256);
  51. g := Random(256);
  52. b := Random(256);
  53. { draw color [r,g,b] at position [x,y] }
  54. pixels[x + y * width] := (r Shl 16) Or (g Shl 8) Or b;
  55. End;
  56. { load pixels to console }
  57. console.load(pixels, width, height, width * 4, format, palette);
  58. { update console }
  59. console.update;
  60. End;
  61. Finally
  62. { free pixels buffer }
  63. If Assigned(pixels) Then
  64. FreeMem(pixels);
  65. console.close;
  66. palette.Free;
  67. format.Free;
  68. console.Free;
  69. End;
  70. Except
  71. On error : TPTCError Do
  72. { report error }
  73. error.report;
  74. End;
  75. End.