Top Related Projects
Simple Directmedia Layer
A simple and easy-to-use library to enjoy videogames programming
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Emscripten: An LLVM-to-WebAssembly Compiler
Godot Engine – Multi-platform 2D and 3D game engine
Simple and Fast Multimedia Library
Quick Overview
GLFW (Graphics Library Framework) is an open-source, multi-platform library for OpenGL, OpenGL ES, and Vulkan development on desktop computers. It provides a simple API for creating windows, contexts, and surfaces, receiving input and events, and is commonly used in game development and graphics applications.
Pros
- Cross-platform compatibility (Windows, macOS, Linux)
- Simple and intuitive API for window management and input handling
- Lightweight and minimal dependencies
- Active development and community support
Cons
- Limited to desktop platforms (no mobile or web support)
- Focused primarily on window creation and input, lacking advanced GUI features
- May require additional libraries for more complex applications
- Learning curve for beginners unfamiliar with graphics programming concepts
Code Examples
Creating a window and OpenGL context:
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit()) return -1;
GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Handling keyboard input:
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
int main() {
// ... window creation code ...
glfwSetKeyCallback(window, key_callback);
// ... main loop ...
}
Getting mouse position:
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
printf("Mouse position: (%f, %f)\n", xpos, ypos);
Getting Started
- Download GLFW from the official website or use a package manager.
- Include GLFW in your project:
- For CMake:
find_package(glfw3 3.3 REQUIRED)
- For manual linking: Add GLFW include directory and link against the library
- For CMake:
- Include the GLFW header in your code:
#include <GLFW/glfw3.h>
- Initialize GLFW, create a window, and set up an OpenGL context:
if (!glfwInit()) return -1; GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", NULL, NULL); if (!window) { glfwTerminate(); return -1; } glfwMakeContextCurrent(window);
- Implement your main loop and handle events:
while (!glfwWindowShouldClose(window)) { // Render here glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate();
Competitor Comparisons
Simple Directmedia Layer
Pros of SDL
- More comprehensive feature set, including audio, input handling, and networking
- Cross-platform support for a wider range of systems, including mobile platforms
- Larger community and ecosystem with more resources and third-party libraries
Cons of SDL
- Heavier and more complex API compared to GLFW's focused approach
- Potentially higher overhead due to its broader feature set
- Steeper learning curve for beginners due to its extensive functionality
Code Comparison
SDL initialization:
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window = SDL_CreateWindow("SDL Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
GLFW initialization:
glfwInit();
GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW Window", NULL, NULL);
glfwMakeContextCurrent(window);
SDL is more verbose but provides built-in rendering capabilities, while GLFW focuses on window creation and OpenGL context management. SDL offers a wider range of functionality out of the box, making it suitable for more diverse projects. GLFW, on the other hand, provides a simpler, more focused API for window management and input handling, making it ideal for OpenGL-centric applications with custom rendering pipelines.
A simple and easy-to-use library to enjoy videogames programming
Pros of raylib
- Higher-level API with more built-in functionality for game development
- Easier to get started with for beginners
- Cross-platform support out of the box
Cons of raylib
- Less flexibility for low-level control
- Smaller community and ecosystem compared to GLFW
- May be overkill for simple windowing and input handling
Code Comparison
raylib example:
#include "raylib.h"
int main() {
InitWindow(800, 450, "raylib example");
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(RAYWHITE);
DrawText("Hello, raylib!", 190, 200, 20, LIGHTGRAY);
EndDrawing();
}
CloseWindow();
return 0;
}
GLFW example:
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit()) return -1;
GLFWwindow* window = glfwCreateWindow(800, 450, "GLFW example", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Pros of ImGui
- Immediate mode GUI library, allowing for quick and easy creation of user interfaces
- Lightweight and easy to integrate into existing projects
- Highly customizable and extensible
Cons of ImGui
- Limited to 2D rendering, unlike GLFW which supports 3D graphics
- Requires more manual management of UI state compared to retained mode GUI libraries
- Less suitable for complex, large-scale applications
Code Comparison
ImGui example:
ImGui::Begin("Hello, world!");
if (ImGui::Button("Click me"))
clicked++;
ImGui::Text("Button clicked %d times", clicked);
ImGui::End();
GLFW example:
glfwInit();
GLFWwindow* window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
Summary
ImGui is a lightweight, immediate mode GUI library ideal for quick prototyping and simple interfaces. GLFW, on the other hand, is a more comprehensive windowing and input library that provides a foundation for both 2D and 3D graphics applications. While ImGui excels in rapid UI development, GLFW offers more control over the application window and input handling, making it better suited for larger, more complex projects.
Emscripten: An LLVM-to-WebAssembly Compiler
Pros of Emscripten
- Enables compilation of C/C++ code to WebAssembly, allowing desktop applications to run in web browsers
- Provides a comprehensive toolchain for web development with C/C++
- Supports a wide range of libraries and APIs, including OpenGL
Cons of Emscripten
- Steeper learning curve compared to GLFW's more focused approach
- May introduce additional complexity for projects that don't require web compatibility
- Performance overhead due to the translation layer between native code and web technologies
Code Comparison
GLFW (C):
#include <GLFW/glfw3.h>
int main(void) {
GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", NULL, NULL);
glfwMakeContextCurrent(window);
}
Emscripten (C++):
#include <emscripten.h>
#include <emscripten/html5.h>
int main() {
EmscriptenWebGLContextAttributes attrs;
emscripten_webgl_init_context_attributes(&attrs);
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE context = emscripten_webgl_create_context("#canvas", &attrs);
}
While GLFW focuses on creating native windows and OpenGL contexts, Emscripten provides tools for compiling C/C++ code to run in web browsers, including WebGL support. GLFW is more straightforward for desktop applications, while Emscripten offers broader web compatibility at the cost of increased complexity.
Godot Engine – Multi-platform 2D and 3D game engine
Pros of Godot
- Full-featured game engine with built-in editor, scripting, and asset management
- Cross-platform support for multiple target platforms (desktop, mobile, web)
- Active community and extensive documentation
Cons of Godot
- Steeper learning curve due to its comprehensive feature set
- Larger project size and resource footprint
- Less suitable for lightweight applications or simple windowing tasks
Code Comparison
GLFW (C):
#include <GLFW/glfw3.h>
int main(void) {
GLFWwindow* window = glfwCreateWindow(640, 480, "GLFW Window", NULL, NULL);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
}
}
Godot (GDScript):
extends Node2D
func _ready():
var window = get_tree().root
window.size = Vector2(640, 480)
window.title = "Godot Window"
Summary
GLFW is a lightweight library for creating windows with OpenGL contexts, while Godot is a comprehensive game engine. GLFW is better suited for low-level graphics programming and custom engine development, whereas Godot provides a complete ecosystem for game development with higher-level abstractions and tools.
Simple and Fast Multimedia Library
Pros of SFML
- Higher-level API with more built-in functionality (graphics, audio, networking)
- Object-oriented design, making it easier for C++ developers
- Cross-platform support with consistent API across different operating systems
Cons of SFML
- Larger library size and potentially higher resource usage
- Less flexibility for low-level control compared to GLFW
- May have slower performance for certain use cases due to abstraction layers
Code Comparison
SFML example:
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Window");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}
GLFW example:
#include <GLFW/glfw3.h>
int main() {
if (!glfwInit()) return -1;
GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW Window", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window)) {
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
GLFW
Introduction
GLFW is an Open Source, multi-platform library for OpenGL, OpenGL ES and Vulkan application development. It provides a simple, platform-independent API for creating windows, contexts and surfaces, reading input, handling events, etc.
GLFW natively supports Windows, macOS and Linux and other Unix-like systems. On Linux both Wayland and X11 are supported.
GLFW is licensed under the zlib/libpng license.
You can download the latest stable release as source or Windows binaries. Each release starting with 3.0 also has a corresponding annotated tag with source and binary archives.
The documentation is available online and is included in all source and binary archives. See the release notes for new features, caveats and deprecations in the latest release. For more details see the version history.
The master
branch is the stable integration branch and should always compile
and run on all supported platforms, although details of newly added features may
change until they have been included in a release. New features and many bug
fixes live in other branches until
they are stable enough to merge.
If you are new to GLFW, you may find the tutorial for GLFW 3 useful. If you have used GLFW 2 in the past, there is a transition guide for moving to the GLFW 3 API.
GLFW exists because of the contributions of many people around the world, whether by reporting bugs, providing community support, adding features, reviewing or testing code, debugging, proofreading docs, suggesting features or fixing bugs.
Compiling GLFW
GLFW is written primarily in C99, with parts of macOS support being written in Objective-C. GLFW itself requires only the headers and libraries for your OS and window system. It does not need any additional headers for context creation APIs (WGL, GLX, EGL, NSGL, OSMesa) or rendering APIs (OpenGL, OpenGL ES, Vulkan) to enable support for them.
GLFW supports compilation on Windows with Visual C++ 2013 and later, MinGW and MinGW-w64, on macOS with Clang and on Linux and other Unix-like systems with GCC and Clang. It will likely compile in other environments as well, but this is not regularly tested.
There are pre-compiled binaries available for all supported compilers on Windows and macOS.
See the compilation guide for more information about how to compile GLFW yourself.
Using GLFW
See the documentation for tutorials, guides and the API reference.
Contributing to GLFW
See the contribution guide for more information.
System requirements
GLFW supports Windows XP and later and macOS 10.11 and later. Linux and other Unix-like systems running the X Window System are supported even without a desktop environment or modern extensions, although some features require a running window or clipboard manager. The OSMesa backend requires Mesa 6.3.
See the compatibility guide in the documentation for more information.
Dependencies
GLFW itself needs only CMake 3.16 or later and the headers and libraries for your OS and window system.
The examples and test programs depend on a number of tiny libraries. These are
located in the deps/
directory.
- getopt_port for examples with command-line options
- TinyCThread for threaded examples
- glad2 for loading OpenGL and Vulkan functions
- linmath.h for linear algebra in examples
- Nuklear for test and example UI
- stb_image_write for writing images to disk
The documentation is generated with Doxygen if CMake can find that tool.
Reporting bugs
Bugs are reported to our issue tracker. Please check the contribution guide for information on what to include when reporting a bug.
Changelog since 3.4
- Added
GLFW_UNLIMITED_MOUSE_BUTTONS
input mode that allows mouse buttons beyond the limit of the mouse button tokens to be reported (#2423) - Updated minimum CMake version to 3.16 (#2541)
- [Cocoa] Added
QuartzCore
framework as link-time dependency - [Cocoa] Removed support for OS X 10.10 Yosemite and earlier (#2506)
- [Wayland] Bugfix: The fractional scaling related objects were not destroyed
- [Wayland] Bugfix:
glfwInit
would segfault on compositor with no seat (#2517) - [Wayland] Bugfix: A drag entering a non-GLFW surface could cause a segfault
- [X11] Bugfix: Running without a WM could trigger an assert (#2593,#2601,#2631)
- [Null] Added Vulkan 'window' surface creation via
VK_EXT_headless_surface
- [Null] Added EGL context creation on Mesa via
EGL_MESA_platform_surfaceless
- [EGL] Allowed native access on Wayland with
GLFW_CONTEXT_CREATION_API
set toGLFW_NATIVE_CONTEXT_API
(#2518)
Contact
On glfw.org you can find the latest version of GLFW, as well as news, documentation and other information about the project.
If you have questions related to the use of GLFW, we have a forum.
If you have a bug to report, a patch to submit or a feature you'd like to request, please file it in the issue tracker on GitHub.
Finally, if you're interested in helping out with the development of GLFW or porting it to your favorite platform, join us on the forum or GitHub.
Top Related Projects
Simple Directmedia Layer
A simple and easy-to-use library to enjoy videogames programming
Dear ImGui: Bloat-free Graphical User interface for C++ with minimal dependencies
Emscripten: An LLVM-to-WebAssembly Compiler
Godot Engine – Multi-platform 2D and 3D game engine
Simple and Fast Multimedia Library
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot