美文网首页
example-00-helloworld

example-00-helloworld

作者: 乌笛 | 来源:发表于2021-04-27 17:38 被阅读0次

include

#include <bx/uint32_t.h>  
// 数据结构统一化
#include "common.h"  
// entry
#include "bgfx_utils.h"  
// example 通用方法
#include "logo.h"  
// logo 图片二进制数据
#include "imgui/imgui.h"  
// imgui 库文件

头文件包含了bx覆写的基础库及example工程特有的util, logo 二进制数据和imgui都是死的内容,没有特别需要研究的内容。

class ExampleHelloWorld : public entry::AppI

entry::AppI 是example中特有的机制,继承的类实现init,update,destroy方法后可以快速的展示需要的特性。

类本身并不能作为程序入口执行,需要后续的宏定义内的内容作为初始化的信息。

ENTRY_IMPLEMENT_MAIN(
      ExampleHelloWorld
    , "00-helloworld"
    , "Initialization and debug text."
    , "https://bkaradzic.github.io/bgfx/examples.html#helloworld"
    );

该宏与 ExampleHelloWorld 信息相互结合,展开后得到 main 方法,该方法后续会由entry_window 启动的线程初始化时调用。

ctor

ExampleHelloWorld(const char* _name, const char* _description, const char* _url)

构造函数接受的三个参数,就是 ENTRY_IMPLEMENT_MAIN 后三个参数,在这个例子中:

_name = "00-helloworld";
_description = "Initialization and debug text.";
_url = "https://bkaradzic.github.io/bgfx/examples.html#helloworld";

init

init 方法中 Args解析了传入的各项参数,原始方法中带有 屏幕 width 和 height 参数。
抛开example这种特殊的调用场景,整个bgfx初始化参数由 bgfx::Init 结构体负责。

bgfx::Init init;
init.type     = args.m_type;
// type 可指定渲染引擎,比如 Opengl、DX 或者 Vulkan 等。
init.vendorId = args.m_pciId;
// 渲染硬件厂商,比如 AMD 或者 Nvidia。
init.resolution.width  = m_width;
init.resolution.height = m_height;
// 显示分辨率
init.resolution.reset  = m_reset;
// 每帧重置上下文相关参数,比如 VSync 或者其他,待观察
bgfx::init(init);
// 初始化bgfx上下文
bgfx::setDebug(m_debug);
// 打印一些硬件信息和驱动相关内容
// Set view 0 clear state.
bgfx::setViewClear(0
    , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
    , 0x303030ff
    , 1.0f
    , 0
    );
// 清理 view 0,标记位  BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH 清理颜色和深度信息,默认清理颜色  0x303030ff,默认深度 1.0F, 模板信息 0
imguiCreate();
// 初始化 imgui 相关内容

imgui 相关信息不做重点研究。一方面这里主要关注的是bgfx的原理和使用,imgui 不是重点,其次,imgui在后续实践过程中不一定会使用,因为其效率相关等问题,目前不做深入探讨。

shutdown

imguiDestroy();

// Shutdown bgfx.
bgfx::shutdown();
// 停止线程
// 释放各种资源
// 检查内存泄漏

update

外层套有大循环
先执行imgui相关内容绘制

// 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();
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();
  1. 设置视口大小
    bgfx::setViewRect ~ glViewport
  2. 清理相关状态
    bgfx:touch(0) ~ glClear
  3. bgfx::dbgText 相关内容,bgfx api 独有,可以理解为 front 相关内容内部支持与实现
  4. 提交或者缓存渲染内容
    单线程 :bgfx::frame() ~ swapbuffer

相关文章

  • example-00-helloworld

    include 头文件包含了bx覆写的基础库及example工程特有的util, logo 二进制数据和imgui...

网友评论

      本文标题:example-00-helloworld

      本文链接:https://www.haomeiwen.com/subject/lqcprltx.html