sprite: migrate to using OAM from libnds

Co-authored-by: Ronny Chan <ronny6993@gmail.com>
This commit is contained in:
lifehackerhansol 2024-10-04 22:25:46 -07:00
parent a7dd6151a4
commit 6d74f51cd1
No known key found for this signature in database
GPG Key ID: 80FB184AFC0B3B0E
3 changed files with 22 additions and 63 deletions

View File

@ -140,6 +140,7 @@ void cGdi::initBg(const std::string& aFileName)
_sprites[index].show();
}
}
oamUpdate(&oamMain);
}
void cGdi::swapLCD(void)
@ -177,7 +178,6 @@ void cGdi::activeFbMain(void)
setMainEngineLayer( MEL_UP );
zeroMemory( _bufferMain1, 0x20000 );
//fillMemory( _bufferMain3, 0x20000, 0x8f008f00 );
fillMemory( _bufferMain3, 0x20000, 0xffffffff );
REG_BLDCNT = BLEND_ALPHA | BLEND_DST_BG2 | BLEND_DST_BG3;
@ -205,7 +205,6 @@ void cGdi::activeFbSub(void)
// 初始化为文字模式
_subEngineMode = SEM_GRAPHICS;
//_subEngineMode = SEM_TEXT;
// BMP bg 的参数设置,从 VRAM地址 0x06200000 开始优先级3
REG_BG2CNT_SUB = BG_BMP16_256x256 | BG_BMP_BASE(0) | BG_PRIORITY_1;
@ -219,8 +218,6 @@ void cGdi::activeFbSub(void)
_bufferSub1 = (u16 *)0x06200000;
_bufferSub2 = (u16 *)new u32[256*192/2];
//fillMemory( _bufferSub2, 0x18000, 0xfc00fc00 );
//fillMemory( _bufferSub1, 0x18000, 0xfc00fc00 );
fillMemory( _bufferSub2, 0x18000, 0xffffffff );
fillMemory( _bufferSub1, 0x18000, 0xffffffff );
@ -237,7 +234,7 @@ void cGdi::activeFbSub(void)
swiWaitForVBlank(); //remove tearing at top screen
// 模式5开两层BG一层BMP一层文字(用于调试)bmp层现在默认关闭
videoSetModeSub( MODE_5_2D | DISPLAY_BG2_ACTIVE );// | DISPLAY_BG2_ACTIVE );
videoSetModeSub( MODE_5_2D | DISPLAY_BG2_ACTIVE );
}
void cGdi::drawLine( s16 x1, s16 y1, s16 x2, s16 y2, GRAPHICS_ENGINE engine )
@ -268,10 +265,6 @@ void cGdi::drawLine( s16 x1, s16 y1, s16 x2, s16 y2, GRAPHICS_ENGINE engine )
xs=x2+1;
xe=x1;
}
//for(int px=xs;px<=xe;px++) {
// drawPixel(px,y1,engine);
// //SetPixel(px,y1,Color);
//}
if( GE_MAIN == engine )
fillRect( _penColor, _penColor, xs, y1, xe - xs + 1, 1, engine );
else
@ -317,18 +310,7 @@ void cGdi::drawLine( s16 x1, s16 y1, s16 x2, s16 y2, GRAPHICS_ENGINE engine )
}
while(py!=ye) {
//if(AALineFlag==false){
drawPixel(x1+(int)px,y1+py,engine);
//}else{
// int Alpha=(int)(px*32);
// if(Alpha<0){
// while(Alpha<=0) Alpha+=32;
// }else{
// while(32<=Alpha) Alpha-=32;
// }
// SetPixelAlpha(x1+(int)px+0,y1+py,Color,32-Alpha);
// SetPixelAlpha(x1+(int)px+1,y1+py,Color,Alpha);
//}
px+=xv;
py+=yv;
}
@ -362,9 +344,9 @@ void cGdi::fillRect( u16 color1, u16 color2, s16 x, s16 y, u16 w, u16 h, GRAPHIC
if( GE_MAIN == engine )
pDest = _bufferMain2 + (y << 8) + x + _layerPitch;//_bufferMain2 + y * 256 + x + _layerPitch;
pDest = _bufferMain2 + (y << 8) + x + _layerPitch;
else
pDest = _bufferSub2 + (y << 8) + x; //_bufferSub2 + y * 256 + x;
pDest = _bufferSub2 + (y << 8) + x;
bool destAligned = !(x & 1);
@ -459,7 +441,6 @@ void cGdi::bitBlt( const void * src, s16 srcW, s16 srcH, s16 destX, s16 destY, u
void cGdi::bitBlt( const void * src, s16 destX, s16 destY, u16 destW, u16 destH, GRAPHICS_ENGINE engine )
{
//dbg_printf("x %d y %d w %d h %d\n", destX, destY, destW, destH );
u16 * pSrc = (u16 *)src;
u16 * pDest = NULL;
@ -487,7 +468,6 @@ void cGdi::bitBlt( const void * src, s16 destX, s16 destY, u16 destW, u16 destH,
// 不是偶数也可以,但要求在内存中 src 的 pitch 凑成偶数
void cGdi::maskBlt( const void * src, s16 destX, s16 destY, u16 destW, u16 destH, GRAPHICS_ENGINE engine )
{
//dbg_printf("x %d y %d w %d h %d\n", destX, destY, destW, destH );
u16 * pSrc = (u16 *)src;
u16 * pDest = NULL;
bool destAligned = !(destX & 1);
@ -619,24 +599,19 @@ void cGdi::textOutRect( s16 x, s16 y, u16 w, u16 h, const char * text, GRAPHICS_
void cGdi::present( GRAPHICS_ENGINE engine )
{
if( GE_MAIN == engine ) { // 翻转主引擎
//u16 * temp = _bufferMain1;
//_bufferMain1 = _bufferMain2;
//_bufferMain2 = temp;
//REG_BG2CNT ^= BG_BMP_BASE( 128 / 16 );
dmaCopyWordsGdi( 3, _bufferMain2 + _layerPitch,
_bufferMain1 + (_mainEngineLayer << 16), 256 * 192 * 2 );
fillMemory( (void *)(_bufferMain2 + _layerPitch), 256 * 192 * 2, 0 );
oamUpdate(&oamMain);
} else if ( GE_SUB == engine ) { // 翻转副引擎
if( SEM_GRAPHICS == _subEngineMode )
dmaCopyWordsGdi( 3, (void *)_bufferSub2, (void *)_bufferSub1, 256 * 192 * 2 );
//else if( SEM_TEXT == _subEngineMode )
// dmaCopyWords( 3, (void *)_bufferSub3, (void *)_bufferSub1, 32768 );
fillMemory( (void *)_bufferSub2, 0x18000, 0xffffffff );
}
//dbg_printf( "\x1b[0;20%f\n", updateTimer() );
}
//special version for window switching

View File

@ -65,11 +65,10 @@ public:
void initBg(const std::string& aFileName);
void drawPixel( u8 x, u8 y, GRAPHICS_ENGINE engine ) {
//if( y > (u8)SCREEN_HEIGHT - 1 ) y = (u8)SCREEN_WIDTH - 1;
if( GE_MAIN == engine )
*(_bufferMain2 + ((u32)y << 8) + x + _layerPitch ) = _penColor; //_bufferMain2[y * SCREEN_WIDTH + x] = _penColor;
*(_bufferMain2 + ((u32)y << 8) + x + _layerPitch ) = _penColor;
else
_bufferSub2[((u32)y << 8) + x] = _penColor; //_bufferSub2[y * SCREEN_WIDTH + x] = _penColor;
_bufferSub2[((u32)y << 8) + x] = _penColor;
}
void drawLine( s16 x1, s16 y1, s16 x2, s16 y2, GRAPHICS_ENGINE engine );

View File

@ -18,6 +18,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <nds.h>
#include "sprite.h"
@ -31,24 +32,22 @@ cSprite::~cSprite()
void cSprite::sysinit()
{
SpriteEntry * psprites = (SpriteEntry *)OAM;
//SpriteRotation * pspriteRotations = (SpriteRotation * )OAM;
oamInit(&oamMain, SpriteMapping_Bmp_1D_128, true);
for(int i = 0; i < 128; i++)
{
psprites[i].attribute[0] = ATTR0_DISABLED;
psprites[i].attribute[1] = 0;
psprites[i].attribute[2] = 0;
psprites[i].filler = 0;
oamMain.oamMemory[i].attribute[0] = ATTR0_DISABLED;
oamMain.oamMemory[i].attribute[1] = 0;
oamMain.oamMemory[i].attribute[2] = 0;
oamMain.oamMemory[i].filler = 0;
}
//DC_FlushRange( _sprites, 128 * sizeof(SpriteEntry) );
//dmaCopy( _sprites, OAM, 128 * sizeof(SpriteEntry) );
oamUpdate(&oamMain);
oamEnable(&oamMain);
}
void cSprite::init( u16 id )
{
//_alpha = 0;
_id = id;
_size = SS_SIZE_32;
@ -59,8 +58,8 @@ void cSprite::init( u16 id )
_priority = 2;
_entry = ((SpriteEntry *)OAM) + _id;
_affine = ((SpriteRotation * )OAM) + _id;
_entry = &oamMain.oamMemory[_id];
_affine = &oamMain.oamRotationMemory[_id];
// initial x = 0, hidden, bitmap obj mode, square shape
_entry->attribute[0] = ATTR0_DISABLED | ATTR0_BMP | ATTR0_SQUARE | 0;
@ -72,11 +71,6 @@ void cSprite::init( u16 id )
_entry->attribute[2] = ATTR2_ALPHA(15) | ATTR2_PRIORITY(0) | 0;
setScale( 1, 1 );
//for(int i=0;i<32*32;i++)
// SPRITE_GFX[i]=RGB15(0,0,27)|(1<<15); //dont forget alpha bit
//update();
}
@ -92,8 +86,7 @@ void cSprite::hide()
void cSprite::setAlpha( u8 alpha )
{
_alpha = alpha & 0x1f;
_entry->attribute[2] = (_entry->attribute[2] & (~0xf000)) | ATTR2_ALPHA(_alpha);
oamSetAlpha(&oamMain, _id, alpha);
}
void cSprite::setPosition( u16 x, u8 y )
@ -118,11 +111,10 @@ void cSprite::setPosition( u16 x, u8 y )
};
x -= offset;
_x = x & 0x1FF;
_entry->attribute[1] = (_entry->attribute[1] & (~0x1FF)) | _x;
y -= offset;
_y = y & 0xFF;
_entry->attribute[0] = (_entry->attribute[0] & (~0xFF)) | _y;
oamSetXY(&oamMain, _id, _x, _y);
}
void cSprite::setSize( SPRITE_SIZE size )
@ -143,13 +135,6 @@ u16 * cSprite::buffer()
return SPRITE_GFX + (_bufferOffset * 64);
}
//void cSprite::update()
//{
// //DC_FlushRange( _sprites, 128 * sizeof(SpriteEntry) );
// SpriteEntry * psprites = (SpriteEntry * )OAM;
// dmaCopy( _sprites, &psprites[_id], sizeof(SpriteEntry) );
//}
void cSprite::setBufferOffset( u32 offset )
{
_bufferOffset = offset;
@ -179,7 +164,7 @@ void cSprite::setScale( float scaleX, float scaleY )
void cSprite::setPriority( u8 priority )
{
_priority = priority;
_entry->attribute[2] = (_entry->attribute[2] & (~0x0C00)) | ATTR2_PRIORITY(_priority);
oamSetPriority(&oamMain, _id, _priority);
}