flower.pp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. {
  2. Ported to FPC by Nikolay Nikolov ([email protected])
  3. }
  4. {
  5. Flower demo for OpenPTC 1.0 C++ API
  6. Copyright (c) Scott Buchanan (aka Goblin)
  7. This source code is licensed under the GNU GPL
  8. }
  9. Program Flower;
  10. {$MODE objfpc}
  11. Uses
  12. ptc, Math;
  13. Function pack(r, g, b : Uint32) : Uint32;
  14. Begin
  15. { pack color integer }
  16. pack := (r Shl 16) Or (g Shl 8) Or b;
  17. End;
  18. Procedure generate_flower(flower : TPTCSurface);
  19. Var
  20. data : PUint8;
  21. x, y, fx, fy, fx2, fy2 : Integer;
  22. TWO_PI : Single;
  23. Begin
  24. { lock surface }
  25. data := flower.lock;
  26. Try
  27. { surface width and height constants for cleaner code }
  28. fx := flower.width;
  29. fy := flower.height;
  30. fx2 := fx Div 2;
  31. fy2 := fy Div 2;
  32. { useful 2*pi constant }
  33. TWO_PI := 2 * PI;
  34. { generate flower image }
  35. For y := 0 To fy - 1 Do
  36. For x := 0 To fx - 1 Do
  37. data[x + y * fx] := Trunc(1.0 * Cos(18*ArcTan2((y - fy2),(x - fx2))) * 255 / TWO_PI +
  38. 0.3 * Sin(15*ArcTan2((y - fy2),(x - fx2))) * 255 / TWO_PI +
  39. Sqrt((y - fy2) * (y - fy2) + (x - fx2) * (x - fx2))) And $FF;
  40. { You might want to move the 1.0 and 0.3 and the 18 and the 15
  41. to parameters passed to the generate function...
  42. the 1.0 and the 0.3 define the 'height' of the flower, while the
  43. 18 and 15 control the number of 'petals' }
  44. Finally
  45. flower.unlock;
  46. End;
  47. End;
  48. Procedure generate(palette : TPTCPalette);
  49. Var
  50. data : PUint32;
  51. i, c : Integer;
  52. Begin
  53. { lock palette data }
  54. data := palette.lock;
  55. Try
  56. { black to yellow }
  57. i := 0;
  58. c := 0;
  59. While i < 64 Do
  60. Begin
  61. data[i] := pack(c, c, 0);
  62. Inc(c, 4);
  63. Inc(i);
  64. End;
  65. { yellow to red }
  66. c := 0;
  67. While i < 128 Do
  68. Begin
  69. data[i] := pack(255, 255 - c, 0);
  70. Inc(c, 4);
  71. Inc(i);
  72. End;
  73. { red to white }
  74. c := 0;
  75. While i < 192 Do
  76. Begin
  77. data[i] := pack(255, c, c);
  78. Inc(c, 4);
  79. Inc(i);
  80. End;
  81. { white to black }
  82. c := 0;
  83. While i < 256 Do
  84. Begin
  85. data[i] := pack(255 - c, 255 - c, 255 - c);
  86. Inc(c, 4);
  87. Inc(i);
  88. End;
  89. Finally
  90. { unlock palette }
  91. palette.unlock;
  92. End;
  93. End;
  94. Var
  95. console : TPTCConsole;
  96. format : TPTCFormat;
  97. flower_surface : TPTCSurface;
  98. surface : TPTCSurface;
  99. palette : TPTCPalette;
  100. area : TPTCArea;
  101. time, delta : Single;
  102. scr, map : PUint8;
  103. width, height, mapWidth : Integer;
  104. xo, yo, xo2, yo2, xo3, yo3 : Single;
  105. offset1, offset2, offset3 : Integer;
  106. x, y : Integer;
  107. Begin
  108. area := Nil;
  109. format := Nil;
  110. palette := Nil;
  111. surface := Nil;
  112. flower_surface := Nil;
  113. console := Nil;
  114. Try
  115. Try
  116. { create format }
  117. format := TPTCFormat.Create(8);
  118. { create console }
  119. console := TPTCConsole.Create;
  120. { create flower surface }
  121. flower_surface := TPTCSurface.Create(640, 400, format);
  122. { generate flower }
  123. generate_flower(flower_surface);
  124. { open console }
  125. console.open('Flower demo', 320, 200, format);
  126. { create surface }
  127. surface := TPTCSurface.Create(320, 200, format);
  128. { create palette }
  129. palette := TPTCPalette.Create;
  130. { generate palette }
  131. generate(palette);
  132. { set console palette }
  133. console.palette(palette);
  134. { set surface palette }
  135. surface.palette(palette);
  136. { setup copy area }
  137. area := TPTCArea.Create(0, 0, 320, 200);
  138. { time data }
  139. time := 0;
  140. delta := 0.04;
  141. { main loop }
  142. While Not console.KeyPressed Do
  143. Begin
  144. { lock surface pixels }
  145. scr := surface.lock;
  146. Try
  147. map := flower_surface.lock;
  148. Try
  149. { get surface dimensions }
  150. width := surface.width;
  151. height := surface.height;
  152. mapWidth := flower_surface.width;
  153. xo := (width / 2) + 120 * sin(time * 1.1 + 1.5);
  154. yo := (height / 2) + 90 * cos(time * 0.8 + 1.1);
  155. offset1 := Trunc(xo) + Trunc(yo) * mapWidth;
  156. xo2 := (width / 2) + 120 * sin(time * 0.9 + 4.2);
  157. yo2 := (height / 2) + 90 * cos(time * 0.7 + 6.9);
  158. offset2 := Trunc(xo2) + Trunc(yo2) * mapWidth;
  159. xo3 := (width / 2) + 120 * sin(time * 0.9 + 3.1);
  160. yo3 := (height / 2) + 90 * cos(time * 1.1 + 1.2);
  161. offset3 := Trunc(xo3) + Trunc(yo3) * mapWidth;
  162. { vertical loop }
  163. For y := 0 To height - 1 Do
  164. { horizontal loop }
  165. For x := 0 To width - 1 Do
  166. scr[x + y * width] := (map[x + y * mapWidth + offset1] +
  167. map[x + y * mapWidth + offset2] +
  168. map[x + y * mapWidth + offset3]) And $FF;
  169. Finally
  170. { unlock surface }
  171. flower_surface.unlock;
  172. End;
  173. Finally
  174. { unlock surface }
  175. surface.unlock;
  176. End;
  177. { copy surface to console }
  178. surface.copy(console, area, area);
  179. { update console }
  180. console.update;
  181. { update time }
  182. time := time + delta;
  183. End;
  184. Finally
  185. If Assigned(console) Then
  186. console.close;
  187. area.Free;
  188. format.Free;
  189. palette.Free;
  190. surface.Free;
  191. flower_surface.Free;
  192. console.Free;
  193. End;
  194. Except
  195. On error : TPTCError Do
  196. { report error }
  197. error.report;
  198. End;
  199. End.