Tofu Engine

~ a 2D game engine w/ an eco-friendly pixel-art retro-soul ~


Getting Started

So, we are going to write our first program. Following a long-dated tradition, we will present a welcoming message to the user (that is, ourselves).

Every Tofu program is built-up at the very least of two files:

The first, unsurprisingly, holds the program instance parameters (e.g. display width and height). The second one, even more unsurprisingly, is the program entry-point script. For our example program, we will be using this configuration file (save it as tofu.config in a folder of your choice named hello-tofu).

title=Hello, Tofu!

More details about the available configuration settings can be found in the reference. For the moment, all we need to know is that with this configuration file we are going to request a 320x240 virtual-screen that will be auto-scaled to fit the physical display as much as possible (while retaining windowed-mode, i.e. we aren’t going full-screen), window-title will be set to Hello, Tofu!, and exit-key (i.e. the ESCAPE keyboard key) will be enabled.

Now it’s time to tackle the program entry-point. We need to write a Lua script returning a “class” adhering to a specific prototype (more on this later). For the moment, just save the following script in the same folder you picked for the configuration file and name it main.lua.

-- Include the modules we'll be using.
local Class = require("tofu.core.class")
local Canvas = require("")
local Display = require("")
local Font = require("")

-- The entry point is a class, created with a helper function.
local Main = Class.define()

-- The message we are displaying, as a "constant".
local MESSAGE <const> = "Hello, Tofu!"

function Main:__ctor()
  -- Create and load a predefined palette, we choose Pico-8's one.
  local palette = Palette.default("pico-8")

  -- Create a default font, palette color `0` as background and `15` as foreground.
  -- Please note that, as default, palette color `0` is set as transparent. This means that
  -- the font background color won't be drawn.
  self.font = Font.default(0, 15)

function Main:process()
  -- Nothing to do, here.

function Main:update(_)
  -- Ditto.

function Main:render(_)
  -- Get a reference to the default canvas (i.e. the the virtual screen)...
  local canvas = Canvas.default()

  -- ... and clear it w/ default background palette color (i.e. palette index #0).

  -- Get the canvas width and height.
  local canvas_width, canvas_height = canvas:size()

  -- We need the font (message) width and height to center it on screen.
  local text_width, text_height = self.font:size(MESSAGE)

  -- Compute vertical and horizontal position for the text.
  local x = (canvas_width - text_width) * 0.5
  local y = (canvas_height - text_height) * 0.5

  -- Finally, draw the message on-screen at the given position.
  self.font:write(canvas, x, y, MESSAGE)

return Main

Now, launch the application with the following command

$ ./tofu --path=./hello-tofu

or with

$ ./tofu

if the tofu.config and main.lua files both reside in the same folder as the engine executable.

When you are done beholding our first program, exit by pressing the ESCAPE keyboard key.