Simple Graphics Library  0.9.5
SGL API
Classes | Typedefs | Enumerations | Functions | Variables
sgl Namespace Reference

Classes

class  BitmapObject
 
class  Color
 
class  CompositeObject
 
class  DisplayDigit
 
class  DoubleDigit
 
class  GraphicalObject
 
class  Multidigit
 
class  ObjectWindow
 
class  OGLWindow
 
class  Pixmap
 
struct  Point
 
class  PopupMenu
 
class  ProceduralWindow
 
class  SevenSegmentDigit
 
class  Stopwatch
 
class  TimeDisplay
 
class  Window
 

Typedefs

using MenuItemFunction = std::function< void()>
 

Enumerations

enum  MouseButton { Left = 0, Right = 2, Unknown = -1 }
 
enum  SpecialKey {
  F1_KEY = 10001, F2_KEY, F3_KEY, F4_KEY,
  F5_KEY, F6_KEY, F7_KEY, F8_KEY,
  F9_KEY, F10_KEY, F11_KEY, F12_KEY,
  LEFT_KEY = 10100, UP_KEY, RIGHT_KEY, DOWN_KEY,
  PAGE_UP_KEY, PAGE_DOWN_KEY, HOME_KEY, END_KEY,
  INSERT_KEY
}
 
enum  CursorShape {
  Right_arrow = 0, Left_arrow, Info, Destroy,
  Help, Cycle, Spray, Wait,
  Text, Crosshair, Up_down, Left_right,
  Top_side, Bottom_side, Left_side, Right_side,
  Top_left_corner, Top_right_corner, Bottom_right_corner, Bottom_left_corner,
  Inherit = 100, None, Full_crosshair
}
 
enum  KeyModifier { NO_KEY = 0, SHIFT_KEY = 1, CTRL_KEY = 2, ALT_KEY = 4 }
 

Functions

void initialize_graphics (unsigned int mode)
 
void initialize_graphics ()
 
bool intersect (const GraphicalObject &obj1, const GraphicalObject &obj2)
 
void draw_point (double x, double y)
 
void draw_point (const Point &pt)
 
void set_point_size (int point_size)
 
void draw_line (double x0, double y0, double x1, double y1)
 
void draw_dashed_line (double x0, double y0, double x1, double y1, int pattern=0x00FF)
 
void draw_rectangle (double x, double y, double width, double height)
 
void fill_rectangle (double x, double y, double width, double height)
 
void draw_circle (double x, double y, double radius)
 
void fill_circle (double x, double y, double radius)
 
void draw_polygon (const std::vector< Point > &pts)
 
void draw_polygon (const Point *pts, int n)
 
void fill_polygon (const std::vector< Point > &pts)
 
void fill_polygon (const Point *pts, int n)
 
void draw_text (const std::string &text, double x, double y, int font_size)
 
void draw_text (const std::string &text, double x, double y, double scale)
 
double text_width (const std::string &text, int font_size)
 
void draw_function (double(*f)(double), double begin_x, double end_x, double delta)
 
void set_color (const Color &color)
 
void set_color (double r, double g, double b)
 
void set_line_width (double width)
 
template<typename T , typename... Args>
void run (Args &&... args)
 
template<typename T , typename... Args>
T * make_window (Args &&... args)
 
int get_screen_width ()
 
int get_screen_height ()
 
void set_random_seed (int seed=-1)
 
int random (int begin, int end)
 
int random (int n)
 
void pause (int msec)
 
bool equals (double d1, double d2, double delta)
 
std::string version ()
 
void create_window (const std::string &title, int x, int y, int width, int height)
 
void run_window ()
 
void update_window ()
 
void set_paint_function (const std::function< void()> &f)
 
void set_mouse_pressed_function (const std::function< void(double, double, MouseButton)> &f)
 
void set_mouse_released_function (const std::function< void(double, double, MouseButton)> &f)
 
void set_mouse_moved_function (const std::function< void(double, double)> &f)
 
void set_mouse_dragged_function (const std::function< void(double, double)> &f)
 
void set_key_pressed_function (const std::function< void(int, double, double)> &f)
 
void set_window_background (const Color &c)
 
void set_window_title (const std::string &str)
 
const Color BLACK (0.0, 0.0, 0.0)
 
const Color RED (1.0, 0.0, 0.0)
 
const Color GREEN (0.0, 1.0, 0.0)
 
