Allows to set view id of NanoVG’s framebuffer manually.

This commit changes the `nvgluCreateFramebuffer()` function to not assign a view id automatically as it would easily reach the limit of `uint8_t`. Instead, a new `nvgluSetViewFramebuffer()` function is created to allow users to assign the view id manually as mentioned in #1095.
This commit is contained in:
Olli Wang 2017-03-26 11:03:52 +08:00
parent 29116854d8
commit b761d1240c
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