Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Getting started with fennel and Love2d

A topic by Alex ☕🇨🇦 created 56 days ago Views: 213 Replies: 2
Viewing posts 1 to 3
Submitted(+1)

Fennel is a clojure styled lisp that transpiles to Lua. Love2d is one of the easiest game frameworks to get started with.

min-love2d-fennel is all you need to get started writing love2d games with fennel. These days the name is a bit of a misnomer, the template comes with builtin tools to manage game modes, a fennel repl and buildtools required to compile a love2d game for Windows, Mac, Linux and HTML5.

Setup

You can always clone min-love2d-fennel and go from there, but an easier way is to use the new fennel-deps tool by @andreyorst.

Simply clone the fennel-deps repo, and run

deps --new io.gitlab.alexjgriffith/min-love2d-fennel my-awesome-game

To get started with fennel on emacs check out fennel-mode. To make working with love2d and fennel easier check out love2d-fennel.el.

The Basics

If you want to start with the most bare bones setup you can delete everything in the template, apart from main.lua and conf.lua. You can then start playing around with the love2d framework in wrap.fnl. The examples below have been taken from the love2d landing page.

Drawing Text

(fn love.draw []
    (love.graphics.print "Hello World!" 400 300))

Drawing and Image

(local images {})

(fn love.load []
    (tset images :whale (love.graphics.newImage "whale.png")))

(fn love.draw []
    (love.graphics.draw images.whale 300 300)

Playing a Sound

(fn love.load []
    (let [sound (love.audio.newSource :music.ogg :stream)]
    (love.audio.play sound)))

Template Features

The entry point for your fennel code is in wrap.fnl. This module wraps the draw, update and keypressed callbacks of love2d in a way that prevents your project from hard crashing if you make a simple error during development.

Game specific code can be found in the module mode-intro. You can modify mode-intro to suit the needs of your game, or you can write your own modes. wrap.fnl expects mode modules to return a table containing the callbacks activate, draw, update and keypressed.

  • The draw callback takes no arguments.
  • The activate callbacks takes no fixed arguments. Any arguments passed to set-mode are received by this callback.
  • The keypressed callback takes the key pressed and the set-mode function as its arguments.
  • The update callback takes dt (delta time) and the set-mode function as its arguments.

In either keypressed or update you can call set-mode to change the currently running mode.

Example

Below is a simple hello world game made using the template.

Replace mode-intro with mode-game in wrap.fnl.

;; mode-game.fnl
(local fennel (require :lib.fennel))
(fn pp [x] (print (fennel.view x)))

(var message "")

(fn activate []
  (pp "Loading mode :mode-game")
  (set message "Hello World!!")
  ;; load any assets you need for this mode here
  )

(fn draw []
  (love.graphics.print message 300 200))

(fn update [dt set-mode])

(fn keypressed [key set-mode])

{: activate : draw : update : keypressed}

To launch your game either run love . in the command line in the directory with your game or use the command C-c C-z if you are using emacs and you’ve setup fennel-mode and love2d-fennel.el.

Building Your Game

If you want to release your game on Windows, Mac or Linux you can use the provided makefile. Replace the header information in makefile and the titles in conf.lua with the information for your game.

You can make individual builds by running make linux, make mac, make windows and make web. Note for the web we target Lua PUC 5.1 rather than luajit, so if you want your games to run on the web use the subset of lua that is compatable with both Lua PUC 5.1 and luajit.

If you want to release your game on itch.io the makefile also provides the command make release, which will use butler to upload your game to itch.io.

Submitted(+1)

Great intro!

This is a great way to get started with Fennel and Love2d, Thank you!