Handy functions for your 3DS homebrews

Discussion in '3DS - Homebrew Development and Emulators' started by xem, Dec 21, 2014.

  1. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    Hello,

    In this thread, I will gather handy functions that are not present in ctrulib, or functions that are more handy or faster than those present in ctrulib.

    "Handy" means that screen coordinates are more logic (i.e. X:0;Y:0 === top-left of the screen; X axis goes right and Y axis goes down).

    "Faster" means that the code is optimized, and each function has an "in bounds" version that avoids doing all the tests and the crops.

    Let's start with the ones written by Capz, MrJPGames and me.

    Feel free to send your own and I'll update the list.

    drawPixelInBounds
    Warning: Spoilers inside!
    drawPixel
    Warning: Spoilers inside!
    drawImageInBounds
    Warning: Spoilers inside!
    drawImage
    Warning: Spoilers inside!
    drawImageWithAlphaInBounds
    Warning: Spoilers inside!

    Coming soon:

    - drawFullScreenImage (to display images that are exactly 400*240 / 320*240)
    - drawImageWithAlpha (like drawImage but with transparency)
    - drawImageScaled (to draw zoomed images. 2x 3x 4x etc. Maybe scale down x2, x3, x4 ... too.)
    - drawImageScaledWithAlpha
    - drawSprite (to display only a part of an image)
    - drawSpriteWithAlpha
    - drawLine
    - drawBox
    - drawCircle

    PS: you can find a quick tutorial explaining how to use your own images in homebrews here:
    https://gbatemp.net/threads/handy-functions-for-your-3ds-homebrews.376679/#post-5221079
     


  2. DarkFlare69

    DarkFlare69 GBAtemp Psycho!

    Member
    4,710
    2,563
    Dec 8, 2014
    United States
    Ohio
    Great job xem. I could probably use this
     
    xem likes this.
  3. Technicmaster0

    Technicmaster0 GBAtemp Psycho!

    Member
    3,120
    647
    Oct 22, 2011
    Gambia, The
    The file urls are local :P
     
    xem likes this.
  4. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    thanks, I fixed that plus many other typos. :P
     
  5. yodamerlin

    yodamerlin Bok bok.

    Member
    309
    162
    Apr 1, 2014
    I'll be replacing my draw functions with these ones!
     
    xem likes this.
  6. Technicmaster0

    Technicmaster0 GBAtemp Psycho!

    Member
    3,120
    647
    Oct 22, 2011
    Gambia, The
    How can you use the drawImage methods? I mean how can you load the bin file into the memory to use it as u8*?
     
  7. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    Hehe, sorry, I'll add this chapter to my tutorial soon:

    0- use ctrulib's project template: https://github.com/smealum/ctrulib/tree/master/template

    1- convert your image to a rotated bin with this tool: http://xem.github.io/3DShomebrew/tools/image-to-bin.html

    2- Name (example: "img.bin") and place your bin file in the data folder of your project

    3- On top of your program (main.c), do
    Code:
    #include "img_bin.h"
    (i.e. the name of your bin file + "_bin.h")

    4- In your program, before the main loop, initialize your framebuffers, like this:
    Code:
        u8* fbTopLeft = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
        u8* fbTopRight = gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, NULL, NULL);
        u8* fbBottom = gfxGetFramebuffer(GFX_BOTTOM, 0, NULL, NULL);
        memset(fbTopLeft, 0, 240 * 400 * 3);
        memset(fbTopRight, 0, 240 * 400 * 3);
        memset(fbBottom, 0, 240 * 320 * 3);
    5- In your program, in the main loop, call drawImage (or drawImageInBounds if you are sure that it won't overflow), like this:
    Code:
    drawImage(fbTopLeft, 100, 100, 200, 50, img_bin);
    (200 and 50 are the dimensions of my test image, you may use your own)

    6- build and play (or emulate)
     
    Technicmaster0 likes this.
  8. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    Hey guys,
    If you copied these functions in your project earlier, please update them now: I just finished optimizing and fixing many bugs.
    Also, I added a version number in each function's description to help you keep track of the future updates (but we should be good now.)
    Thanks!
     
  9. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    Yay! There is now a working drawImageWithAlphaInBounds function! (v.0.0.1)

    The out-of-bounds version will arrive in the next days
     
    SLiV3R likes this.
  10. Cid2mizard

    Cid2mizard GBAtemp Fan

    Member
    363
    210
    Aug 16, 2007
    France
    Maubeuge
    I'll test your work , thank you. Is it possible to ZOOM intergrer a variable to show the bigger picture ? This will avoid me to convert a pixel art image at size 16 * 16 and 64 * 64 ... it takes too much room for anything ...​


    Edit : drawImageWithAlphaInBounds() have no gfx3dSide_t side for stereoscopie ?
     
  11. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    zoom is possible indeed, I'll make a function allowing that. :P

    My functions take a framebuffer as parameter, so for stereoscopy you have to call the function twice, for the left and right framebuffers.
     
  12. Cid2mizard

    Cid2mizard GBAtemp Fan

    Member
    363
    210
    Aug 16, 2007
    France
    Maubeuge
    This morning i tested "void drawImageInBounds(u8* fb, s16 x, s16 y, u16 w, u16 h, const u8* image)", i think he've problem...

    I use same bin file and same code for 2 tests.

    Code:
    for(lignes = 0; lignes < 15; lignes++)
        {
            for(colonnes = 0; colonnes < 20; colonnes++)
            {
                gfxDrawSprite(GFX_BOTTOM, GFX_BOTTOM, (u8*)TILES_bin, 16, 16, lignes*16, colonnes*16);
                //drawImageInBounds(screenBottom, colonnes*16, lignes*16, 16, 16, (u8*)TILES_bin);
            }
        }
    With "void gfxDrawSprite(gfxScreen_t screen, gfx3dSide_t side, u8* spriteData, u16 width, u16 height, s16 x, s16 y)" from Github :

    [​IMG]

    With "void drawImageInBounds(u8* fb, s16 x, s16 y, u16 w, u16 h, const u8* image)" from you :

    [​IMG]
     
  13. yodamerlin

    yodamerlin Bok bok.

    Member
    309
    162
    Apr 1, 2014
    Right, added the drawPixel functions to wave. It will no longer crash randomly now... hopefully.
     
  14. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    woops, looks like a line is missing. Can you test if you have the same problem with drawSprite please?
     
  15. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    Cid2mizard likes this.
  16. Cid2mizard

    Cid2mizard GBAtemp Fan

    Member
    363
    210
    Aug 16, 2007
    France
    Maubeuge
    image-to-bin tool and drawImage(u8* fb, s16 x, s16 y, u16 w, u16 h, const u8* image) good works, thank you :).
     
  17. DarkFlare69

    DarkFlare69 GBAtemp Psycho!

    Member
    4,710
    2,563
    Dec 8, 2014
    United States
    Ohio
    Are you gonna constantly update this thread? If so, im gonna add this to my bookmarks bar
     
  18. xem
    OP

    xem GBAtemp Regular

    Member
    125
    85
    Nov 22, 2014
    France
    Valbonne
    yep. the functions seem to be good now, but I'll ad many more of them - and maybe make a library someday :)
     
  19. DarkFlare69

    DarkFlare69 GBAtemp Psycho!

    Member
    4,710
    2,563
    Dec 8, 2014
    United States
    Ohio
    Okay, cool :D
     
  20. kprovost7314

    kprovost7314 GBAtemp's Official Bara Master

    Member
    1,691
    890
    Dec 24, 2014
    United States
    In that bara manga ( ͡° ͜ʖ ͡°)
    @xem Are you still working on this?