image.pp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Image 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 ImageExample;
  10. {$MODE objfpc}
  11. Uses
  12. ptc;
  13. Procedure load(surface : TPTCSurface; filename : String);
  14. Var
  15. F : File;
  16. width, height : Integer;
  17. pixels : PByte;
  18. y : Integer;
  19. tmp : TPTCFormat;
  20. tmp2 : TPTCPalette;
  21. Begin
  22. { open image file }
  23. ASSign(F, filename);
  24. Reset(F, 1);
  25. { skip header }
  26. Seek(F, 18);
  27. { get surface dimensions }
  28. width := surface.width;
  29. height := surface.height;
  30. { allocate image pixels }
  31. pixels := GetMem(width * height * 3);
  32. { read image pixels one line at a time }
  33. For y := height - 1 DownTo 0 Do
  34. BlockRead(F, pixels[width * y * 3], width * 3);
  35. { load pixels to surface }
  36. tmp := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
  37. tmp2 := TPTCPalette.Create;
  38. surface.load(pixels, width, height, width * 3, tmp, tmp2);
  39. tmp2.Free;
  40. tmp.Free;
  41. { free image pixels }
  42. FreeMem(pixels);
  43. End;
  44. Var
  45. console : TPTCConsole;
  46. format : TPTCFormat;
  47. surface : TPTCSurface;
  48. Begin
  49. Try
  50. { create console }
  51. console := TPTCConsole.Create;
  52. { create format }
  53. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  54. Try
  55. { try to open the console matching the image resolution }
  56. console.open('Image example', 320, 200, format);
  57. Except
  58. On TPTCError Do
  59. { fallback to the default resolution }
  60. console.open('Image example', format);
  61. End;
  62. { create surface }
  63. surface := TPTCSurface.Create(320, 200, format);
  64. format.Free;
  65. { load image to surface }
  66. load(surface, 'image.tga');
  67. { copy surface to console }
  68. surface.copy(console);
  69. { update console }
  70. console.update;
  71. { read key }
  72. console.ReadKey;
  73. { close console }
  74. console.close;
  75. console.Free;
  76. surface.Free;
  77. Except
  78. On error : TPTCError Do
  79. { report error }
  80. error.report;
  81. End;
  82. End.