GamelleGamelle is a tiny 2D game engine. It provides:
UiPhysicsYou can create a new game project with:
$ gamelle init mygame
$ cd mygame
$ makeThe make command starts the game in development mode: Editing src/mygame.ml will automatically reload your game on every change. Game assets added to the folder assets/ are automatically available through the dynamic Assets module.
To export your game as a single HTML file, compiled with js_of_ocaml and including all game assets:
$ make htmlThe type allowing input/output operations. Every side-effecting function requires a named argument ~io of this type.
val run : 'state -> (io:io -> 'state -> 'state) -> unitrun initial_state fn is the game main loop. The function fn will be called at every frame to react to player inputs and draw the game state on the screen.
open Gamelle
type state = ...
let initial_state = ...
let () =
Gamelle.run initial_state @@ fun ~io current_state ->
let new_state = (* TODO: react to player inputs and update the current state *) in
(* TODO: draw the new game state *)
new_statemodule Color : sig ... endColors.
module Point : sig ... endPoints: x and y positions in 2D.
module Vec : sig ... endVectors: directions in 2D.
module Size : sig ... endSizes: width and height dimensions.
module Box : sig ... endAxis-aligned bounding boxes.
module Circle : sig ... endCircles.
module Polygon : sig ... endPolygons.
module Shape : sig ... endArbitrary shapes: segments, circles and polygons.
Game assets like images, fonts and sounds which are added to the assets/ folder are automatically loaded and available through the dynamic Assets module. For example, a bitmap file assets/foo.png is accessible as Assets.foo : Bitmap.t.
module Bitmap : sig ... endBitmap images: PNG, JPEG.
draw ~io ~at bitmap draws the image bitmap at position at on the screen. Same as Bitmap.draw.
Example:
(* draw the assets/player.png bitmap at position x=100, y=200 *)
draw ~io Assets.player ~at:(Point.v 100. 200.)module Font : sig ... endTypefaces used to render text on screen.
module Text : sig ... endText rendering.
val draw_string :
io:io ->
?color:Color.t ->
?font:Font.t ->
?size:int ->
at:Point.t ->
string ->
unitdraw_string ~io ~at txt prints the string txt at position at on the screen. Same as Text.draw.
?color is the text color, see View.color?font is the typeface used to render the text, see View.font?size is the font size, see View.font_sizeExamples:
draw_string ~io "Hello World" ~at:(Input.mouse_pos ~io);
draw_string ~io ~color:Color.red ~at:(Point.v 200. 100.) "Bloody!";
draw_string ~io ~at:Point.zero "Why so serious?" ~font:Assets.comic_sans
~size:50module Sound : sig ... endAudio sounds and musics: MP3, OGG.
module Input : sig ... endPlayer inputs: mouse and keyboard events.
module Ui : sig ... endGraphical user interface: buttons, checkboxes, text inputs.
module Window : sig ... endConfigure the game window.
val clock : io:io -> floatclock ~io returns the number of elapsed seconds since the game started until the current frame. The clock is defined at the beginning of a frame, calling clock multiple times will always produce the same result.
Examples:
(* circle moving around a circle *)
let center =
Vec.(100.0 * v (1. +. cos (clock ~io)) (1. +. sin (clock ~io)))
in
Circle.fill ~io (Circle.v center 10.0) (* show elapsed time since the last click *)
Gamelle.run 0. @@ fun ~io last_click ->
let now = clock ~io in
let last_click =
if Input.is_up ~io `click_left then now else last_click
in
let elapsed = now -. last_click in
draw_string ~io ~at:Point.zero
(Printf.sprintf "Time since clicked: %fs" elapsed);
last_clickval dt : io:io -> floatdt ~io is the duration of a frame, which is fixed to a 60fps framerate.
Example:
Gamelle.run (Point.v 200. 200., Vec.zero)
@@ fun ~io (position, velocity) ->
let acceleration = Vec.v 0. 9.81 in
(* gravity *)
let velocity = Vec.(velocity + (dt ~io * acceleration)) in
let position = Vec.(position + (dt ~io * velocity)) in
Circle.draw ~io (Circle.v position 20.0);
(position, velocity)module Ease : sig ... endEasing functions, to smooth changes over time.
module Anim : sig ... endAnimations.