stretch.pp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  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. Try
  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. tmp := TPTCFormat.Create(24, $00FF0000, $0000FF00, $000000FF);
  38. Try
  39. tmp2 := TPTCPalette.Create;
  40. Try
  41. surface.load(pixels, width, height, width * 3, tmp, tmp2);
  42. Finally
  43. tmp2.Free;
  44. End;
  45. Finally
  46. tmp.Free;
  47. End;
  48. Finally
  49. { free image pixels }
  50. FreeMem(pixels);
  51. End;
  52. End;
  53. Var
  54. console : TPTCConsole;
  55. surface : TPTCSurface;
  56. image : TPTCSurface;
  57. format : TPTCFormat;
  58. timer : TPTCTimer;
  59. area : TPTCArea;
  60. color : TPTCColor;
  61. time : Double;
  62. zoom : Single;
  63. x, y, x1, y1, x2, y2, dx, dy : Integer;
  64. Begin
  65. format := Nil;
  66. color := Nil;
  67. timer := Nil;
  68. image := Nil;
  69. surface := Nil;
  70. console := Nil;
  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.