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

Manurocker95

Game Developer & Pokémon Master
Member
Joined
May 29, 2016
Messages
1,512
Trophies
0
Age
29
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,798
Country
Spain
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://

Okay, it seems the image was too big. Thx, btw

Edit: It works on Citra but on my 2DS seems not. Any idea?
 
Last edited by Manurocker95,

Urbanshadow

Well-Known Member
Member
Joined
Oct 16, 2015
Messages
1,578
Trophies
0
Age
33
XP
1,723
Country
Okay, it seems the image was too big. Thx, btw

Edit: It works on Citra but on my 2DS seems not. Any idea?

Citra can't handle relative paths, but the system does. I use relative paths for sfil to load my textures, and then just put them in the citra's sdmc root (since citra will think they are absolute paths to the citra sdmc). For the system, everything goes into the 3ds/namehere/ folder. And that just works fine for me.
 

nop90

Well-Known Member
Member
Joined
Jan 11, 2014
Messages
1,556
Trophies
0
Location
Rome
XP
3,136
Country
Italy
Citra can't handle relative paths, but the system does. I use relative paths for sfil to load my textures, and then just put them in the citra's sdmc root (since citra will think they are absolute paths to the citra sdmc). For the system, everything goes into the 3ds/namehere/ folder. And that just works fine for me.
If you build a cia and install it on your 3ds, it will not work.
 
Last edited by nop90,
  • Like
Reactions: Urbanshadow

Urbanshadow

Well-Known Member
Member
Joined
Oct 16, 2015
Messages
1,578
Trophies
0
Age
33
XP
1,723
Country
Hey guys! Is there anyway to grab the sf2d's matrix stacks so I can make matrix transformations (via citro3d) to the whole scene or to already drawed textures in the sf2d frame? I'd prefer not to modify sf2d's source, but I'll do if I must.

Explanation: Matrix transformations are order dependent. Having a draw_texture_rotate_scale is great, but lacking a draw_texture_scale_rotate blocks the lib to do several cool things like runtime cartesian to isometric drawing, and things like that.

Update: I found out by myself, but I'm putting this here in case anyone steps over the same landmine.

As it turns out, there's this function called sf2d_set_transform which lets you do exactly what I wanted. It has a little catch, though, which is the need to know exactly what you want, because you need to generate the transformation matrix by hand.

Code:
sf2d_texture* tile = sfil_load_PNG_file("./tile.png", SF2D_PLACE_RAM);

// Transformation matrix setup
// Be aware we apply the operations in the inverse order we want them!
// In this case I'll set up a 45º rotation and a y axis 1/2 scale to transform 
// a square texture tile into an isometric tile in runtime
C3D_Mtx isotrans;
Mtx_Identity(&isotrans);
Mtx_Scale(&isotrans, 1.0, 0.5, 1.0);
Mtx_RotateZ(&isotrans, PI/4, true);

while(aptmainloop())
{
       hidScanInput();
       if (hidKeysHeld() & KEY_START) break;

       sf2d_start_frame(GFX_TOP, GFX_LEFT);
           // We can, then, setup the transform...
           sf2d_set_transform(&isotrans);
               // ...draw whatever...
               sf2d_draw_texture(tile, 0, 0);
           // ...and finally disable it to continue drawing as usual
           sf2d_set_transform(NULL);
           sf2d_draw_fill_circle(200, 120, 5, RGBA8(0xff, 0xff, 0xff, 0xff));
       sf2d_end_frame();

       sf2d_swapbuffers();
}
This example lets you transform a tile into 2.5d isometric perspective, while still correctly putting a white 5px dot at the center of the screen. It should work with sf2d_draw_texture_part too, but I haven't tested it yet.
 
Last edited by Urbanshadow,

jockep

Well-Known Member
Member
Joined
Apr 12, 2017
Messages
191
Trophies
0
Age
33
XP
214
Country
Sweden
Trying to make install only gives me:
Code:
/bin/sh: /c/3DS: No such file or directory
make.exe": *** [build] Error 127

Anyone knows how to fix this? I'm not very used with c/c++ at all, been using mostly C#, php, java etc.
 

Manurocker95

Game Developer & Pokémon Master
Member
Joined
May 29, 2016
Messages
1,512
Trophies
0
Age
29
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,798
Country
Spain
Trying to make install only gives me:
Code:
/bin/sh: /c/3DS: No such file or directory
make.exe": *** [build] Error 127

Anyone knows how to fix this? I'm not very used with c/c++ at all, been using mostly C#, php, java etc.

Do you have devkitpro and portlibs correctly installed?
 
  • Like
Reactions: jockep

jockep

