ArthurSonzogni / smk

SMK - Simple multimedia kit - C++ WebAssembly
MIT License
125 stars 18 forks source link

Resizing #21

Closed Jim-Marsden closed 2 years ago

Jim-Marsden commented 2 years ago

Hey! I've recently started using smk and have really appreciated it. I have a lot of respect for what you've managed to do.

I fully acknowledge that this maybe a personal issue but I haven't been able to figure out how to resize my resolution to the window or canvas. In either a standalone window or a browser. Is there a resize event or anything like that that I haven't been able to find?

ArthurSonzogni commented 2 years ago

Thank you!

You can probably keep track of the dimension.

glm::vec2 dimensions_;

and when drawing the next frame compare with the new one:

glm::vec new_dimensions = window.dimensions();
if (dimensions_ != new_dimensions) {
  // Screen resize
  dimensions_ = new_dimensions_;
}

Alternatively, maybe we can add a new API to query the same bit of information from the RenderTarget.

Jim-Marsden commented 2 years ago

I don't know how other libraries like SDL2 or SFML handle it, but they have an event you can check for if the window was resized. It might be worth looking into for expectations?

Thanks!

Jim-Marsden commented 2 years ago

I tried it, and it does not maintain aspect ratio. http://swiftfoxkit.games/smk(This link is a placeholder).

Again, I'm so excited about this toolkit, it's amazing!

ArthurSonzogni commented 2 years ago

What kind of smk::View are you using? You need to specify its size with the same aspect ratio than the window.

Here is the default view: https://github.com/ArthurSonzogni/smk/blob/734737635c946eb52e58b1640537d423cafa0ce4/src/smk/RenderTarget.cpp#L235-L237

Jim-Marsden commented 2 years ago

Okay, so progress, and I was also being dumb.

I was only testing the .wasm file and not a local binary.

Resizing is working properly, however, It's not detecting the resizing of the canvas.

Jim-Marsden commented 2 years ago

My solution for completeness for future reference. I don't know enough about wasm/emscripten to provide meaningful feedback but I feel like this is a good thing for smk to handle for people?

void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    std::cout << "!\n";

    if(windowptr) {
        smk::View default_view;
        default_view.SetCenter(width / 2, height / 2);
        default_view.SetSize(width, height);
        windowptr->SetView(default_view);
    }
}

void inline emscripten_size(){
#ifdef __EMSCRIPTEN__
    if(!windowptr) return;
    static auto view = windowptr->view();
    double x, y;
    emscripten_get_element_css_size("canvas", &x, &y);
    view.SetCenter(x / 2, y / 2);
    view.SetSize(x, y);
    windowptr->SetView(view);
#endif
}