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

  • Thread starter Thread starter xerpi
  • Start date Start date
  • Views Views 97,216
  • Replies Replies 501
  • Likes Likes 32
Sometimes I get a flickering while reloading both screens, I don't know yet why it does it. I'm only swap buffers after I close both topscreen and bottomscreen frames.

Edit: I fixed the issue giving 1 to wait_for_vblank instead of 0. The last issue I have is that sometimes text appears to be glitched.
 
Last edited by EventAssistant,
Has anyone tried using "part_scale" functions? On citra I get nothing rendered instead of the texture and on hardware I get this beautiful crash

Code:
sf2d_draw_texture_part_scale(botBg, 30, 10, 40, 0, 40, 20, 16 / 40, 23 / 20);
    sf2d_draw_texture_part_rotate_scale(botBg, 30, 10, 0, 40, 0, 40, 20, 16 / 40, 23 / 20);

try this way

Code:
sf2d_draw_texture_part_scale(botBg,30,10,40,0,40,20,16.0/40.0,23.0/20.0);
sf2d_draw_texture_part_rotate_scale(botBg,30,10,0,40,0,40,20,16.0/40.0,23.0/20.0);
 
  • Like
Reactions: cearp
try this way

Code:
sf2d_draw_texture_part_scale(botBg,30,10,40,0,40,20,16.0/40.0,23.0/20.0);
sf2d_draw_texture_part_rotate_scale(botBg,30,10,0,40,0,40,20,16.0/40.0,23.0/20.0);
Can't believe I spent 3 hours trying to figure out what was the issue and the fix being so simple... Thanks!

Isn't the compiler supposed to be smart enough to convert numerics on function calls or am I missing something here?
 
Can't believe I spent 3 hours trying to figure out what was the issue and the fix being so simple... Thanks!

Isn't the compiler supposed to be smart enough to convert numerics on function calls or am I missing something here?

The copiler rounds to an int a division by two int values, and a constant numeral without a decimal point is an int.

But don't worry too much, I knew the solution because I spent a lot of time debugging the some issue too. :rofl2:
 
For some reason, this line of code is stopping my app (in TWLoader, but music still plays).
Code:
sf2d_create_texture_mem_RGBA8(&textureData, 32, 32, TEXFMT_RGBA8, SF2D_PLACE_RAM);
Why is that?
 
For some reason, this line of code is stopping my app (in TWLoader, but music still plays).
Code:
sf2d_create_texture_mem_RGBA8(&textureData, 32, 32, TEXFMT_RGBA8, SF2D_PLACE_RAM);
Why is that?
This create a sf2d_texture from your already existing data, so the first parameter must be a valid pointer to raw RGBA8 image data.
 
This create a sf2d_texture from your already existing data, so the first parameter must be a valid pointer to raw RGBA8 image data.
textureData is already valid. It's declared as
Code:
u32 textureData[1024];
if that matters.

Here's the code. The line that stops the app is commented "Stops TWLoader for some reason".
 
Last edited by RocketRobz,
textureData is already valid. It's declared as
Code:
u32 textureData[1024];
if that matters.

Here's the code. The line that stops the app is commented "Stops TWLoader for some reason".

don't do "&textureData", simply "textureData"
 
textureData is allocated on the stack, the address gets invalid when textureData goes out of scope(when the function returns) and 4kb is alot of memory, maybe it just doesn't fit on the stack

EDIT: of course gets textureData copied to a new texture, forget the first answer
EDIT 2: After looking into the source code of sf2d I found what's very likely the error. The hardware tiling doesn't work with small textures(like 32*32). Try to generate a larger picture and use a subset of it
 
Last edited by catlover007,
textureData is allocated on the stack, the address gets invalid when textureData goes out of scope(when the function returns) and 4kb is alot of memory, maybe it just doesn't fit on the stack

EDIT: of course gets textureData copied to a new texture, forget the first answer
EDIT 2: After looking into the source code of sf2d I found what's very likely the error. The hardware tiling doesn't work with small textures(like 32*32). Try to generate a larger picture and use a subset of it

that array consumes 0x1000 bytes from the stack while the normal stacksize is 0x8000, so it should fit just fine.
also, that should work with old sf2d, but as you said, the hardware-based tiler needs a texture with a minimum size of 64x
 
So, I'm playing around with homebrew lately and then saw this library. It's awesome and everything works perfectly. But I have a weird problem with the stereoscopic 3D effect:
The effect works as intended but I can't only see the elevated image, I can also see both of the images the 3DS draws. So it's like, I see the 3D image but also the two non-3D images, even though I can only see them very weak but on both eyes. I don't know if you get what I mean but it's hard to explain.

