Small Steps II — Making Things

Use Timers

Use Timers

Explore one topic in small steps. Try the examples one at a time and take a break whenever you need. Choose how many extra exercises you want to try.

Running a function when it is time

What you will learn

An event is something a program responds to. A callback is a function entrusted to run when that event occurs. Use Timer to run a function about once a second.

Try it

Example · example1.cpp
int ticks = 0;

void on_timer()
{
    ticks = ticks + 1;
    print("Tick ", ticks);
}

void small_main()
{
    Timer timer;

    timer.start(1.0, on_timer);
    sleep(3.2);
    timer.stop();
}

on_timer has no () in start(1.0, on_timer) because you are specifying the function to call later, rather than calling it now.

ticks, outside all functions, is a global variable shared by several functions. Each callback adds 1. Timer events are processed during sleep too; stop stops the timer. Scheduling does not guarantee an exact number of calls.

Displaying a timer's value

What you will learn

A callback can change a value while the window loop reads that value and draws it.

Try it

Example · events_2_example2.cpp
int seconds = 0;

void on_second()
{
    seconds = seconds + 1;
}

void small_main()
{
    Window window;
    window.open(500, 250);

    Timer timer;
    timer.start(1.0, on_second);

    while (window.is_open())
    {
        window.clear(Black);
        window.draw_text(30, 80, format("Seconds: ", seconds), White, 28);
        window.show();
    }

    timer.stop();
}

on_second increases the global seconds, and draw_text displays its current value. format returns a String joining text and values. Unlike print, it does not produce output itself.

Timer is not a separate computation thread. Callbacks run when show or sleep processes events, so a long calculation may delay them. Use StopWatch for accurate elapsed-time measurement.

Exercise

Make a callback run every 0.5 seconds, increasing count by 1 and printing it. Call stop on the Timer after about 2.2 seconds.

Exercise starter · exercise1_starter.cpp
int count = 0;

void on_timer()
{
    // Increase and print count.
}

void small_main()
{
    Timer timer;

    // start the timer, wait about 2.2 seconds, then stop it.
}

Hint

Use timer.start(0.5, on_timer) and increase count in the callback.

show solution
int count = 0;

void on_timer()
{
    count = count + 1;
    print("Count: ", count);
}

void small_main()
{
    Timer timer;
    timer.start(0.5, on_timer);

    sleep(2.2);

    timer.stop();
}
Try More (Optional)

Exercise

Have a Timer callback increase level by 1 each second. In the Window loop, show the current level in the title with set_title("Level ", level).

Exercise starter · events_2_exercise2_starter.cpp
int level = 1;

void on_second()
{
    // Increase level.
}

void small_main()
{
    Window window;
    window.open(500, 300);

    Timer timer;
    timer.start(1.0, on_second);

    while (window.is_open())
    {
        // show level in the title.
        window.clear(Black);
        window.show();
    }

    timer.stop();
}

Hint

Change only the global int level in the callback and update the Window title in the main loop.

show solution
int level = 1;

void on_second()
{
    level = level + 1;
}

void small_main()
{
    Window window;
    window.open(500, 300);

    Timer timer;
    timer.start(1.0, on_second);

    while (window.is_open())
    {
        window.set_title("Level ", level);
        window.clear(Black);
        window.show();
    }

    timer.stop();
}
← Previous lessonNext lesson →