const Color BLUE (0.0, 0.0, 1.0)
 
const Color YELLOW (1.0, 1.0, 0.0)
 
const Color MAGENTA (1.0, 0.0, 1.0)
 
const Color CYAN (0.0, 1.0, 1.0)
 
const Color DARK_RED (0.75, 0.0, 0.0)
 
const Color DARK_GREEN (0.0, 0.75, 0.0)
 
const Color DARK_BLUE (0.0, 0.0, 0.75)
 
const Color LIGHT_RED (1.0, 0.75, 0.75)
 
const Color LIGHT_GREEN (0.75, 1.0, 0.75)
 
const Color LIGHT_BLUE (0.75, 0.75, 1.0)
 
const Color GRAY (0.5, 0.5, 0.5)
 
const Color LIGHT_GRAY (0.8, 0.8, 0.8)
 
const Color DARK_GRAY (0.2, 0.2, 0.2)
 
const Color WHITE (1.0, 1.0, 1.0)
 
void procedural_default_paint_function ()
 
void procedural_default_mouse_pressed_function (double, double, MouseButton)
 
void procedural_default_mouse_released_function (double, double, MouseButton)
 
void procedural_default_mouse_moved_function (double, double)
 
void procedural_default_mouse_dragged_function (double, double)
 
void procedural_default_key_pressed_function (int, double, double)
 

Variables

const Color BLACK
 
const Color RED
 
const Color GREEN
 
const Color BLUE
 
const Color YELLOW
 
const Color MAGENTA
 
const Color CYAN
 
const Color WHITE
 
const Color DARK_RED
 
const Color DARK_GREEN
 
const Color DARK_BLUE
 
const Color LIGHT_RED
 
const Color LIGHT_GREEN
 
const Color LIGHT_BLUE
 
const Color GRAY
 
const Color LIGHT_GRAY
 
const Color DARK_GRAY
 
const int MAX_WINDOWS = 10
 

Detailed Description

All client accessible classes, functions, and constants reside in the sgl namespace.

Typedef Documentation

◆ MenuItemFunction

using sgl::MenuItemFunction = typedef std::function<void()>

A type for menu item event handling.

Enumeration Type Documentation

◆ CursorShape

enum sgl::CursorShape
strong

Cursor shapes map directly to the GLUT equivalents.

◆ KeyModifier

enum sgl::KeyModifier
strong

KeyModifer is an enumerated type that maps directly to the GLUT equivalents for the corresponding key modifers: SHIFT, CRTL, and ALT. These can be combined with bitwise OR to represent combinations such as CTRL-ALT.

◆ MouseButton

enum sgl::MouseButton
strong

MouseButton is an enumerated type that maps directly to the GLUT equivalents for the corresponding mouse buttons.

◆ SpecialKey

SpecialKey is an enumerated type that maps directly to the GLUT equivalents for the corresponding special keys.

Function Documentation

◆ create_window()

void sgl::create_window ( const std::string &  title,
int  x,
int  y,
int  width,
int  height 
)

Creates a graphics window for the SGL's procedural interface. Clients need not derive a custom Window class and override methods. Clients using the procedural interface control the application's state through global variables rather than instance variables. Clients must call this function before registering any callbacks and before calling run_window.

Parameters
titlethe text to appear in the window's title bar
xthe x component of the (x, y) location of the window's left-top corner
ythe y component of the (x, y) location of the window's left-top corner
widththe window's width
heightthe window's height
Returns
nothing

◆ draw_circle()

void sgl::draw_circle ( double  x,
double  y,
double  radius 
)

Draws a circle in the current drawing color.

Parameters
xthe x coordinate in the viewport coordinate system of the circle's center
ythe y coordinate in the viewport coordinate system of the circle's center
radiusthe radius of the circle in viewport coordinates
Returns
nothing

◆ draw_dashed_line()

void sgl::draw_dashed_line ( double  x0,
double  y0,
double  x1,
double  y1,
int  pattern = 0x00FF 
)

Draws a strippled line in the current drawing color.

Parameters
x0the x coordinate in viewport coordinates of the first point
y0the y coordinate in viewport coordinates of the first point
x1the x coordinate in viewport coordinates of the second point
y1the y coordinate in viewport coordinates of the second point
patterna 32-bit pattern used to render the line; defaults to dashes: 0x00FF
Returns
nothing

◆ draw_function()

