Using GLFW with Code::Blocks

From CodeBlocks
Jump to: navigation, search


What is GLFW

"GLFW is a free, open source, portable framework for OpenGL application development.

In short, it is a link library that constitutes a powerful API for handling operating

system specific tasks, such as opening an OpenGL window and reading keyboard, mouse and joystick input."

You want to write a program using OpenGL and worry whether messing with W32 API, GLUT, or SDL is the least evil?
In this case, GLFW is for you. GLFW lets you create a window with an OpenGL context (including full-screen windows), sample mouse and joysticks and read keyboard input in a platform-independent way. Additionally, GLFW offers portable high resolution timing and multithreading/message functionality. All of this goes into a 36kB library that is zlib-licensed, which more or less means you can do with it whatever you want, as long as you do not claim authorship of the library or have some other obviously illegal intent. All in all, this is certainly one of the best available OpenGL toolkits.

Prerequisites

  1. Internet Connection
  2. Some archiving program (tar, 7z, WinZip, PowerArchiver, ...)
  3. Code::Blocks 1.0RC2 or later (for the application template)
  4. A working MinGW installation (alternatively, Borland or MSVC will work, too)


Building the Libraries

  1. Download the GLFW sources (including documentation) from: http://prdownloads.sourceforge.net/glfw/glfw-2.5.zip?download
  2. Unzip the archive anywhere you like (I will assume C:\glfw for simplicity).
  3. Compile
    • For Windows users, there is a batch file which allows to compile using a variety of compilers. Open a DOS window, cd to the directory where GLFW lives (e.g. C:\glfw), and type compile make mgw. If you use the Borland compiler, type compile make bcc instead, and for MSVC, type compile make msvc.
    • Linux users will just want to type make.

Build time is ~1 minute.


Installing GLFW

  1. Navigate to the lib\win32 folder (lib\x11 for Linux).
  2. Copy libglfw.a, libglfwdll.a, and glfw.dll from inside the lib\win32 folder (lib\x11 for Linux) to where you keep your libraries (could be C:\MinGW\dll, C:\MinGW\lib, or C:\CodeBlocks\lib, whatever).
  3. Go up two levels (to the GLFW base directory), and then into include/GL. Copy glfw.h to the GL folder inside your system includes folder (e.g. C:\MinGW\include\GL or C:\codeblocks\include\GL)

Building a GLFW Project

To build a program with GLFW, you need to include GL/glfw.h (no need to include GL/gl.h) and link to glfw, opengl32, glu32, gdi32, user32, and kernel32.

Code::Blocks 1.0RC2 includes a template to build a statically linked GLFW application. Since GLFW is really lightweight, there is actually no reason not to statically link to it, but if you insist on dynamic linkage, just change the include library in "Build Options" accordingly.

Make a new project using the GLFW template, and hit the blue gear, you should see a window with a few spinning triangles:

The stereotypical OpenGL spinning triangle demo