Small Steps VI — Growing into C++
Cleaning up the window before the runtime
Clean up the window before shutting down the runtime.
What you will learn
A runtime provides the foundation for features such as windows and sound. Clean up objects using those features before shutting down their foundation.
Try it
#include <small.h>
int main(int argc, char* argv[])
{
Small::initialize_small(argc, argv);
{
Small::Window window;
window.set_title("Real main()");
window.open(500, 300);
while (window.is_open())
{
window.clear(Small::Black);
window.draw_text(30, 80, "This is ordinary C++ main()");
window.show();
}
} // Destroy Window before shutting down the runtime.
Small::shutdown_small();
return 0;
}
When the inner brace scope ends, the Window object is cleaned up. Then call shutdown_small. Closing a window and ending an object's lifetime are different actions.
argc and argv receive information passed on the command line. This lesson is optional advanced material. You do not need to memorize all pointer notation yet.
Exercise
Write #include <small.h> and int main(int argc, char* argv[]). Pass argc and argv to Small::initialize_small, print argc, and call Small::shutdown_small() before the program finishes.
#include <small.h>
int main(int argc, char* argv[])
{
// Initialize Small with argc and argv.
// print argc, then shut Small down before returning.
return 0;
}
Hint
After Small::initialize_small(argc, argv);, use Small::print("argc: ", argc);. Call Small::shutdown_small(); before return 0;.
show solution
#include <small.h>
int main(int argc, char* argv[])
{
Small::initialize_small(argc, argv);
Small::print("argc: ", argc);
Small::shutdown_small();
return 0;
}