Skip to content

Keeping the Window Open

Ryan G edited this page Aug 28, 2017 · 4 revisions

Let's expand on just opening a Window:

import dgt;

void main()
{
    WindowConfig config;
    Window window = Window("Your First Window", 800, 600, config);
    Rectanglei camera = Rectanglei(0, 0, 800, 600);
    while(window.isOpen)
    {
        window.begin(Color.white, camera);
        window.end();
    }
}

A window should open up with a white background and it should pretty much do nothing, except close when you ask it to.

New lines

Rectanglei camera = Rectanglei(0, 0, 800, 600); creates a rectangle with its top-left at (0, 0), a width of 800, and a height of 600. (Note: The type is Rectanglei because it is a Rectangle made of integers, equivalent to Rectangle!int.)

while(window.isOpen) does what you might expect: loops while the window is still open. The window will close if you click the 'X' button or for platform-specific things: Alt-F4 is the preferred wait to close on Windows but on macOS you would use CMD-Q. This line takes care of all that for you.

window.begin(Color.white, camera) starts each iteration of our game loop. The first parameter, Color.white, defines the clear color. This means any portion of the window not covered when we draw things (which we'll get to later) will be white. The second parameter is the camera, which defines what chunk of game space will actually make it to the window. We defined the camera earlier, so we just pass it now.

window.end() ends our iteration of the loop. If we did any drawing, this is where it would get displayed on the screen.

Clone this wiki locally