Homebrew [Release] {beta} sf2dlib - Simple and Fast 2D library (using the GPU)

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,858
Country
Italy
What about pixel formatting for textures?
I tried to use same function i use in luaGraphics for opening PNGs ( https://github.com/Rinnegatamante/lpp-3ds/blob/master/source/luaGraphics.cpp ) and i got this result:
f.png
 

xerpi

Well-Known Member
OP
Member
Joined
Dec 25, 2011
Messages
212
Trophies
1
Age
28
Location
Barcelona
XP
1,331
Country
  • Like
Reactions: Margen67

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,858
Country
Italy
The GPU uses a Z-order "tiling" format for textures, you need to convert your textures first, for example with this function:
Code:
void texture_tile32(const u32 *src, u32 *dst, int width, int height);

Yes, i used them but still corrupt images:
Code:
static int lua_loadimg(lua_State *L)
{
    int argc = lua_gettop(L);
    if (argc != 1) return luaL_error(L, "wrong number of arguments");
    char* text = (char*)(luaL_checkstring(L, 1));
    Handle fileHandle;
    u32 bytesRead;
    u16 magic;
    u64 long_magic;
    FS_path filePath=FS_makePath(PATH_CHAR, text);
    FS_archive script=(FS_archive){ARCH_SDMC, (FS_path){PATH_EMPTY, 1, (u8*)""}};
    FSUSER_OpenFileDirectly(NULL, &fileHandle, script, filePath, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
    FSFILE_Read(fileHandle, &bytesRead, 0, &magic, 2);
    Bitmap* bitmap;
    if (magic == 0x5089){
        FSFILE_Read(fileHandle, &bytesRead, 0, &long_magic, 8);
        FSFILE_Close(fileHandle);
        svcCloseHandle(fileHandle);
        if (long_magic == 0x0A1A0A0D474E5089) bitmap = decodePng(text); // same as openPng but without flip and B<->R channel change
    }else if (magic == 0x4D42){
        FSFILE_Close(fileHandle);
        svcCloseHandle(fileHandle);
        bitmap = LoadBitmap(text);
    }else if (magic == 0xD8FF){
        FSFILE_Close(fileHandle);
        svcCloseHandle(fileHandle);
        bitmap = OpenJPG(text);
    }
    if(!bitmap) return luaL_error(L, "Error loading image");
    sf2d_texture *tex = sf2d_create_texture(bitmap->width, bitmap->height, GPU_RGBA8, SF2D_PLACE_VRAM);
    texture_tile32((u32*)bitmap->pixels, (u32 *)tex->data, bitmap->width, bitmap->height);
    free(bitmap->pixels);
    free(bitmap);
    lua_pushinteger(L, (u32)(tex));
    return 1;
}
 
static int lua_drawimg(lua_State *L)
{
    int argc = lua_gettop(L);
    if (argc != 3) return luaL_error(L, "wrong number of arguments");
    int x = luaL_checkinteger(L,1);
    int y = luaL_checkinteger(L,2);
    sf2d_texture* texture = (sf2d_texture*)luaL_checkinteger(L,3);
    sf2d_draw_texture(texture, x, y);
    return 0;
}
 

KingOfHell

Well-Known Member
Newcomer
Joined
Mar 15, 2015
Messages
74
Trophies
0
Age
36
XP
83
Country
United States
Glad to see someone got around to it. I was planning on doing a whole GUI system, but haven't gotten that far yet.
 

Reisyukaku

Onii-sama~
Developer
Joined
Feb 11, 2014
Messages
1,534
Trophies
2
Website
reisyukaku.org
XP
5,422
Country
United States
The sample game compiles just fine but doesnt work on the 3DS. In a CIA cfw, it doesnt create and icon on HM and just crashes back to sysnand when running via devmenu, and in ninjhax its just a black screen that you need to reboot out of.
 

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,858
Country
Italy
The sample game compiles just fine but doesnt work on the 3DS. In a CIA cfw, it doesnt create and icon on HM and just crashes back to sysnand when running via devmenu, and in ninjhax its just a black screen that you need to reboot out of.

What sample game are you talking about?
Anyway GPU rendering is not available on all firmwares.
 

erk_is_my_homeboy

Member
Newcomer
Joined
Feb 26, 2015
Messages
15
Trophies
0
Location
Maryland
XP
62
Country
United States
I am having similar issues to Reisyukaku, when I compile the sample and run on hardware I only get a black screen with some random pixels turned on, or it just crashes back to home menu. I am running on a black N3DS 9.0.0-20U using ninjhax on a retail copy of Cubic Ninja. Could someone please provide a verified working .3dsx and .smdh of the sample so I can confirm it isn't just my compile set up?

Regardless, good luck on the project and I look forward to using this and seeing how others do so.
 

Cid2mizard

Well-Known Member
Member
Joined
Aug 16, 2007
Messages
401
Trophies
1
Age
43
Location
Maubeuge
XP
2,481
Country
France
It should be noted that it is necessary to have https://github.com/smealum/aemstro To test , I copied the / sf2dlib and / sample in my repertoire devkitpro I then made a Build sf2dlib which went well . As against the sample does not compile error in the directory paths. We could not have a simple explanation for installation ?​
 

erk_is_my_homeboy

Member
Newcomer
Joined
Feb 26, 2015
Messages
15
Trophies
0
Location
Maryland
XP
62
Country
United States
You have to install the sf2dlib as well as build it. If you are using make, just type 'make install' after you use 'make' to compile sf2dlib. If you are not using make, you'll need to manually move some files around. Put sf2dlib/libsf2d/lib/sflibsf2d.a into your devkitPro/libctru/lib folder. Then put sf2dlib/libsf2d/include/sf2d.h into devkitPro/libctru/include. I hope this makes sense, I find compiling is much easier using linux and make.

I still have not been able to do anything with this library, the sample still gives me a black screen and/or crashes. Attached is a zip of my compiled .3dsx and .smdh of the sample, anyone who wants to can test it out!
 

Attachments

  • sample.zip
    81.4 KB · Views: 153
  • Like
Reactions: Cid2mizard

Rinnegatamante

Well-Known Member
Member
Joined
Nov 24, 2014
Messages
3,162
Trophies
2
Age
29
Location
Bologna
Website
rinnegatamante.it
XP
4,858
Country
Italy
You have to install the sf2dlib as well as build it. If you are using make, just type 'make install' after you use 'make' to compile sf2dlib. If you are not using make, you'll need to manually move some files around. Put sf2dlib/libsf2d/lib/sflibsf2d.a into your devkitPro/libctru/lib folder. Then put sf2dlib/libsf2d/include/sf2d.h into devkitPro/libctru/include. I hope this makes sense, I find compiling is much easier using linux and make.

I still have not been able to do anything with this library, the sample still gives me a black screen and/or crashes. Attached is a zip of my compiled .3dsx and .smdh of the sample, anyone who wants to can test it out!

You need to update libctru to solve black screen issue.
 
  • Like
Reactions: Margen67

Cid2mizard

Well-Known Member
Member
Joined
Aug 16, 2007
Messages
401
Trophies
1
Age
43
Location
Maubeuge
XP
2,481
Country
France
Install & sample perfect, thank you...

Edit : i will convert my projects with sf2dlib, i begin with 100 Boxes 2DS for test...but i have a gfx bug on top of bottom screen... a diagonal line on 6 tiles...


100boxes_bug.png




Code:
int main()
{
    sf2d_init();
 
    srand(osGetTime());
 
    Tile[0].texture = sf2d_create_texture(grey_img.width, grey_img.height, GPU_RGBA8, SF2D_PLACE_VRAM);
    sf2d_fill_texture_from_RGBA8(Tile[0].texture, grey_img.pixel_data, grey_img.width, grey_img.height);
    sf2d_texture_tile32(Tile[0].texture);
 
    Tile[1].texture = sf2d_create_texture(green_img.width, green_img.height, GPU_RGBA8, SF2D_PLACE_VRAM);
    sf2d_fill_texture_from_RGBA8(Tile[1].texture, green_img.pixel_data, green_img.width, green_img.height);
    sf2d_texture_tile32(Tile[1].texture);
 
    Tile[2].texture = sf2d_create_texture(yellow_img.width, yellow_img.height, GPU_RGBA8, SF2D_PLACE_VRAM);
    sf2d_fill_texture_from_RGBA8(Tile[2].texture, yellow_img.pixel_data, yellow_img.width, yellow_img.height);
    sf2d_texture_tile32(Tile[2].texture);
 
    sf2d_texture *tex_cursor = sf2d_create_texture(cursor_img.width, cursor_img.height, GPU_RGBA8, SF2D_PLACE_VRAM);
    sf2d_fill_texture_from_RGBA8(tex_cursor, cursor_img.pixel_data, cursor_img.width, cursor_img.height);
    sf2d_texture_tile32(tex_cursor);
 
 
    while (aptMainLoop()) {
 
        hidScanInput();
        kDown = hidKeysDown();
        hidTouchRead(&Stylus);
 
        if (kDown & KEY_START) break;
 
        manageInput();
 
        //sf2d_start_frame(GFX_TOP, GFX_LEFT);
        //sf2d_end_frame();
 
        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
 
        for(lignes = 0; lignes < MAX_TILEX; lignes++)
        {
            for(colonnes = 0; colonnes < MAX_TILEY; colonnes++)
            {
            //Les tiles à afficher
                sf2d_draw_texture(Tile[level_courant[lignes*MAX_TILEY + colonnes]].texture, lignes*TILE_SIZEX, colonnes*TILE_SIZEY);
            }
        }
 
        sf2d_draw_texture(tex_cursor, TILE_X*TILE_SIZEX-4, TILE_Y*TILE_SIZEY-4);
 
        sf2d_end_frame();
 
        sf2d_swapbuffers();
    }
 
    sf2d_free_texture(tex_cursor);
    sf2d_free_texture(Tile[0].texture);
    sf2d_free_texture(Tile[1].texture);
    sf2d_free_texture(Tile[2].texture);
 
    sf2d_fini();
    return 0;
}

Edit 2 :

If i move the grid an color background, i have no graphic bug

normal.png



Edit 3 :

Sample works with Citra but not with 3DS NINJHAX !!!
 

erk_is_my_homeboy

Member
Newcomer
Joined
Feb 26, 2015
Messages
15
Trophies
0
Location
Maryland
XP
62
Country
United States
Thank you Rinnegatamante!! You are right, the sample on github does not work properly. Your program works fine (nice asteroids game, and nice CPU/GPU switching), and I took the sample code from xerpi's first post, and it works just fine on my 3DS. I am going to start to play with this library now, it looks very promising. Sad thing that the project I am working on right now uses console based graphics, so it can't make use of this! But I may end up using it anyway.
 

Cid2mizard

Well-Known Member
Member
Joined
Aug 16, 2007
Messages
401
Trophies
1
Age
43
Location
Maubeuge
XP
2,481
Country
France
(I haven't been able to test this lib on a real 3DS, because there's some problem with the ctrulib code on at least the 4.X FW)

I'm use Ninjhax 1.1b with 3DS 4.5.0-8E, maybe we have same problem... Just 4.X FW

Edit : This code works on real 3DS, i think the problem came from image texture function and not draw rectangle/triangle functions.

Code:
int main()
{
    sf2d_init();
    sf2d_set_clear_color(RGBA8(0x0, 0x00, 0x00, 0x00));
 
    while (aptMainLoop()) {
 
        hidScanInput();
        if (hidKeysDown() & KEY_START) break;
 
        sf2d_start_frame(GFX_TOP, GFX_LEFT);
            //Draws a 100x100 yellow rectangle (255, 255, 00, 255) at (150, 70)
            sf2d_draw_rectangle(150, 70, 100, 100, RGBA8(0xFF, 0xFF, 0x00, 0xFF));
        sf2d_end_frame();
 
        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
            //Draws a 70x100 blue rectangle (0, 0, 00, 255) at (120, 30)
            sf2d_draw_rectangle(120, 30, 70, 100, RGBA8(0x00, 0x00, 0xFF, 0xFF));
        sf2d_end_frame();
 
        sf2d_swapbuffers();
    }
 
    sf2d_fini();
    return 0;
}


I think bug came from SF2D_PLACE_VRAM

i replace SF2D_PLACE_VRAM to SF2D_PLACE_RAM and put (u32*) for pixel_data, it works on real hardware...

Code:
sf2d_texture *tex1 = sf2d_create_texture(citra_img.width, citra_img.height, GPU_RGBA8, SF2D_PLACE_RAM);
    sf2d_fill_texture_from_RGBA8(tex1, (u32*) citra_img.pixel_data, citra_img.width, citra_img.height);

Finally you are using the GPU ?
 
  • Like
Reactions: Margen67 and filfat

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • No one is chatting at the moment.
    SylverReZ @ SylverReZ: https://www.youtube.com/watch?v=a6v3cT3b59A&t=12s