ds sprite coding help

Discussion in 'NDS - Emulation and Homebrew' started by keven3477, Oct 19, 2014.

  1. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    hi im new at making hombrew and I read some manuals on how to make homebrew.
    however I tried to make a hombrew display a sprite but it didn't come out good.
    I tried using the manual in patater to display the ship but the pixels were all fizzy and separated didn't even look like a ship at all.
    Ive already spent 3 months searching how to succesfuly display sprites on google but no luck.
    what im asking is can anyone give me a good tutorial on displaying 2d sprites

    code I used to display sprite

    Code:
    void initSprites(OAMTable * oam, SpriteInfo *spriteInfo) {
        /*  Define some sprite configuration specific constants.
        *
        *  We will use these to compute the proper index into memory for certain
        *  tiles or palettes.
        *
        *  OFFSET_MULTIPLIER is calculated based on the following formula from
        *  GBATEK (http://nocash.emubase.de/gbatek.htm#dsvideoobjs):
        *      TileVramAddress = TileNumber * BoundaryValue
        *  Since SPRITE_GFX is a uint16*, the compiler will increment the address
        *  it points to by 2 for each change in 1 of the array index into
        *  SPRITE_GFX. (The compiler does pointer arithmetic.)
        */
        static const int BYTES_PER_16_COLOR_TILE = 32;
        static const int COLORS_PER_PALETTE = 16;
        static const int BOUNDARY_VALUE = 32; /* This is the default boundary value
                                              * (can be set in REG_DISPCNT) */
        static const int OFFSET_MULTIPLIER = BOUNDARY_VALUE /
                                            sizeof(SPRITE_GFX[0]);
     
        /* Keep track of the available tiles */
        int nextAvailableTileIdx = 0;
     
        /* Create the ship sprite. */
        static const int SHUTTLE_OAM_ID = 0;
        assert(SHUTTLE_OAM_ID < SPRITE_COUNT);
        SpriteInfo * shuttleInfo = &spriteInfo[SHUTTLE_OAM_ID];
        SpriteEntry * shuttle = &oam->oamBuffer[SHUTTLE_OAM_ID];
     
        /* Initialize shuttleInfo */
        shuttleInfo->oamId = SHUTTLE_OAM_ID;
        shuttleInfo->width = 64;
        shuttleInfo->height = 64;
        shuttleInfo->angle = 462;
        shuttleInfo->entry = shuttle;
     
        /*
        *  Configure attribute 0.
        *
        *  OBJCOLOR_16 will make a 16-color sprite. We specify that we want an
        *  affine sprite (via isRotateScale) here because we would like to rotate
        *  the ship.
        */
        shuttle->y = SCREEN_HEIGHT / 2 - shuttleInfo->height;
        shuttle->isRotateScale = true;
        /* This assert is a check to see a matrix is available to store the affine
        * transformation matrix for this sprite. Of course, you don't have to have
        * the matrix id match the affine id, but if you do make them match, this
        * assert can be helpful. */
        assert(!shuttle->isRotateScale || (shuttleInfo->oamId < MATRIX_COUNT));
        shuttle->isSizeDouble = false;
        shuttle->blendMode = OBJMODE_NORMAL;
        shuttle->isMosaic = false;
        shuttle->colorMode = OBJCOLOR_16;
        shuttle->shape = OBJSHAPE_SQUARE;
     
        /*
        *  Configure attribute 1.
        *
        *  rotationIndex refers to the loation of affine transformation matrix. We
        *  set it to a location computed with a macro. OBJSIZE_64, in our case
        *  since we are making a square sprite, creates a 64x64 sprite.
        */
        shuttle->x = SCREEN_WIDTH / 2 - shuttleInfo->width * 2 +
                        shuttleInfo->width / 2;
        shuttle->rotationIndex = shuttleInfo->oamId;
        shuttle->size = OBJSIZE_64;
     
        /*
        *  Configure attribute 2.
        *
        *  Configure which tiles the sprite will use, which priority layer it will
        *  be placed onto, which palette the sprite should use, and whether or not
        *  to show the sprite.
        */
        shuttle->gfxIndex = nextAvailableTileIdx;
        nextAvailableTileIdx += orangeShuttleTilesLen / BYTES_PER_16_COLOR_TILE;
        shuttle->priority = OBJPRIORITY_0;
        shuttle->palette = shuttleInfo->oamId;
     
        /* Rotate the sprite */
        rotateSprite(&oam->matrixBuffer[shuttleInfo->oamId],
                    shuttleInfo->angle);
     
     
     
        /* Copy over the sprite palettes */
        dmaCopyHalfWords(SPRITE_DMA_CHANNEL,
                        orangeShuttlePal,
                        &SPRITE_PALETTE[shuttleInfo->oamId *
                                        COLORS_PER_PALETTE],
                        orangeShuttlePalLen);
     
     
        /* Copy the sprite graphics to sprite graphics memory */
        dmaCopyHalfWords(SPRITE_DMA_CHANNEL,
                        orangeShuttleTiles,
                        &SPRITE_GFX[shuttle->gfxIndex * OFFSET_MULTIPLIER],
                        orangeShuttleTilesLen);
     
    }
    the red square in the file attached is the patater ship
     

    Attached Files:

  2. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    anyone?
    sorry for double posting
     
  3. Technicmaster0

    Technicmaster0 GBAtemp Psycho!

    Member
    3,065
    633
    Oct 22, 2011
    Gambia, The
    Why don't you use NFLib? It makes coding much easier.
     
    Wolfgange likes this.
  4. PypeBros

    PypeBros Advanced Member

    Newcomer
    54
    50
    Oct 29, 2007
    Belgium
    Belgium
    It sounds like you're experiencing something like "yellow clouds" on this image:[​IMG]
    That could be due to a mis-configuration of tiled vs. bitmap mode.
     
  5. Wolfgange

    Wolfgange GBAtemp Regular

    Member
    224
    84
    Feb 9, 2012
    United States
    Liek mentioned above, I 100% recommend nightfox lib. With any questions you can ask foxi4. There's a nice full-length tutorial he made here:
    https://gbatemp.net/threads/ds-programming-for-newbies.322106/


    If you wanted to get better at c++ before hand, or take a look at some DS Graphics code in context, I recommend looking at an open source DS homebrew game (Such as MineDS).
     
  6. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    i decide to use nightfox recently but I couldn't figure out how to convert into an .img file using the tools however I managed to fix the sprite so it finaly works as a header file
     
  7. Wolfgange

    Wolfgange GBAtemp Regular

    Member
    224
    84
    Feb 9, 2012
    United States
    Nice. I know when I used NFLib, you have to manually open wingrit.exe and load your image and process it into a .img. So mess around with wingrit and you will figure out how.
     
  8. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    i managed to convert 1 sprite into .img and .pal but im having trouble displaying the sprite. i could manage to only display a small gray square whith 4 lines instead of the full sprite which is supposed to be a yu-gi-oh spell card template
     
  9. Wolfgange

    Wolfgange GBAtemp Regular

    Member
    224
    84
    Feb 9, 2012
    United States
    Just making sure, did you look at all the sprite examples in the NFLib download?
     
  10. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    yes but I found out (after trial and error) sprites have to be in a size of 32 or 64 of width or length so it could show properly and I finaly managed to display a sprite. I just wish I knew what sizes are allowed because I want to display a sprite higher than 100, I have checked and 128 or 256 don't work
     
  11. keven3477
    OP

    keven3477 Fresh Prince of Lemonade

    Member
    866
    1,296
    Jul 12, 2012
    United States
    Somwhere you can never find.
    please delete this double post