3.5. Displaying Graphics

As mentioned in Section 3.3, the window's paint method is responsible for drawing graphical images. These images are rendered within the virtual viewport. The SGL supports the following drawing primitives:

The details of these drawing functions are provided in the SGL documentation.

Example 3.2 draws a picture of a clock using several of the graphics drawing functions:

Example 3.2. Clock Picture

#include <GL/sgl.hpp>

class ClockWindow: public sgl::Window {
public:
    ClockWindow(): sgl::Window("3:00", 100, 100, 300, 300, 
                               -100.0, 100.0, -100.0, 100.0) {}

    void paint() override {
        set_color(sgl::BLUE);
        //  Draw outline of clock
        sgl::draw_circle(0.0, 0.0, 45.0);    //  Center at (0,0), radius of 45
        //  Draw hour hand
        sgl::set_line_width(4);          //  Thicker line
        sgl::draw_line(0.0, 0.0, 25.0, 0.0); //  Shorter line
        //  Draw minute hand
        sgl::set_line_width(2);          //  Thinner line
        sgl::draw_line(0.0, 0.0, 0.0, 40.0); //  Longer line
    }
};

int main() {
    //  Create a window instance and execute it
    sgl::run<ClockWindow>();
}


The clock consists of a circle and two lines. The circle is centered at the center of the window. Figure 3.4 shows the running program.

Figure 3.4. Drawing a Clock Face with a Circle and Two Lines

Drawing a Clock Face with a Circle and Two Lines



Copyright  ©2019 Richard L. HaltermanVersion 0.9.5February 17, 2019
Creative Commons License This work is licensed under a Creative Commons License.