console.pp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Console 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 ConsoleExample;
  10. {$MODE objfpc}
  11. Uses
  12. ptc;
  13. Var
  14. console : TPTCConsole;
  15. palette : TPTCPalette;
  16. data : Array[0..255] Of DWord;
  17. i : Integer;
  18. pixels : PByte;
  19. width, height, pitch : Integer;
  20. format : TPTCFormat;
  21. bits, bytes : Integer;
  22. x, y : Integer;
  23. color : DWord;
  24. pixel : PByte;
  25. _data : PByte;
  26. Begin
  27. Try
  28. { create console }
  29. console := TPTCConsole.Create;
  30. { open the console with one page }
  31. console.open('Console example', 1);
  32. { create palette }
  33. palette := TPTCPalette.Create;
  34. { generate palette }
  35. For i := 0 To 255 Do
  36. data[i] := i;
  37. { load palette data }
  38. palette.load(data);
  39. { set console palette }
  40. console.palette(palette);
  41. { loop until a key is pressed }
  42. While Not console.KeyPressed Do
  43. Begin
  44. { lock console }
  45. pixels := console.lock;
  46. { get console dimensions }
  47. width := console.width;
  48. height := console.height;
  49. pitch := console.pitch;
  50. { get console format }
  51. format := console.format;
  52. { get format information }
  53. bits := format.bits;
  54. bytes := format.bytes;
  55. { draw random pixels }
  56. For i := 1 To 100 Do
  57. Begin
  58. { get random position }
  59. x := Random(width);
  60. y := Random(height);
  61. { generate random color integer }
  62. color := (Random(256) Shl 0) Or
  63. (Random(256) Shl 8) Or
  64. (Random(256) Shl 16) Or
  65. (Random(256) Shl 24);
  66. { calculate pointer to pixel [x,y] }
  67. pixel := pixels + y * pitch + x * bytes;
  68. { check bits }
  69. Case bits Of
  70. { 32 bits per pixel }
  71. 32 : PDWord(pixel)^ := color;
  72. 24 : Begin
  73. { 24 bits per pixel }
  74. _data := pixel;
  75. _data[0] := (color And $000000FF) Shr 0;
  76. _data[1] := (color And $0000FF00) Shr 8;
  77. _data[2] := (color And $00FF0000) Shr 16;
  78. End;
  79. { 16 bits per pixel }
  80. 16 : PWord(pixel)^ := color;
  81. { 8 bits per pixel }
  82. 8 : PByte(pixel)^ := color;
  83. End;
  84. End;
  85. { unlock console }
  86. console.unlock;
  87. { update console }
  88. console.update;
  89. End;
  90. palette.Free;
  91. console.close;
  92. console.Free;
  93. Except
  94. On error : TPTCError Do
  95. { report error }
  96. error.report;
  97. End;
  98. End.