Small Steps II — Making Things

Opening a window

Open a window.

What you will learn

Window is a type for working with a window on the screen. An object is an actual instance of that type. Create an object named window and open a window.

Try it

Example · example1.cpp
void small_main()
{
    Window window;
    window.set_title("My First Window");
    window.open(640, 480);

    while (window.is_open())
    {
        window.show();
    }
}

open(640, 480) opens a window 640 pixels wide and 480 pixels high. A pixel is a small dot making up the screen. set_title sets the title.

Repeat show while is_open is true. show displays the screen and handles input such as closing the window. This call is needed for the window to respond. The loop ends when you press its close button.

Later drawing examples reuse this window-opening structure.

Exercise

Open a 500 × 300 Window and set its title to your name. Keep it open until it is closed.

Exercise starter · exercise1_starter.cpp
void small_main()
{
    Window window;

    // Set a title and open a 500 x 300 window.
}

Hint

You can call set_title before open too.

show solution
void small_main()
{
    Window window;
    window.set_title("Alex");
    window.open(500, 300);

    while (window.is_open())
        window.show();
}
← Previous lessonNext lesson →