- 项目文件
源文件就"helloworld.cpp"一个,logo.h是logo的字节,以数组的形式存着
static const uint8_t s_logo[4000]
- 代码大纲
- 源码分析
入口在最下面
ENTRY_IMPLEMENT_MAIN(
ExampleHelloWorld
, "00-helloworld"
, "Initialization and debug text."
, "https://bkaradzic.github.io/bgfx/examples.html#helloworld"
);
这是一个宏定义,在"bgfx\examples\common\entry\entry.h"里面有定义:
#if ENTRY_CONFIG_IMPLEMENT_MAIN
#define ENTRY_IMPLEMENT_MAIN(_app, ...) \
int _main_(int _argc, char** _argv) \
{ \
_app app(__VA_ARGS__); \
return entry::runApp(&app, _argc, _argv); \
}
#else
#define ENTRY_IMPLEMENT_MAIN(_app, ...) \
_app s_ ## _app ## App(__VA_ARGS__)
#endif // ENTRY_CONFIG_IMPLEMENT_MAIN
项目属性中已经定义了"ENTRY_CONFIG_IMPLEMENT_MAIN",其实就是会定义main函数,并生成_app类的对象,调用"entry::runApp"方法.定义如下:
int runApp(AppI* _app, int _argc, const char* const* _argv)
{
_app->init(_argc, _argv, s_width, s_height);
bgfx::frame();
WindowHandle defaultWindow = { 0 };
setWindowSize(defaultWindow, s_width, s_height);
#if BX_PLATFORM_EMSCRIPTEN
s_app = _app;
emscripten_set_main_loop(&updateApp, -1, 1);
#else
while (_app->update() )
{
if (0 != bx::strLen(s_restartArgs) )
{
break;
}
}
#endif // BX_PLATFORM_EMSCRIPTEN
return _app->shutdown();
}
这里包含了环境的设置,及渲染的主循环,跟平时手写openGL的代码结构已经差不多了.主要还是调用
_app->init();
_app->update();
_app->shutdown();
退回来看helloworld.cpp文件.ENTRY_IMPLEMENT_MAIN宏调用为入口,传入的ExampleHelloWorld类名,以及窗口名称描述等信息,启动各种设置及渲染循环.具体的实现代码,则在ExampleHelloWorld的虚函数里面.
ExampleHelloWorld构造函数,很明显,仅仅是通过父类设置窗口标题信息.
init函数:设置窗口尺寸信息,并调用bgfx框架的初始化方法,开启调试,设置清除颜色,以及创建ui元素.
bgfx::init(init);
setDebug();
setViewClear();
imguiCreate();
shutdown函数:销毁ui元素,关闭框架.
imguiDestroy();
bgfx::shutdown();
update函数是该程序的主要代码实现,直接在源码上注释(ui用法还没研究,先不管了,主要理解渲染的代码):
bool update() override
{
//处理事件
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
//ui操作,暂不研究
imguiBeginFrame(m_mouseState.m_mx
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
showExampleDialog(this);
imguiEndFrame();
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
// Use debug font to print information about this example.
bgfx::dbgTextClear();
//绘制logo图片
bgfx::dbgTextImage(
bx::max<uint16_t>(uint16_t(m_width /2/8 ), 20)-20
, bx::max<uint16_t>(uint16_t(m_height/2/16), 6)-6
, 40
, 12
, s_logo
, 160
);
//绘制文本
bgfx::dbgTextPrintf(0, 1, 0x0f, "Color can be changed with ANSI \x1b[9;me\x1b[10;ms\x1b[11;mc\x1b[12;ma\x1b[13;mp\x1b[14;me\x1b[0m code too.");
bgfx::dbgTextPrintf(80, 1, 0x0f, "\x1b[;0m \x1b[;1m \x1b[; 2m \x1b[; 3m \x1b[; 4m \x1b[; 5m \x1b[; 6m \x1b[; 7m \x1b[0m");
bgfx::dbgTextPrintf(80, 2, 0x0f, "\x1b[;8m \x1b[;9m \x1b[;10m \x1b[;11m \x1b[;12m \x1b[;13m \x1b[;14m \x1b[;15m \x1b[0m");
const bgfx::Stats* stats = bgfx::getStats();
bgfx::dbgTextPrintf(0, 2, 0x0f, "Backbuffer %dW x %dH in pixels, debug text %dW x %dH in characters."
, stats->width
, stats->height
, stats->textWidth
, stats->textHeight
);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
//提交一帧
bgfx::frame();
return true;
}
return false;
}
主要绘制用到的就两个函数dbgTextImage和dbgTextPrintf,后面应该有专门的例子演示具体用法,helloworld了解框架就差不都了.