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

Discussion in '3DS - Homebrew Development and Emulators' started by flarn2006, Oct 17, 2015.

  1. flarn2006
    OP

    flarn2006 GBAtemp Fan

    Member
    376
    164
    Apr 6, 2014
    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:

    [​IMG]
    [​IMG]

    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):

    [​IMG]
    [​IMG]

    And this is what it looks like after switching back:

    [​IMG]
    [​IMG]

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

    TheCruel Developer

    Banned
    1,351
    2,883
    Dec 6, 2013
    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.
     
  3. bnosam

    bnosam Newbie

    Newcomer
    7
    8
    Sep 21, 2015
    Canada
    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, Oct 17, 2015
  4. flarn2006
    OP

    flarn2006 GBAtemp Fan

    Member
    376
    164
    Apr 6, 2014
    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?
     
  5. TheCruel

    TheCruel Developer

    Banned
    1,351
    2,883
    Dec 6, 2013
    United States
    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.
     
  6. machinamentum

    machinamentum GBAtemp Regular

    Member
    163
    450
    Jul 5, 2015
    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.
     
  7. flarn2006
    OP

    flarn2006 GBAtemp Fan

    Member
    376
    164
    Apr 6, 2014
    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.
     
  8. machinamentum

    machinamentum GBAtemp Regular

    Member
    163
    450
    Jul 5, 2015
    United States
    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.