void sgl::draw_function ( double(*)(double)  f,
double  begin_x,
double  end_x,
double  delta 
)

Draws a 2-D mathematical function, f(x). Plots the line segments joining (begin_x, f(begin_x)), (begin_x + delta, f(begin_x + delta)), (begin_x + delta + delta, f(begin_x + delta + delta)), ..., (end_x, f(end_x)).

Parameters
fa pointer to a function that accepts a single double-precision floating-point value
begin_xthe lowest x argument to evaluate
end_xthe highest x argument to evaluate
deltathe change in x; first begin_x will be evaluated, then begin_x + delta, then begin_x + delta + delta, ..., until end_x is reached.
Returns
nothing

◆ draw_line()

void sgl::draw_line ( double  x0,
double  y0,
double  x1,
double  y1 
)

Draws a line in the current drawing color.

Parameters
x0the x coordinate in viewport coordinates of the first point
y0the y coordinate in viewport coordinates of the first point
x1the x coordinate in viewport coordinates of the second point
y1the y coordinate in viewport coordinates of the second point
Returns
nothing

◆ draw_point() [1/2]

void sgl::draw_point ( double  x,
double  y 
)

Draws the point (x,y) in the current drawing color.

Parameters
xthe x coordinate of the (x,y) location.
ythe y coordinate of the (x,y) location.
Returns
nothing

◆ draw_point() [2/2]

void sgl::draw_point ( const Point pt)

Draws the given Point object in the current drawing color.

Parameters
ptthe Point object to draw.
Returns
nothing

◆ draw_polygon() [1/2]

void sgl::draw_polygon ( const std::vector< Point > &  pts)

Draws the outline of a polygon specified by the points in a std::vector of Points. The points adjacent in the polygon also must be adjacent in the vector.

Parameters
ptsa std::vector of Point objects that comprise the vertices of the polygon. Adjacent vertices in the polygon must adjacent in the vector.
Returns
nothing

◆ draw_polygon() [2/2]

void sgl::draw_polygon ( const Point pts,
int  n 
)

Draws the outline of a polygon specified by the points in an array of Point objects. The points adjacent in the polygon also must be adjacent in the vector.

Parameters
ptsa std::vector of Point objects that comprise the vertices of the polygon. Adjacent vertices in the polygon must adjacent in the vector.
nthe number of points in the array.
Returns
nothing

◆ draw_rectangle()

void sgl::draw_rectangle ( double  x,
double  y,
double  width,
double  height 
)

Draws a rectangular frame in the current drawing color.

Parameters
xthe x coordinate in the viewport coordinate system of the left-bottom corner of the rectangle
ythe y coordinate in the viewport coordinate system of the left-bottom corner of the rectangle
widththe width in viewport coordinates of the rectangle
heightthe height in viewport coordinates of the rectangle
Returns
nothing

◆ draw_text() [1/2]

void sgl::draw_text ( const std::string &  text,
double  x,
double  y,
int  font_size 
)

Draw a piece of graphical text in the window. The provided (x,y) location specifies the left-bottom portion of the text. This version of draw_text uses fixed-size bitmap fonts.

Parameters
textthe string of text to display
xthe x coordinate in the viewport coordinate system of the text's left-most extent
ythe y coordinate in the viewport coordinate system of the text's baseline
font_sizethe size of the font: 10, 12, or 18.

◆ draw_text() [2/2]

void sgl::draw_text ( const std::string &  text,
double  x,
double  y,
double  scale 
)

Draw a piece of graphical text in the window. This version of draw_text uses a scalable stroke font. The provided (x,y) location specifies the left-bottom portion of the text.

Parameters
textthe string of text to display
xthe x coordinate in the viewport coordinate system of the text's left-most extent
ythe y coordinate in the viewport coordinate system of the text's baseline
scalethe scale of the font; this generally will be value less than one (for example, 0.1 or 0.25).

◆ equals()

bool sgl::equals ( double  d1,
double  d2,
double  delta 
)

Determines if two floating-point numbers are close enough in value to be considered "equal." The call equals(a, b, c) returns true if |a - b| < c; otherwise, it returns true.

Parameters
d1the first value to compare
d2the second value to compare
deltathe tolerance factor: the distance between d1 and d2 must be less than delta for d1 and d2 to be considered "equal"
Returns
true, if d1 and d2 are within delta of each other; otherwise, false

