main.pp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. program main;
  2. {$L drunkenlogo.bin.o}
  3. {$L palette.bin.o}
  4. {$apptype arm9}
  5. {$define ARM9}
  6. {$mode objfpc}
  7. uses
  8. ctypes, nds9;
  9. var
  10. drunkenlogo_bin_end: array [0..0] of u8; cvar; external;
  11. drunkenlogo_bin: array [0..0] of u8; cvar; external;
  12. drunkenlogo_bin_size: u32; cvar; external;
  13. palette_bin_end: array [0..0] of u8; cvar; external;
  14. palette_bin: array [0..0] of u8; cvar; external;
  15. palette_bin_size: u32; cvar; external;
  16. angle: u32;
  17. scrollX, scrollY: s16;
  18. scaleX, scaleY: s16;
  19. rcX, rcY: s16;
  20. keys: u32;
  21. s, c: s16;
  22. begin
  23. irqInit();
  24. irqSet(IRQ_VBLANK, nil);
  25. // set the mode for 2 text layers and two extended background layers
  26. videoSetMode(MODE_5_2D or DISPLAY_BG3_ACTIVE);
  27. // set the sub background up for text display (we could just print to one
  28. // of the main display text backgrounds just as easily
  29. videoSetModeSub(MODE_0_2D or DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text
  30. // set the first bank as background memory and the third as sub background memory
  31. // B and D are not used
  32. vramSetMainBanks( VRAM_A_MAIN_BG_0x06000000, VRAM_B_LCD, VRAM_C_SUB_BG, VRAM_D_LCD);
  33. // set up text background for text
  34. SUB_BG0_CR^ := BG_MAP_BASE(31);
  35. BG_PALETTE_SUB[255] := u32(RGB15(31,31,31));//by default font will be rendered with color 255
  36. //consoleInit() is a lot more flexible but this gets you up and running quick
  37. consoleInitDefault(pu16(SCREEN_BASE_BLOCK_SUB(31)), pu16(CHAR_BASE_BLOCK_SUB(0)), 16);
  38. // set up our bitmap background
  39. BG3_CR^ := BG_BMP8_256x256;
  40. // these are rotation backgrounds so you must set the rotation attributes:
  41. // these are fixed point numbers with the low 8 bits the fractional part
  42. // this basicaly gives it a 1:1 translation in x and y so you get a nice flat bitmap
  43. BG3_XDX^ := 1 shl 8;
  44. BG3_XDY^ := 0;
  45. BG3_YDX^ := 0;
  46. BG3_YDY^ := 1 shl 8;
  47. // our bitmap looks a bit better if we center it so scroll down (256 - 192) / 2
  48. BG3_CX^ := 0;
  49. BG3_CY^ := 32 shl 8;
  50. dmaCopy(@drunkenlogo_bin, BG_GFX, 256*256);
  51. dmaCopy(@palette_bin, BG_PALETTE, 256*2);
  52. angle := 0;
  53. // the screen origin is at the rotation center...so scroll to the rotation
  54. // center + a small 32 pixle offset so our image is centered
  55. scrollX := 0 + 128;
  56. scrollY := 32 + 96 ;
  57. //scale is fixed point
  58. scaleX := 1 shl 8;
  59. scaleY := 1 shl 8;
  60. //this is the screen pixel that the image will rotate about
  61. rcX := 128;
  62. rcY := 96;
  63. while true do
  64. begin
  65. printf(#10#10#9 + 'Hello DS devers' + #10);
  66. printf(#9 + 'www.drunkencoders.com' + #10);
  67. printf(#9 + 'BG Rotation demo' + #10);
  68. iprintf('Angle %3d(actual) %3d(degrees)' + #10, [angle and $1FF, (angle and $1FF) * 360 div 512]);
  69. iprintf('Scroll X: %4d Y: %4d' + #10, [scrollX, scrollY]);
  70. iprintf('Rot center X: %4d Y: %4d' + #10, [rcX, rcY]);
  71. iprintf('Scale X: %4d Y: %4d' + #10, [scaleX, scaleY]);
  72. scanKeys();
  73. keys := keysHeld();
  74. if ( keys and KEY_L ) <> 0 then angle := angle + 1;
  75. if ( keys and KEY_R ) <> 0 then angle := angle - 1;
  76. if ( keys and KEY_LEFT ) <> 0 then scrollX := scrollX + 1;
  77. if ( keys and KEY_RIGHT ) <> 0 then scrollX := scrollX - 1;
  78. if ( keys and KEY_UP ) <> 0 then scrollY := scrollY + 1;
  79. if ( keys and KEY_DOWN ) <> 0 then scrollY := scrollY - 1;
  80. if ( keys and KEY_A ) <> 0 then scaleX := scaleX + 1;
  81. if ( keys and KEY_B ) <> 0 then scaleX := scaleX - 1;
  82. if ( keys and KEY_START ) <> 0 then rcX := rcX + 1;
  83. if ( keys and KEY_SELECT ) <> 0 then rcY := rcY + 1;
  84. if ( keys and KEY_X ) <> 0 then scaleY := scaleY + 1;
  85. if ( keys and KEY_Y ) <> 0 then scaleY := scaleY - 1;
  86. // Compute sin and cos
  87. s := SIN_bin[angle and $1FF] shr 4;
  88. c := COS_bin[angle and $1FF] shr 4;
  89. swiWaitForVBlank();
  90. // Set the background registers
  91. BG3_XDX^ := cuint16(( c * scaleX ) shr 8);
  92. BG3_XDY^ := cuint16((-s * scaleX ) shr 8);
  93. BG3_YDX^ := cuint16(( s * scaleY ) shr 8);
  94. BG3_YDY^ := cuint16(( c * scaleY ) shr 8);
  95. BG3_CX^ := cuint32((scrollX shl 8) - rcX * (c - s));
  96. BG3_CY^ := cuint32((scrollY shl 8) - rcY * (s + c));
  97. // clear the console screen (ansi escape sequence)
  98. printf(#27 + '[2J');
  99. end;
  100. end.