Merge pull request #1097 from olliwang/nanovg

Allows to set view id of NanoVG’s framebuffer manually.
This commit is contained in:
Branimir Karadžić 2017-03-25 21:11:27 -07:00 committed by GitHub
commit 379cd570f9
2 changed files with 39 additions and 15 deletions

View File

@ -1137,47 +1137,68 @@ bgfx::TextureHandle nvglImageHandle(NVGcontext* ctx, int image)
return tex->id;
}
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width,
int height, int imageFlags) {
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags, uint8_t viewId)
{
NVGLUframebuffer* framebuffer = nvgluCreateFramebuffer(ctx, width, height, imageFlags);
if (framebuffer != nullptr)
{
nvgluSetViewFramebuffer(viewId, framebuffer);
}
return framebuffer;
}
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags)
{
NVGLUframebuffer* framebuffer = new NVGLUframebuffer;
framebuffer->ctx = ctx;
framebuffer->image = nvgCreateImageRGBA(ctx, width, height, imageFlags | NVG_IMAGE_PREMULTIPLIED, NULL);
bgfx::TextureHandle texture = nvglImageHandle(ctx, framebuffer->image);
if (!bgfx::isValid(texture)) {
if (!bgfx::isValid(texture))
{
nvgluDeleteFramebuffer(framebuffer);
return NULL;
return nullptr;
}
framebuffer->handle = bgfx::createFrameBuffer(1, &texture, false);
if (!bgfx::isValid(framebuffer->handle))
{
nvgluDeleteFramebuffer(framebuffer);
return NULL;
return nullptr;
}
static uint8_t s_ViewId = 1; // may have a better way to assign new view id
framebuffer->viewId = s_ViewId++;
bgfx::setViewFrameBuffer(framebuffer->viewId, framebuffer->handle);
bgfx::setViewSeq(framebuffer->viewId, true);
return framebuffer;
}
void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer) {
static NVGcontext* s_prevCtx = NULL;
void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer)
{
static NVGcontext* s_prevCtx = nullptr;
static uint8_t s_prevViewId;
if (framebuffer != NULL) {
if (framebuffer != nullptr)
{
s_prevCtx = framebuffer->ctx;
s_prevViewId = nvgViewId(framebuffer->ctx);
nvgViewId(framebuffer->ctx, framebuffer->viewId);
} else if (s_prevCtx != NULL) {
} else if (s_prevCtx != nullptr) {
nvgViewId(s_prevCtx, s_prevViewId);
}
}
void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer) {
if (framebuffer == NULL)
void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer)
{
if (framebuffer == nullptr)
return;
if (bgfx::isValid(framebuffer->handle))
{
bgfx::destroyFrameBuffer(framebuffer->handle);
}
if (framebuffer->image > 0)
{
nvgDeleteImage(framebuffer->ctx, framebuffer->image);
}
delete framebuffer;
}
void nvgluSetViewFramebuffer(uint8_t viewId, NVGLUframebuffer* framebuffer)
{
framebuffer->viewId = viewId;
bgfx::setViewFrameBuffer(viewId, framebuffer->handle);
bgfx::setViewSeq(viewId, true);
}

View File

@ -30,6 +30,7 @@ void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId);
// Example:
// float scale = 2;
// NVGLUframebuffer* fb = nvgluCreateFramebuffer(ctx, 100 * scale, 100 * scale, 0);
// nvgluSetViewFramebuffer(VIEW_ID, fb);
// nvgluBindFramebuffer(fb);
// nvgBeginFrame(ctx, 100, 100, scale);
// // renders anything offscreen
@ -44,8 +45,10 @@ void nvgViewId(struct NVGcontext* ctx, unsigned char _viewId);
// nvgFillPaint(ctx, paint);
// nvgFill(ctx);
// nvgEndFrame(ctx);
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags, uint8_t viewId);
NVGLUframebuffer* nvgluCreateFramebuffer(NVGcontext* ctx, int width, int height, int imageFlags);
void nvgluBindFramebuffer(NVGLUframebuffer* framebuffer);
void nvgluDeleteFramebuffer(NVGLUframebuffer* framebuffer);
void nvgluSetViewFramebuffer(uint8_t viewId, NVGLUframebuffer* framebuffer);
#endif // NANOVG_BGFX_H_HEADER_GUARD