◆ fill_circle()

void sgl::fill_circle ( double  x,
double  y,
double  radius 
)

Draws a filled circle in the current drawing color.

Parameters
xthe x coordinate in the viewport coordinate system of the circle's center
ythe y coordinate in the viewport coordinate system of the circle's center
radiusthe radius of the circle in viewport coordinates
Returns
nothing

◆ fill_polygon() [1/2]

void sgl::fill_polygon ( const std::vector< Point > &  pts)

Draws a filled polygon specified by the points in a std::vector of Points. The points adjacent in the polygon also must be adjacent in the vector.

Parameters
ptsa std::vector of Point objects that comprise the vertices of the polygon. Adjacent vertices in the polygon must adjacent in the vector.
Returns
nothing

◆ fill_polygon() [2/2]

void sgl::fill_polygon ( const Point pts,
int  n 
)

Draws a filled polygon specified by the points in an array of Point objects. The points adjacent in the polygon also must be adjacent in the vector.

Parameters
ptsan array of Point objects that comprise the vertices of the polygon. Adjacent vertices in the polygon must adjacent in the vector.
nthe number of points in the array.
Returns
nothing

◆ fill_rectangle()

void sgl::fill_rectangle ( double  x,
double  y,
double  width,
double  height 
)

Draws a filled rectangle in the current drawing color.

Parameters
xthe x coordinate in the viewport coordinate system of the left-bottom corner of the rectangle
ythe y coordinate in the viewport coordinate system of the left-bottom corner of the rectangle
widththe width in viewport coordinates of the rectangle
heightthe height in viewport coordinates of the rectangle
Returns
nothing

◆ get_screen_height()

int sgl::get_screen_height ( )

Gets the height of the screen in pixels.

Returns
the height of the screen in pixels; returns zero if the screen's height cannot be determined

◆ get_screen_width()

int sgl::get_screen_width ( )

Gets the width of the screen in pixels.

Returns
the width of the screen in pixels; returns zero if the screen's width cannot be determined

◆ initialize_graphics() [1/2]

void sgl::initialize_graphics ( unsigned int  mode)

Global inialization function, called once per program execution. This version allows the client to specify a initial display mode. All SGL programs should call this function or its overloaded equivalent before creating and using a graphical window.

Parameters
modethe initial display mode
Returns
nothing

◆ initialize_graphics() [2/2]

void sgl::initialize_graphics ( )

Global inialization function, called once per program execution. This version provides double buffering, RGBA mode, and no depth buffer. All SGL programs should call this function or its overloaded equivalent before creating and using a graphical window.

Returns
nothing

◆ intersect()

bool sgl::intersect ( const GraphicalObject obj1,
const GraphicalObject obj2 
)

Determines if the bounding boxes of two graphical objects overlap.

Parameters
obj1one of the graphical objects
obj2the other graphical object
Returns
true, if obj1's bounding box intersects obj2's bounding box

◆ make_window()

template<typename T , typename... Args>
T * sgl::make_window ( Args &&...  args)
inline

Creates an SGL window of type T passing the arguments found in the Args parameter to the window's constructor. Does NOT call the window's run method to start the application.

Template Parameters
Tthe type of SGL window to create
Argsthe argument pack to pass to the window's constructor
Parameters
argsthe instantiated parameters to pass to the window's constructor

Creates an SGL window of type T passing the arguments found in the Args parameter to the window's constructor. Does NOT call the window's run method to start the application.

Template Parameters
Tthe type of SGL window to create
Argsthe argument pack to pass to the window's constructor
Parameters
argsthe instantiated parameters to pass to the window's constructor
Returns
a pointer to the newly created window object.

◆ pause()

void sgl::pause ( int  msec)

Pauses the program's execution for a period of milliseconds.

Parameters
msecthe number of milliseconds to pause
Returns
nothing

◆ procedural_default_paint_function()

void sgl::procedural_default_paint_function ( )

Converts an integer to a string so it can be displayed with draw_text.

Parameters
ithe integer to convert
Returns
the string representation of i Converts a double to a string so it can be displayed with draw_text.
Parameters
dthe double to convert
Returns
the string representation of d

◆ random() [1/2]

int sgl::random ( int  begin,
int  end 
)

Returns a pseudorandom number in the specified range.

Parameters
beginthe lowest integer in the set of potential return values.
endthe highest integer in the set of potential return values.
Returns
a pseudorandom integer, r, such that begin <= r <= end

