Search code examples
c++directxspritedirectx-10isometric

How do I draw a large (500+) number of sprites to build an isometric tiled map in DirectX 10?


So, I've been puttering around with a few books on DirectX (specifically 10) and am trying my hand at building a game that uses it, but I'm stumped by a problem that none of the books seem to mention: I'm building an isometric map with sprites and as the board gets larger, the program slows down dramatically. Each tile is 64x45 and by the time that I have constructed a 36x19 (684) tile map with sprites, it takes roughly 6 seconds for my application to load up and about 5 seconds between each frame. Since debugging with breakpoints hasn't yielded any good clues I've narrowed down the area in my current code where the problem is to this specific section:

Any help would be appreciated.

    const FLOAT clearcolor[4] = {0.0f, 0.0f, 0.0f, 0.0f}; 
    pD3DDevice->ClearRenderTargetView(pRenderTargetView, clearcolor);
    if(spriteObject != NULL){
        spriteObject->Begin(D3DX10_SPRITE_SORT_TEXTURE);        
        pD3DDevice->OMGetBlendState(&pOriginalBlendState10, OriginalBlendFactor, &OriginalSampleMask);
        if(pBlendState10)
        {
            FLOAT NewBlendFactor[4] = {0,0,0,0};
            pD3DDevice->OMSetBlendState(pBlendState10, NewBlendFactor,0xffffffff);
        }
        D3DXMatrixScaling(&matScale, 64, 45, 1.0f);

        for(int y = 0;y < 36;y++){
            px=0;
            if(y % 2 == 1)
                px-=32;
            for(int x = 0;x < 19;x++){  
                D3DXMatrixTranslation(&matTrans, px, viewport.Height - py, 0.1f);
                testTile.matWorld = (matScale * matTrans);
                spriteObject->DrawSpritesImmediate(&testTile, 1, 0, 0);
                px+=64;
            }
            py+=23;
        }
        spriteObject->End();
        pD3DDevice->OMSetBlendState(pOriginalBlendState10, OriginalBlendFactor, OriginalSampleMask);

Solution

  • The whole point of DrawSpritesImmediate is that you want to call it with an array of hundreds or thousands of sprites... not one sprite at a time.

    Alternatively, try changing your "Immediate" call to using DrawSpritesBuffered, followed by a ID3DX10Sprite::Flush (outside the loops, just before the End()).

    But even with that... 5s/frame sounds awfully slow... are you sure you're not using the software "reference rasterizer" instead of the HW ?