Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel committed Aug 19, 2020
0 parents commit 57fa135
Show file tree
Hide file tree
Showing 50 changed files with 39,126 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.dub
docs.json
__dummy.html
docs/
/glfwd
glfwd.so
glfwd.dylib
glfwd.dll
glfwd.a
glfwd.lib
glfwd-test-*
*.exe
*.o
*.obj
*.lst
build/
reference/
dub.selections.json
example/dub.selections.json
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2002-2006 Marcus Geelnard
Copyright (c) 2006-2016 Camilla Löwy <[email protected]>

This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would
be appreciated but is not required.

2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.

3. This notice may not be removed or altered from any source
distribution.

164 changes: 164 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
# GLFW-D
A translation from C to D of [GLFW](https://github.com/glfw/glfw) version 3.3.2.

GLFW is a multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input.
(See also: [what is GLFW](https://www.glfw.org/faq.html#11---what-is-glfw))

GLFW has 5 targets, but currently only three are availabe in this translation.

| GLFW Target | Translated | Primarily used for |
|--------------------------------------------------------------------------------|------------|--------------------------|
| Win32 | ✔️ Yes | Windows |
| [X11](https://en.wikipedia.org/wiki/X_Window_System) | ✔️ Yes | Linux (older) |
| [Cocoa](https://en.wikipedia.org/wiki/Cocoa_(API)) | ❌ No | macOS |
| [Wayland](https://en.wikipedia.org/wiki/Wayland_%28display_server_protocol%29) | ❌ No | Linux (newer) |
| Osmesa | ✔️ Yes | Off-screen rendering (?) |

The translation sticks close to the C source code, so all uses of e.g. `memcpy`, `printf` and pointer arithmetic are intact and not replaced with idiomatic D.
The Doxygen documentation in `glfw.h` and `glfwnative.h` (here `glfw3/api.d` and `glfw3/apinative.d`) is translated to use DDoc `Params:` and `Returns:` sections.

The translation is licensed under the [zlib/libpng license](http://www.glfw.org/license.html) like the original code.
The translation is not affiliated with the original project.

## Basic usage

If you are using dub, add this package as a dependency.
In `dub.sdl`:
```
dependency "glfw-d" version="~>1.0.0"
```
In `dub.json`:
```
"dependencies": {
"glfw-d": "~>1.0.0"
}
```

Then you should be ready to go.
```D
import glfw3.api;
void main() {
// call GLFW functions, such as:
glfwInit();
glfwTerminate();
}
```
Check out the [example subpackage](https://github.com/dkorpel/glfw-d/tree/master/example) for an example of a GLFW application that creates a window and reads input:
```
dub run glfw-d:example
```
Note that you probably want to use OpenGL / Vulkan bindings ([bindbc-opengl](https://code.dlang.org/packages/bindbc-opengl) or [erupted](https://code.dlang.org/packages/erupted)) in order to actually display anything in your window.

See also: the [tutorial on glfw.org](https://www.glfw.org/docs/latest/quick.html)

## Reasons for using it
Using GLFW in your D project usually involves depending on a binding to the C-library, (such as [bindbc-glfw](https://github.com/BindBC/bindbc-glfw) or [derelict-glfw3](https://github.com/DerelictOrg/DerelictGLFW3)).
You'd then include a `glfw3.dll` with your releases for Windows users, and ask Linux users to install glfw using their package manager.
Your application then has to call a function that loads the function pointers at runtime, and then you check whether that succeeded or whether the shared library was missing or corrupted.

If you statically linked to GLFW, you don't have the hassle of run-time loading: everything you need is in the executable itself.
You could add pre-compiled static libraries to your repository, but you need to have one for each combination of settings you want: Windows / Linux, 32-bit / 64-bit, debug / release, etc.
You also need to make sure to link the correct C runtime library (e.g. `libcmt.lib` / `msvcrt.dll`).
It's very easy for issues to arise:
- [linker warnings and errors with bindbc-glfw](https://forum.dlang.org/post/[email protected])
- [Static link of glfw3 library fails for me](https://forum.dlang.org/post/[email protected])

With this translation, you can simply use Dub, and your D compiler settings (C runtime, optimization flags, debug info) also apply to GLFW.

Compile times are pretty short.
I get these results from the 'total' time of `time dub build glfw-d --force` on Debian Linux:

| build type | time (s) |
|--------------|----------|
| dmd debug | 0.5 |
| dmd release | 1.1 |
| ldc2 debug | 1.0 |
| ldc2 release | 2.7 |

Dub caches builds, so these compile times only apply the first time.

### Disadvantages
- The C sources are more battle-tested.
There is a chance the translation introduced new bugs.
- While GLFW is pretty stable, it is still being maintained by a group of contributors and new releases with new features and fixes come out.
Once GLFW 3.4 comes out, this translation will lag behind for who knows how long.
However, you can always switch back to compiling the C sources.

### Todo
- Thoroughly test on platforms
So far I used this library for my own OpenGL application and succesfully ran it on Debian Linux with X11 and Windows 7, but there are aspects that are not yet tested.
I have not used this with Vulkan or OpenGL ES.
I haven't used the native api (`glfw3/apinative.d` here) or functions for custom cursor creation yet.

- Add Wayland support
This platform needs to be translated still.

- Add Cocoa support.
This is low priority though since I don't have a computer that runs macOS and have little personal interest in this platform.

## Advanced usage

### Adding glfw-d to an existing D application using GLFW
If you are already using different static bindings to GLFW (e.g. [bindbc-glfw](https://code.dlang.org/packages/bindbc-glfw) with `BindGLFW_Static`), you can still use those with this package:
You don't have to change `import bindbc.glfw;` to `import glfw3.api;`.
Simply add the dependency to glfw-d and remove the linkage with the C library from `libs`, `sourceFiles` or `lflags` in your dub file.

### Configurations
Dub should automatically pick a configuration based on your operating system.
If you are on Linux but don't want to use the x11 target, you can e.g. add to your `dub.sdl`:
```
subConfiguration "glfw-d" "osmesa"
```
Or `dub.json`:
```
"subConfigurations": {
"glfw-d": "osmesa"
}
```

To expose the native API, set the appropriate version identifiers.
The available window API versions are:
- `GLFW_EXPOSE_NATIVE_WIN32`
- `GLFW_EXPOSE_NATIVE_COCOA`
- `GLFW_EXPOSE_NATIVE_X11`
- `GLFW_EXPOSE_NATIVE_WAYLAND`

The available context API versions are:
- `GLFW_EXPOSE_NATIVE_WGL`
- `GLFW_EXPOSE_NATIVE_NSGL`
- `GLFW_EXPOSE_NATIVE_GLX`
- `GLFW_EXPOSE_NATIVE_EGL`
- `GLFW_EXPOSE_NATIVE_OSMESA`

Example `dub.sdl`:
```
versions "GLFW_EXPOSE_NATIVE_WIN32" "GLFW_EXPOSE_NATIVE_WGL"
```

`dub.json`:
```
"versions": ["GLFW_EXPOSE_NATIVE_WIN32", "GLFW_EXPOSE_NATIVE_WGL"]
```

### Compiling manually
If you don't want to use dub, it is not hard to compile it manually since all source files you need are in a single folder: `source/glfw3`.
Look in `dub.sdl` which source files and version identifier your desired platform uses, then pass them to a D compiler:
```
# from the root of this repository
cd source/glfw3
dmd -version=_GLFW_WIN32 -I../ -m64 -lib -of=../../build/glfw.lib context.d init.d input.d monitor.d vulkan.d window.d mappings.d internal.d api.d win32_platform.d win32_init.d win32_joystick.d win32_monitor.d win32_time.d win32_thread.d win32_window.d wgl_context.d egl_context.d osmesa_context.d directinput8.d
```

### BetterC
Since it's a direct translation of a C codebase, it might compile [with `-betterC`](https://dlang.org/spec/betterc.html), but there might be linking errors because of certain C macros that are functions in druntime, such as:
```
core.sys.posix.sys.select.FD_SET
core.sys.posix.sys.select.FD_ZERO
```
This might or might not give linker errors in your application, depending on your compiler and settings.

## Building a shared library DLL
Building a shared library from the D sources should be possible, but I haven't tried it yet.
It may be as simple as adding `export` to functions that had `GLFWAPI` in the C sources, and adding a configuration with `targetType "sharedLibrary"` in `dub.sdl`,
but it also may be more difficult than that.
120 changes: 120 additions & 0 deletions dub.sdl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
name "glfw-d"
description "D translation of GLFW, a multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input"
authors "dkorpel"
copyright "Copyright © 2020, dkorpel"
license "Zlib"
targetName "glfw-d"
targetPath "build"
targetType "library"

subPackage "example"

dflags "-preview=dip1000" "-preview=dip25"
dflags "-preview=fieldwise"
dflags "-preview=markdown"
dflags "-preview=fixAliasThis"
dflags "-preview=intpromote"
dflags "-preview=dtorfields"
dflags "-mixin=build/mixin.d"

sourcePaths // by default dub includes everything in source/, which we don't want here
sourceFiles "source/glfw3/context.d"
sourceFiles "source/glfw3/init.d"
sourceFiles "source/glfw3/input.d"
sourceFiles "source/glfw3/monitor.d"
sourceFiles "source/glfw3/vulkan.d"
sourceFiles "source/glfw3/window.d"
sourceFiles "source/glfw3/mappings.d"
sourceFiles "source/glfw3/internal.d" // dmd wants to have _GLFWmapping.init even though it should be 0
sourceFiles "source/glfw3/api.d" // GLFWGamepadState.init

configuration "x11" {
platforms "linux"
versions "_GLFW_X11"
libs "X11" platform="linux"
sourceFiles "source/glfw3/x11_header.d"
buildOptions "betterC"

sourceFiles "source/glfw3/x11_platform.d"
sourceFiles "source/glfw3/x11_init.d"
sourceFiles "source/glfw3/x11_monitor.d"
sourceFiles "source/glfw3/x11_window.d"
sourceFiles "source/glfw3/xkb_unicode.d"
sourceFiles "source/glfw3/posix_time.d"
sourceFiles "source/glfw3/posix_thread.d"
sourceFiles "source/glfw3/glx_context.d"
sourceFiles "source/glfw3/egl_context.d"
sourceFiles "source/glfw3/osmesa_context.d"
sourceFiles "source/glfw3/linux_joystick.d"
}

configuration "windows" {
platforms "windows"
versions "_GLFW_WIN32"
buildOptions "betterC"

sourceFiles "source/glfw3/win32_platform.d"
sourceFiles "source/glfw3/win32_init.d"
sourceFiles "source/glfw3/win32_joystick.d"
sourceFiles "source/glfw3/win32_monitor.d"
sourceFiles "source/glfw3/win32_time.d"
sourceFiles "source/glfw3/win32_thread.d"
sourceFiles "source/glfw3/win32_window.d"
sourceFiles "source/glfw3/wgl_context.d"
sourceFiles "source/glfw3/egl_context.d"
sourceFiles "source/glfw3/osmesa_context.d"
sourceFiles "source/glfw3/directinput8.d"

libs "Gdi32" "User32"

// For cross-compiling:
// dflags "-mtriple=x86_64-windows-msvc" platform="ldc2"
}

configuration "wayland" {
platforms "linux"
versions "_GLFW_WAYLAND"
//buildOptions "betterC"
//dependency "wayland" version="0.2.0"
//dependency "wayland:client" version="0.2.0"
//dependency "wayland:client" path="../wayland-d/"
dependency "xkbcommon-d" version="0.5.1"

sourceFiles "source/glfw3/wl_platform.d"
sourceFiles "source/glfw3/wl_init.d"
sourceFiles "source/glfw3/wl_monitor.d"
sourceFiles "source/glfw3/wl_window.d"
sourceFiles "source/glfw3/linux_joystick.d"
sourceFiles "source/glfw3/posix_time.d"
sourceFiles "source/glfw3/posix_thread.d"
sourceFiles "source/glfw3/xkb_unicode.d"
sourceFiles "source/glfw3/egl_context.d"
sourceFiles "source/glfw3/osmesa_context.d"
}

configuration "osmesa" {
platforms "linux"
versions "_GLFW_OSMESA"
sourceFiles "source/glfw3/null_init.c"
sourceFiles "source/glfw3/null_monitor.c"
sourceFiles "source/glfw3/null_window.c"
sourceFiles "source/glfw3/null_joystick.c"
sourceFiles "source/glfw3/posix_time.c"
sourceFiles "source/glfw3/posix_thread.c"
sourceFiles "source/glfw3/osmesa_context.c"
}

configuration "cocoa" {
platforms "osx" // probably not the right identifier
versions "_GLFW_COCOA"
sourceFiles "source/glfw3/cocoa_platform.d"
sourceFiles "source/glfw3/cocoa_init.d"
sourceFiles "source/glfw3/cocoa_joystick.d"
sourceFiles "source/glfw3/cocoa_monitor.d"
sourceFiles "source/glfw3/cocoa_time.d"
sourceFiles "source/glfw3/cocoa_window.d"
sourceFiles "source/glfw3/posix_thread.d"
sourceFiles "source/glfw3/nsgl_context.d"
sourceFiles "source/glfw3/egl_context.d"
sourceFiles "source/glfw3/osmesa_context.d"
}
Loading

0 comments on commit 57fa135

Please sign in to comment.