Is there a way to fix that? Or can't we just make better 3D yet?

EDIT: Here is my code:
Code:
#include <3ds.h>
#include <sf2d.h>
#include <stdio.h>
#include <string.h>

#define CONFIG_3D_SLIDERSTATE (*(volatile float*)0x1FF81080)

#define WIDTH 400
#define HEIGHT 240
#define SCREEN_SIZE WIDTH * HEIGHT * 2
#define BUF_SIZE SCREEN_SIZE * 2

extern const struct images
{
    unsigned int      width;
    unsigned int      height;
    unsigned int      bytes_per_pixel;
    unsigned char     pixel_data[];
} logo, background;

int main()
{
    sf2d_init();     
    sf2d_set_3D(true);
    sf2d_set_clear_color(RGBA8(0x00, 0x00, 0x00, 0x00));

    float offset = 0;
    
    sf2d_texture *sfBackground  = sf2d_create_texture_mem_RGBA8(background.pixel_data, background.width, background.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);
    sf2d_texture *sfShip  = sf2d_create_texture_mem_RGBA8(logo.pixel_data, logo.width, logo.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);
    
    while (aptMainLoop()) {
        offset = CONFIG_3D_SLIDERSTATE * 10;
    
        hidScanInput();
        if (hidKeysDown() & KEY_START) break;
                
        sf2d_start_frame(GFX_TOP, GFX_LEFT); // render for left eye
                sf2d_draw_texture(sfBackground, 0, 0);
                sf2d_draw_texture(sfShip, 80, 30);
        sf2d_end_frame();
        
        sf2d_start_frame(GFX_TOP, GFX_RIGHT); // render for right eye
            sf2d_draw_texture(sfBackground, 0, 0);
            sf2d_draw_texture(sfShip, 80 - offset, 30);
        sf2d_end_frame();

        sf2d_swapbuffers();
    }

    sf2d_fini();
    return 0;
}
 
Last edited by StuntHacks,
Yo! I'm messing with hb stuff recently and I got installed sf2d and sfil libs without problems (once i got the portlibs installed) but Sfil lib seems not to work when using sfil_load_PNG_file. I know that's the problem because whenever I comment that line, the hb compiles.

I get this error on Visual Studio:

c:/devkitPro/libctru/lib\libsfil.a(sfil_png.o): In function `_sfil_read_png_file_fn':
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:11: undefined reference to `png_get_io_ptr'
1> c:/devkitPro/libctru/lib\libsfil.a(sfil_png.o): In function `_sfil_load_PNG_generic':
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:24: undefined reference to `png_create_read_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:29: undefined reference to `png_create_info_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:36: undefined reference to `png_set_longjmp_fn'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:37: undefined reference to `png_destroy_read_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:43: undefined reference to `png_set_read_fn'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:44: undefined reference to `png_set_sig_bytes'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:45: undefined reference to `png_read_info'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:50: undefined reference to `png_get_IHDR'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:57: undefined reference to `png_set_expand'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:79: undefined reference to `png_get_valid'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:85: undefined reference to `png_read_update_info'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:102: undefined reference to `png_read_image'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:104: undefined reference to `png_destroy_read_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:113: undefined reference to `png_destroy_info_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:115: undefined reference to `png_destroy_read_struct'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:83: undefined reference to `png_set_packing'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:55: undefined reference to `png_get_valid'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:77: undefined reference to `png_set_expand_gray_1_2_4_to_8'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:80: undefined reference to `png_set_tRNS_to_alpha'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:68: undefined reference to `png_set_gray_to_rgb'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:64: undefined reference to `png_set_filler'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:61: undefined reference to `png_set_scale_16'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:72: undefined reference to `png_set_palette_to_rgb'
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:73: undefined reference to `png_set_filler'
1> c:/devkitPro/libctru/lib\libsfil.a(sfil_png.o): In function `sfil_load_PNG_file':
1> c:/Users/Usuario/Desktop/3DS/sfillib-master/libsfil/source/sfil_png.c:134: undefined reference to `png_sig_cmp'
1>collect2.exe : error : ld returned 1 exit status

