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

  • Thread starter Thread starter xerpi
  • Start date Start date
  • Views Views 97,215
  • Replies Replies 501
  • Likes Likes 32
  • Like
Reactions: MRJPGames
I'm updating some code (Handy3ds) from the lib versions of september 2015 to the last commits on github, but now using sftd, printing with size bigger than 10 results in a blurred string.

I tested it with the font of my last release (Hack.ttf) and with roboto-normal.ttf.

Before
View attachment 41179

After
View attachment 41178

The font ins't the same, but I hope the problem i clear. Don' mind for the wrong text positio, it's easy to fix.

Hints?
I think I know what's happening, are you first drawing with a small size, and then drawing another text with the same font but a (much) bigger font size?
 
I think I know what's happening, are you first drawing with a small size, and then drawing another text with the same font but a (much) bigger font size?

Yes. Every frame I first draw FPS info on left top corner with size 10, and then I draw the bigger text.


Edit: following your hint, I loaded th font twice (in two distinct variables) and used the first for printing in size 10 and the second to print in size 16. With this workaround I fixed the problem.
 
Last edited by nop90,
Yes. Every frame I first draw FPS info on left top corner with size 10, and then I draw the bigger text.


Edit: following your hint, I loaded th font twice (in two distinct variables) and used the first for printing in size 10 and the second to print in size 16. With this workaround I fixed the problem.
But that should not be needed, as I'm also using sftdlib I would like to see this fixed! But good to know about a temporary workaround for the time being (haven't had this issue before but good to know anyways).
 
I'm having a little problem compiling the code at the OP. Whenever I try to compile I get "undefined reference for sf2d_init" and all the other sf2d functions but I can compile the example program in the library folder. Am I doing something wrong?
 
But that should not be needed, as I'm also using sftdlib I would like to see this fixed! But good to know about a temporary workaround for the time being (haven't had this issue before but good to know anyways).

Did you add the option lo link the sf2d library in the makefile LIB variable?

It should be like the following line:

Code:
LIBS := -lsf2d -lctru -lm
 
I'm having a little problem compiling the code at the OP. Whenever I try to compile I get "undefined reference for sf2d_init" and all the other sf2d functions but I can compile the example program in the library folder. Am I doing something wrong?

Probably you're not using -lsf2d in your makefile, or it can't find the path to the compiled lib (forgot to include it in the make?).
 
if you added -lsf2d, then another cause could be the order of the libs. -sf2d have to be before -lctru as in my example.

If this doesn't solve, then please post your makefile.
 
if you added -lsf2d, then another cause could be the order of the libs. -sf2d have to be before -lctru as in my example.

If this doesn't solve, then please post your makefile.
Uhm... I don't have ANY problem with any of my libs, if you are not talking to me, why did you quote my post? I think you should be talking to @kprovost7314 because he DOES have an issue with his sf2dlib XD
 
Great library! Finally I've made some progress with learning homebrew development (citro3d with it's poor documentation was the reason why I couldn't understand anything) :D

Is there an implemented way to create transparecy mask from a color (eg. I want to turn every pink pixel from a texture to a transparent one)? Or I just have to do it manualy by scanning trough every pixel and changing it into a transparent one if the color mathes?
 
Is there an implemented way to create transparecy mask from a color (eg. I want to turn every pink pixel from a texture to a transparent one)? Or I just have to do it manualy by scanning trough every pixel and changing it into a transparent one if the color mathes?

It's simpler to use an image with an alpha channel (RGBA8) instead of an image in RGB8 with color mask. You can easily convert your images with GIMP.
 
It's simpler to use an image with an alpha channel (RGBA8) instead of an image in RGB8 with color mask. You can easily convert your images with GIMP.
Thanks! I know I can, I just wanted to port some stuff from my PC project where I'm using bitmaps with color masks.
 
Thanks! I know I can, I just wanted to port some stuff from my PC project where I'm using bitmaps with color masks.

