|
Xtreme - Reference
This is a reference of all graphics classes and methods of
Xtreme, as of version 0.4.
Arc/FilledArc
Circle/FilledCircle
Color
Echo
Ellipse/FilledEllipse
GetCharHeight
GetCharWidth
GetMaxColor
GetStringHeight
GetStringSize
GetStringWidth
GetWindowSize
Line
LineRel
LineTo
MoveRel
MoveTo
Pixel
QueryCharWidth
QueryMaxCol
QueryMousePos
QueryMouseStat
QueryStrWidth
QueryWinWidth
Rect/FilledRect
SetFont
Text
Undo
UndoMark
VerticalText
Viewport
Parameters in square brackets are optional. Unless specified,
parameters are of type int.
Graphics Output
- Text output
Writing out text works like with cout. Just push the
string into the stream. Numbers (int and
double) work just the same. The colour can be
selected using Color().
The position can be specified using
MoveTo() or
MoveRel(). Use
SetFont() to change the font
and/or size of the text.
int main ()
{
xstream xs ("Window",0,0,200,150);
xs << "Hello World" << endl;
xs << MoveTo(20,100) << Color(Blue);
xs << "pi = " << 3.1415 << "..." << endl;
return 0;
}
- VerticalText (x, y, str)
X11 won't let you rotate text 90 deg. As a replacement, Xtreme
offers VerticalText(). The string str is
written, one character just below the other. (x,y)
specifies the position of the first character.
int main ()
{
xstream xs ("Window",0,0,200,150);
xs << Color(Yellow);
xs << VerticalText(10,10,"Hello World");
return 0;
}
-
Arc (x, y, startangle, endangle,
radius [,color])
FilledArc (x, y, startangle,
endangle, radius [,color])
Draws an arc. (x,y) is the center, startangle
and endangle denote the angles.
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Arc(100,100,0,90,95,Red);
return 0;
}
-
Circle (x, y, radius [,color])
FilledCircle (x, y, radius
[,color])
Draws a circle, center (x,y), radius as specified.
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Color(Green);
xs << Circle(100,100,95);
xs << Circle(100,100,85,Yellow);
return 0;
}
- Clear ()
Clears out the window.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << "Hello World" << endl;
xs << Clear();
xs << "Just like a new one!" << endl;
return 0;
}
-
Ellipse (x, y, startangle,
endangle, xradius, yradius [,color])
FilledEllipse (x, y, startangle,
endangle, xradius, yradius [,color])
Draws an ellipse, center (x,y), angles as specified.
xradius and yradius are the radii (in x and
y direction) of the ellipse.
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Ellipse(100,100,0,180,95,45,Yellow);
return 0;
}
-
Line (x1, y1, x2, y2
[,color])
Draws a line from (x1,y1) to (x2,y2).
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Line(10,10,190,190);
xs << Line(10,190,190,10);
return 0;
}
-
LineRel (dx, dy
[,color])
Draws a line from the current cursor position (as set with
MoveTo() or
MoveRel()) to the point
(current x + dx, current y + dy). Both
dx and dy may be negative.
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << MoveTo(0,0);
xs << LineRel(100,100);
xs << LineRel(-50,0);
return 0;
}
-
LineTo (x, y
[,color])
Draws a line from the current cursor position (as set with
MoveTo() or
MoveRel()) to (x,y).
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << MoveTo(0,0);
xs << LineTo(100,100);
xs << LineTo(50,0);
return 0;
}
-
MoveRel (dx, dy)
Moves the current cursor position by dx pixels
to the right and dy pixels down, where dx
and dy may be negative (correponding to left and
up shift).
main ()
{
xstream xs ("Window",0,0,200,200);
xs << MoveTo(0,0);
xs << LineTo(100,100);
xs << MoveRel(50,0);
xs << LineRel(50,50);
return 0;
}
- MoveTo (x, y)
Sets the current cursor position to (x,y).
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << MoveTo(0,0);
xs << LineTo(100,100);
xs << MoveTo(50,50);
xs << LineRel(50,50);
return 0;
}
-
Pixel (x, y,
color)
Draws one pixel at (x,y) using the specified
color. (The color must not be omitted.)
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Pixel(100,100,Yellow);
return 0;
}
-
Rect (x1, y1, x2, y2
[,color])
FilledRect (x1, y1, x2, y2
[,color])
Draws a rectangle. Upper left corner is (x1,y1),
lower right corner (x2,y2).
If the color argument is omitted, the current color as set with
Color() is used.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << FilledRect(20,20,180,180,Green);
xs << Rect(25,25,175,175,White);
return 0;
}
Settings
- Color (color)
Sets the default drawing color, which is used whenever the
color parameter of a graphics object is omitted. This color
is also used for text output. The following colors are
pre-defined:
Black = 0
Blue = 1
Green = 2
Cyan = 3
Red = 4
Magenta = 5
Brown = 6
LightGray = 7
Gray = 8
LightBlue = 9
LightGreen = 10
LightCyan = 11
LightRed = 12
LightMagenta = 13
Yellow = 14
White = 15
Example:
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Color(LightBlue);
xs << "Hello light blue world!" << endl;
return 0;
}
- Echo (on_off)
Turn echo mode on/off. If echo mode is on, everything read
from the stream is also written on the window. Since this
clutters the window, it's probably not desired in most
cases. Echo mode is turned on by writing out Echo(1),
while Echo(0) switches echo mode off.
int main ()
{
int i;
xstream xs ("Window",0,0,200,200);
xs << Echo(0);
xs << QueryWinWidth();
xs >> i;
xs << Echo(1);
xs << "i = " << i << endl;
return 0;
}
-
SetFont (fontname, size,
attribs)
Select font and size for text. Unfortunately not all Unix
installations offer the same range of fonts; here are some
safe bets (passed in fontname as char*):
- "courier" (typewriter font)
- "helvetica" (a sans serif font, a.k.a. Arial)
- "times" (a serif font, a.k.a. Times New Roman)
- "fixed" (the system font)
Available sizes may wary, too. Good bets are 10, 12, 14, 18,
20, and 24 pt. The third parameter, attribs, is
reserved for future extensions like boldface, italics etc.
For now, it should be set to zero.
Default font is Courier 14pt.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << SetFont("times",18,0);
xs << "Hello Times 18pt World" << endl;
return 0;
}
-
Viewport (x1, y1,
x2, y2)
Limits the actual output region to the specified rectangle.
Text or graphics exceeding this region will be clipped.
Default region is the entire window.
int main ()
{
xstream xs ("Window",0,0,200,200);
xs << Circle(100,100,95,Green);
xs << Viewport(50,50,150,150);
xs << Circle(100,100,60,Yellow);
return 0;
}
Querying Xtreme
- QueryMaxCol ()
Query the number of colors available. The returned value is the
highest available color, that is, the number of colors minus
one. (Colors start with 0.) The answer can be read into an
int variable.
Alternatively,
XtremeGetMaxColor() can
be used.
int main ()
{
int maxcolor;
xstream xs ("Window",0,0,200,200);
xs << QueryMaxCol();
xs >> maxcolor;
xs << maxcolor+1 << " colors avail." << endl;
return 0;
}
- QueryMousePos
()
Queries the current mouse position. If the mouse is currently
not inside the window, -1 -1 is returned. The result
is to be read into two int variables, e.g. x and y.
int main ()
{
int x, y;
xstream xs ("Window",0,0,200,200);
xs << QueryMousePos();
xs >> x >> y;
xs << "Mouse at (" << x << "," << y << ")" << endl;
return 0;
}
- QueryMouseStat
()
Queries the current mouse position and state, i.e. what
buttons are pressed. If the mouse isn't inside the window,
-1 -1 0 is returned. The result is to be read
into three int variables, as illustrated below.
The third integer represents the state of the mouse buttons.
Bit 0 corresponds to 1.st button, bit 1 to 2.nd button etc.
int main ()
{
int x, y, buttons;
xstream xs ("Window",0,0,200,200);
xs << QueryMouseStat();
xs >> x >> y >> buttons;
xs << "Mouse at (" << x << "," << y << ")" << endl;
if (buttons & 1) xs << "button 1" << endl;
if (buttons & 2) xs << "button 2" << endl;
if (buttons & 4) xs << "button 3" << endl;
if (buttons & 8) xs << "button 4" << endl;
if (buttons & 16) xs << "button 5" << endl;
return 0;
}
- QueryCharWidth (),
QueryCharHeight ()
Queries width and height of a character. The result is
to be read into an int variable. Note that
some fonts have uniform character width ("fixed" and
"courier"), while others have varying widths ("times"
and "helvetica"). The height, on the other hand, is
uniform for all fonts.
Alternative functions are
XtremeGetCharWidth() and
XtremeGetCharHeight()
int main ()
{
int height;
xstream xs ("Window",0,0,200,200);
xs << SetFont("courier",18,0);
xs << QueryCharHeight();
xs >> height;
xs << "Height: " << height << " pixels" << endl;
return 0;
}
- QueryStrWidth
(string)
Queries the width of a string, in pixels. This is useful
for centered text output, as is shown in the example below.
The result is to be read into an int variable.
Alternative function
XtremeGetStringWidth()
int main ()
{
int width;
xstream xs ("Window",0,0,200,200);
xs << QueryStrWidth("Hello World");
xs >> width;
xs << MoveTo((200-width)/2,10);
xs << "Hello World" << endl;
return 0;
}
- QueryWinWidth (),
QueryWinHeight ()
Queries the width and height of the window. The result
is to be read in an int variable.
Alternative function is
XtremeGetWindowSize()
int main ()
{
int width, height;
xstream xs ("Window",0,0,200,200);
xs >> width;
xs << QueryWinHeight();
xs >> height;
xs << "Window size " << width << "x" << height
<< endl;
return 0;
}
Undo features
One common technique to simulate motion of an object is
to just draw it on the new position, "on top" of the old
position, and clear out the usually small reminder by putting
a black stripe on it. Do not use this technique with Xtreme!
It will make Xtreme a memory hogging snail.
Xtreme offers a special mechanism for moving objects on a fixed
background:
UndoMark() and
Undo(). This concept is
explained in the tutorial.
- UndoMark ()
Mark any output that follows as "undo-able". Anything written
to the stream after a UndoMark can be disposed by
submitting an Undo command.
- Undo ()
Undo any output since the last UndoMark. This
restores the window as it was when the last UndoMark
was set.
You can undo as many levels as you set UndoMarks.
If no corresponding UndoMark is found, the screen
is cleared.
The following example shows a ball (the moving object) going
through some pipe (the fixed background):
int main ()
{
int i, j;
xstream xs ("Window",0,0,200,200);
xs << Line(5,80,195,80,Blue);
xs << Line(5,120,195,120,Blue);
for (i=15; i<185; i++) {
xs << UndoMark();
xs << Circle(i,100,10,Red);
usleep(10000); // wait for 10 ms
xs << Undo(); // remove ball
}
return 0;
}
Functions
To make querying more convenient, the following functions are
provided. All functions take at least an xstream
(denoted as xs) as a parameter.
- int XtremeGetMaxColor
(xs)
The function equivalent to
QueryMaxCol().
Returns the highest color.
int main ()
{
int maxcolor;
xstream xs ("Window",0,0,200,200);
maxcolor = XtremeGetMaxColor(xs);
xs << maxcolor+1 << " colors" << endl;
return 0;
}
- void XtremeGetWindowSize
(xs, width, height)
The function equivalent to
QueryWinWidth() and
QueryWinHeight().
width and height are integers filled
by the function.
int main ()
{
int width, height;
xstream xs ("Window",0,0,200,200);
XtremeGetWindowSize(xs,width,height);
xs << "Size: " << width << "x" << height
<< endl;
return 0;
}
- void XtremeGetStringSize
(xs, s, width, height)
The function equivalent to
QueryStrWidth() and
QueryCharHeight().
width and height are integers filled by
the function.
int main ()
{
int width, height;
xstream xs ("Window",0,0,200,200);
xs << SetFont("times",18);
XtremeGetStringSize(xs,"Hello World",width,height);
xs << "String size: " << width << "x"
<< height << endl;
return 0;
}
- int XtremeGetStringWidth
(xs, s)
The function equivalent to
QueryStrWidth().
- int XtremeGetStringHeight
(xs, s)
The function equivalent to
QueryCharHeight(*s).
int main ()
{
int width, height;
xstream xs ("Window",0,0,200,200);
xs << SetFont("helvetica",20);
width = XtremeGetStringWidth(xs,"Hello World");
height = XtremeGetStringHeight(xs,"Hello World");
xs << "string size: " << width << "x"
<< height << endl;
return 0;
}
- int XtremeGetCharWidth
(xs, ch)
The function equivalent to
QueryCharWidth().
- int XtremeGetCharHeight
(xs, ch)
The function equivalent to
QueryCharHeight().
int main ()
{
int width, height;
xstream xs ("Window",0,0,200,200);
xs << SetFont("courier",24); // monospaced font!
width = XtremeGetCharWidth(xs);
height = XtremeGetCharHeight(xs);
xs << "char size: " << width << "x" << height
<< endl;
return 0;
}
|