Homebrew How can I use sf2dlib for the bottom screen, and that + Caelina (GL) for the top?

flarn2006

Well-Known Member
OP
Member
Joined
Apr 6, 2014
Messages
394
Trophies
0
Age
30
XP
523
Country
United States
In my graphing calculator program, I'm planning on adding a 3D graphing capability. It's an obvious addition, considering it's a 3DS app after all.

I used sf2dlib to draw the graphics. I'm planning on using Caelina, which is basically a port of OpenGL, for the 3D drawing. The bottom screen will always be drawn with sf2dlib. The top screen will also be drawn in sf2dlib, like it is now, for the 2D mode, but when you activate 3D mode, it will switch to using Caelina to draw the top screen.

I already have a branch for testing this. I made it so you switch modes by pressing A (this is most likely just a placeholder, so I can test it before I implement a menu) but I'm running into a problem. Obviously, when I first start the program, it works fine. When I switch to 3D mode, the top screen is drawn correctly, but none of the graphics appear on the bottom screen. It's just solid gray, the shade of gray I have set as the sf2dlib clear color.

When I switch back to 2D mode, it really starts acting weird. In Citra, everything drawn with sf2dlib is blue, which is the color of the last vertex it drew with 3D mode on. On an actual 3DS, it looks really weird, with everything being drawn with seemingly random vertex colors that I had used. It goes back to normal if I suspend and resume the app.

Since suspending it fixes the glitched graphics in 2D mode, I'm guessing there's some function I can call when I exit 3D mode to reset the GPU or something. But there's still the issue of the bottom screen not drawing while in 3D mode, which that doesn't fix.

Here's the source code for the branch that exhibits this issue.

This is what it looks like when I first launch the app:

YxmP0rC.png

htnTS51.png


This is what it looks like after I enable 3D mode (note that the bottom screen is supposed to look exactly the same as the previous image):

O4v0AXW.png

eqj5j0L.png


And this is what it looks like after switching back:

1MXod09.png

h11Z7my.png


How am I supposed to use these libraries together in the way I want?
 

TheCruel

Developer
Banned
Joined
Dec 6, 2013
Messages
1,350
Trophies
2
XP
3,130
Country
United States
Both libs handle their own GPU command lists, and ctrulib handles only one at a time. The libs assume you won't be messing with internal GPU state and aren't compatible really. The suspension fix is likely due to the APT resume event resetting the GPU state. That's what I would suggest doing when swapping back and forth between the libs. However, that's probably not very possible since resetting uses internal variables/buffers that you don't have access to. So I can only think of resetting the libs completely whenever you switch (like sf2d_fini() and s2d_init()). But this would have to be done on every frame render from the sounds of it, and it's terribly inefficient and dumb.

You could add some function to one or both libs to expose some internal GPU command buffers to manually swap back and forth, if you insist. Ultimately I would suggest just choosing one lib or the other.
 

bnosam

Member
Newcomer
Joined
Sep 21, 2015
Messages
7
Trophies
0
Location
Canada
XP
67
Country
Canada
I haven't been able to get them to work myself. They both access the GPU directly so you would have to modify both to work in conjunction with each other. I would think it would just be easier to modify Caelina to simply just display textures in the 2D way that libsf2d does.
 
Last edited by bnosam,

flarn2006

Well-Known Member
OP
Member
Joined
Apr 6, 2014
Messages
394
Trophies
0
Age
30
XP
523
Country
United States
Ultimately I would suggest just choosing one lib or the other.

The problem with that is sftdlib, which I use for drawing text, only works with sf2dlib. I don't think it would be too hard to do all the 2D drawing with Caelina as well, with that one exception. Is there a text drawing library that works with Caelina?
 

TheCruel

Developer
Banned
Joined
Dec 6, 2013
Messages
1,350
Trophies
2
XP
3,130
Country
United States
The problem with that is sftdlib, which I use for drawing text, only works with sf2dlib. I don't think it would be too hard to do all the 2D drawing with Caelina as well, with that one exception. Is there a text drawing library that works with Caelina?
Unfortunately, you would likely need to recreate the text drawing stuff. Perhaps tweak sf2d's to use GL functions instead. It would still use freetype, just have to bind the font textures using Caelina.
 

machinamentum

Well-Known Member
Member
Joined
Jul 5, 2015
Messages
163
Trophies
0
XP
549
Country
United States
The problem with that is sftdlib, which I use for drawing text, only works with sf2dlib. I don't think it would be too hard to do all the 2D drawing with Caelina as well, with that one exception. Is there a text drawing library that works with Caelina?
You can use stb_truetype which rasters out ttf fonts to an image buffer that you can feed into an OpenGL texture. There's even an OpenGL example included in the stub_truetype header. Since it is only a single header file, it is as simple as dropping the header into your project and calling a few functions to get basic text drawing working.
 

flarn2006

Well-Known Member
OP
Member
Joined
Apr 6, 2014
Messages
394
Trophies
0
Age
30
XP
523
Country
United States
You can use stb_truetype which rasters out ttf fonts to an image buffer that you can feed into an OpenGL texture. There's even an OpenGL example included in the stub_truetype header. Since it is only a single header file, it is as simple as dropping the header into your project and calling a few functions to get basic text drawing working.

Does it actually put the characters together, so I don't need to handle spacing or anything like that?

Also, the code is all in a header file? I thought that only worked for templates.
 

machinamentum

Well-Known Member
Member
Joined
Jul 5, 2015
Messages
163
Trophies
0
XP
549
Country
United States
Does it actually put the characters together, so I don't need to handle spacing or anything like that?

Also, the code is all in a header file? I thought that only worked for templates.
The sample code does handle the spacing and relative positioning of characters based on the font used.

You can put any code at all in a header file. You just can't have duplicate symbols across compilation units. The stb libraries have the interface and the implementation in the header file, protected with #include guards. You actually need to #define STB_TRUETYPE_IMPLEMENTATION before including the header file, in just one of your compilation units/cpp files, which will include + compile the implementation.
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
    BakerMan @ BakerMan: well then show them how to scratch it (this is a verbalase joke)