Module Graphics
Machine-independent graphics primitives.
Initializations
val open_graph : string -> unitShow the graphics window or switch the screen to graphic mode. The graphics window is cleared and the current point is set to (0, 0). The string argument is used to pass optional information on the desired graphics mode, the graphics window size, and so on. Its interpretation is implementation-dependent. If the empty string is given, a sensible default is selected.
val size_x : unit -> intSee
Graphics.size_y.
val size_y : unit -> intReturn the size of the graphics window. Coordinates of the screen pixels range over
0 .. size_x()-1and0 .. size_y()-1. Drawings outside of this rectangle are clipped, without causing an error. The origin (0,0) is at the lower left corner. Some implementation (e.g. X Windows) represent coordinates by 16-bit integers, hence wrong clipping may occur with coordinates below-32768or above32676.
Colors
type color= intA color is specified by its R, G, B components. Each component is in the range
0..255. The three components are packed in anint:0xRRGGBB, whereRRare the two hexadecimal digits for the red component,GGfor the green component,BBfor the blue component.
val rgb : int -> int -> int -> colorrgb r g breturns the integer encoding the color with red componentr, green componentg, and blue componentb.r,gandbare in the range0..255.
val set_color : color -> unitSet the current drawing color.
val background : colorSee
Graphics.foreground.
val foreground : colorDefault background and foreground colors (usually, either black foreground on a white background or white foreground on a black background).
Graphics.clear_graphfills the screen with thebackgroundcolor. The initial drawing color isforeground.
Some predefined colors
Point and line drawing
val point_color : int -> int -> colorReturn the color of the given point in the backing store (see "Double buffering" below).
val lineto : int -> int -> unitDraw a line with endpoints the current point and the given point, and move the current point to the given point.
val rlineto : int -> int -> unitDraw a line with endpoints the current point and the current point translated of the given vector, and move the current point to this point.
val curveto : (int * int) -> (int * int) -> (int * int) -> unitcurveto b c ddraws a cubic Bezier curve starting from the current point to pointd, with control pointsbandc, and moves the current point tod.
val draw_rect : int -> int -> int -> int -> unitdraw_rect x y w hdraws the rectangle with lower left corner atx,y, widthwand heighth. The current point is unchanged. RaiseInvalid_argumentifworhis negative.
val draw_poly_line : (int * int) array -> unitdraw_poly_line pointsdraws the line that joins the points given by the array argument. The array contains the coordinates of the vertices of the polygonal line, which need not be closed. The current point is unchanged.
val draw_poly : (int * int) array -> unitdraw_poly polygondraws the given polygon. The array contains the coordinates of the vertices of the polygon. The current point is unchanged.
val draw_segments : (int * int * int * int) array -> unitdraw_segments segmentsdraws the segments given in the array argument. Each segment is specified as a quadruple(x0, y0, x1, y1)where(x0, y0)and(x1, y1)are the coordinates of the end points of the segment. The current point is unchanged.
val draw_arc : int -> int -> int -> int -> int -> int -> unitdraw_arc x y rx ry a1 a2draws an elliptical arc with centerx,y, horizontal radiusrx, vertical radiusry, from anglea1to anglea2(in degrees). The current point is unchanged. RaiseInvalid_argumentifrxorryis negative.
val draw_ellipse : int -> int -> int -> int -> unitdraw_ellipse x y rx rydraws an ellipse with centerx,y, horizontal radiusrxand vertical radiusry. The current point is unchanged. RaiseInvalid_argumentifrxorryis negative.
val draw_circle : int -> int -> int -> unitdraw_circle x y rdraws a circle with centerx,yand radiusr. The current point is unchanged. RaiseInvalid_argumentifris negative.
val set_line_width : int -> unitSet the width of points and lines drawn with the functions above. Under X Windows,
set_line_width 0selects a width of 1 pixel and a faster, but less precise drawing algorithm than the one used whenset_line_width 1is specified. RaiseInvalid_argumentif the argument is negative.
Text drawing
val draw_char : char -> unitSee
Graphics.draw_string.
val draw_string : string -> unitDraw a character or a character string with lower left corner at current position. After drawing, the current position is set to the lower right corner of the text drawn.
val set_font : string -> unitSet the font used for drawing text. The interpretation of the argument to
set_fontis implementation-dependent.
Filling
val fill_rect : int -> int -> int -> int -> unitfill_rect x y w hfills the rectangle with lower left corner atx,y, widthwand heighth, with the current color. RaiseInvalid_argumentifworhis negative.
val fill_poly : (int * int) array -> unitFill the given polygon with the current color. The array contains the coordinates of the vertices of the polygon.
val fill_arc : int -> int -> int -> int -> int -> int -> unitFill an elliptical pie slice with the current color. The parameters are the same as for
Graphics.draw_arc.
val fill_ellipse : int -> int -> int -> int -> unitFill an ellipse with the current color. The parameters are the same as for
Graphics.draw_ellipse.
val fill_circle : int -> int -> int -> unitFill a circle with the current color. The parameters are the same as for
Graphics.draw_circle.
Images
type imageThe abstract type for images, in internal representation. Externally, images are represented as matrices of colors. Images are bound to the current graphics window and should not be reused after closing this graphics window with
close_graph.
val transp : colorIn matrices of colors, this color represent a 'transparent' point: when drawing the corresponding image, all pixels on the screen corresponding to a transparent pixel in the image will not be modified, while other points will be set to the color of the corresponding point in the image. This allows superimposing an image over an existing background.
val make_image : color array array -> imageConvert the given color matrix to an image. Each sub-array represents one horizontal line. All sub-arrays must have the same length; otherwise, exception
Graphic_failureis raised.
val draw_image : image -> int -> int -> unitDraw the given image with lower left corner at the given point.
val get_image : int -> int -> int -> int -> imageCapture the contents of a rectangle on the screen as an image. The parameters are the same as for
Graphics.fill_rect.
val create_image : int -> int -> imagecreate_image w hreturns a new imagewpixels wide andhpixels tall, to be used in conjunction withblit_image. The initial image contents are random, except that no point is transparent.
val blit_image : image -> int -> int -> unitblit_image img x ycopies screen pixels into the imageimg, modifyingimgin-place. The pixels copied are those inside the rectangle with lower left corner atx,y, and width and height equal to those of the image. Pixels that were transparent inimgare left unchanged.
Mouse and keyboard events
type status={}To report events.
type event=|Button_downA mouse button is pressed
|Button_upA mouse button is released
|Key_pressedA key is pressed
|Mouse_motionThe mouse is moved
|PollDon't wait; return immediately
To specify events to wait for.
val wait_next_event : event list -> statusWait until one of the events specified in the given event list occurs, and return the status of the mouse and keyboard at that time. If
Pollis given in the event list, return immediately with the current status. If the mouse cursor is outside of the graphics window, themouse_xandmouse_yfields of the event are outside the range0..size_x()-1, 0..size_y()-1. Keypresses are queued, and dequeued one by one when theKey_pressedevent is specified and thePollevent is not specified.
val loop_at_exit : event list -> (status -> unit) -> unitLoop before exiting the program, the list given as argument is the list of handlers and the events on which these handlers are called. To exit cleanly the loop, the handler should raise Exit. Any other exception will be propagated outside of the loop.
- since
- 4.01
Mouse and keyboard polling
val mouse_pos : unit -> int * intReturn the position of the mouse cursor, relative to the graphics window. If the mouse cursor is outside of the graphics window,
mouse_pos()returns a point outside of the range0..size_x()-1, 0..size_y()-1.
Sound
Double buffering
val auto_synchronize : bool -> unitBy default, drawing takes place both on the window displayed on screen, and in a memory area (the 'backing store'). The backing store image is used to re-paint the on-screen window when necessary.
To avoid flicker during animations, it is possible to turn off on-screen drawing, perform a number of drawing operations in the backing store only, then refresh the on-screen window explicitly.
auto_synchronize falseturns on-screen drawing off. All subsequent drawing commands are performed on the backing store only.auto_synchronize truerefreshes the on-screen window from the backing store (as persynchronize), then turns on-screen drawing back on. All subsequent drawing commands are performed both on screen and in the backing store.The default drawing mode corresponds to
auto_synchronize true.
val synchronize : unit -> unitSynchronize the backing store and the on-screen window, by copying the contents of the backing store onto the graphics window.
val display_mode : bool -> unitSet display mode on or off. When turned on, drawings are done in the graphics window; when turned off, drawings do not affect the graphics window. This occurs independently of drawing into the backing store (see the function
Graphics.remember_modebelow). Default display mode is on.
val remember_mode : bool -> unitSet remember mode on or off. When turned on, drawings are done in the backing store; when turned off, the backing store is unaffected by drawings. This occurs independently of drawing onto the graphics window (see the function
Graphics.display_modeabove). Default remember mode is on.