Home > Blockchain >  Dereference pointer from unnamed namespace not working
Dereference pointer from unnamed namespace not working

Time:02-22

For a Wii homebrew game engine I'm working on, I have this (shortened) script that handles printing text:

#include <grrlib.h>

#include "graphics.hpp"

#include "Vera_ttf.h"

namespace {
    GRRLIB_ttfFont *font = GRRLIB_LoadTTF(Vera_ttf, Vera_ttf_size);
}

namespace graphics {
namespace module {

void print(const char *str, int x, int y) {
    GRRLIB_PrintfTTF(x, y, font, str, 12, 0xFFFFFFFF);
}

} // module
} // graphics

This code compiles, however, when trying to call the print function above, nothing is rendered. Weirdly enough, removing the unnamed namespace and changing the print function to this:

void print(const char *str, int x, int y) {
    static GRRLIB_ttfFont *font = GRRLIB_LoadTTF(Vera_ttf, Vera_ttf_size);

    GRRLIB_PrintfTTF(x, y, font, str, 12, 0xFFFFFFFF);
}

works fine. However, I would like the font variable to be changeable by another setFont function. How can I achieve this?

Here's is the GRRLIB_PrintfTTF function code if anyone needs it: https://github.com/GRRLIB/GRRLIB/blob/master/GRRLIB/GRRLIB/GRRLIB_ttf.c

CodePudding user response:

Since you have to initialize the library with GRRLIB_Init(), you can provide a similar init function to ensure that your variables are initialized after the library.

#include <grrlib.h>

#include "graphics.hpp"

#include "Vera_ttf.h"

namespace {
    GRRLIB_ttfFont *font = NULL;
}

namespace graphics {
    void InitGraphics() {
        font = GRRLIB_LoadTTF(Vera_ttf, Vera_ttf_size);
    }

namespace module {

void print(const char *str, int x, int y) {
    GRRLIB_PrintfTTF(x, y, font, str, 12, 0xFFFFFFFF);
}

} // module
} // graphics

Call graphics::InitGraphics() after you call GRRLIB_Init()

  • Related