I have LibPng installed too. Do I need something else? :(
 
You forgot to tell the linker that it should link your project with libpng.

Add after -lsf2d(in the line which begins with LIBS := ) in your makefile
Code:
-lpng -lz

And double check if the line beginning with LIBDIRS is
Code:
LIBDIRS   := $(CTRULIB) $(PORTLIBS)
 
You forgot to tell the linker that it should link your project with libpng.

Add after -lsf2d(in the line which begins with LIBS := ) in your makefile
Code:
-lpng -lz

And double check if the line beginning with LIBDIRS is
Code:
LIBDIRS   := $(CTRULIB) $(PORTLIBS)

I was missing that. Thanks!
 
Okay, I got it to compile but whenever I try to draw the image, it keeps in black screen and i dunno what the heck i am doing wrong.

Can someone guide me on what happens, pls?

Code:
#if __INTELLISENSE__
typedef unsigned int __SIZE_TYPE__;
typedef unsigned long __PTRDIFF_TYPE__;
#define __attribute__(q)
#define __builtin_strcmp(a,b) 0
#define __builtin_strlen(a) 0
#define __builtin_memcpy(a,b) 0
#define __builtin_va_list void*
#define __builtin_va_start(a,b)
#define __extension__
#endif

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

#include <3ds.h>
#include <sf2d.h>
#include <sfil.h>

int main()
{
    // Apt
    aptInit();

    // SF2D
    sf2d_init();
    sf2d_set_clear_color(RGBA8(0, 0, 0, 0));
    sf2d_set_3D(false);

    sf2d_texture *texture = sfil_load_PNG_file("3ds/data/Project/Art/image.png",SF2D_PLACE_RAM);

    while (aptMainLoop())
    {
        hidScanInput();

       //Salimos pulsando Select
       if (hidKeysDown() & KEY_SELECT)
       {
           break;
       }

        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
        sf2d_draw_texture(texture, 320/2 - texture->width/2, 240/2 - texture->height/2);
        sf2d_end_frame();

        sf2d_swapbuffers();
    }

    // Exit services
    aptExit();
    sf2d_fini();

    return 0;
}
 
Okay, I got it to compile but whenever I try to draw the image, it keeps in black screen and i dunno what the heck i am doing wrong.

Can someone guide me on what happens, pls?

Code:
#if __INTELLISENSE__
typedef unsigned int __SIZE_TYPE__;
typedef unsigned long __PTRDIFF_TYPE__;
#define __attribute__(q)
#define __builtin_strcmp(a,b) 0
#define __builtin_strlen(a) 0
#define __builtin_memcpy(a,b) 0
#define __builtin_va_list void*
#define __builtin_va_start(a,b)
#define __extension__
#endif

#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif

#include <3ds.h>
#include <sf2d.h>
#include <sfil.h>

int main()
{
    // Apt
    aptInit();

    // SF2D
    sf2d_init();
    sf2d_set_clear_color(RGBA8(0, 0, 0, 0));
    sf2d_set_3D(false);

    sf2d_texture *texture = sfil_load_PNG_file("3ds/data/Project/Art/image.png",SF2D_PLACE_RAM);

    while (aptMainLoop())
    {
        hidScanInput();

       //Salimos pulsando Select
       if (hidKeysDown() & KEY_SELECT)
       {
           break;
       }

        sf2d_start_frame(GFX_BOTTOM, GFX_LEFT);
        sf2d_draw_texture(texture, 320/2 - texture->width/2, 240/2 - texture->height/2);
        sf2d_end_frame();

        sf2d_swapbuffers();
    }

    // Exit services
    aptExit();
    sf2d_fini();

    return 0;
}
First of all calling aptInit isn't needed anymore. Ctrulib initializes the apt Services anyways. Try to put the alpha value of the clear color to 255, to make it opaque.

Does it crashes, or it is possible to exit the app?
 
First of all calling aptInit isn't needed anymore. Ctrulib initializes the apt Services anyways. Try to put the alpha value of the clear color to 255, to make it opaque.

Does it crashes, or it is possible to exit the app?
Oh, i see.

It keeps on black screen, but i'm testing on citra. Gonna try on the console.

Edit: It crashes

Edit2: Drawing a rectangle (and not drawing the texture) works. I don't get what i am doing wrong.
 
Last edited by Manurocker95,
Oh, i see.

It keeps on black screen, but i'm testing on citra. Gonna try on the console.

Edit: It crashes

Edit2: Drawing a rectangle (and not drawing the texture) works. I don't get what i am doing wrong.
Try to change the path to sd://3ds/data/Project/Art/image.png
The current path assumes that image.png is located in path where your executable is/3ds/data/Project/Art
I don't think this is the place where have your image

EDIT: sdmc:/ not sd://
 
Last edited by catlover007,

Site & Scene News

Popular threads in this forum