1# Moving from GLFW 2 to 3 {#moving_guide} 2 3[TOC] 4 5This is a transition guide for moving from GLFW 2 to 3. It describes what has 6changed or been removed, but does _not_ include 7[new features](@ref news) unless they are required when moving an existing code 8base onto the new API. For example, the new multi-monitor functions are 9required to create full screen windows with GLFW 3. 10 11 12## Changed and removed features {#moving_removed} 13 14### Renamed library and header file {#moving_renamed_files} 15 16The GLFW 3 header is named @ref glfw3.h and moved to the `GLFW` directory, to 17avoid collisions with the headers of other major versions. Similarly, the GLFW 183 library is named `glfw3,` except when it's installed as a shared library on 19Unix-like systems, where it uses the [soname][] `libglfw.so.3`. 20 21[soname]: https://en.wikipedia.org/wiki/soname 22 23__Old syntax__ 24```c 25#include <GL/glfw.h> 26``` 27 28__New syntax__ 29```c 30#include <GLFW/glfw3.h> 31``` 32 33 34### Removal of threading functions {#moving_threads} 35 36The threading functions have been removed, including the per-thread sleep 37function. They were fairly primitive, under-used, poorly integrated and took 38time away from the focus of GLFW (i.e. context, input and window). There are 39better threading libraries available and native threading support is available 40in both [C++11][] and [C11][], both of which are gaining traction. 41 42[C++11]: https://en.cppreference.com/w/cpp/thread 43[C11]: https://en.cppreference.com/w/c/thread 44 45If you wish to use the C++11 or C11 facilities but your compiler doesn't yet 46support them, see the [TinyThread++][] and [TinyCThread][] projects created by 47the original author of GLFW. These libraries implement a usable subset of the 48threading APIs in C++11 and C11, and in fact some GLFW 3 test programs use 49TinyCThread. 50 51[TinyThread++]: https://gitorious.org/tinythread/tinythreadpp 52[TinyCThread]: https://github.com/tinycthread/tinycthread 53 54However, GLFW 3 has better support for _use from multiple threads_ than GLFW 552 had. Contexts can be made current on any thread, although only a single 56thread at a time, and the documentation explicitly states which functions may be 57used from any thread and which must only be used from the main thread. 58 59__Removed functions__ 60> `glfwSleep`, `glfwCreateThread`, `glfwDestroyThread`, `glfwWaitThread`, 61> `glfwGetThreadID`, `glfwCreateMutex`, `glfwDestroyMutex`, `glfwLockMutex`, 62> `glfwUnlockMutex`, `glfwCreateCond`, `glfwDestroyCond`, `glfwWaitCond`, 63> `glfwSignalCond`, `glfwBroadcastCond` and `glfwGetNumberOfProcessors`. 64 65__Removed types__ 66> `GLFWthreadfun` 67 68 69### Removal of image and texture loading {#moving_image} 70 71The image and texture loading functions have been removed. They only supported 72the Targa image format, making them mostly useful for beginner level examples. 73To become of sufficiently high quality to warrant keeping them in GLFW 3, they 74would need not only to support other formats, but also modern extensions to 75OpenGL texturing. This would either add a number of external 76dependencies (libjpeg, libpng, etc.), or force GLFW to ship with inline versions 77of these libraries. 78 79As there already are libraries doing this, it is unnecessary both to duplicate 80the work and to tie the duplicate to GLFW. The resulting library would also be 81platform-independent, as both OpenGL and stdio are available wherever GLFW is. 82 83__Removed functions__ 84> `glfwReadImage`, `glfwReadMemoryImage`, `glfwFreeImage`, `glfwLoadTexture2D`, 85> `glfwLoadMemoryTexture2D` and `glfwLoadTextureImage2D`. 86 87 88### Removal of GLFWCALL macro {#moving_stdcall} 89 90The `GLFWCALL` macro, which made callback functions use [\_\_stdcall][stdcall] 91on Windows, has been removed. GLFW is written in C, not Pascal. Removing this 92macro means there's one less thing for application programmers to remember, i.e. 93the requirement to mark all callback functions with `GLFWCALL`. It also 94simplifies the creation of DLLs and DLL link libraries, as there's no need to 95explicitly disable `@n` entry point suffixes. 96 97[stdcall]: https://msdn.microsoft.com/en-us/library/zxk0tw93.aspx 98 99__Old syntax__ 100```c 101void GLFWCALL callback_function(...); 102``` 103 104__New syntax__ 105```c 106void callback_function(...); 107``` 108 109 110### Window handle parameters {#moving_window_handles} 111 112Because GLFW 3 supports multiple windows, window handle parameters have been 113added to all window-related GLFW functions and callbacks. The handle of 114a newly created window is returned by @ref glfwCreateWindow (formerly 115`glfwOpenWindow`). Window handles are pointers to the 116[opaque][opaque-type] type @ref GLFWwindow. 117 118[opaque-type]: https://en.wikipedia.org/wiki/Opaque_data_type 119 120__Old syntax__ 121```c 122glfwSetWindowTitle("New Window Title"); 123``` 124 125__New syntax__ 126```c 127glfwSetWindowTitle(window, "New Window Title"); 128``` 129 130 131### Explicit monitor selection {#moving_monitor} 132 133GLFW 3 provides support for multiple monitors. To request a full screen mode window, 134instead of passing `GLFW_FULLSCREEN` you specify which monitor you wish the 135window to use. The @ref glfwGetPrimaryMonitor function returns the monitor that 136GLFW 2 would have selected, but there are many other 137[monitor functions](@ref monitor_guide). Monitor handles are pointers to the 138[opaque][opaque-type] type @ref GLFWmonitor. 139 140__Old basic full screen__ 141```c 142glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_FULLSCREEN); 143``` 144 145__New basic full screen__ 146```c 147window = glfwCreateWindow(640, 480, "My Window", glfwGetPrimaryMonitor(), NULL); 148``` 149 150@note The framebuffer bit depth parameters of `glfwOpenWindow` have been turned 151into [window hints](@ref window_hints), but as they have been given 152[sane defaults](@ref window_hints_values) you rarely need to set these hints. 153 154 155### Removal of automatic event polling {#moving_autopoll} 156 157GLFW 3 does not automatically poll for events in @ref glfwSwapBuffers, meaning 158you need to call @ref glfwPollEvents or @ref glfwWaitEvents yourself. Unlike 159buffer swap, which acts on a single window, the event processing functions act 160on all windows at once. 161 162__Old basic main loop__ 163```c 164while (...) 165{ 166 // Process input 167 // Render output 168 glfwSwapBuffers(); 169} 170``` 171 172__New basic main loop__ 173```c 174while (...) 175{ 176 // Process input 177 // Render output 178 glfwSwapBuffers(window); 179 glfwPollEvents(); 180} 181``` 182 183 184### Explicit context management {#moving_context} 185 186Each GLFW 3 window has its own OpenGL context and only you, the application 187programmer, can know which context should be current on which thread at any 188given time. Therefore, GLFW 3 leaves that decision to you. 189 190This means that you need to call @ref glfwMakeContextCurrent after creating 191a window before you can call any OpenGL functions. 192 193 194### Separation of window and framebuffer sizes {#moving_hidpi} 195 196Window positions and sizes now use screen coordinates, which may not be the same 197as pixels on machines with high-DPI monitors. This is important as OpenGL uses 198pixels, not screen coordinates. For example, the rectangle specified with 199`glViewport` needs to use pixels. Therefore, framebuffer size functions have 200been added. You can retrieve the size of the framebuffer of a window with @ref 201glfwGetFramebufferSize function. A framebuffer size callback has also been 202added, which can be set with @ref glfwSetFramebufferSizeCallback. 203 204__Old basic viewport setup__ 205```c 206glfwGetWindowSize(&width, &height); 207glViewport(0, 0, width, height); 208``` 209 210__New basic viewport setup__ 211```c 212glfwGetFramebufferSize(window, &width, &height); 213glViewport(0, 0, width, height); 214``` 215 216 217### Window closing changes {#moving_window_close} 218 219The `GLFW_OPENED` window parameter has been removed. As long as the window has 220not been destroyed, whether through @ref glfwDestroyWindow or @ref 221glfwTerminate, the window is "open". 222 223A user attempting to close a window is now just an event like any other. Unlike 224GLFW 2, windows and contexts created with GLFW 3 will never be destroyed unless 225you choose them to be. Each window now has a close flag that is set to 226`GLFW_TRUE` when the user attempts to close that window. By default, nothing else 227happens and the window stays visible. It is then up to you to either destroy 228the window, take some other action or ignore the request. 229 230You can query the close flag at any time with @ref glfwWindowShouldClose and set 231it at any time with @ref glfwSetWindowShouldClose. 232 233__Old basic main loop__ 234```c 235while (glfwGetWindowParam(GLFW_OPENED)) 236{ 237 ... 238} 239``` 240 241__New basic main loop__ 242```c 243while (!glfwWindowShouldClose(window)) 244{ 245 ... 246} 247``` 248 249The close callback no longer returns a value. Instead, it is called after the 250close flag has been set, so it can optionally override its value, before 251event processing completes. You may however not call @ref glfwDestroyWindow 252from the close callback (or any other window related callback). 253 254__Old syntax__ 255```c 256int GLFWCALL window_close_callback(void); 257``` 258 259__New syntax__ 260```c 261void window_close_callback(GLFWwindow* window); 262``` 263 264@note GLFW never clears the close flag to `GLFW_FALSE`, meaning you can use it 265for other reasons to close the window as well, for example the user choosing 266Quit from an in-game menu. 267 268 269### Persistent window hints {#moving_hints} 270 271The `glfwOpenWindowHint` function has been renamed to @ref glfwWindowHint. 272 273Window hints are no longer reset to their default values on window creation, but 274instead retain their values until modified by @ref glfwWindowHint or @ref 275glfwDefaultWindowHints, or until the library is terminated and re-initialized. 276 277 278### Video mode enumeration {#moving_video_modes} 279 280Video mode enumeration is now per-monitor. The @ref glfwGetVideoModes function 281now returns all available modes for a specific monitor instead of requiring you 282to guess how large an array you need. The `glfwGetDesktopMode` function, which 283had poorly defined behavior, has been replaced by @ref glfwGetVideoMode, which 284returns the current mode of a monitor. 285 286 287### Removal of character actions {#moving_char_up} 288 289The action parameter of the [character callback](@ref GLFWcharfun) has been 290removed. This was an artefact of the origin of GLFW, i.e. being developed in 291English by a Swede. However, many keyboard layouts require more than one key to 292produce characters with diacritical marks. Even the Swedish keyboard layout 293requires this for uncommon cases like ü. 294 295__Old syntax__ 296```c 297void GLFWCALL character_callback(int character, int action); 298``` 299 300__New syntax__ 301```c 302void character_callback(GLFWwindow* window, int character); 303``` 304 305 306### Cursor position changes {#moving_cursorpos} 307 308The `glfwGetMousePos` function has been renamed to @ref glfwGetCursorPos, 309`glfwSetMousePos` to @ref glfwSetCursorPos and `glfwSetMousePosCallback` to @ref 310glfwSetCursorPosCallback. 311 312The cursor position is now `double` instead of `int`, both for the direct 313functions and for the callback. Some platforms can provide sub-pixel cursor 314movement and this data is now passed on to the application where available. On 315platforms where this is not provided, the decimal part is zero. 316 317GLFW 3 only allows you to position the cursor within a window using @ref 318glfwSetCursorPos (formerly `glfwSetMousePos`) when that window is active. 319Unless the window is active, the function fails silently. 320 321 322### Wheel position replaced by scroll offsets {#moving_wheel} 323 324The `glfwGetMouseWheel` function has been removed. Scrolling is the input of 325offsets and has no absolute position. The mouse wheel callback has been 326replaced by a [scroll callback](@ref GLFWscrollfun) that receives 327two-dimensional floating point scroll offsets. This allows you to receive 328precise scroll data from for example modern touchpads. 329 330__Old syntax__ 331```c 332void GLFWCALL mouse_wheel_callback(int position); 333``` 334 335__New syntax__ 336```c 337void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); 338``` 339 340__Removed functions__ 341> `glfwGetMouseWheel` 342 343 344### Key repeat action {#moving_repeat} 345 346The `GLFW_KEY_REPEAT` enable has been removed and key repeat is always enabled 347for both keys and characters. A new key action, `GLFW_REPEAT`, has been added 348to allow the [key callback](@ref GLFWkeyfun) to distinguish an initial key press 349from a repeat. Note that @ref glfwGetKey still returns only `GLFW_PRESS` or 350`GLFW_RELEASE`. 351 352 353### Physical key input {#moving_keys} 354 355GLFW 3 key tokens map to physical keys, unlike in GLFW 2 where they mapped to 356the values generated by the current keyboard layout. The tokens are named 357according to the values they would have in the standard US layout, but this 358is only a convenience, as most programmers are assumed to know that layout. 359This means that (for example) `GLFW_KEY_LEFT_BRACKET` is always a single key and 360is the same key in the same place regardless of what keyboard layouts the users 361of your program have. 362 363The key input facility was never meant for text input, although using it that 364way worked slightly better in GLFW 2. If you were using it to input text, you 365should be using the character callback instead, on both GLFW 2 and 3. This will 366give you the characters being input, as opposed to the keys being pressed. 367 368GLFW 3 has key tokens for all keys on a standard 105 key keyboard, so instead of 369having to remember whether to check for `a` or `A`, you now check for 370@ref GLFW_KEY_A. 371 372 373### Joystick function changes {#moving_joystick} 374 375The `glfwGetJoystickPos` function has been renamed to @ref glfwGetJoystickAxes. 376 377The `glfwGetJoystickParam` function and the `GLFW_PRESENT`, `GLFW_AXES` and 378`GLFW_BUTTONS` tokens have been replaced by the @ref glfwJoystickPresent 379function as well as axis and button counts returned by the @ref 380glfwGetJoystickAxes and @ref glfwGetJoystickButtons functions. 381 382 383### Win32 MBCS support {#moving_mbcs} 384 385The Win32 port of GLFW 3 will not compile in [MBCS mode][MBCS]. However, 386because the use of the Unicode version of the Win32 API doesn't affect the 387process as a whole, but only those windows created using it, it's perfectly 388possible to call MBCS functions from other parts of the same application. 389Therefore, even if an application using GLFW has MBCS mode code, there's no need 390for GLFW itself to support it. 391 392[MBCS]: https://msdn.microsoft.com/en-us/library/5z097dxa.aspx 393 394 395### Support for versions of Windows older than XP {#moving_windows} 396 397All explicit support for version of Windows older than XP has been removed. 398There is no code that actively prevents GLFW 3 from running on these earlier 399versions, but it uses Win32 functions that those versions lack. 400 401Windows XP was released in 2001, and by now (January 2015) it has not only 402replaced almost all earlier versions of Windows, but is itself rapidly being 403replaced by Windows 7 and 8. The MSDN library doesn't even provide 404documentation for version older than Windows 2000, making it difficult to 405maintain compatibility with these versions even if it was deemed worth the 406effort. 407 408The Win32 API has also not stood still, and GLFW 3 uses many functions only 409present on Windows XP or later. Even supporting an OS as new as XP (new 410from the perspective of GLFW 2, which still supports Windows 95) requires 411runtime checking for a number of functions that are present only on modern 412version of Windows. 413 414 415### Capture of system-wide hotkeys {#moving_syskeys} 416 417The ability to disable and capture system-wide hotkeys like Alt+Tab has been 418removed. Modern applications, whether they're games, scientific visualisations 419or something else, are nowadays expected to be good desktop citizens and allow 420these hotkeys to function even when running in full screen mode. 421 422 423### Automatic termination {#moving_terminate} 424 425GLFW 3 does not register @ref glfwTerminate with `atexit` at initialization, 426because `exit` calls registered functions from the calling thread and while it 427is permitted to call `exit` from any thread, @ref glfwTerminate must only be 428called from the main thread. 429 430To release all resources allocated by GLFW, you should call @ref glfwTerminate 431yourself, from the main thread, before the program terminates. Note that this 432destroys all windows not already destroyed with @ref glfwDestroyWindow, 433invalidating any window handles you may still have. 434 435 436### GLU header inclusion {#moving_glu} 437 438GLFW 3 does not by default include the GLU header and GLU itself has been 439deprecated by [Khronos][]. __New projects should not use GLU__, but if you need 440it for legacy code that has been moved to GLFW 3, you can request that the GLFW 441header includes it by defining @ref GLFW_INCLUDE_GLU before the inclusion of the 442GLFW header. 443 444[Khronos]: https://en.wikipedia.org/wiki/Khronos_Group 445 446__Old syntax__ 447```c 448#include <GL/glfw.h> 449``` 450 451__New syntax__ 452```c 453#define GLFW_INCLUDE_GLU 454#include <GLFW/glfw3.h> 455``` 456 457There are many libraries that offer replacements for the functionality offered 458by GLU. For the matrix helper functions, see math libraries like [GLM][] (for 459C++), [linmath.h][] (for C) and others. For the tessellation functions, see for 460example [libtess2][]. 461 462[GLM]: https://github.com/g-truc/glm 463[linmath.h]: https://github.com/datenwolf/linmath.h 464[libtess2]: https://github.com/memononen/libtess2 465 466 467## Name change tables {#moving_tables} 468 469 470### Renamed functions {#moving_renamed_functions} 471 472| GLFW 2 | GLFW 3 | Notes | 473| --------------------------- | ----------------------------- | ----- | 474| `glfwOpenWindow` | @ref glfwCreateWindow | All channel bit depths are now hints 475| `glfwCloseWindow` | @ref glfwDestroyWindow | | 476| `glfwOpenWindowHint` | @ref glfwWindowHint | Now accepts all `GLFW_*_BITS` tokens | 477| `glfwEnable` | @ref glfwSetInputMode | | 478| `glfwDisable` | @ref glfwSetInputMode | | 479| `glfwGetMousePos` | @ref glfwGetCursorPos | | 480| `glfwSetMousePos` | @ref glfwSetCursorPos | | 481| `glfwSetMousePosCallback` | @ref glfwSetCursorPosCallback | | 482| `glfwSetMouseWheelCallback` | @ref glfwSetScrollCallback | Accepts two-dimensional scroll offsets as doubles | 483| `glfwGetJoystickPos` | @ref glfwGetJoystickAxes | | 484| `glfwGetWindowParam` | @ref glfwGetWindowAttrib | | 485| `glfwGetGLVersion` | @ref glfwGetWindowAttrib | Use `GLFW_CONTEXT_VERSION_MAJOR`, `GLFW_CONTEXT_VERSION_MINOR` and `GLFW_CONTEXT_REVISION` | 486| `glfwGetDesktopMode` | @ref glfwGetVideoMode | Returns the current mode of a monitor | 487| `glfwGetJoystickParam` | @ref glfwJoystickPresent | The axis and button counts are provided by @ref glfwGetJoystickAxes and @ref glfwGetJoystickButtons | 488 489 490### Renamed types {#moving_renamed_types} 491 492| GLFW 2 | GLFW 3 | Notes | 493| ------------------- | --------------------- | | 494| `GLFWmousewheelfun` | @ref GLFWscrollfun | | 495| `GLFWmouseposfun` | @ref GLFWcursorposfun | | 496 497 498### Renamed tokens {#moving_renamed_tokens} 499 500| GLFW 2 | GLFW 3 | Notes | 501| --------------------------- | ---------------------------- | ----- | 502| `GLFW_OPENGL_VERSION_MAJOR` | `GLFW_CONTEXT_VERSION_MAJOR` | Renamed as it applies to OpenGL ES as well | 503| `GLFW_OPENGL_VERSION_MINOR` | `GLFW_CONTEXT_VERSION_MINOR` | Renamed as it applies to OpenGL ES as well | 504| `GLFW_FSAA_SAMPLES` | `GLFW_SAMPLES` | Renamed to match the OpenGL API | 505| `GLFW_ACTIVE` | `GLFW_FOCUSED` | Renamed to match the window focus callback | 506| `GLFW_WINDOW_NO_RESIZE` | `GLFW_RESIZABLE` | The default has been inverted | 507| `GLFW_MOUSE_CURSOR` | `GLFW_CURSOR` | Used with @ref glfwSetInputMode | 508| `GLFW_KEY_ESC` | `GLFW_KEY_ESCAPE` | | 509| `GLFW_KEY_DEL` | `GLFW_KEY_DELETE` | | 510| `GLFW_KEY_PAGEUP` | `GLFW_KEY_PAGE_UP` | | 511| `GLFW_KEY_PAGEDOWN` | `GLFW_KEY_PAGE_DOWN` | | 512| `GLFW_KEY_KP_NUM_LOCK` | `GLFW_KEY_NUM_LOCK` | | 513| `GLFW_KEY_LCTRL` | `GLFW_KEY_LEFT_CONTROL` | | 514| `GLFW_KEY_LSHIFT` | `GLFW_KEY_LEFT_SHIFT` | | 515| `GLFW_KEY_LALT` | `GLFW_KEY_LEFT_ALT` | | 516| `GLFW_KEY_LSUPER` | `GLFW_KEY_LEFT_SUPER` | | 517| `GLFW_KEY_RCTRL` | `GLFW_KEY_RIGHT_CONTROL` | | 518| `GLFW_KEY_RSHIFT` | `GLFW_KEY_RIGHT_SHIFT` | | 519| `GLFW_KEY_RALT` | `GLFW_KEY_RIGHT_ALT` | | 520| `GLFW_KEY_RSUPER` | `GLFW_KEY_RIGHT_SUPER` | | 521 522