main.pp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. program main;
  2. {$apptype arm9}
  3. {$define ARM9}
  4. {$mode objfpc}
  5. uses
  6. ctypes, nds9;
  7. var
  8. sprites: array [0..127] of SpriteEntry;
  9. spriteRotations: pSpriteRotation;
  10. i, angle: integer;
  11. //turn off all the sprites
  12. procedure initSprites();
  13. var
  14. i: integer;
  15. begin
  16. for i := 0 to 127 do
  17. begin
  18. sprites[i].st.attribute[0] := ATTR0_DISABLED;
  19. sprites[i].st.attribute[1] := 0;
  20. sprites[i].st.attribute[2] := 0;
  21. sprites[i].st.attribute[3] := 0;
  22. end;
  23. end;
  24. //copy our sprite to object attribute memory
  25. procedure updateOAM();
  26. begin
  27. DC_FlushRange(@sprites, 128 * sizeof(SpriteEntry));
  28. dmaCopy(@sprites, OAM, 128 * sizeof(SpriteEntry));
  29. end;
  30. begin
  31. //rotation attributes overlap so assign then to the same location
  32. spriteRotations := pSpriteRotation(@sprites);
  33. //turn everything on
  34. powerON(POWER_ALL_2D);
  35. //irqs are nice
  36. irqInit();
  37. irqSet(IRQ_VBLANK, nil);
  38. //enable vram and map it to the right places
  39. vramSetMainBanks( VRAM_A_MAIN_SPRITE, //A and B maped consecutivly as sprite memory
  40. VRAM_B_MAIN_SPRITE, //this gives us 256KB which is the max
  41. VRAM_C_MAIN_BG_0x06000000, //map C to background memory
  42. VRAM_D_LCD //not using D
  43. );
  44. //set the video mode
  45. videoSetMode( MODE_0_2D or
  46. DISPLAY_SPR_ACTIVE or //turn on sprites
  47. DISPLAY_BG0_ACTIVE or //turn on background 0
  48. DISPLAY_SPR_1D or //this is used when in tile mode
  49. DISPLAY_SPR_1D_BMP //and this in bitmap mode
  50. );
  51. // black backdrop
  52. BG_PALETTE[0] := u32(RGB15(0,0,0));
  53. BG0_CR^ := BG_MAP_BASE(31);//use bg0 for the text
  54. BG_PALETTE[255] := u32(RGB15(31,31,31));//by default font rendered with color 255
  55. //consoleInit() is a lot more flexible but this gets you up and running quick
  56. consoleInitDefault(pu16(SCREEN_BASE_BLOCK(31)), pu16(CHAR_BASE_BLOCK(0)), 16);
  57. //turn off the sprites
  58. initSprites();
  59. // direct bitmap sprite
  60. // print at using ansi escape sequence \x1b[line;columnH
  61. printf(#27 + '[1;1H' + 'Direct Bitmap:');
  62. sprites[0].st.attribute[0] := ATTR0_BMP or ATTR0_ROTSCALE_DOUBLE or 10;
  63. sprites[0].st.attribute[1] := ATTR1_SIZE_32 or 20;
  64. sprites[0].st.attribute[2] := ATTR2_ALPHA(1)or 0;
  65. // red 32*32 square for 1d bitmap mode
  66. for i := 0 to 32*32 - 1 do
  67. SPRITE_GFX[i] := RGB15(31,0,0) or (1 shl 15); //dont forget alpha bit
  68. // 256 color sprite
  69. // print at using ansi escape sequence \x1b[line;columnH
  70. printf(#27 + '[9;1H' + '256 color:');
  71. sprites[1].st.attribute[0] := ATTR0_COLOR_256 or ATTR0_ROTSCALE_DOUBLE or 75;
  72. sprites[1].st.attribute[1] := ATTR1_SIZE_32 or 20; // size 64x64, x 10
  73. sprites[1].st.attribute[2] := 64;
  74. // Blue for 256 color sprite
  75. SPRITE_PALETTE[1] := RGB15(0,0,31);
  76. // blue 64*64 square for 256 color mode (must write two pixles at time)
  77. for i := 0 to 32*16 - 1 do
  78. SPRITE_GFX[i+64*16] := (1 shl 8) or 1;
  79. // 16 color sprite
  80. // print at using ansi escape sequence \x1b[line;columnH
  81. printf(#27 + '[16;1H' + '16 color:');
  82. sprites[2].st.attribute[0] := ATTR0_COLOR_16 or ATTR0_ROTSCALE_DOUBLE or 135;
  83. sprites[2].st.attribute[1] := ATTR1_SIZE_32 or 20;
  84. sprites[2].st.attribute[2] := ATTR2_PALETTE(1) or 96;
  85. //yellow for 16 color sprite (it is using palette 1 so colors 16-31)
  86. SPRITE_PALETTE[17] := RGB15(31,31,0);
  87. // yellow 32*32 square for 16 color mode (must write 4 pixels at a time)
  88. for i := 0 to 32*8 - 1 do
  89. SPRITE_GFX[i+96*16] := (1 shl 12) or (1 shl 8) or (1 shl 4) or 1;
  90. angle:=0;
  91. //we tied all our sprites to the same rotation attributes (0)
  92. spriteRotations[0].hdx := 256;
  93. spriteRotations[0].hdy := 0;
  94. spriteRotations[0].vdx := 0;
  95. spriteRotations[0].vdy := 256;
  96. while true do
  97. begin
  98. angle := angle + 1;
  99. spriteRotations[0].hdx := COS_bin[angle and $1FF] shr 4;
  100. spriteRotations[0].hdy := SIN_bin[angle and $1FF] shr 4;
  101. spriteRotations[0].vdx := -spriteRotations[0].hdy;
  102. spriteRotations[0].vdy := spriteRotations[0].hdx;
  103. swiWaitForVBlank();
  104. updateOAM();
  105. end;
  106. end.