對于我正在開發的 Wii 自制游戲引擎,我有這個(縮短的)腳本來處理列印文本:
#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
這段代碼可以編譯,但是,當試圖呼叫print
上面的函式時,什么都沒有呈現。奇怪的是,洗掉未命名的命名空間并將print
函式更改為:
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);
}
作業正常。但是,我希望字體變數可以被另一個setFont
函式改變。我怎樣才能做到這一點?
GRRLIB_PrintfTTF
如果有人需要,這是功能代碼: https ://github.com/GRRLIB/GRRLIB/blob/master/GRRLIB/GRRLIB/GRRLIB_ttf.c
uj5u.com熱心網友回復:
由于您必須使用 GRRLIB_Init() 來初始化庫,因此您可以提供類似的 init 函式來確保在庫之后初始化您的變數。
#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
呼叫 GRRLIB_Init() 后呼叫 graphics::InitGraphics()
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/430112.html
上一篇:c std向量用現有物件初始化