Skip to content

API rework#9

Merged
mrizaln merged 25 commits intomainfrom
api-rework
Nov 27, 2025
Merged

API rework#9
mrizaln merged 25 commits intomainfrom
api-rework

Conversation

@mrizaln
Copy link
Copy Markdown
Owner

@mrizaln mrizaln commented Nov 27, 2025

This PR fixes #8

There are many changes introduced in this PR:

  • Rework the logic for library initialization and add initialization hints.

    I changed the signature for glfw_cpp::init to

    void glfw_cpp::init(const glfw_cpp::InitHint&);

    where glfw_cpp::InitHint is initialization hints as described in the docs.

    This change in turn removes the need to initialize which client API to use for subsequent Window created by GLFW.

    The struct is an aggregate of all the hints available as its field, making setting them easier at initialization:

    auto glfw = glfw_cpp::init({ .platform = glfw_cpp::Platform::Wayland });
  • Make the window creation hints setting in-line with GLFW and aggregate all the hints into one type.

    Initialization hints now can be set independent of Window creation. The hints also "sticks" globally just like what GLFW does. The interface for this is glfw_cpp::Instance::apply_hint*.

    The change removes the previous limitation of only one client API for all windows. Now with this you can mix the API.

  • Add missing hints, add several new type representing the hints, and replace current Hint struct with a new one that aggregates all the hints into one struct with (may be fields).

    All the hints including platform specific ones are now included in the library. The glfw_cpp::Api struct and its corresponding types inside glfw_cpp::api namespace are largely the same. The hints can be set using previous function:

    auto glfw = glfw_cpp::init({});    // default init hints
    
    // this function takes `glfw_cpp::PartialHint` in which the fields are all optional;
    // the function will only set the hint that you specify here
    glfw->apply_hint({ .api = glfw_cpp::api::NoApi{}, .window = { .resizable = false } });
    
    // this funciton takes `glfw_cpp::FullHint` in which none of the fields are not optional;
    // the function will set the hint you specify here and the other ones with default values.
    glfw->apply_hint({ .api = glfw_cpp::api::NoApi{}, .window = { .resizable = false } });
  • Remove the conception of bind()/unbind() or make the context management match underlying GLFW.

    Since the abstraction provided by both functions are faulty, I decided to remove both functions and revert to how the underlying library does the context management. Two functions are introduced as wrapper:

    • glfw_cpp::make_current which wraps glfwMakeContextCurrent, and
    • glfw_cpp::get_current which wraps glfwGetCurrentContext.
  • Rename glfw_cpp::Window::poll and glfw_cpp::Window::display to glfw_cpp::Window::swap_events and glfw_cpp::Window::swap_buffers.

    glfw_cpp::Window::poll doesn't do any polling, it just swaps the window event queue. The polling is done by glfw_cpp::Instance::poll_events which makes the function name misleading. Furthermore they have too similar of function name so it might introduce confusion. glfw_cpp::Window::display also just swaps buffers. The new name also more in-line with glfwSwapBuffer which is what it calls under.

The bind()/unbind() and its attached thread concept is not suitable with
the context management of GLFW.

Binding was meant to be equivalent with making context current with
glfwMakeContextCurrent() but with an addition of tracking the calling
thread inside Window. This however won't work correctly since when
changing current context the previous window should unbind() itself
before the new one bind() itself. In the case unbind() not called for
previous window, both will have the same attached thread which should
not happen!

In GLFW there is no such thing anyway so removing these interfaces is
best.
In the meantime I also removed task queue.
@mrizaln mrizaln merged commit 0546319 into main Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Complete rework of the API

1 participant