Advanced.pp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. unit advanced;
  2. interface
  3. uses
  4. ctypes, nds9, scrolling, RotBackgrounds, TextBackgrounds, Multilayer;
  5. procedure advMosaic();
  6. procedure advRotating();
  7. procedure advScaling();
  8. procedure advExtendedPalette();
  9. procedure advMultipleLayers();
  10. implementation
  11. procedure advMosaic();
  12. var
  13. bg: cint;
  14. keys: integer;
  15. mosaic_x: integer;
  16. mosaic_y: integer;
  17. begin
  18. videoSetMode(MODE_0_2D);
  19. vramSetBankA(VRAM_A_MAIN_BG);
  20. bg := bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 0,1);
  21. dmaCopy(@TextBackgroundsTiles, bgGetGfxPtr(bg), sizeof(TextBackgroundsTiles));
  22. dmaCopy(@Layer256x256Map, bgGetMapPtr(bg), Layer256x256MapLen);
  23. dmaCopy(@TextBackgroundsPal, BG_PALETTE, sizeof(TextBackgroundsPal));
  24. bgMosaicEnable(bg);
  25. while ((keys and KEY_B) = 0) do
  26. begin
  27. scanKeys();
  28. keys := keysDown();
  29. if (keys and KEY_UP) <> 0 then mosaic_y := mosaic_y - 1;
  30. if (keys and KEY_DOWN) <> 0 then mosaic_y := mosaic_y + 1;
  31. if (keys and KEY_LEFT) <> 0 then mosaic_x := mosaic_x - 1;
  32. if (keys and KEY_RIGHT) <> 0 then mosaic_x := mosaic_x + 1;
  33. if (mosaic_x > 15) then mosaic_x := 15;
  34. if (mosaic_x < 0) then mosaic_x := 0;
  35. if (mosaic_y > 15) then mosaic_y := 15;
  36. if (mosaic_y < 0) then mosaic_y := 0;
  37. swiWaitForVBlank();
  38. bgSetMosaic(mosaic_x, mosaic_y);
  39. consoleClear();
  40. iprintf('Press B to exit'#10);
  41. iprintf('DX: %d DY: %d', mosaic_x, mosaic_y);
  42. end;
  43. end;
  44. procedure advRotating();
  45. var
  46. bg: cint;
  47. keys: integer;
  48. angle: integer;
  49. mosaic_x: integer;
  50. mosaic_y: integer;
  51. center_x: integer;
  52. center_y: integer;
  53. begin
  54. videoSetMode(MODE_5_2D);
  55. vramSetBankA(VRAM_A_MAIN_BG);
  56. bg := bgInit(3, BgType_ExRotation, BgSize_ER_256x256, 0,1);
  57. dmaCopy(@TextBackgroundsTiles, bgGetGfxPtr(bg), sizeof(TextBackgroundsTiles));
  58. dmaCopy(@TextBackgroundsPal, BG_PALETTE, sizeof(TextBackgroundsPal));
  59. dmaCopy(@Layer256x256Map, bgGetMapPtr(bg), Layer256x256MapLen);
  60. bgMosaicEnable(bg);
  61. while ((keys and KEY_B) = 0) do
  62. begin
  63. scanKeys();
  64. keys := keysHeld();
  65. if (keys and KEY_UP) <> 0 then center_y := center_y - 1;
  66. if (keys and KEY_DOWN) <> 0 then center_y := center_y + 1;
  67. if (keys and KEY_LEFT) <> 0 then center_x := center_x - 1;
  68. if (keys and KEY_RIGHT) <> 0 then center_x := center_x + 1;
  69. if (keys and KEY_L) <> 0 then angle := angle - 1;
  70. if (keys and KEY_R) <> 0 then angle := angle + 1;
  71. if (center_x > 256) then center_x := 256;
  72. if (center_x < 0) then center_x := 0;
  73. if (center_y > 192) then center_y := 192;
  74. if (center_y < 0) then center_y := 0;
  75. swiWaitForVBlank();
  76. bgSetRotate(bg, angle);
  77. bgSetScroll(bg, center_x, center_y);
  78. bgSetCenter(bg, center_x, center_y);
  79. bgUpdate();
  80. consoleClear();
  81. iprintf('Press B to exit.'#10);
  82. iprintf('Angle: %d '#10'center X: %d center Y: %d', angle, center_x, center_y);
  83. end;
  84. end;
  85. procedure advScaling();
  86. var
  87. bg: cint;
  88. keys, scale_x, scale_y: integer;
  89. begin
  90. videoSetMode(MODE_5_2D);
  91. vramSetBankA(VRAM_A_MAIN_BG);
  92. bg := bgInit(3, BgType_ExRotation, BgSize_ER_256x256, 0,1);
  93. dmaCopy(@TextBackgroundsTiles, bgGetGfxPtr(bg), sizeof(TextBackgroundsTiles));
  94. dmaCopy(@TextBackgroundsPal, BG_PALETTE, sizeof(TextBackgroundsPal));
  95. dmaCopy(@Layer256x256Map, bgGetMapPtr(bg), Layer256x256MapLen);
  96. bgMosaicEnable(bg);
  97. scale_x := 1 shl 8;
  98. scale_y := 1 shl 8;
  99. while ((keys and KEY_B) = 0) do
  100. begin
  101. scanKeys();
  102. keys := keysHeld();
  103. if (keys and KEY_UP) <> 0 then scale_y := scale_y - 1;
  104. if (keys and KEY_DOWN) <> 0 then scale_y := scale_y + 1;
  105. if (keys and KEY_LEFT) <> 0 then scale_x := scale_x + 1;
  106. if (keys and KEY_RIGHT) <> 0 then scale_x := scale_x - 1;
  107. swiWaitForVBlank();
  108. bgSetScale(bg, scale_x , scale_y );
  109. bgUpdate();
  110. consoleClear();
  111. iprintf('Press B to exit.'#10);
  112. iprintf('scale X: %d scale Y: %d', scale_x, scale_y);
  113. end;
  114. end;
  115. procedure advExtendedPalette();
  116. var
  117. paletteSlots: array [0..3] of pcuint16;
  118. i: integer;
  119. bg: integer;
  120. begin
  121. (*
  122. When extended palettes are enabled all tiled backgrounds which utilize
  123. 16 bit map entries will use extended palettes. Everything else will continue
  124. to use standard palette memory.
  125. Each tile on the screen may chose one of 16 256-color palettes. Each background
  126. has its own set of 16 palettes meaning you can have 4*16*256 colors on screen
  127. Each background uses 8K of palette memory starting at the base of the vram bank
  128. you allocate (which bank is up to you within limits, see the vram usage table
  129. to determine which banks can be mapped for textures). These 8K blocks are often
  130. refered to as "slots" with each background getting its own slot.
  131. By default, Background 0 uses slot 0 ... Background 3 uses slot 3. It is possible
  132. to assign Background 0 to slot 2 and Background 1 to slot 3 (only these two are configurable)
  133. For more information: <a href="http://nocash.emubase.de/gbatek.htm#dsvideoextendedpalettes">gbatek</a>
  134. *)
  135. for i := 0 to 3 do
  136. paletteSlots[i]^ := VRAM_E[i * 16 * 256];
  137. videoSetMode(MODE_0_2D);
  138. vramSetBankA(VRAM_A_MAIN_BG);
  139. //enable extended palettes for background. Once on, the standard BG_PALETTE will
  140. //be ignored for tiled backgrounds with 16 bit map entries (everything else still uses
  141. //the standard palette)
  142. bgExtPaletteEnable();
  143. bg := bgInit(0, BgType_Text8bpp, BgSize_T_256x256, 0, 1);
  144. dmaCopy(@TextBackgroundsTiles, bgGetGfxPtr(bg), sizeof(TextBackgroundsTiles));
  145. dmaCopy(@Layer256x256Map, bgGetMapPtr(bg), Layer256x256MapLen);
  146. //lock vram E as we are going to use it for
  147. //extended palettes (see documentation for which vram
  148. //banks can be utilized). You cannot write to it once
  149. //it is mapped to the GPU. These means you should
  150. //not update your ext palettes outside of a blank period
  151. vramSetBankE(VRAM_E_LCD);
  152. dmaCopy(@TextBackgroundsPal, paletteSlots[0], sizeof(TextBackgroundsPal));
  153. //tell the GPU to use vram E as extended palette memory
  154. vramSetBankE(VRAM_E_BG_EXT_PALETTE);
  155. scroll(bg, 256, 256);
  156. end;
  157. procedure advMultipleLayers();
  158. var
  159. bg1, bg2, bg3: cint;
  160. keys: integer;
  161. bg1_hidden, bg2_hidden, bg3_hidden: boolean;
  162. begin
  163. videoSetMode(MODE_5_2D);
  164. vramSetBankA(VRAM_A_MAIN_BG);
  165. //initialize the backgrounds
  166. bg1 := bgInit(0, BgType_Text8bpp, BgSize_ER_256x256, 0,1);
  167. bg2 := bgInit(1, BgType_Text8bpp, BgSize_ER_256x256, 1,1);
  168. bg3 := bgInit(2, BgType_ExRotation, BgSize_ER_256x256, 2,1);
  169. //make sure the floor is on the bottom (by default hardware layer 0 will be rendered last)
  170. bgSetPriority(bg1, 3);
  171. bgSetPriority(bg2, 2);
  172. bgSetPriority(bg3, 1);
  173. //they all share tiles and a palette
  174. dmaCopy(@MultilayerTiles, bgGetGfxPtr(bg1), sizeof(MultilayerTiles));
  175. dmaCopy(@MultilayerPal, BG_PALETTE, sizeof(MultilayerPal));
  176. //all we need to do is copy in the maps
  177. dmaCopy(@Layer_1Map, bgGetMapPtr(bg1), Layer_1MapLen);
  178. dmaCopy(@Layer_2Map, bgGetMapPtr(bg2), Layer_2MapLen);
  179. dmaCopy(@Layer_3Map, bgGetMapPtr(bg3), Layer_3MapLen);
  180. keys := 0;
  181. bg1_hidden := false;
  182. bg2_hidden := false;
  183. bg3_hidden := false;
  184. while ((keys and KEY_B) = 0) do
  185. begin
  186. scanKeys();
  187. keys := keysDown();
  188. if (keys and KEY_UP) <> 0 then bg1_hidden := not bg1_hidden;
  189. if (keys and KEY_DOWN) <> 0 then bg2_hidden := not bg2_hidden;
  190. if (keys and KEY_LEFT) <> 0 then bg3_hidden := not bg3_hidden;
  191. swiWaitForVBlank();
  192. if bg1_hidden then bgHide(bg1) else bgShow(bg1);
  193. if bg2_hidden then bgHide(bg2) else bgShow(bg2);
  194. if bg3_hidden then bgHide(bg3) else bgShow(bg3);
  195. consoleClear();
  196. iprintf('Press UP DOWN LEFT to toggle the layers'#10#10);
  197. if bg1_hidden then
  198. iprintf('Floor (UP): %s'#10, 'hidden')
  199. else
  200. iprintf('Floor (UP): %s'#10, 'displayed');
  201. if bg2_hidden then
  202. iprintf('Walls (DOWN): %s'#10, 'hidden')
  203. else
  204. iprintf('Walls (DOWN): %s'#10, 'displayed');
  205. if bg3_hidden then
  206. iprintf('Decorations (LEFT): %s'#10, 'hidden')
  207. else
  208. iprintf('Decorations (LEFT): %s'#10, 'displayed');
  209. end;
  210. end;
  211. end.