Giter Site home page Giter Site logo

Demo on Retina display about nanovg HOT 13 CLOSED

memononen avatar memononen commented on August 16, 2024
Demo on Retina display

from nanovg.

Comments (13)

memononen avatar memononen commented on August 16, 2024

I did that intentionally, but you have a good point.

Your suggested solution does not handle font rendering and tesselation properly. If you look the second image carefully you'll see that the fonts are blurry and the tesselation especially on the small blue dots is quite coarse.

I wonder if the proper solution would pass in scale to nvgBeginFrame(), or whether we would expect the user to do the scaling manually. I'm slightly in favor of adding scale to nvgBeginFrame(), what do you think?

The implementation would be something like:

in example code:

int winWidth, winHeight;
glfwGetWindowSize(window, &winWidth, &winHeight);
nvgBeginFrame(vg, width, height, (float)width/(float)winWidth, (float)height/(float)winHeight);

in nvgBeginFrame:

void nvgBeginFrame(struct NVGcontext* ctx, int width, int height, float sx, float sy)
{
...
vg->nstates = 0;  // probably should add state reset there anyways...
nvgSave(vg);
nvgReset(vg);
nvgScale(vg, sx, sy);
}

Maybe the retina scaling could be part of initialization too.

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

As a user, I would definitely not want to do it manually, so adding sx, sy to nvgBeginFrame would work best for me.

from nanovg.

memononen avatar memononen commented on August 16, 2024

Do you know how other libraries do it? glfw seems to have window size vs. framebuffer size. That is one option. HTML5 has devicePixelRatio [1].

[1] http://www.quirksmode.org/blog/archives/2012/06/devicepixelrati.html

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

SDL2 is the same as GLFW, just with different names: SDL_GetWindowSize and SDL_GL_GetDrawableSize

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

With Anttweakbar you call TwWindowSize with the window size reported by glfwGetWindowSize, rather than framebuffer size, but then there is a fontscaling option. I forgot the logic of this, but it works seamlessly across retina and non-retina displays.

            window_container.get_window_size(&w, &h);
            window_container.get_framebuffer_size(&fb_w, &fb_h);
            int scale = (fb_w <= w) ? 1 : fb_w / w;
            if(scale > 1)
            {
                std::stringstream s;
                s << " GLOBAL fontscaling=" << scale << " ";
                TwDefine(s.str().c_str());
            }

from nanovg.

memononen avatar memononen commented on August 16, 2024

CoreGraphics seems to use some sort of scale value:
http://stackoverflow.com/questions/5414631/retina-display-core-graphics-font-quality

I think one scale value is probably the best. A name like contentScale, pixelRatio or devicePixelRatio could work, it would at least be consistent what is being used out there. Probably makes sense to put that as parameter in nvgBeginFrame since we pass the frame buffer size there too (vs. passing it on init).

Whether we should pass frame buffer or window size to nvgBeginFrame is good question too. HTML canvas and CoreGraphics [1] both seem to use window coordinates in this kind of case too, maybe that is the correct option.

Going this route, I think the best way to implement pixel scaling is to store the device pixel ration in to the context and then scale ctx->tessTol and ctx->distTol with pixel ratio before use (or make a function which calculates them based in pixel ratio, call in init too), in addition multiply 'scale' in nvgText() by pixel ration too.

Note to self, nvgTextBounds and nvgVertMetrics need scaling too.

[1] http://stackoverflow.com/questions/4779221/in-iphone-app-how-to-detect-the-screen-resolution-of-the-device

from nanovg.

memononen avatar memononen commented on August 16, 2024

Would you like to make a PR about these retina support or should I go ahead and implement it?

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

PR (purchase request?). Please do go ahead :)

from nanovg.

memononen avatar memononen commented on August 16, 2024

PR = pull request, I'll implement that.

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

Regarding PR, I swear I knew that.

Regarding adding scale support, I was just looking at your comments again and I just realized how simple it would be from your first response, I was previously under the impression that it would require further changes deeper in NanoVG (e.g. to how tessellation is handled). Sorry about that.

from nanovg.

memononen avatar memononen commented on August 16, 2024

no worries, it's almost done :)

from nanovg.

memononen avatar memononen commented on August 16, 2024

This is implemented now, can you try it out? One thing left to do is to up the cache size too based on retina display. The implementation did not end up being as simple as I hoped, while working on this I also found a couple of bugs in my demo code which were not showing up on non-retina displays :)

from nanovg.

andrewcorrigan avatar andrewcorrigan commented on August 16, 2024

It works. It looks absolutely stunning too.

from nanovg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.