Skip to content

Commit

Permalink
Merge pull request #12 from dkorpel/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
dkorpel authored Apr 1, 2021
2 parents d1b595f + 49556dd commit 7c29c79
Show file tree
Hide file tree
Showing 39 changed files with 2,885 additions and 485 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/d.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ jobs:

- name: Build OpenGL triangle example
run: dub -q build glfw-d:triangle-gl

- name: Build Vulkan triangle example
run: dub -q build glfw-d:triangle-vulkan
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ The translation is not affiliated with the original project.
If you are using dub, add this package as a dependency.
In `dub.sdl`:
```
dependency "glfw-d" version="~>1.0.1"
dependency "glfw-d" version="~>1.0.2"
```
In `dub.json`:
```
"dependencies": {
"glfw-d": "~>1.0.1"
"glfw-d": "~>1.0.2"
}
```

Expand All @@ -45,14 +45,16 @@ void main() {
}
```
Example GLFW projects can be found in the [examples folder](https://github.com/dkorpel/glfw-d/tree/master/examples/).
You can run them from the root of the repository using:
You can run them from the root of this repository using:
```
dub run glfw-d:empty-window
dub run glfw-d:triangle-gl
dub run glfw-d:triangle-vulkan
```
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)
See also: the [tutorial on glfw.org](https://www.glfw.org/docs/latest/quick.html) for a quick introduction.

If you have any trouble, feel free to open an [Issue](https://github.com/dkorpel/glfw-d/issues) or [Discussion](https://github.com/dkorpel/glfw-d/discussions).

## 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)).
Expand All @@ -69,7 +71,7 @@ It's very easy for issues to arise:
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:
I get these results from the 'total' time of `time dub build glfw-d --force` on my Debian Linux box:

| build type | time (s) |
|--------------|----------|
Expand All @@ -84,13 +86,13 @@ Dub caches builds, so these compile times only apply the first time.
- 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.
Once GLFW 3.4 comes out, this translation might get behind.
However, you can easily switch back to compiling the C sources if this becomes an issue.

### 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 have not used this with OpenGL ES.
I haven't used the native api (`glfw3/apinative.d` here) or functions for custom cursor creation yet.

- Add Wayland support
Expand Down Expand Up @@ -147,20 +149,24 @@ versions "GLFW_EXPOSE_NATIVE_WIN32" "GLFW_EXPOSE_NATIVE_WGL"
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
# From the root of this repository, enter the source folder
cd source/glfw3
# Windows example:
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
# Linux example:
dmd -version=_GLFW_X11 -L=X11 -I../ -lib -of=../../build/glfw.a context.d init.d input.d monitor.d vulkan.d window.d mappings.d internal.d api.d x11_platform.d x11_init.d x11_monitor.d x11_window.d xkb_unicode.d posix_time.d posix_thread.d glx_context.d egl_context.d osmesa_context.d linux_joystick.d linuxinput.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:
Since it's a direct translation of a C codebase, it compiles [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.
Building a shared library from the D sources could be possible, but it's not a supported use-case currently.
You're welcome to try it, but I can't guide you here.
7 changes: 3 additions & 4 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ targetType "library"

subPackage "examples/empty-window"
subPackage "examples/triangle-gl"
//subPackage "examples/triangle-vulkan"
subPackage "examples/triangle-vulkan"

dflags "-preview=dip1000" "-preview=dip25"
dflags "-preview=dip1000"
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
Expand Down Expand Up @@ -48,6 +46,7 @@ configuration "x11" {
sourceFiles "source/glfw3/egl_context.d"
sourceFiles "source/glfw3/osmesa_context.d"
sourceFiles "source/glfw3/linux_joystick.d"
sourceFiles "source/glfw3/linuxinput.d" // missing symbol on ubuntu-dmd-2.085: glfw3.linuxinput.ff_effect.__init
}

configuration "windows" {
Expand Down
3 changes: 2 additions & 1 deletion examples/empty-window/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ void printMonitorState() {
printf(" current video mode: %dx%d %dHz r%dg%db%d\n", mode.width, mode.height, mode.refreshRate, mode.redBits, mode.greenBits, mode.blueBits);
printf(" position: %d, %d\n", xpos, ypos);
glfwGetMonitorWorkarea(mt, &xpos, &ypos, &width, &height);
printf(" work area: %d, %d to %d, %d\n", xpos, ypos, width, height);
printf(" work area: (%d, %d), size (%d, %d)\n", xpos, ypos, width, height);
}
}

void printJoystickState() {
for (int js = GLFW_JOYSTICK_1; js <= GLFW_JOYSTICK_LAST; js++) {
if (glfwJoystickPresent(js)) {
//glfwSetJoystickRumble(js, /*slow*/ 0.25, /*fast*/ 0.25);
printf("Joystick %d has name `%s` and GUID `%s`\n", js, glfwGetJoystickName(js), glfwGetJoystickGUID(js));
int buttonsLength, axesLength, hatsLength;
const(ubyte)* buttonsPtr = glfwGetJoystickButtons(js, &buttonsLength);
Expand Down
12 changes: 2 additions & 10 deletions examples/triangle-gl/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,15 @@ int main() {

const GLuint program = getProgram();
const GLuint vaoTriangle = getTriangleVao();
const GLint mvp_location = glGetUniformLocation(program, "MVP");

double oldTime = glfwGetTime();
while (!glfwWindowShouldClose(window)) {
const newTime = glfwGetTime();
const elapsedTime = newTime - oldTime;
oldTime = newTime;

int width, height;
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / cast(float) height;
glViewport(0, 0, width, height);

glUseProgram(program);
glBindVertexArray(vaoTriangle);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawArrays(GL_TRIANGLES, /*first*/ 0, /*count*/ 3);

glfwSwapBuffers(window);
glfwPollEvents();
Expand All @@ -66,12 +59,11 @@ extern(C) @nogc nothrow void errorCallback(int error, const(char)* description)
// SHADER PROGRAM /////////////////////////

immutable string vertexShaderSource = "#version 330
uniform mat4 MVP;
layout(location = 0) in vec2 position;
layout(location = 1) in vec3 color;
out vec3 fragColor;
void main() {
gl_Position = vec4(position, 0.0, 1.0); // MVP *
gl_Position = vec4(position, 0.0, 1.0);
fragColor = color;
}";

Expand Down
2 changes: 1 addition & 1 deletion examples/triangle-gl/dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ authors "dkorpel"
copyright "Copyright © 2021, dkorpel"
license "Zlib"
targetType "executable"
targetName "glfw-example"
targetName "triangle-gl"
targetPath "../../build"
dependency "glfw-d" path="../../"
dependency "bindbc-opengl" version="~>0.13.0"
Expand Down
20 changes: 20 additions & 0 deletions examples/triangle-vulkan/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Vulkan triangle example

![Screenshot](screenshot.png)

This example opens a window and draws a triangle using Vulkan.

It is a translation of [triangle-vulkan.c](https://github.com/glfw/glfw/blob/33cd8b865d9289cfbcf3d95e6e68e4050b94fcd3/tests/triangle-vulkan.c) from the GLFW test suite.
Instead of [glad](https://github.com/glfw/glfw/blob/33cd8b865d9289cfbcf3d95e6e68e4050b94fcd3/deps/glad_vulkan.c) and glfw's vulkan module, it uses [erupteD](https://github.com/ParticlePeter/ErupteD) as the Vulkan function loader.

From this folder, run it with:
```
dub run
```

From the root of the repository, run it with:
```
dub run glfw-d:triangle-vulkan
```

See also: [the GLFW Vulkan guide](https://www.glfw.org/docs/latest/vulkan_guide.html)
Loading

0 comments on commit 7c29c79

Please sign in to comment.