RLTK: Initialization

· ☕ 2 min read

I just merged the new initialization branch to RLTK. Previously, you fired up RLTK with a simple - but not self-explanatory - context request:

1
let context = Rltk::init_simple8x8(80, 50, "Hello Rust World", "resources");

There’s nothing really wrong with this. You’re asking for an 80x50 terminal, specifying a title, and the location of the assets it uses. Only the assets system has changed, and “resources” might actually be embedded into your executable. Or you might want to specify more complex initialization, and suddenly you are obtaining a mutable context and modifying it a lot.

I looked at how other systems handle initialization, and decided to adopt the builder model (don’t worry, the old code still works - but has a deprecation warning now). With the builder model, the simplest initialization (but still adding a title) looks like this:

1
2
3
let context = RltkBuilder::simple80x50()
    .with_title("Hello RLTK World")
    .build();

You might decide to be fancy, and utilize a second sparse layer with the baked-in VGA font:

1
2
3
4
5
let context = RltkBuilder::simple80x50()
    .with_font("vga8x16.png", 8, 16)
    .with_sparse_console(80, 25, "vga8x16.png")
    .with_title("RLTK Example 2 - Sparse Consoles")
    .build();

That’s quite readable! You might even want a really complicated setup with tiles:

1
2
3
4
5
6
7
8
let context = RltkBuilder::new()
    .with_dimensions(WIDTH, HEIGHT)
    .with_tile_dimensions(16, 16)
    .with_title("RLTK Example 07 - Tiles")
    .with_font("example_tiles.png", 16, 16)
    .with_simple_console(WIDTH, HEIGHT, "example_tiles.png")
    .with_sparse_console(WIDTH, HEIGHT, "example_tiles.png")
    .build();

I think this is much more readable and intuitive for the user. You can still specify resource directories if you don’t want to embed content, but it’s generally handled for you.

Share this post
Support the author with