primitives.pp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. (*
  2. Easy GL2D
  3. Relminator 2011
  4. Richard Eric M. Lope BSN RN
  5. Http://Rel.Phatcode.Net
  6. A very small, simple, yet very fast DS 2D rendering lib using the DS' 3D core.
  7. --
  8. Translated in Object Pascal by Francesco Lombardi - 2012
  9. http://itaprogaming.free.fr
  10. *)
  11. program primitives;
  12. {$mode objfpc}
  13. uses
  14. nds9, ctypes, gl2d;
  15. const
  16. HALF_WIDTH = (SCREEN_WIDTH div 2);
  17. HALF_HEIGHT = (SCREEN_HEIGHT div 2);
  18. BRAD_PI = (1 shl 14);
  19. // Simple box, triangle, and putpixel demo
  20. procedure simple(frame: cint);
  21. var
  22. red, green, blue: cint;
  23. i: integer;
  24. x, y: integer;
  25. begin
  26. // set up GL2D for 2d mode
  27. glBegin2D();
  28. // Do some funky color cycling
  29. red := abs(sinLerp(frame * 220) * 31) shr 12;
  30. green := abs(sinLerp(frame * 140) * 31) shr 12;
  31. blue := abs(sinLerp(frame * 40) * 31) shr 12;
  32. // fill the whole screen with a gradient box
  33. glBoxFilledGradient( 0, 0, 255, 191,
  34. RGB15( red, green, blue ),
  35. RGB15( blue, 31 - red, green ),
  36. RGB15( green, blue, 31 - red ),
  37. RGB15( 31 - green, red, blue )
  38. );
  39. // draw a black box
  40. glBoxFilled( 200, 10,
  41. 250, 180,
  42. RGB15(0,0,0)
  43. );
  44. // draw a border around the black box
  45. glBox( 200, 10,
  46. 250, 180,
  47. RGB15(0,31,0)
  48. );
  49. // draw a triangle
  50. glTriangleFilled( 20, 100,
  51. 200, 30,
  52. 60, 40,
  53. RGB15(31,0,31)
  54. );
  55. // draw a gradient triangle
  56. glTriangleFilledGradient( 20, 100,
  57. 200, 30,
  58. 60, 40,
  59. RGB15(blue,red,green),
  60. RGB15(green,blue, red),
  61. RGB15(red,green,blue)
  62. );
  63. // translucent mode
  64. // Poly ID 1
  65. glPolyFmt(POLY_ALPHA(16) or POLY_CULL_NONE or POLY_ID(1));
  66. glBoxFilledGradient( 10, 50, 230, 150,
  67. RGB15( green, 0, 0 ),
  68. RGB15( 0, red, 0 ),
  69. RGB15( 31, 0, blue ),
  70. RGB15( 0, red, 31 )
  71. );
  72. // translucent mode
  73. // Poly ID 2
  74. glPolyFmt(POLY_ALPHA(16) or POLY_CULL_NONE or POLY_ID(2));
  75. glTriangleFilledGradient( 70, 10,
  76. 20, 130,
  77. 230, 180,
  78. RGB15(red,green,blue),
  79. RGB15(blue,red,green),
  80. RGB15(green,blue, red)
  81. );
  82. i := 0;
  83. // restore to normal(solid) rendering
  84. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE or POLY_ID(3));
  85. // draw a circle using putpixel
  86. {
  87. for i := 0 to 128 do
  88. begin
  89. x := sar(cosLerp(i * 256) * 80, 12);
  90. y := sar(sinLerp(i * 256) * 70, 12);
  91. glPutPixel( HALF_WIDTH + x,
  92. HALF_HEIGHT + y,
  93. RGB15(red, green, blue)
  94. );
  95. end;
  96. }
  97. while i < BRAD_PI * 2 do
  98. begin
  99. x := SarLongint(cosLerp(i) * 80, 12);
  100. y := SarLongint(sinLerp(i) * 70, 12);
  101. glPutPixel( HALF_WIDTH + x, HALF_HEIGHT + y, RGB15(red, green, blue) );
  102. inc(i, 256);
  103. end;
  104. glEnd2D();
  105. end;
  106. // oldskool lines demo
  107. procedure lines(frame: cint);
  108. var
  109. red, green, blue: cint;
  110. i: cint;
  111. px, py, px2, py2: cint;
  112. begin
  113. // Do some funky color cycling
  114. red := abs(sinLerp(frame * 220) * 31) shr 12 ;
  115. green := abs(sinLerp(frame * 140) * 31) shr 12 ;
  116. blue := abs(sinLerp(frame * 40) * 31) shr 12 ;
  117. // set up GL2D for 2d mode
  118. glBegin2D();
  119. // draw a bunch (4096/32) of colored lines
  120. // using some funky trig equations
  121. i := frame;
  122. while i < ((1 shl 12) + frame) do
  123. begin
  124. px := SarLongint(sinLerp(frame * 130) * 130, 12) * cosLerp( (i * 100));
  125. py := SarLongint(sinLerp(frame * 280) * 70, 12) * sinLerp( (i * 200));
  126. px2 := SarLongint(sinLerp(frame * 330) * 100, 12) * cosLerp(((i * 300 + BRAD_PI)));
  127. py2 := SarLongint(sinLerp(frame * 140) * 80, 12) * sinLerp(((i * 400 + BRAD_PI)));
  128. glLine( HALF_WIDTH + SarLongint(px, 12), HALF_HEIGHT + SarLongint(py, 12),
  129. HALF_WIDTH + SarLongint(px2, 12), HALF_HEIGHT + SarLongint(py2, 12),
  130. RGB15(red, green, blue)
  131. );
  132. glLine( HALF_WIDTH + SarLongint(py2, 12), HALF_HEIGHT + SarLongint(px, 12),
  133. HALF_WIDTH + SarLongint(py, 12), HALF_HEIGHT + SarLongint(px2, 12),
  134. RGB15(green, blue, red)
  135. );
  136. inc(i, 32);
  137. end;
  138. glEnd2D();
  139. end;
  140. // Some radially displaced pixels
  141. procedure pixels(frame: cint);
  142. var
  143. radius, red, green, blue: cint;
  144. i, angle: cint;
  145. x, y, a2,x2,y2: cint;
  146. begin
  147. // Elastic radius
  148. radius := 40 + (abs(sinLerp(frame * 20) * 80) shr 12);
  149. // Do some funky color cycling
  150. red := abs(sinLerp(frame * 220) * 31) shr 12 ;
  151. green := abs(sinLerp(frame * 140) * 31) shr 12 ;
  152. blue := abs(sinLerp(frame * 40) * 31) shr 12 ;
  153. // speed opf animation
  154. i := (frame * 140) and 32767;
  155. // duh!
  156. angle := 0;
  157. // set up GL2D for 2d mode
  158. glBegin2D();
  159. // Draw a full revolution of some radially dispalced pixels
  160. for angle := 0 to 512 do
  161. begin
  162. a2 := (angle * 64) + i;
  163. x := cosLerp(angle * 64 * 2) * radius;
  164. y := sinLerp(x div 32 + a2) * radius;
  165. x := cosLerp((y div 64) + (angle * 64)) * (radius + 20);
  166. y := sinLerp(x div 64 + a2) * radius;
  167. x2 := -y;
  168. y2 := x;
  169. glPutPixel( HALF_WIDTH + SarLongint(x, 12),
  170. HALF_HEIGHT + SarLongint(y, 12),
  171. RGB15(red, green, blue)
  172. );
  173. glPutPixel( HALF_WIDTH + SarLongint(x2, 12),
  174. HALF_HEIGHT + SarLongint(y2, 12),
  175. RGB15(green, blue, red)
  176. );
  177. end;
  178. glEnd2D();
  179. end;
  180. // Demo titles
  181. var
  182. demo_titles: array [0..2] of pchar = ('Lovely Wobbly ',
  183. 'OldSkool ',
  184. 'This Suxxors! ');
  185. var
  186. frame: integer = 0;
  187. demonum: integer = 0;
  188. key: integer;
  189. begin
  190. defaultExceptionHandler();
  191. // Set it to my favorite mode
  192. videoSetMode( MODE_5_3D );
  193. consoleDemoInit();
  194. // Initialize GL in 3d mode
  195. glScreen2D();
  196. iprintf(#$1b'[1;1HEasy GL2D Primitives Example');
  197. iprintf(#$1b'[3;1HRelminator');
  198. iprintf(#$1b'[4;1Hhttp://rel.betterwebber.com');
  199. iprintf(#$1b'[6;1HPress Arrow keys to Change FX');
  200. while true do
  201. begin
  202. // increment frame counter
  203. inc(frame);
  204. // get input
  205. scanKeys();
  206. key := keysDown();
  207. // process input
  208. if ((key and KEY_DOWN) <> 0) or ((key and KEY_RIGHT) <> 0) then
  209. demonum := (demonum + 1) mod 3;
  210. if ((key and KEY_UP) <> 0) or ((key and KEY_LEFT) <> 0) then
  211. begin
  212. dec(demonum);
  213. if demonum < 0 then
  214. demonum := 2;
  215. end;
  216. // figure out what demo should be viewed
  217. case demonum of
  218. 0: pixels(frame);
  219. 1: lines(frame);
  220. 2: simple(frame);
  221. else
  222. pixels(frame);
  223. end;
  224. glFlush(0);
  225. swiWaitForVBlank();
  226. iprintf(#$1b'[10;1HDemo# %i/3', demonum+1);
  227. iprintf(#$1b'[11;1HTitle: %s', demo_titles[demonum] );
  228. end;
  229. end.