When investigating a new programming language or development environment, it is traditional to write a "Hello world" program. Example 3.1 is the SGL equivalent.
Example 3.1. Hello World in SGL
#include <GL/sgl.hpp> class HelloWindow: public sgl::Window { public: HelloWindow(): sgl::Window("Simple Window", // Window's title 100, 50, // Left-top corner 400, 300, // Width and height -10.0, 90.0, // Minimum and maximum x -5.0, 95.0) // Minimum and maximum y {} void paint() override { sgl::set_color(sgl::BLUE); sgl::draw_text("Hello, world!", 30, 40, 18); } }; int main() { // Create a window and call its run method sgl::run<HelloWindow>(); }
Figure 3.1 provides a screenshot of the
running hello program.
In an SGL application,
the client must provide at least one concrete subclass of
sgl::Window
. In Example 3.1, we derive the class
HelloWindow
. An instance of our concrete
subclass of Window
constitutes the application's
main window. HelloWindow
's constructor passes
the following information to the superclass constructor:
a string of text that will be displayed in the window's title bar,
the window's physical position and size in screen pixels, and
the horizontal and vertical ranges of the viewport within the window in which graphics will be drawn.
The title is a string, window's physical location and size is specified by four integer values, and the viewport's coordinate system is specified by four double-precision floating-point values:
HelloWindow(): sgl::Window("Simple Window", // Window's title 100, 50, // Left-top corner 400, 300, // Width and height -10.0, 90.0, // Minimum and maximum x -5.0, 95.0) // Minimum and maximum y {}
Figure 3.2 shows how the four integer parameters affect the window's position and size.
As Figure 3.2 shows, in this
example
the left-top corner of the window's content area
is 100 pixels from the
left edge of the screen and 50 pixels down from the top of the
screen.
The window is 400 pixels wide and 300 pixels tall.
The content area of the window represents a viewport into a
virtual two-dimensional area.
The four remaining values represent, in order, the minimum
x, maximum x,
minimum y, and maximum
y coordinates of this virtual viewport.
Figure 3.3 shows how this coordinate
system relates to the physical window and the placement of the
"Hello, world!" text.
The SGL drawing routines such as sgl::draw_text
use the coordinate system of the virtual
viewport, which means a location is specified with double-precision
floating-point
numbers, not integers. Unlike many computer graphics coordinate
systems, the y axis is
not inverted; the orientation is
identical to the rectangular
Cartesian coordinate system used in mathematics.
The draw_axes
method of the
sgl::Window
class is available for use within the
paint
method to draw the viewport's
x and y axes, along with
additional grid lines.
Copyright ©2019 Richard L. Halterman | Version 0.9.5 | February 17, 2019 |