Home

Generating a conf.toml file for ggez

2D games in Rust with ggez

TL; DR - If you need a conf.toml file for ggez, try using the one in the repo first, if not you can generate one using.
use std::fs::File;

use ggez;

pub fn main() -> ggez::GameResult {
    let conf = ggez::conf::Conf::new();
    let mut config_file = File::create("conf.toml")?;
    conf.to_toml_file(&mut config_file);
    println!("Generated conf.toml");
    Ok(())
}

You might need to generate a conf.toml so you can specify a default window resolution, whether the game starts fullscreen or windowed.

The default one in the repo is fine, but as more options get added as ggez is worked on yours might get out of date, so it's always useful to know how to quickly generate one.

Once you've generated on you can place it in the resources directory that lives in the same directory as your binary and ggez will automatically pick it up by default.

Running the above code results in

[window_mode]
width = 800.0
height = 600.0
maximized = false
fullscreen_type = "Windowed"
borderless = false
min_width = 0.0
min_height = 0.0
max_width = 0.0
max_height = 0.0
resizable = false

[window_setup]
title = "An easy, good game"
samples = "Zero"
vsync = true
icon = ""
srgb = true

[backend]
type = "OpenGL"
major = 3
minor = 2

[modules]
gamepad = true
audio = true

You'll notice, the linked one in the ggez repo is already out of date as we have the modules section present in our generated conf.toml which isn't present in the ggez repo.

Setup for your conf.toml during development

During development, it's useful to keep a conf.toml with the rest of your source code. By adding the CARGO_MANIFEST_DIR you can add a resources dir to your rust project.

If you didn't do this, you'd have to copy your conf.toml into your build directories e.g (target/debug/resources) each time you did a clean build of your project, and a cargo clean would delete it.

mygame/
    resources/
    src/
    target/
    Cargo.lock
    Cargo.toml

You then place your conf.toml in the resources dir and add the following code

fn main() {
    let mut builder = ContextBuilder::new("mygame", "myname");
    if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
        let path = path::PathBuf::from(manifest_dir).join("resources");
        println!("Adding 'resources' path {:?}", path);
        builder = builder.add_resource_path(path);
    }
    let (ctx, event_loop) = &mut builder.build().unwrap();
}

You'll find other useful file handling examples in the ggez example directory ggez/files.rs at cf4cba34591367b38bbcbfa810a8cad48570120c · ggez/ggez · GitHub

Code for this article is available at GitHub - joetsoi/ggez-tutorials in the generateconf directory.

Meta

Code tested on