1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
The graphics library
</TITLE>
</HEAD>
<BODY >
<A HREF="manual063.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual065.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>
<H1>Chapter 22: The graphics library</H1>
The <TT>graphics</TT> library provides a set of portable drawing primitives.
Drawing takes place
in a separate window that is created when <TT>open</TT><TT>_</TT><TT>graph</TT> is called.<BR>
<BR>
<FONT COLOR=purple>Unix:</FONT>
<BLOCKQUOTE>
This library is implemented under the X11 windows system.
Programs that use the <TT>graphics</TT> library must be linked as follows:
<PRE>
ocamlc -custom <I>other options</I> graphics.cma <I>other files</I> \
-cclib -lgraphics -cclib -lX11
</PRE>
For interactive use of the <TT>graphics</TT> library, do:
<PRE>
ocamlmktop -custom -o mytop graphics.cma -cclib -lgraphics \
-cclib -lX11
./mytop
</PRE>
Here are the graphics mode specifications supported by <TT>open</TT><TT>_</TT><TT>graph</TT> on
the X11 implementation of this library:
the argument to <TT>open</TT><TT>_</TT><TT>graph</TT> has the format
<TT>"</TT><I>display-name</I> <I>geometry</I><TT>"</TT>,
where <I>display-name</I> is the name of the X-windows display to
connect to, and <I>geometry</I> is a standard X-windows geometry
specification. The two components are separated by a space. Either can
be omitted, or both. Examples:
<DL COMPACT=compact>
<DT>
<TT>open</TT><TT>_</TT><TT>graph "foo:0"</TT><DD>
connects to the display <TT>foo:0</TT> and creates a window with the default geometry
<DT><TT>open</TT><TT>_</TT><TT>graph "foo:0 300x100+50-0"</TT><DD>
connects to the display <TT>foo:0</TT> and creates a window 300 pixels wide
by 100 pixels tall, at location (50,0)
<DT><TT>open</TT><TT>_</TT><TT>graph " 300x100+50-0"</TT><DD>
connects to the default display and creates a window 300 pixels wide
by 100 pixels tall, at location (50,0)
<DT><TT>open</TT><TT>_</TT><TT>graph ""</TT><DD>
connects to the default display and creates a window with the default
geometry.
</DL>
</BLOCKQUOTE>
<FONT COLOR=purple>Windows:</FONT>
<BLOCKQUOTE>
This library is available only under the toplevel application
<TT>ocamlwin.exe</TT>. Before using it, the Caml part of this library must be
loaded in-core, either by typing
<PRE>
#load "graphics.cmo";;
</PRE>
in the input windows or by using the ``Load'' entry of the ``File''
menu.
</BLOCKQUOTE>The screen coordinates are interpreted as shown in the figure below.
Notice that the coordinate system used is the same as in mathematics:
<I>y</I> increases from the bottom of the screen to the top of the screen,
and angles are measured counterclockwise (in degrees).
Drawing is clipped to the screen.
<BR>
<BR>
<DIV ALIGN=center>
<IMG SRC="libgraph.gif">
</DIV>
<BR>
<H2>22.1 Module <TT>Graphics</TT>: machine-independent graphics primitives</H2><A NAME="s:Graphics"></A>
<A NAME="@manual828"></A><PRE>
exception Graphic_failure of string
</PRE>
<A NAME="@manual829"></A><BLOCKQUOTE>
Raised by the functions below when they encounter an error.
</BLOCKQUOTE>
<H3>Initializations </H3>
<PRE>
val open_graph: string -> unit
</PRE>
<A NAME="@manual830"></A><BLOCKQUOTE>
Show 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.
</BLOCKQUOTE>
<PRE>
val close_graph: unit -> unit
</PRE>
<A NAME="@manual831"></A><BLOCKQUOTE>
Delete the graphics window or switch the screen back to
text mode.
</BLOCKQUOTE>
<PRE>
val clear_graph : unit -> unit
</PRE>
<A NAME="@manual832"></A><BLOCKQUOTE>
Erase the graphics window.
</BLOCKQUOTE>
<PRE>
val size_x : unit -> int
val size_y : unit -> int
</PRE>
<A NAME="@manual833"></A><A NAME="@manual834"></A><BLOCKQUOTE>
Return the size of the graphics window. Coordinates of the screen
pixels range over <CODE>0 .. size_x()-1</CODE> and <CODE>0 .. size_y()-1</CODE>.
Drawings outside of this rectangle are clipped, without causing
an error. The origin (0,0) is at the lower left corner.
</BLOCKQUOTE>
<H3>Colors </H3>
<PRE>
type color = int
</PRE>
<BLOCKQUOTE>
A color is specified by its R, G, B components. Each component
is in the range <CODE>0..255</CODE>. The three components are packed in
an <CODE>int</CODE>: <CODE>0xRRGGBB</CODE>, where <CODE>RR</CODE> are the two hexadecimal digits for
the red component, <CODE>GG</CODE> for the green component, <CODE>BB</CODE> for the
blue component.
</BLOCKQUOTE>
<PRE>
val rgb: int -> int -> int -> color
</PRE>
<A NAME="@manual835"></A><BLOCKQUOTE>
<CODE>rgb r g b</CODE> returns the integer encoding the color with red
component <CODE>r</CODE>, green component <CODE>g</CODE>, and blue component <CODE>b</CODE>.
<CODE>r</CODE>, <CODE>g</CODE> and <CODE>b</CODE> are in the range <CODE>0..255</CODE>.
</BLOCKQUOTE>
<PRE>
val set_color : color -> unit
</PRE>
<A NAME="@manual836"></A><BLOCKQUOTE>
Set the current drawing color.
</BLOCKQUOTE>
<PRE>
val black : color
val white : color
val red : color
val green : color
val blue : color
val yellow : color
val cyan : color
val magenta : color
</PRE>
<A NAME="@manual837"></A><A NAME="@manual838"></A><A NAME="@manual839"></A><A NAME="@manual840"></A><A NAME="@manual841"></A><A NAME="@manual842"></A><A NAME="@manual843"></A><A NAME="@manual844"></A><BLOCKQUOTE>
Some predefined colors.
</BLOCKQUOTE>
<PRE>
val background: color
val foreground: color
</PRE>
<A NAME="@manual845"></A><A NAME="@manual846"></A><BLOCKQUOTE>
Default background and foreground colors (usually, either black
foreground on a white background or white foreground on a
black background).
<CODE>clear_graph</CODE> fills the screen with the <CODE>background</CODE> color.
The initial drawing color is <CODE>foreground</CODE>.
</BLOCKQUOTE>
<H3>Point and line drawing </H3>
<PRE>
val plot : int -> int -> unit
</PRE>
<A NAME="@manual847"></A><BLOCKQUOTE>
Plot the given point with the current drawing color.
</BLOCKQUOTE>
<PRE>
val point_color : int -> int -> color
</PRE>
<A NAME="@manual848"></A><BLOCKQUOTE>
Return the color of the given point.
</BLOCKQUOTE>
<PRE>
val moveto : int -> int -> unit
</PRE>
<A NAME="@manual849"></A><BLOCKQUOTE>
Position the current point.
</BLOCKQUOTE>
<PRE>
val current_point : unit -> int * int
</PRE>
<A NAME="@manual850"></A><BLOCKQUOTE>
Return the position of the current point.
</BLOCKQUOTE>
<PRE>
val lineto : int -> int -> unit
</PRE>
<A NAME="@manual851"></A><BLOCKQUOTE>
Draw a line with endpoints the current point and the given point,
and move the current point to the given point.
</BLOCKQUOTE>
<PRE>
val draw_arc : int -> int -> int -> int -> int -> int -> unit
</PRE>
<A NAME="@manual852"></A><BLOCKQUOTE>
<CODE>draw_arc x y rx ry a1 a2</CODE> draws an elliptical arc with center
<CODE>x,y</CODE>, horizontal radius <CODE>rx</CODE>, vertical radius <CODE>ry</CODE>, from angle
<CODE>a1</CODE> to angle <CODE>a2</CODE> (in degrees). The current point is unchanged.
</BLOCKQUOTE>
<PRE>
val draw_ellipse : int -> int -> int -> int -> unit
</PRE>
<A NAME="@manual853"></A><BLOCKQUOTE>
<CODE>draw_ellipse x y rx ry</CODE> draws an ellipse with center
<CODE>x,y</CODE>, horizontal radius <CODE>rx</CODE> and vertical radius <CODE>ry</CODE>.
The current point is unchanged.
</BLOCKQUOTE>
<PRE>
val draw_circle : int -> int -> int -> unit
</PRE>
<A NAME="@manual854"></A><BLOCKQUOTE>
<CODE>draw_circle x y r</CODE> draws a circle with center <CODE>x,y</CODE> and
radius <CODE>r</CODE>. The current point is unchanged.
</BLOCKQUOTE>
<PRE>
val set_line_width : int -> unit
</PRE>
<A NAME="@manual855"></A><BLOCKQUOTE>
Set the width of points and lines drawn with the functions above.
Under X Windows, <CODE>set_line_width 0</CODE> selects a width of 1 pixel
and a faster, but less precise drawing algorithm than the one
used when <CODE>set_line_width 1</CODE> is specified.
</BLOCKQUOTE>
<H3>Text drawing </H3>
<PRE>
val draw_char : char -> unit
val draw_string : string -> unit
</PRE>
<A NAME="@manual856"></A><A NAME="@manual857"></A><BLOCKQUOTE>
Draw 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.
</BLOCKQUOTE>
<PRE>
val set_font : string -> unit
val set_text_size : int -> unit
</PRE>
<A NAME="@manual858"></A><A NAME="@manual859"></A><BLOCKQUOTE>
Set the font and character size used for drawing text.
The interpretation of the arguments to <CODE>set_font</CODE> and
<CODE>set_text_size</CODE> is implementation-dependent.
</BLOCKQUOTE>
<PRE>
val text_size : string -> int * int
</PRE>
<A NAME="@manual860"></A><BLOCKQUOTE>
Return the dimensions of the given text, if it were drawn with
the current font and size.
</BLOCKQUOTE>
<H3>Filling </H3>
<PRE>
val fill_rect : int -> int -> int -> int -> unit
</PRE>
<A NAME="@manual861"></A><BLOCKQUOTE>
<CODE>fill_rect x y w h</CODE> fills the rectangle with lower left corner
at <CODE>x,y</CODE>, width <CODE>w</CODE> and height <CODE>h</CODE>, with the current color.
</BLOCKQUOTE>
<PRE>
val fill_poly : (int * int) array -> unit
</PRE>
<A NAME="@manual862"></A><BLOCKQUOTE>
Fill the given polygon with the current color. The array
contains the coordinates of the vertices of the polygon.
</BLOCKQUOTE>
<PRE>
val fill_arc : int -> int -> int -> int -> int -> int -> unit
</PRE>
<A NAME="@manual863"></A><BLOCKQUOTE>
Fill an elliptical pie slice with the current color. The
parameters are the same as for <CODE>draw_arc</CODE>.
</BLOCKQUOTE>
<PRE>
val fill_ellipse : int -> int -> int -> int -> unit
</PRE>
<A NAME="@manual864"></A><BLOCKQUOTE>
Fill an ellipse with the current color. The
parameters are the same as for <CODE>draw_ellipse</CODE>.
</BLOCKQUOTE>
<PRE>
val fill_circle : int -> int -> int -> unit
</PRE>
<A NAME="@manual865"></A><BLOCKQUOTE>
Fill a circle with the current color. The
parameters are the same as for <CODE>draw_circle</CODE>.
</BLOCKQUOTE>
<H3>Images </H3>
<PRE>
type image
</PRE>
<BLOCKQUOTE>
The abstract type for images, in internal representation.
Externally, images are represented as matrices of colors.
</BLOCKQUOTE>
<PRE>
val transp : color
</PRE>
<A NAME="@manual866"></A><BLOCKQUOTE>
In 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.
</BLOCKQUOTE>
<PRE>
val make_image : color array array -> image
</PRE>
<A NAME="@manual867"></A><BLOCKQUOTE>
Convert the given color matrix to an image.
Each sub-array represents one horizontal line. All sub-arrays
must have the same length; otherwise, exception <CODE>Graphic_failure</CODE>
is raised.
</BLOCKQUOTE>
<PRE>
val dump_image : image -> color array array
</PRE>
<A NAME="@manual868"></A><BLOCKQUOTE>
Convert an image to a color matrix.
</BLOCKQUOTE>
<PRE>
val draw_image : image -> int -> int -> unit
</PRE>
<A NAME="@manual869"></A><BLOCKQUOTE>
Draw the given image with lower left corner at the given point.
</BLOCKQUOTE>
<PRE>
val get_image : int -> int -> int -> int -> image
</PRE>
<A NAME="@manual870"></A><BLOCKQUOTE>
Capture the contents of a rectangle on the screen as an image.
The parameters are the same as for <CODE>fill_rect</CODE>.
</BLOCKQUOTE>
<PRE>
val create_image : int -> int -> image
</PRE>
<A NAME="@manual871"></A><BLOCKQUOTE>
<CODE>create_image w h</CODE> returns a new image <CODE>w</CODE> pixels wide and <CODE>h</CODE>
pixels tall, to be used in conjunction with <CODE>blit_image</CODE>.
The initial image contents are random, except that no point
is transparent.
</BLOCKQUOTE>
<PRE>
val blit_image : image -> int -> int -> unit
</PRE>
<A NAME="@manual872"></A><BLOCKQUOTE>
<CODE>blit_image img x y</CODE> copies screen pixels into the image <CODE>img</CODE>,
modifying <CODE>img</CODE> in-place. The pixels copied are those inside the
rectangle with lower left corner at <CODE>x,y</CODE>, and width and height
equal to those of the image. Pixels that were transparent in
<CODE>img</CODE> are left unchanged.
</BLOCKQUOTE>
<H3>Mouse and keyboard events </H3>
<PRE>
type status =
{ mouse_x : int; (* X coordinate of the mouse *)
mouse_y : int; (* Y coordinate of the mouse *)
button : bool; (* true if a mouse button is pressed *)
keypressed : bool; (* true if a key has been pressed *)
key : char } (* the character for the key pressed *)
</PRE>
<BLOCKQUOTE>
To report events.
</BLOCKQUOTE>
<PRE>
type event =
Button_down (* A mouse button is pressed *)
| Button_up (* A mouse button is released *)
| Key_pressed (* A key is pressed *)
| Mouse_motion (* The mouse is moved *)
| Poll (* Don't wait; return immediately *)
</PRE>
<BLOCKQUOTE>
To specify events to wait for.
</BLOCKQUOTE>
<PRE>
val wait_next_event : event list -> status
</PRE>
<A NAME="@manual873"></A><BLOCKQUOTE>
Wait 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 <CODE>Poll</CODE> is given in the event list, return immediately
with the current status. If the mouse cursor is outside of the
graphics window, the <CODE>mouse_x</CODE> and <CODE>mouse_y</CODE> fields of the event are
outside the range <CODE>0..size_x()-1, 0..size_y()-1</CODE>. Keypresses
are queued, and dequeued one by one when the <CODE>Key_pressed</CODE>
event is specified.
</BLOCKQUOTE>
<H3>Mouse and keyboard polling </H3>
<PRE>
val mouse_pos : unit -> int * int
</PRE>
<A NAME="@manual874"></A><BLOCKQUOTE>
Return the position of the mouse cursor, relative to the
graphics window. If the mouse cursor is outside of the graphics
window, <CODE>mouse_pos()</CODE> returns a point outside of the range
<CODE>0..size_x()-1, 0..size_y()-1</CODE>.
</BLOCKQUOTE>
<PRE>
val button_down : unit -> bool
</PRE>
<A NAME="@manual875"></A><BLOCKQUOTE>
Return <CODE>true</CODE> if the mouse button is pressed, <CODE>false</CODE> otherwise.
</BLOCKQUOTE>
<PRE>
val read_key : unit -> char
</PRE>
<A NAME="@manual876"></A><BLOCKQUOTE>
Wait for a key to be pressed, and return the corresponding
character. Keypresses are queued.
</BLOCKQUOTE>
<PRE>
val key_pressed : unit -> bool
</PRE>
<A NAME="@manual877"></A><BLOCKQUOTE>
Return <CODE>true</CODE> if a keypress is available; that is, if <CODE>read_key</CODE>
would not block.
</BLOCKQUOTE>
<H3>Sound </H3>
<PRE>
val sound : int -> int -> unit
</PRE>
<A NAME="@manual878"></A><BLOCKQUOTE>
<CODE>sound freq dur</CODE> plays a sound at frequency <CODE>freq</CODE> (in hertz)
for a duration <CODE>dur</CODE> (in milliseconds).
</BLOCKQUOTE>
<HR>
<A HREF="manual063.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual065.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>
|