Gfx
All errors are reported with this exception. The payload is an informative message.
Type of windows.
val resource_ready : 'a resource -> bool
Tests if the resource is available.
val get_resource : 'a resource -> 'a
Returns the content of the resource.
val get_resource_opt : 'a resource -> 'a option
Returns Some v
if the resource is ready and None
otherwise.
val create : string -> window
create s
returns a window and a rendering surface from the string s
. The string has the form "name:WxH:flags"
. For the JavaScript backend, name
is element id of the canvas representing the window. In the SDL backend, name
is the window title.
val get_window_size : window -> int * int
returns the dimensions in screen pixels of the window
val set_window_size : window -> int -> int -> unit
sets the dimensions in pixels of the window
val set_context_logical_size : context -> int -> int -> unit
set_context_logical_size ctx w h
sets the logical size of the context. The initial values are the same dimentions as the window. The logical size reflects the range of pixels that are shown in the context. For instance, If the logical size is 100x100 but the window size is 400x400, each logical pixel will be automatically zoomed and displayed as a 4x4 pixel in the window.
val get_context_logical_size : context -> int * int
get_context_logical_size ctx w h
returns the logical size of the context.
val set_transform : context -> float -> bool -> bool -> unit
set_transform ctx angle hflip vflip
stores a transformation in the context. The transformation is a rotation of angle
(in radians), on horizontal reflection (if hflip
is true
) and a vertical reflection (if vflip
) is true
).
val get_transform : context -> float * bool * bool
get_transform ctx
returns the transformation currently associated with the context.
val reset_transform : context -> unit
create_surface ctx w h
: creates a surface for the given rendering context.
val surface_size : surface -> int * int
returns the dimensions of a surface
blit dst src x y
copies surface src
on surface dst
at point (x,y)
blit_scale ctx dst src dx dy dw dh
copies surface src
on surface dst
at point (dx,dy)
scaling it to dw
width and dh
height
val blit_full :
context ->
surface ->
surface ->
int ->
int ->
int ->
int ->
int ->
int ->
int ->
int ->
unit
blit_full ctx dst src sx sy sw sh dx dy dw dh
copies the surface extracted from src
at point (sx, sy)
with dimensions (sw, sh)
on surface dst
at point (dx,dy)
scaling it to dw
width and dh
height
val color : int -> int -> int -> int -> color
color r g b a
returns a color built from components red green blue and alpha. all values must be integers between 0 and 255 inclusive.
fill_rect ctx dst x y w h
draws and fills a rectangle on surface surface dst
at coordinates (x, y)
and with dimensions w * h
. The rectangle is filled with current color
load_image ctx path
loads an image whose content is given by an implementation dependent string (filename, url, … ). Common image types are supported (PNG, JPEG, …). The returned resource may not be extracted used until resource_ready
returns true
.
val load_file : string -> string resource
load_file path
creates a resource that, when ready, resolves to the content of the file denoted by path.
val load_font : string -> string -> int -> font
load_font fn extra size
loads font fn
at size size
given in points. The extra
parameters allows to pass implementation dependent options.
fn
is a font name. If it does not exist, it is silently replaced by a close matching font or default font by the browser.fn
must be a path to the .ttf
file containing the font.render_text ctx txt f c
returns a surface containing the text txt
rendered using font f
with color c
.
val measure_text : string -> font -> int * int
mesure_text txt f
returns the size (width height) of the surface that render_text
would return, without creating it.
The type of input events. The string describing keyboard events is implementation defined.
val poll_event : unit -> event
poll_event ()
returns the next event in the event queue
main_loop f k
calls a f
repeteadly.
requestAnimationFrame
and is called no faster than 60 times/seconds. The callback f
is given a float representing the elapsed time in milliseconds since the begining of the program. if f
returns None
, then it will continue being called. If f
returns Some v
then the continuation k v
is called instead (once).One would typically write code such as:
let text_file = Gfx.load_file "foo.txt" in
Gfx.main_loop
(fun _dt -> Gfx.get_resource_opt text_file)
(fun content ->
(* do something with the content *)
)
val commit : context -> unit
commit ctx
: renders the rendering context on to its underlying window. This should be the last graphical operation of the current frame.
open_formatter src
opens a formatter for debugging purposes.
src
is the file path.src
must be the ID of an element whose innerHTML is appended to. It is recommended to style the element with the CSS property white-space:pre to preserve white spaces.set_debug_formatter fmt
sets the current formatter. The default value is Format.stderr.