console.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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: IPTCConsole;
  15. palette: IPTCPalette;
  16. data: array [0..255] of DWord;
  17. i: Integer;
  18. pixels: PByte;
  19. width, height, pitch: Integer;
  20. format: IPTCFormat;
  21. bits, bytes: Integer;
  22. x, y: Integer;
  23. color: DWord;
  24. pixel: PByte;
  25. _data: PByte;
  26. begin
  27. try
  28. try
  29. { create console }
  30. console := TPTCConsoleFactory.CreateNew;
  31. { open the console with one page }
  32. console.open('Console example', 1);
  33. { create palette }
  34. palette := TPTCPaletteFactory.CreateNew;
  35. { generate palette }
  36. for i := 0 to 255 do
  37. data[i] := i;
  38. { load palette data }
  39. palette.Load(data);
  40. { set console palette }
  41. console.Palette(palette);
  42. { loop until a key is pressed }
  43. while not console.KeyPressed do
  44. begin
  45. { lock console }
  46. pixels := console.Lock;
  47. try
  48. { get console dimensions }
  49. width := console.width;
  50. height := console.height;
  51. pitch := console.pitch;
  52. { get console format }
  53. format := console.format;
  54. { get format information }
  55. bits := format.bits;
  56. bytes := format.bytes;
  57. { draw random pixels }
  58. for i := 1 to 100 do
  59. begin
  60. { get random position }
  61. x := Random(width);
  62. y := Random(height);
  63. { generate random color integer }
  64. color := (DWord(Random(256)) shl 0) or
  65. (DWord(Random(256)) shl 8) or
  66. (DWord(Random(256)) shl 16) or
  67. (DWord(Random(256)) shl 24);
  68. { calculate pointer to pixel [x,y] }
  69. pixel := pixels + y * pitch + x * bytes;
  70. { check bits }
  71. case bits of
  72. { 32 bits per pixel }
  73. 32: PDWord(pixel)^ := color;
  74. 24: begin
  75. { 24 bits per pixel }
  76. _data := pixel;
  77. _data[0] := (color and $000000FF) shr 0;
  78. _data[1] := (color and $0000FF00) shr 8;
  79. _data[2] := (color and $00FF0000) shr 16;
  80. end;
  81. { 16 bits per pixel }
  82. 16: PWord(pixel)^ := color;
  83. { 8 bits per pixel }
  84. 8: PByte(pixel)^ := color;
  85. end;
  86. end;
  87. finally
  88. { unlock console }
  89. console.Unlock;
  90. end;
  91. { update console }
  92. console.Update;
  93. end;
  94. finally
  95. if Assigned(console) then
  96. console.Close;
  97. end;
  98. except
  99. on error: TPTCError do
  100. { report error }
  101. error.report;
  102. end;
  103. end.