Well-Known Member
Member
Joined
Apr 12, 2017
Messages
191
Trophies
0
Age
33
XP
214
Country
Sweden
Do you have devkitpro and portlibs correctly installed?

I can compile and run my own project (Simple console on top screen with text). I do have the precompiled version of portlibs extracted within the devkitpro dir, not sure what i'm doing wrong.
 
Last edited by jockep,

Manurocker95

Game Developer & Pokémon Master
Member
Joined
May 29, 2016
Messages
1,512
Trophies
0
Age
29
Location
Madrid
Website
manuelrodriguezmatesanz.com
XP
2,798
Country
Spain
I can compile and run my own project (Simple console on top screen with text). I do have the precompiled version of portlibs extracted within the devkitarm dir, not sure what i'm doing wrong.

Try installing the latest devkitpro version and placing portlibs again.
 
  • Like
Reactions: jockep

jockep

Well-Known Member
Member
Joined
Apr 12, 2017
Messages
191
Trophies
0
Age
33
XP
214
Country
Sweden
Try installing the latest devkitpro version and placing portlibs again.

Should i uninstall devkit first?

EDIT: I fixed it, it was stupidly easy, i had a space in the folder where i stored all libraries to install. Thanks for answering and taking your time though.
 
Last edited by jockep,

Dysproh

Well-Known Member
Newcomer
Joined
Dec 30, 2016
Messages
48
Trophies
0
Age
34
XP
126
Country
United States
I tried using this, but I am getting one error, (the image I am using is called mouse)
main.o: In function `main':
/home/(name omitted)/projects/cr3ds/application/source/main.c:80: undefined reference to `mouse'
This is the sf2dlib code I am using:
sf2d_texture *tex_image = sf2d_create_texture_mem_RGBA8(mouse.pixel_data, mouse.width, mouse.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);
sf2d_start_frame(GFX_TOP,GFX_LEFT);
sf2d_draw_texture(tex_image, y, x);
sf2d_end_frame();

sf2d_free_texture(tex_image);
sf2d_fini();
return 0;
If it helps, I am running Ubuntu 16.04.
 
Last edited by Dysproh,

nop90

Well-Known Member
Member
Joined
Jan 11, 2014
Messages
1,556
Trophies
0
Location
Rome
XP
3,136
Country
Italy
I tried using this, but I am getting one error, (the image I am using is called mouse)
main.o: In function `main':
/home/(name omitted)/projects/cr3ds/application/source/main.c:80: undefined reference to `mouse'
This is the sf2dlib code I am using:
sf2d_texture *tex_image = sf2d_create_texture_mem_RGBA8(mouse.pixel_data, mouse.width, mouse.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);
sf2d_start_frame(GFX_TOP,GFX_LEFT);
sf2d_draw_texture(tex_image, y, x);
sf2d_end_frame();

sf2d_free_texture(tex_image);
sf2d_fini();
return 0;
If it helps, I am running Ubuntu 16.04.

Reading the error it doesn't seem a problem with sf2dlib but related a to missing declaration of the mouse struct. The code you pasted is not enough to found the problem
 
  • Like
Reactions: Joel16

Dysproh

Well-Known Member
Newcomer
Joined
Dec 30, 2016
Messages
48
Trophies
0
Age
34
XP
126
Country
United States
Reading the error it doesn't seem a problem with sf2dlib but related a to missing declaration of the mouse struct. The code you pasted is not enough to found the problem
Ok, here is my entire main.c:
Code:
#include <string.h>
#include <stdio.h>

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

extern const struct {
    unsigned int width;
    unsigned int height;
    unsigned int bytes_per_pixel;
    unsigned char pixel_data[];
} mouse;

int main(int argc, char **argv) {

    gfxInitDefault();
    consoleInit(GFX_TOP, NULL);

    printf("Welcome to \n");
   
    int x = 80;
    int y = 133;

    // Main loop
    while (aptMainLoop()) {
       
       

        gspWaitForVBlank();
        hidScanInput();

        // Your code goes here

        u32 kDown = hidKeysDown();

        //Determines is key is being held
        u32 kPressed = hidKeysHeld();
       
        //If they are holding a D-pad key, change the mouse position accordingly
        if (kPressed & KEY_DOWN)
            x = x - 1;
        if (kPressed & KEY_UP)
            x = x + 1;
        if (kPressed & KEY_LEFT)
            y = y - 1;
        if (kPressed & KEY_RIGHT)
            y = y + 1;
        if (y > 266)
            y = 266;
        if (y < 0)
            y = 0;
        if (x > 159)
            x = 159;
        if (x < 0)
            x = 0;



        if (kDown & KEY_START)
            break; // break in order to return to hbmenu

        /*u8* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);

       

        memset(fb, 0, 240*400*3);

        printf("\n                  X: %d", x);
        printf("Y: %d", y);

        fb[3*(x+y*240)] = 0xFF;*/
       
        sf2d_texture *tex_image = sf2d_create_texture_mem_RGBA8(mouse.pixel_data, mouse.width, mouse.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);
        sf2d_start_frame(GFX_TOP,GFX_LEFT);
        sf2d_draw_texture(tex_image, y, x);
        sf2d_end_frame();

        sf2d_free_texture(tex_image);
        sf2d_fini();
        return 0;

        // Flush and swap framebuffers
        gfxFlushBuffers();
        gfxSwapBuffers();
    }

    gfxExit();
    return 0;
}
 

