« Go back

Game-Loop and Event handling

Now after we can create a Window, we have to handle Events and a game loop. First things first: The game loop you can see below is the while loop with the running condition.

To manage when the loop will cancel, we have to check the events.
Events are messages from the Window, which tell us if the user pressed a key, moved the mouse or whether he has made a click.
In the code below we only check if the Quit event is thrown. This Event is always thrown if you click on the red X in the right upper corner.

Show Raw
import std.stdio;

import Dgame.Window;
import Dgame.Window.Event;
import Dgame.System.Keyboard;

void main() {
    Window wnd = Window(640, 480, "Dgame Test");

    bool running = true;

    Event event;
    while (running) {
        wnd.clear();
        
        while (wnd.poll(&event)) {
            switch (event.type) {
                case Event.Type.Quit:
                    writeln("Quit Event");
                    running = false;
                break;
                    
                case Event.Type.KeyDown:
                    writeln("Pressed key ", event.keyboard.key);
                    
                    if (event.keyboard.key == Keyboard.Key.Esc)
                        running = false; // or: wnd.push(Event.Type.Quit);
                    
                break;
                    
                default: break;
            }
        }
        
        wnd.display();
    }
}

If you close the Window, you should also see the message Quit Event.


Now let's move ahead and let us try Keyboard and Mouse Events.

Show Raw
import std.stdio;

import Dgame.Window.Window;
import Dgame.Window.Event;
import Dgame.System.Keyboard;

void main() {
    Window wnd = Window(640, 480, "Dgame Test");

    bool running = true;

    Event event;
    while (running) {
        wnd.clear();
        
        while (wnd.poll(&event)) {
            switch (event.type) {
                case Event.Type.Quit:
                    writeln("Quit Event");
                    running = false;
                break;
                    
                case Event.Type.KeyDown:
                    writeln("Pressed key ", event.keyboard.key);
                    
                    if (event.keyboard.key == Keyboard.Key.Esc)
                        running = false; // or: wnd.push(Event.Type.Quit);
                    
                break;
                    
                case Event.Type.MouseButtonDown:
                    writefln("Mouse down at %d:%d", event.mouse.button.x, event.mouse.button.y);
                break;
                    
                case Event.Type.MouseMotion:
                    writefln("Mouse moved to %d:%d. Relative to %d:%d.",
                             event.mouse.motion.x, event.mouse.motion.y,
                             event.mouse.motion.rel_x, event.mouse.motion.rel_y);
                break;

                case Event.Type.MouseWheel:
                    writefln("Mouse wheel to %d:%d.", event.mouse.wheel.x, event.mouse.wheel.y);
                break;
                    
                default: break;
            }
        }
        
        wnd.display();
    }
}

With that code above you can check if someone pressed a key, moved the mouse (and to which coordinates) or if a mouse click was made (and on which coordinates).

Note the line: running = false; // or: wnd.push(Event.Type.Quit);
You know what running = false; does, but what is wnd.push(Event.Type.Quit) doing? The push method of our Window pushes a new Event on the Event Queue with the Event.Type Quit. Therefore you would see, after you have pressed the Escape key, the output Quit Event.


In both cases you should see this:

« Go back