palette.pp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Palette 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 PaletteExample;
  10. {$MODE objfpc}
  11. uses
  12. ptc;
  13. var
  14. console: IPTCConsole;
  15. surface: IPTCSurface;
  16. format: IPTCFormat;
  17. palette: IPTCPalette;
  18. data: array [0..255] of Uint32;
  19. pixels: PUint8;
  20. width, height: Integer;
  21. i: Integer;
  22. x, y, index: Integer;
  23. begin
  24. try
  25. try
  26. { create console }
  27. console := TPTCConsoleFactory.CreateNew;
  28. { create format }
  29. format := TPTCFormatFactory.CreateNew(8);
  30. { open console }
  31. console.open('Palette example', format);
  32. { create surface }
  33. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  34. { create palette }
  35. palette := TPTCPaletteFactory.CreateNew;
  36. { generate palette }
  37. for i := 0 to 255 do
  38. data[i] := i;
  39. { load palette data }
  40. palette.Load(data);
  41. { set console palette }
  42. console.Palette(palette);
  43. { set surface palette }
  44. surface.Palette(palette);
  45. { loop until a key is pressed }
  46. while not console.KeyPressed do
  47. begin
  48. { lock surface }
  49. pixels := surface.lock;
  50. try
  51. { get surface dimensions }
  52. width := surface.width;
  53. height := surface.height;
  54. { draw random pixels }
  55. for i := 1 to 100 do
  56. begin
  57. { get random position }
  58. x := Random(width);
  59. y := Random(height);
  60. { get random color index }
  61. index := Random(256);
  62. { draw color [index] at position [x,y] }
  63. pixels[x + y * width] := index;
  64. end;
  65. finally
  66. { unlock surface }
  67. surface.unlock;
  68. end;
  69. { copy to console }
  70. surface.copy(console);
  71. { update console }
  72. console.update;
  73. end;
  74. finally
  75. if Assigned(console) then
  76. console.close;
  77. end;
  78. except
  79. on error: TPTCError do
  80. { report error }
  81. error.report;
  82. end;
  83. end.