Dysproh

Well-Known Member
Newcomer
Joined
Dec 30, 2016
Messages
48
Trophies
0
Age
34
XP
126
Country
United States
I managed to get most of it working, but now it freezes when loading the image, specifically at
homebrew_freeze.png

I used the image so you could see the colors as they are in SublimeText. Is there something wrong with my code?
 

Dysproh

Well-Known Member
Newcomer
Joined
Dec 30, 2016
Messages
48
Trophies
0
Age
34
XP
126
Country
United States
At a minimum you need to initialize sf2d. Add a call to sf2d_init before the render loop.
sf2d is initialized. Is the loading of the image into ram supposed to be pre-initialization? The way I have it it is post-initialization.

My main.c file is this:
Code:
#include <3ds.h>
#include <sf2d.h>
#include <stdio.h>

extern const struct {
    unsigned int width;
    unsigned int height;
    unsigned int bytes_per_pixel;
    unsigned char pixel_data[];
} mouse;

int main(int argc, char **argv)
{
    int x = 80;
    int y = 133;
    //Initialize the sf2d lib
    sf2d_init();
    consoleInit(GFX_TOP, NULL);
    //Set the background color
    sf2d_set_clear_color(RGBA8(0x40, 0x40, 0x40, 0xFF));
    printf("Loading texture(s)...");
    sf2d_texture *tex_image = sf2d_create_texture_mem_RGBA8(mouse.pixel_data, mouse.width, mouse.height, TEXFMT_RGBA8, SF2D_PLACE_RAM);

    // Main loop
    while (aptMainLoop())
    {
        //Scan all the inputs. This should be done once for each frame
        hidScanInput();

     

        //Determines is key is being held
        u32 kPressed = hidKeysHeld();

        sf2d_start_frame(GFX_TOP, GFX_LEFT);
        sf2d_draw_texture(tex_image, 0, 0);
        sf2d_end_frame();
     
        //If they are holding a D-pad key, change the mouse position accordingly
        if (kPressed & KEY_DOWN)
            x = x - 1;
        if (kPressed & KEY_UP)
            x = x + 1;
        if (kPressed & KEY_LEFT)
            y = y - 1;
        if (kPressed & KEY_RIGHT)
            y = y + 1;
        if (y > 266)
            y = 266;
        if (y < 0)
            y = 0;
        if (x > 159)
            x = 159;
        if (x < 0)
            x = 0;

        //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
        //hidKeysHeld returns information about which buttons are currently pressed (regardless if they were pressed or not pressed in the previous frame)
        //hidKeysUp returns information about which buttons are not pressed but were pressed in the previous frame
        u32 kDown = hidKeysDown();

        if (kDown & KEY_START) break; // break in order to return to hbmenu
     
        //Swap the buffers
        sf2d_swapbuffers();
    }
    sf2d_free_texture(tex_image);
    sf2d_fini();
    return 0;
}

EDIT: Issue resolved by setting TEXTURE_MIN_SIZE to 64 and recompiling sf2dlib

EDIT2: 1 more thing, it shows a duplicate image 120px away on the y-axis and same on x-axis.
 
Last edited by Dysproh,

Pepepuhn

Active Member
Newcomer
Joined
Sep 30, 2013
Messages
30
Trophies
0
Age
36
XP
326
Country
Germany
How can I have a small texture tiled accross the whole screen?
In older versions of sf2d I simply could use:
Code:
sf2d_draw_quad_uv(tex_bg, 0, 0, 400, 240, 0, 0, 400/64, 240/64, GPU_TEXTURE_WRAP_S(GPU_REPEAT) | GPU_TEXTURE_WRAP_T(GPU_REPEAT));
However sf2d_draw_quad_uv doesn't accept that many parameters anymore.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BakerMan
    I rather enjoy a life of taking it easy. I haven't reached that life yet though.
  • Xdqwerty
    what are you looking at?
    BakerMan @ BakerMan: