BitmapSprites.pp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. program BitmapSprites;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. //a simple sprite structure
  6. //it is generally preferred to separate your game object
  7. //from OAM
  8. type
  9. TMySprite = record
  10. gfx: pcuint16;
  11. size: SpriteSize;
  12. format: SpriteColorFormat;
  13. rotationIndex: integer;
  14. paletteAlpha: integer;
  15. x: integer;
  16. y: integer;
  17. end;
  18. var
  19. sprites: array [0..2] of TMySprite;
  20. i, angle: integer;
  21. begin
  22. with Sprites[0] do
  23. begin
  24. gfx := nil;
  25. size := SpriteSize_32x32;
  26. format := SpriteColorFormat_Bmp;
  27. rotationIndex := 0;
  28. paletteAlpha := 15;
  29. x := 20;
  30. y := 15;
  31. end;
  32. with Sprites[1] do
  33. begin
  34. gfx := nil;
  35. size := SpriteSize_32x32;
  36. format := SpriteColorFormat_256Color;
  37. rotationIndex := 0;
  38. paletteAlpha := 0;
  39. x := 20;
  40. y := 80;
  41. end;
  42. with Sprites[2] do
  43. begin
  44. gfx := nil;
  45. size := SpriteSize_32x32;
  46. format := SpriteColorFormat_16Color;
  47. rotationIndex := 0;
  48. paletteAlpha := 1;
  49. x := 20;
  50. y := 136;
  51. end;
  52. videoSetModeSub(MODE_0_2D);
  53. consoleDemoInit();
  54. //initialize the sub sprite engine with 1D mapping 128 byte boundary
  55. //and no external palette support
  56. oamInit(oamSub, SpriteMapping_Bmp_1D_128, false);
  57. vramSetBankD(VRAM_D_SUB_SPRITE);
  58. //allocate some space for the sprite graphics
  59. for i := 0 to 2 do
  60. sprites[i].gfx := oamAllocateGfx(oamSub, sprites[i].size, sprites[i].format);
  61. //ugly positional printf
  62. iprintf(#27 + '[1;1H' + 'Direct Bitmap:');
  63. iprintf(#27 + '[9;1H' + '256 color:');
  64. iprintf(#27 + '[16;1H' + '16 color:');
  65. //fill bmp sprite with the color red
  66. dmaFillHalfWords(ARGB16(1,31,0,0), sprites[0].gfx, 32*32*2);
  67. //fill the 256 color sprite with index 1 (2 pixels at a time)
  68. dmaFillHalfWords((1 shl 8) or 1, sprites[1].gfx, 32*32);
  69. //fill the 16 color sprite with index 1 (4 pixels at a time)
  70. dmaFillHalfWords((1 shl 12) or (1 shl 8) or (1 shl 4) or 1, sprites[2].gfx, 32*32 div 2);
  71. //set index 1 to blue...this will be the 256 color sprite
  72. SPRITE_PALETTE_SUB[1] := RGB15(0,31,0);
  73. //set index 17 to green...this will be the 16 color sprite
  74. SPRITE_PALETTE_SUB[16 + 1] := RGB15(0,0,31);
  75. angle := 0;
  76. while true do
  77. begin
  78. for i := 0 to 2 do
  79. begin
  80. oamSet(
  81. oamSub, //sub display
  82. i, //oam entry to set
  83. sprites[i].x, sprites[i].y, //position
  84. 0, //priority
  85. sprites[i].paletteAlpha, //palette for 16 color sprite or alpha for bmp sprite
  86. sprites[i].size,
  87. sprites[i].format,
  88. sprites[i].gfx,
  89. sprites[i].rotationIndex,
  90. true, //double the size of rotated sprites
  91. false, //don't hide the sprite
  92. false, false, //vflip, hflip
  93. false //apply mosaic
  94. );
  95. end;
  96. oamRotateScale(oamSub, 0, angle, (1 shl 8), (1 shl 8));
  97. angle := angle + 64;
  98. swiWaitForVBlank();
  99. //send the updates to the hardware
  100. oamUpdate(oamSub);
  101. end;
  102. end.