◆ random() [2/2]

int sgl::random ( int  n)

Returns a pseudorandom number in the range 0...n - 1.

Parameters
nthe number of distinct potential pseudorandom values.
Returns
a pseudorandom integer, r, such that 0 <= r < n

◆ run()

template<typename T , typename... Args>
void sgl::run ( Args &&...  args)
inline

Creates an SGL window of type T passing the arguments found in the Args parameter to the window's constructor. Calls the window's run method to start the application.

Template Parameters
Tthe type of SGL window to create
Argsthe argument pack to pass to the window's constructor
Parameters
argsthe instantiated parameters to pass to the window's constructor

◆ run_window()

void sgl::run_window ( )

Starts the graphical event loop for a window created via create_window. Used for the SGL's procedural interface.

Returns
nothing

◆ set_color() [1/2]

void sgl::set_color ( const Color color)

Sets the current drawing color.

Parameters
colorthe color to use for subsequent drawing operations.
Returns
nothing

◆ set_color() [2/2]

void sgl::set_color ( double  r,
double  g,
double  b 
)

Sets the current drawing color according to the given RGB values.

Parameters
rthe intensity of red to use for subsequent drawing operations.
gthe intensity of green to use for subsequent drawing operations.
bthe intensity of blue to use for subsequent drawing operations.
Returns
nothing

◆ set_key_pressed_function()

void sgl::set_key_pressed_function ( const std::function< void(int, double, double)> &  f)

Registers the client's custom keypress callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_line_width()

void sgl::set_line_width ( double  width)

Sets the current line width.

Parameters
widththe width to use for subsequent line drawing operations.
Returns
nothing

◆ set_mouse_dragged_function()

void sgl::set_mouse_dragged_function ( const std::function< void(double, double)> &  f)

Registers the client's custom mouse dragged callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_mouse_moved_function()

void sgl::set_mouse_moved_function ( const std::function< void(double, double)> &  f)

Registers the client's custom mouse moved callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_mouse_pressed_function()

void sgl::set_mouse_pressed_function ( const std::function< void(double, double, MouseButton)> &  f)

Registers the client's custom mouse pressed callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_mouse_released_function()

void sgl::set_mouse_released_function ( const std::function< void(double, double, MouseButton)> &  f)

Registers the client's custom mouse released callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_paint_function()

void sgl::set_paint_function ( const std::function< void()> &  f)

Registers the client's custom painting callback function with the graphical window. Used for the SGL's procedural interface.

Parameters
fthe client's callback function
Returns
nothing

◆ set_point_size()

void sgl::set_point_size ( int  point_size)

Sets the size of a rendred point

Parameters
thepoint size
Returns
nothing

◆ set_random_seed()

void sgl::set_random_seed ( int  seed = -1)

Sets the pseudorandom number generator seed.

Parameters
seedthe pseudorandom number seed. If omitted, the seed is generated pseudorandomly from the operating system's time.
Returns
nothing

◆ set_window_background()

void sgl::set_window_background ( const Color c)

Changes the background color of the graphics window. Client must call update_window to force the repainting for it to take effect. Used for the SGL's procedural interface.

Parameters
cthe desired background color
Returns
nothing

◆ set_window_title()

void sgl::set_window_title ( const std::string &  str)

Sets the title of the window. Used for the SGL's procedural interface.

Parameters
strthe text of the new title
Returns
nothing

◆ text_width()

double sgl::text_width ( const std::string &  text,
int  font_size 
)

Returns the length of the graphical text in units of the viewport coordinate system.

Parameters
textthe text string to measure
font_sizethe size of the font: 10, 12, or 18.
Returns
the length of the graphical text in units of the viewport coordinate system.

◆ update_window()

void sgl::update_window ( )

Requests the graphical framework to repaint the window. Used for the SGL's procedural interface. Schedules the application's painting callback for execution.

Returns
nothing

◆ version()

std::string sgl::version ( )

Converts an integer to a string so it can be displayed with draw_text.

Parameters
ithe integer to convert
Returns
the string representation of i Converts a double to a string so it can be displayed with draw_text.
Parameters
dthe double to convert
Returns
the string representation of d Returns the SGL version number of this library.

Variable Documentation

◆ BLACK

const Color sgl::BLACK(0.0, 0.0, 0.0)

Some commonly used colors