summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/examples/20-nanovg/nanovg.cpp
diff options
context:
space:
mode:
Diffstat (limited to '3rdparty/bgfx/examples/20-nanovg/nanovg.cpp')
-rw-r--r--3rdparty/bgfx/examples/20-nanovg/nanovg.cpp115
1 files changed, 82 insertions, 33 deletions
diff --git a/3rdparty/bgfx/examples/20-nanovg/nanovg.cpp b/3rdparty/bgfx/examples/20-nanovg/nanovg.cpp
index e6f89ce7c3c..77e8d41f114 100644
--- a/3rdparty/bgfx/examples/20-nanovg/nanovg.cpp
+++ b/3rdparty/bgfx/examples/20-nanovg/nanovg.cpp
@@ -29,6 +29,8 @@
#include <bx/string.h>
#include <bx/timer.h>
+#include <bimg/decode.h>
+
#include "entry/entry.h"
#include "imgui/imgui.h"
#include "nanovg/nanovg.h"
@@ -39,6 +41,9 @@ BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wunused-parameter");
#include "blendish.h"
BX_PRAGMA_DIAGNOSTIC_POP();
+namespace
+{
+
#define ICON_SEARCH 0x1F50D
#define ICON_CIRCLED_CROSS 0x2716
#define ICON_CHEVRON_RIGHT 0xE75E
@@ -68,17 +73,16 @@ static char* cpToUTF8(int cp, char* str)
str[n] = '\0';
switch (n)
{
- case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000;
- case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000;
- case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000;
- case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800;
- case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0;
- case 1: str[0] = char(cp);
+ case 6: str[5] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x4000000; BX_FALLTHROUGH;
+ case 5: str[4] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x200000; BX_FALLTHROUGH;
+ case 4: str[3] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x10000; BX_FALLTHROUGH;
+ case 3: str[2] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0x800; BX_FALLTHROUGH;
+ case 2: str[1] = 0x80 | (cp & 0x3f); cp = cp >> 6; cp |= 0xc0; BX_FALLTHROUGH;
+ case 1: str[0] = char(cp); BX_FALLTHROUGH;
}
return str;
}
-
void drawWindow(struct NVGcontext* vg, const char* title, float x, float y, float w, float h)
{
float cornerRadius = 3.0f;
@@ -939,13 +943,48 @@ struct DemoData
int images[12];
};
+int createImage(struct NVGcontext* _ctx, const char* _filePath, int _imageFlags)
+{
+ uint32_t size;
+ void* data = load(_filePath, &size);
+ if (NULL == data)
+ {
+ return 0;
+ }
+
+ bimg::ImageContainer* imageContainer = bimg::imageParse(
+ entry::getAllocator()
+ , data
+ , size
+ , bimg::TextureFormat::RGBA8
+ );
+ unload(data);
+
+ if (NULL == imageContainer)
+ {
+ return 0;
+ }
+
+ int texId = nvgCreateImageRGBA(
+ _ctx
+ , imageContainer->m_width
+ , imageContainer->m_height
+ , _imageFlags
+ , (const uint8_t*)imageContainer->m_data
+ );
+
+ bimg::imageFree(imageContainer);
+
+ return texId;
+}
+
int loadDemoData(struct NVGcontext* vg, struct DemoData* data)
{
for (uint32_t ii = 0; ii < 12; ++ii)
{
char file[128];
bx::snprintf(file, 128, "images/image%d.jpg", ii+1);
- data->images[ii] = nvgCreateImage(vg, file, 0);
+ data->images[ii] = createImage(vg, file, 0);
if (data->images[ii] == 0)
{
printf("Could not load %s.\n", file);
@@ -988,13 +1027,6 @@ void freeDemoData(struct NVGcontext* vg, struct DemoData* data)
nvgDeleteImage(vg, data->images[i]);
}
-#if defined(_MSC_VER) && (_MSC_VER < 1800)
-inline float round(float _f)
-{
- return float(int(_f) );
-}
-#endif
-
void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float height, float mx, float my)
{
struct NVGtextRow rows[3];
@@ -1076,10 +1108,10 @@ void drawParagraph(struct NVGcontext* vg, float x, float y, float width, float h
nvgBeginPath(vg);
nvgFillColor(vg, nvgRGBA(255,192,0,255) );
nvgRoundedRect(vg
- , bx::fround(bounds[0])-4.0f
- , bx::fround(bounds[1])-2.0f
- , bx::fround(bounds[2]-bounds[0])+8.0f
- , bx::fround(bounds[3]-bounds[1])+4.0f
+ , bx::fround(bounds[0])-4.0f
+ , bx::fround(bounds[1])-2.0f
+ , bx::fround(bounds[2]-bounds[0])+8.0f
+ , bx::fround(bounds[3]-bounds[1])+4.0f
, (bx::fround(bounds[3]-bounds[1])+4.0f)/2.0f-1.0f
);
nvgFill(vg);
@@ -1203,13 +1235,19 @@ void renderDemo(struct NVGcontext* vg, float mx, float my, float width, float he
class ExampleNanoVG : public entry::AppI
{
- void init(int _argc, char** _argv) BX_OVERRIDE
+public:
+ ExampleNanoVG(const char* _name, const char* _description)
+ : entry::AppI(_name, _description)
+ {
+ }
+
+ void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
- m_width = 1280;
- m_height = 720;
- m_debug = BGFX_DEBUG_TEXT;
+ m_width = _width;
+ m_height = _height;
+ m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::init(args.m_type, args.m_pciId);
@@ -1229,17 +1267,17 @@ class ExampleNanoVG : public entry::AppI
imguiCreate();
m_nvg = nvgCreate(1, 0);
- bgfx::setViewSeq(0, true);
+ bgfx::setViewMode(0, bgfx::ViewMode::Sequential);
loadDemoData(m_nvg, &m_data);
bndSetFont(nvgCreateFont(m_nvg, "droidsans", "font/droidsans.ttf") );
- bndSetIconImage(nvgCreateImage(m_nvg, "images/blender_icons16.png", 0) );
+ bndSetIconImage(createImage(m_nvg, "images/blender_icons16.png", 0) );
m_timeOffset = bx::getHPCounter();
}
- int shutdown() BX_OVERRIDE
+ int shutdown() override
{
freeDemoData(m_nvg, &m_data);
@@ -1253,10 +1291,24 @@ class ExampleNanoVG : public entry::AppI
return 0;
}
- bool update() BX_OVERRIDE
+ bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
+ 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();
+
int64_t now = bx::getHPCounter();
const double freq = double(bx::getHPFrequency() );
float time = (float)( (now-m_timeOffset)/freq);
@@ -1268,11 +1320,6 @@ class ExampleNanoVG : public entry::AppI
// 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::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/20-nanovg");
- bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: NanoVG is small antialiased vector graphics rendering library.");
-
nvgBeginFrame(m_nvg, m_width, m_height, 1.0f);
renderDemo(m_nvg, float(m_mouseState.m_mx), float(m_mouseState.m_my), float(m_width), float(m_height), time, 0, &m_data);
@@ -1302,4 +1349,6 @@ class ExampleNanoVG : public entry::AppI
DemoData m_data;
};
-ENTRY_IMPLEMENT_MAIN(ExampleNanoVG);
+} // namespace
+
+ENTRY_IMPLEMENT_MAIN(ExampleNanoVG, "20-nanovg", "NanoVG is small antialiased vector graphics rendering library.");