I was thinking that writing a shader that sets an alpha value of 0 to a given RGB value is very easy.

This way you could have a custom bmpDraw funcion that does what you want. I knoe only a little of shaders coding, you could find such an task on some tutorial on line.

I'll try to implement it as an exercise when I have time, but now I'm working on too many things.
 
Unfortunately, PICA has no support for fragment shaders, only vertex and geometry shaders. So you can you only output vertex colors and not texture colors. You'd have to get more creative, I don't know.

If I were you, I'd just use imagemagick or something to quickly convert all your images to have the transparency. But maybe there is some solution I'm not thinking about.

EDIT: the command is something like: convert input.bmp -transparent <color> output.png
 
Last edited by TheCruel,
  • Like
Reactions: MRJPGames
Unfortunately, PICA has no support for fragment shaders, only vertex and geometry shaders. So you can you only output vertex colors and not texture colors. You'd have to get more creative, I don't know.

If I were you, I'd just use imagemagick or something to quickly convert all your images to have the transparency. But maybe there is some solution I'm not thinking about.

EDIT: the command is something like: convert input.bmp -transparent <color> output.png
Alternatively, instead of converting the image offline, the image could very easily be converted in memory to RBGA format during the loading process (as the asker has noted). This way different versions of the same game dont have to ship with an entirely different set of images.

Example:
Code:
u8 *inputBuffer; // Assume buffer has already been loaded and is RGB (24 bpp) format
u8 *outputBuffer = linearAlloc(width * height * 4); // allocate enough space for 32 bpp
for (int y = 0; y < height; ++y) {
  for (int x = 0; x < width; ++x) {
    u32 inputIndex  = (x + y * height) * 3;
    u32 outputIndex = (x + y * height) * 4;
    u8 r = inputBuffer[inputIndex + 0]; // Note: reverse order if buffer is BGR
    u8 g = inputBuffer[inputIndex + 1];
    u8 b = inputBuffer[inputIndex + 2];
    outputBuffer[outputIndex + 0] = r;
    outputBuffer[outputIndex + 1] = g;
    outputBuffer[outputIndex + 2] = b;
    if (r == 255 && g == 255 && b == 255) outputBuffer[outputIndex + 3] = 0; // write complete transparency for magenta pixels
    else outputBuffer[outputIndex + 3] = 255; // write complete opacity for other pixels
  }
}
 
Last edited by machinamentum,
  • Like
Reactions: MOHRCORE
I was thinking that writing a shader that sets an alpha value of 0 to a given RGB value is very easy.

This way you could have a custom bmpDraw funcion that does what you want. I knoe only a little of shaders coding, you could find such an task on some tutorial on line.

I'll try to implement it as an exercise when I have time, but now I'm working on too many things.

EDIT: Sorry, machinamentum already said what I wrote. But I still can add, that it would be easier to do it with sf2d_get_pixel (sf2d_texture *texture, int x, int y) and sf2d_set_pixel (sf2d_texture *texture, int x, int y, u32 new_color).

However in this case, you'll need sf2d_texture, but that's probably what you need an image for.

Example code:

Code:
void applyColorMask(sf2d_texture & tex, const u32 color, const u32 replace)
{
    for(int i1 = 0; i1 < tex.width; i1++)
    {
        for(int i2 = 0; i2 < tex.height; i2++)
        {
            if(sf2d_get_pixel(&tex, i1, i2) == color)
                sf2d_set_pixel(&tex, i1, i2, replace);
        }
    }
}
 
Last edited by MOHRCORE,
Sorry for double post, but it's another (noob?) question this time, so I wanted to separate it from my reply.

How do I create a texture on VRAM? When I use sf2d_create_texture_mem_RGBA8 I can set the place of allocation to SF2D_PLACE_VRAM instead of SF2D_PLACE_RAM, but it freezes/crashes the program.
 

Site & Scene News

Popular threads in this forum