{"id":26474173,"url":"https://github.com/mrizaln/glfw-cpp","last_synced_at":"2026-03-14T21:50:27.528Z","repository":{"id":236998048,"uuid":"793596573","full_name":"mrizaln/glfw-cpp","owner":"mrizaln","description":"C++ GLFW wrapper with multi-window and multithreading in mind","archived":false,"fork":false,"pushed_at":"2025-03-18T10:59:28.000Z","size":433,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T11:25:02.378Z","etag":null,"topics":["cpp","glfw","multithreading","opengl"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mrizaln.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-29T14:17:37.000Z","updated_at":"2025-03-18T10:59:26.000Z","dependencies_parsed_at":"2024-04-29T15:35:58.505Z","dependency_job_id":"37f92c27-a334-40ef-8175-21085cc46925","html_url":"https://github.com/mrizaln/glfw-cpp","commit_stats":null,"previous_names":["mrizaln/glfw-cpp"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrizaln%2Fglfw-cpp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrizaln%2Fglfw-cpp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrizaln%2Fglfw-cpp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mrizaln%2Fglfw-cpp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mrizaln","download_url":"https://codeload.github.com/mrizaln/glfw-cpp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244520023,"owners_count":20465624,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp","glfw","multithreading","opengl"],"created_at":"2025-03-19T22:45:19.638Z","updated_at":"2025-12-25T18:26:47.954Z","avatar_url":"https://github.com/mrizaln.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# glfw-cpp\n\nA C++ wrapper for GLFW with RAII support and with multi-window and multithreading in mind.\n\nThis wrapper is customized for my needs and isn't a direct one-to-one port of the GLFW C API to C++. I primarily use it to simplify window management for my graphics projects. You can see my projects here::\n\n- [game-of-life](https://github.com/mrizaln/game-of-life)\n\n## Features\n\n- RAII handles.\n- Type-safe enums.\n- Easy multi-threading (for one window/context per thread or multiple window/context per thread).\n- Poll-based event handling instead of callback-based (`swap_events()` and `EventQueue`).\n- No namespace pollution from including GLFW.\n- Emscripten support (via [`contrib.glfw`](https://github.com/pongasoft/emscripten-glfw) port from Emscripten).\n- Vulkan support.\n\n## Dependencies\n\n- C++20\n- GLFW 3.4\n\nYou can use your system package manager or c++ package manager like Conan or even manually clone the [GLFW](https://github.com/glfw/glfw) repository and call `add_subdirectory` to add it as GLFW dependency.\n\n\u003e see [example](./example/CMakeLists.txt) and [the instruction to build them](#building-examples)\n\n## Usage\n\n### Setting up\n\nYou can clone this repository (or add as submodule) inside your project. I recommend using FetchContent though as it is easier to do.\n\n```cmake\n# If you are using FetchContent\n# -----------------------------\ninclude(FetchContent)\n\nFetchContent_Declare(\n  glfw-cpp\n  GIT_REPOSITORY https://github.com/mrizaln/glfw-cpp\n  GIT_TAG v0.12.2)\nFetchContent_MakeAvailable(glfw-cpp)\n\n# # If you clone/submodule the repository instead do this\n# add_subdirectory(path/to/the/cloned/repository)\n\nadd_executable(main main.cpp)\ntarget_link_libraries(main PRIVATE glfw-cpp ...)  # you don't need to link to glfw here, glfw-cpp already link to it\n```\n\n### Example\n\n\u003e [multi_multi_thread.cpp](./example/source/new/multi_multi_thread.cpp)\n\n```cpp\n#include \u003cglbinding/gl/gl.h\u003e\n#include \u003cglbinding/glbinding.h\u003e\n#include \u003cglfw_cpp/glfw_cpp.hpp\u003e\n\n#include \u003ccmath\u003e\n#include \u003ccstdlib\u003e\n#include \u003cctime\u003e\n#include \u003cthread\u003e\n\nusing namespace gl;    // from \u003cglbinding/gl/g.h\u003e\n\nvoid window_thread(glfw_cpp::Window\u0026\u0026 window)\n{\n    glfw_cpp::make_current(window.handle());\n    glbinding::initialize(glfw_cpp::get_proc_address);\n\n    auto multiplier = 1.0f / static_cast\u003cfloat\u003e(std::rand() % 10 + 1);\n    auto elapsed    = 0.0f;\n\n    while (not window.should_close()) {\n        namespace ev = glfw_cpp::event;\n\n        // swap events enqueued by glfw_cpp::poll_events()\n        window.swap_events().visit(ev::Overload{\n            [\u0026](const ev::KeyPressed\u0026         e) { if (e.key == glfw_cpp::KeyCode::Q) window.request_close(); },\n            [\u0026](const ev::FramebufferResized\u0026 e) { glViewport(0, 0, e.width, e.height); },\n            [\u0026](const auto\u0026                    ) { /* catch-all; do nothing */ },\n        });\n\n        elapsed += static_cast\u003cfloat\u003e(window.delta_time());\n\n        const auto r = (std::sin(multiplier * 23.0f / 8.0f * elapsed) + 1.0f) * 0.1f + 0.4f;\n        const auto g = (std::cos(multiplier * 13.0f / 8.0f * elapsed) + 1.0f) * 0.2f + 0.3f;\n        const auto b = (std::sin(multiplier * 41.0f / 8.0f * elapsed) + 1.5f) * 0.2f;\n\n        glClearColor(r, g, b, 1.0f);\n        glClear(GL_COLOR_BUFFER_BIT);\n\n        window.swap_buffers();\n    };\n}\n\nint main()\n{\n    std::srand(static_cast\u003cunsigned\u003e(std::time(nullptr)));\n\n    auto glfw = glfw_cpp::init({});\n\n    glfw-\u003eapply_hint({\n        .api = glfw_cpp::api::OpenGL{\n            .version_major = 3,\n            .version_minor = 3,\n            .profile       = glfw_cpp::gl::Profile::Core,\n        },\n    });\n\n    auto window1 = glfw-\u003ecreate_window(800, 600, \"Hello glfw-cpp 1\");\n    auto window2 = glfw-\u003ecreate_window(800, 600, \"Hello glfw-cpp 2\");\n    auto window3 = glfw-\u003ecreate_window(800, 600, \"Hello glfw-cpp 3\");\n    auto window4 = glfw-\u003ecreate_window(800, 600, \"Hello glfw-cpp 4\");\n\n    auto thread1 = std::jthread{ window_thread, std::move(window1) };\n    auto thread2 = std::jthread{ window_thread, std::move(window2) };\n    auto thread3 = std::jthread{ window_thread, std::move(window3) };\n    auto thread4 = std::jthread{ window_thread, std::move(window4) };\n\n    while (glfw-\u003ehas_window_opened()) {\n        using glfw_cpp::operator\"\"_fps;\n        glfw-\u003epoll_events(120_fps);   // automatically queue events to its windows\n    }\n}\n```\n\nNo manual cleanup necessary!\n\nThe above example is multi threaded with one context and one window per thread. For emscripten example, see [this one](./example/source/emscripten/main.cpp). For other examples, head to [example](./example) directory.\n\n### Building examples\n\n#### Native\n\nTo build this project examples you need to have GLFW 3.4 and Vulkan SDK installed on your system.\n\nIf you are on Fedora, you can install them by running this command:\n\n```sh\nsudo dnf install glfw glfw-devel vulkan-headers vulkan-loader vulkan-loader-devel vulkan-validation-layers\n```\n\n- Build\n\n  ```sh\n  cmake -S . -B build/Native\n  cmake --build build/Native\n  ```\n\n- Run the executable\n\n  ```sh\n  ./build/Native/\u003cexample\u003e\n  ```\n\n#### Emscripten\n\nMake sure you have Emscripten SDK installed and is active for the current shell.\n\nThe Emscripten example is built separately since it runs very differently from usual C++ code.\n\nNo manual dependencies installation is needed for this one as `glfw-cpp` uses [`contrib.glfw3`](https://github.com/pongasoft/emscripten-glfw) port which will be installed automatically by Emscripten.\n\n- Build\n\n  ```sh\n  emcmake cmake -S . -B build/Emscripten\n  cmake --build build/Emscripten\n  ```\n\n- View the result in browser\n\n  ```sh\n  emrun ./build/Emscripten/emscripten.html\n  ```\n\n## Documentation\n\nThe project is documented using Doxygen. There is a Doxygen configuration in [docs](./docs/Doxygen) that can be used to generate a HTML documentation page.\n\nFrom the root of the project, just run this command (require `doxygen` binary to be installed). The output of the HTML page is in `docs/doxygen/html`\n\n```sh\ndoxygen docs/Doxygen\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrizaln%2Fglfw-cpp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrizaln%2Fglfw-cpp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrizaln%2Fglfw-cpp/lists"}