« Go back

Listen to your Battery

You already know how to draw Text. We tested this knowledge with drawing the framerate onto the screen. But if you are on a Laptop, you maybe want to know, how much percent of your battery remains, how much time this means and in which state your laptop is (charged or on battery etc.).

To do that, we can use the Battery struct. With a call to the static method System.getBatteryInfo(); you get a Battery struct which holds the remaining percent of your battery, the current state and the remaining seconds.

Show Raw
import std.stdio;

import Dgame.Window.Window;
import Dgame.Window.Event;
import Dgame.Graphic.Text;
import Dgame.System.Battery;
import Dgame.System.Font;

void main() {
    Window wnd = Window(640, 480, "Dgame Test");
    Font fnt = Font("samples/font/arial.ttf", 22);
    Text curPower = new Text(fnt);
    curPower.setPosition(100, 200);

    bool running = true;
    Event event;
    while (running) {
        while (wnd.poll(&event)) {
            switch (event.type) {
                case Event.Type.Quit:
                    writeln("Quit Event");
                    running = false;
                default: break;
        System.Battery battery = System.getBatteryInfo();
        curPower.format("%d seconds remain with %d %%. State: %s",
                        battery.seconds, battery.percent, battery.state);

If you are on a PC, you should see something like this:

Battery on a PC

If you are on a Laptop or something else with a Battery, you should see something like this:

Battery on a Laptop

« Go back