stretch.pp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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: TPTCSurface; filename: String);
  14. var
  15. F: File;
  16. width, height: Integer;
  17. pixels: PByte = nil;
  18. y: Integer;
  19. tmp: TPTCFormat;
  20. tmp2: TPTCPalette;
  21. begin
  22. { open image file }
  23. AssignFile(F, filename);
  24. Reset(F, 1);
  25. try
  26. { skip header }
  27. Seek(F, 18);
  28. { get surface dimensions }
  29. width := surface.width;
  30. height := surface.height;
  31. { allocate image pixels }
  32. pixels := GetMem(width * height * 3);
  33. { read image pixels one line at a time }
  34. for y := height - 1 downto 0 do
  35. BlockRead(F, pixels[width * y * 3], width * 3);
  36. { load pixels to surface }
  37. {$IFDEF FPC_LITTLE_ENDIAN}
  38. tmp := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
  39. {$ELSE FPC_LITTLE_ENDIAN}
  40. tmp := TPTCFormat.Create(24, $000000FF, $0000FF00, $00FF0000);
  41. {$ENDIF FPC_LITTLE_ENDIAN}
  42. try
  43. tmp2 := TPTCPalette.Create;
  44. try
  45. surface.load(pixels, width, height, width * 3, tmp, tmp2);
  46. finally
  47. tmp2.Free;
  48. end;
  49. finally
  50. tmp.Free;
  51. end;
  52. finally
  53. { free image pixels }
  54. FreeMem(pixels);
  55. { close file }
  56. CloseFile(F);
  57. end;
  58. end;
  59. var
  60. console: TPTCConsole = nil;
  61. surface: TPTCSurface = nil;
  62. image: TPTCSurface = nil;
  63. format: TPTCFormat = nil;
  64. timer: TPTCTimer = nil;
  65. area: TPTCArea = nil;
  66. color: TPTCColor = nil;
  67. time: Double;
  68. zoom: Single;
  69. x, y, x1, y1, x2, y2, dx, dy: Integer;
  70. begin
  71. try
  72. try
  73. { create console }
  74. console := TPTCConsole.Create;
  75. { create format }
  76. format := TPTCFormat.Create(32, $00FF0000, $0000FF00, $000000FF);
  77. { open the console }
  78. console.open('Stretch example', format);
  79. { create surface matching console dimensions }
  80. surface := TPTCSurface.Create(console.width, console.height, format);
  81. { create image surface }
  82. image := TPTCSurface.Create(320, 140, format);
  83. { load image to surface }
  84. load(image, 'stretch.tga');
  85. { setup stretching parameters }
  86. x := surface.width div 2;
  87. y := surface.height div 2;
  88. dx := surface.width div 2;
  89. dy := surface.height div 3;
  90. { create timer }
  91. timer := TPTCTimer.Create;
  92. { start timer }
  93. timer.start;
  94. color := TPTCColor.Create(1, 1, 1);
  95. { loop until a key is pressed }
  96. while not console.KeyPressed do
  97. begin
  98. { get current time from timer }
  99. time := timer.time;
  100. { clear surface to white background }
  101. surface.clear(color);
  102. { calculate zoom factor at current time }
  103. zoom := 2.5 * (1 - cos(time));
  104. { calculate zoomed image coordinates }
  105. x1 := Trunc(x - zoom * dx);
  106. y1 := Trunc(y - zoom * dy);
  107. x2 := Trunc(x + zoom * dx);
  108. y2 := Trunc(y + zoom * dy);
  109. { setup image copy area }
  110. area := TPTCArea.Create(x1, y1, x2, y2);
  111. try
  112. { copy and stretch image to surface }
  113. image.copy(surface, image.area, area);
  114. { copy surface to console }
  115. surface.copy(console);
  116. { update console }
  117. console.update;
  118. finally
  119. area.Free;
  120. end;
  121. end;
  122. finally
  123. console.close;
  124. console.Free;
  125. surface.Free;
  126. format.Free;
  127. image.Free;
  128. color.Free;
  129. timer.Free;
  130. end;
  131. except
  132. on error: TPTCError do
  133. { report error }
  134. error.report;
  135. end;
  136. end.