Giter VIP home page Giter VIP logo

glfw-ocaml's Introduction

GLFW-OCaml

A GLFW binding for OCaml.

Installing

The current release is version 3.3.1-2. Please install the GLFW library and its development files before attempting to install GLFW-OCaml.

With opam

Opam is a package manager for OCaml. This is the recommended way, simply run:

opam install glfw-ocaml

or if you would rather use the latest revision:

opam pin git+https://github.com/SylvainBoilard/GLFW-OCaml.git

Building from source

GLFW-OCaml uses Dune as its build system. Fetch the source by running:

wget https://github.com/SylvainBoilard/GLFW-OCaml/archive/3.3.1-2.zip
unzip GLFW-OCaml-3.3.1-2.zip
cd GLFW-OCaml-3.3.1-2

or for the latest revision:

git clone https://github.com/SylvainBoilard/GLFW-OCaml.git
cd GLFW-OCaml

Then build and install by running:

dune build
dune install --prefix=<install_directory> # For example "/usr/local" or "/opt" (run as root)

Usage

GLFW-OCaml is a pretty straight-forward binding from the original API. Please refer to the GLFW manual for detailed information. All functions and values are found in module GLFW.

Naming conventions

Functions have the glfw prefix removed and their first letter uncapitalized. For example glfwCreateWindow would become createWindow.

Values have the GLFW_ and where appropriate the KEY_ or MOD_ prefixes removed, are converted to PascalCase and typed according to their usage. For example GLFW_KEY_LEFT_BRACKET would become constructor LeftBracket of type key and GLFW_NO_RESET_NOTIFICATION would become constructor NoResetNotification of type context_robustness. Names with a single letter prefix directly appended such as GLFW_IBEAM_CURSOR become IBeamCursor.

Mouse buttons and joysticks

Instead of defining values for individual mouse buttons and joysticks, a plain 0-indexed number is used, although there are mouse_button_{left,right,middle} values defined for the most common mouse buttons. The maximum number of mouse buttons and connected joysticks can be accessed with the mouse_button_max_count and joystick_max_count values.

Window hints, attributes and input modes

Window hints, attributes and input modes make use of GADTs to retain the polymorphic aspect of the corresponding original functions while ensuring type correctness.

Unlike the other functions where you can omit the labels of arguments, you will find you have to use them with getWindowAttrib and getInputMode. This is because the return type of these functions is polymorphic and the compiler will try to put any unlabeled arguments there. This makes for most peculiar errors.

Error handling

Errors are propagated with exceptions instead of through a callback function. While there is a definition for an InvalidEnum exception, one should never be raised if you are not using unsafe features; otherwise that would be a GLFW-OCaml bug.

Callbacks and user pointers

There is no need for user pointers in GLFW-OCaml as you can capture your data inside a closure and use that closure as a callback function while preserving type information.

Gamma ramps and images

The GammaRamp module provides two functions create and make to create gamma ramps while insuring all channels have the same length. The former builds a gamma ramp from three user-supplied channels while the later builds an empty gamma ramp with three channels of the supplied length. You can freely access and modify the contents of an existing gamma ramp but are required to use one of these functions to build one.

The Image module likewise provides a create function to create an image from raw pixel data with acceptable dimensions. Once created the dimensions of an image are immutable but you can still change its pixels by modifying the byte array.

Return values

Some of the original functions return their result via pointer arguments. In GLFW-OCaml these functions instead return a tuple with the individual elements in the same order as they appear in the original function.

GLFW_DONT_CARE and NULL-able values

Wherever GLFW_DONT_CARE or a NULL pointer would be a legal value, an option type is used to wrap the value and None is used to represent GLFW_DONT_CARE or NULL.

glfwGetProcAddress and Vulkan

The glfwGetProcAddress function is not supported because it would require writing an entire OpenGL wrapper to make the functions returned by GLFW usable from OCaml. There are several OpenGL bindings available for OCaml that you can use instead.

The Vulkan related functions are not supported as of now but we might look into it at some point or on request.

glfw-ocaml's People

Contributors

sylvainboilard avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

glfw-ocaml's Issues

difficulties on macos, arm64

I had already brew install glfw

first attempt to opam instal glfw-ocaml failed with:

# File "glfw-ocaml/dune", line 9, characters 15-25:
# 9 |   (names       GLFW_stubs)
#                    ^^^^^^^^^^
# (cd _build/default/glfw-ocaml && /usr/bin/cc -O2 -fno-strict-aliasing -fwrapv -pthread -D_FILE_OFFSET_BITS=64 -O2 -fno-strict-aliasing -fwrapv -pthread -g -I /Users/paul/.opam/oktree/lib/ocaml -o GLFW_stubs.o -c GLFW_stubs.c)
# GLFW_stubs.c:1:10: fatal error: 'GLFW/glfw3.h' file not found
# #include <GLFW/glfw3.h>
#          ^~~~~~~~~~~~~~
# 1 error generated.

from this answer https://stackoverflow.com/a/67378304/202168 I fixed that by export CPATH=/opt/homebrew/include

installed successfully

then I copied the code from example window.ml and tried to compile it:

ocamlfind ocamlopt -package glfw-ocaml -thread -linkpkg -o glfw.native glfw.ml
File "glfw.ml", line 8, characters 15-32:
8 |   let window = GLFW.createWindow 640 480 "Hello World" () in
                   ^^^^^^^^^^^^^^^^^
Warning 6 [labels-omitted]: labels width, height, title were omitted in the application of this function.
File "glfw.ml", line 10, characters 2-25:
10 |   GLFW.makeContextCurrent (Some window);
       ^^^^^^^^^^^^^^^^^^^^^^^
Warning 6 [labels-omitted]: label window was omitted in the application of this function.
File "glfw.ml", line 12, characters 13-35:
12 |   while not (GLFW.windowShouldClose window) do
                  ^^^^^^^^^^^^^^^^^^^^^^
Warning 6 [labels-omitted]: label window was omitted in the application of this function.
File "glfw.ml", line 16, characters 4-20:
16 |     GLFW.swapBuffers window;
         ^^^^^^^^^^^^^^^^
Warning 6 [labels-omitted]: label window was omitted in the application of this function.
Undefined symbols for architecture arm64:
  "_caml_glClearColorBufferBit", referenced from:
      _camlGlfw__entry in glfw.o
      _camlGlfw__1 in glfw.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
File "caml_startup", line 1:
Error: Error during linking (exit code 1)

I found a couple of answers:

the solution there seems to revolve around having a CMakeLists.txt file with target_link_libraries(<project> glfw)

but I have no idea how to relate that back to glfw-ocaml ...do you have any tips? thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.