GLFW is a free, open-source, multi-platform library for OpenGL and OpenGL ES especially design for game development. OpenGL itself does not provide features to handle window creation and other input, so GLFW is very useful to create the necessary context, manage windows, timing, read and control user input via keyboard, mouse, joysticks and other purposes. There are many other library alternatives to GLFW such as SDL and freeglut but if you are looking best library for 3D game development then GLFW is best. GLFW community is large and very active, at the time of updating this article latest stable version of GLFW is 3.3.2 which is released on January 20, 2020. It comes with documentation, high standard tutorials and guides and lots of test programs.
Here, I will guide you through a step by step procedure to setup GLFW 3 in Codeblocks IDE on Windows 10.
Visit GLFW download page https://www.glfw.org/download.html and download 32 bit/64 bit windows binary files from the Windows pre-compiled binaries section. You have to make sure that bit of your C++ compiler and downloaded GLFW library files should match. Currently, GLFW releases are tested with MinGW, MinGW-w64 and Visual C++ 2010 and above version.
Steps to setup GLFW library and create a project in Code::blocks:
- Extract downloaded zip file.
- Create a folder in any directory and give a name "GLFW" (eg. c:\GLFW).
- Browse extracted files in step 1. Copy "include" folder and paste it inside the directory that you have created in step 2. Copy lib folder of corresponding to your compiler for e.g if you are using minGW then copy lib-mingw folder and paste it inside the directory that you have created in step 2 and rename it to lib. Also copy glfw3.dll file from bin folder to "C:\Windows\System32" if you are using 32-bit library files or copy to "C:\Windows\System" if you are using 64-bit library files.
- Now, open the code::blocks and go to File -> New -> Project or click create a new project link from the start page.
- From the project wizard select "empty project" and click "Go" button and provide all necessary information. Also, do not forget to add main.cpp file.
- Go to the menu bar and select project-> Project properties. A dialogue box will appear and click "Project's build options". Another dialogue box will appear and click the "Search directories" tab. Under the "Compiler" tab add the path of GLFW include folder (e.g. c:\GLFW\include) and under "Linker" tab add the path of GLFW lib folder (e.g. c:\GLFW\lib).
- Now go to "Linker Setting" tab and add three linker libglfw3.a, libglfw3dll.a and libopengl32.a. Linker libglfw3.a and libglfw3dll.a are available inside GLFW lib folder and libopengl32.a is available inside minGW compiler lib folder.
#define GLFW_DLL
#include <GLFW/glfw3.h>
#include <iostream>
#include <stdexcept>
GLFWwindow* window = NULL;
GLFWmonitor* monitor = NULL;
int main(int argc, char *argv[]) {
if(!glfwInit())
throw std::runtime_error("glfwInit failed");
window = glfwCreateWindow(800, 600, "test window",NULL, NULL);
if(!window)
throw std::runtime_error("glfwOpenWindow failed.");
// GLFW settings
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
// process pending events
glfwPollEvents();
glClearColor(1, 0, 0, 1); // black
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
}
// clean up and exit
glfwTerminate();
return 0;
}
You have to make sure to define GLFW_DLL and include glfw3.h header always on top of your source code as shown in the above sample code otherwise the compiler will throw undefine reference error.
Please watch the video below for a detailed explanation.