Code Monkey home page Code Monkey logo

Comments (7)

janekb04 avatar janekb04 commented on May 28, 2024

Hi, glfw::init returns an object of type GlfwLibrary. That object terminates GLFW on destruction, so you have to store it somewhere. You cannot really store it in a smart pointer because I didn't think of moving it anywhere. You'll probably have to store it inside a struct or a tuple and store that in a smart pointer. I haven't tested it, but I think that this should work:

// Aggregate type
struct holder {
    glfw::GlfwLibrary lib;
};
auto GLFW = std::make_unique<holder>(glfw::init());

You'll have to make sure that this object outlives all glfw objects and commands.

I admit that this wasn't the best design choice on my part, and I am planning to revise this and other parts of the library's interface.

from glfwpp.

Golgovskiy avatar Golgovskiy commented on May 28, 2024

It's just as I thought, but I tried putting init into a main() function before, hoping it stay in scope until the end of program. However it didn't have any effect.
Maybe storing it was removed by compiler since I didn't use it anywhere later.
And yes, I noted the storing of object in example and nodiscard inside.
What is different about not using vulkan thou? Why does it work?

from glfwpp.

janekb04 avatar janekb04 commented on May 28, 2024

Using Vulkan shouldn’t have any effect. Could you send your main function, or at least it’s relevant parts?

from glfwpp.

Golgovskiy avatar Golgovskiy commented on May 28, 2024
  
       glfw::init();

       glfw::WindowHints{
           .clientApi = glfw::ClientApi::None,
       }.apply();

       window = glfw::Window(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);

       ...
       // load of vulkan stuff here
       ...

       while (!window->shouldClose()) {
           glfw::pollEvents();
       }

from glfwpp.

janekb04 avatar janekb04 commented on May 28, 2024

The problem with this code is that the GlfwLibrary isn’t stored anywhere. It gets destructed immediately after the call to glfw::init. You have to assign the result of init to a variable.

int main() {
       auto GLFW = glfw::init();

       glfw::WindowHints{
           .clientApi = glfw::ClientApi::None,
       }.apply();

       window = glfw::Window(WIDTH, HEIGHT, "Vulkan", nullptr, nullptr);

       ...
       // load of vulkan stuff here
       ...

       while (!window->shouldClose()) {
           glfw::pollEvents();
       }
}

from glfwpp.

Golgovskiy avatar Golgovskiy commented on May 28, 2024

Oh, sorry. I actually stored the thing like you shown after previous comments.
Now it opens the window, but assertion error pops up anyway after it's closed.

P.S. I've just discovered that this problem lies in try catch block. I took it out of it and now it works properly.

Okay, i think my issue is fixed. Many thanks. Would love to find a way to actually put it inside the exception catcher thou.

from glfwpp.

janekb04 avatar janekb04 commented on May 28, 2024

It just came to my mind that there is an obscure trick you could use to declare in the exception block. You could allocate the library object using a shared pointer. Then, you would throw a custom exception that would internally store that same shared pointer. This would ensure that the library doesn’t get terminated until the catch block completes.
This is convoluted, and personally I’d just wrap the try catch block with a second try catch block. The key thing is to make the scope of the library object extend past the window.

from glfwpp.

Related Issues (14)

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.