stretch.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Stretch 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 StretchExample;
  10. {$MODE objfpc}
  11. uses
  12. ptc;
  13. procedure load(surface: IPTCSurface; filename: AnsiString);
  14. var
  15. F: File;
  16. width, height: Integer;
  17. pixels: PByte = nil;
  18. y: Integer;
  19. format: IPTCFormat;
  20. begin
  21. { open image file }
  22. AssignFile(F, filename);
  23. Reset(F, 1);
  24. try
  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. {$IFDEF FPC_LITTLE_ENDIAN}
  37. format := TPTCFormatFactory.CreateNew(24, $00FF0000, $0000FF00, $000000FF);
  38. {$ELSE FPC_LITTLE_ENDIAN}
  39. format := TPTCFormatFactory.CreateNew(24, $000000FF, $0000FF00, $00FF0000);
  40. {$ENDIF FPC_LITTLE_ENDIAN}
  41. surface.Load(pixels, width, height, width * 3, format, TPTCPaletteFactory.CreateNew);
  42. finally
  43. { free image pixels }
  44. FreeMem(pixels);
  45. { close file }
  46. CloseFile(F);
  47. end;
  48. end;
  49. var
  50. console: IPTCConsole;
  51. surface: IPTCSurface;
  52. image: IPTCSurface;
  53. format: IPTCFormat;
  54. timer: IPTCTimer;
  55. area: IPTCArea;
  56. time: Double;
  57. zoom: Single;
  58. x, y, x1, y1, x2, y2, dx, dy: Integer;
  59. begin
  60. try
  61. try
  62. { create console }
  63. console := TPTCConsoleFactory.CreateNew;
  64. { create format }
  65. format := TPTCFormatFactory.CreateNew(32, $00FF0000, $0000FF00, $000000FF);
  66. { open the console }
  67. console.open('Stretch example', format);
  68. { create surface matching console dimensions }
  69. surface := TPTCSurfaceFactory.CreateNew(console.width, console.height, format);
  70. { create image surface }
  71. image := TPTCSurfaceFactory.CreateNew(320, 140, format);
  72. { load image to surface }
  73. load(image, 'stretch.tga');
  74. { setup stretching parameters }
  75. x := surface.width div 2;
  76. y := surface.height div 2;
  77. dx := surface.width div 2;
  78. dy := surface.height div 3;
  79. { create timer }
  80. timer := TPTCTimerFactory.CreateNew;
  81. { start timer }
  82. timer.start;
  83. { loop until a key is pressed }
  84. while not console.KeyPressed do
  85. begin
  86. { get current time from timer }
  87. time := timer.time;
  88. { clear surface to white background }
  89. surface.clear(TPTCColorFactory.CreateNew(1, 1, 1));
  90. { calculate zoom factor at current time }
  91. zoom := 2.5 * (1 - cos(time));
  92. { calculate zoomed image coordinates }
  93. x1 := Trunc(x - zoom * dx);
  94. y1 := Trunc(y - zoom * dy);
  95. x2 := Trunc(x + zoom * dx);
  96. y2 := Trunc(y + zoom * dy);
  97. { setup image copy area }
  98. area := TPTCAreaFactory.CreateNew(x1, y1, x2, y2);
  99. { copy and stretch image to surface }
  100. image.copy(surface, image.area, area);
  101. { copy surface to console }
  102. surface.copy(console);
  103. { update console }
  104. console.update;
  105. end;
  106. finally
  107. if Assigned(console) then
  108. console.close;
  109. end;
  110. except
  111. on error: TPTCError do
  112. { report error }
  113. error.report;
  114. end;
  115. end.