RatCornu / scala-native-sfml

Scala Native 3 bindings for SFML library

Home Page:https://lafeychine.github.io/scala-native-sfml/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[WIP] SFML bindings for Scala Native 3

This repository is a safe Scala Native binding of the SFML graphic library.

Example

import scala.util.Using

import sfml.graphics.*
import sfml.window.*

@main def main =
    Using.Manager { use =>
        val window = use(RenderWindow(VideoMode(1024, 768), "Hello world"))

        val texture = use(Texture())
        texture.loadFromFile("cat.png")

        val sprite = Sprite(texture)

        while window.isOpen() do
            for event <- window.pollEvent() do
                event match {
                    case _: Event.Closed => window.close()
                    case _               => ()
                }

            window.clear(Color.Black())

            window.draw(sprite)

            window.display()
    }

Differences from SFML

Resource management

When an object manipulates system resources, it must extend the AutoCloseable trait and has to be explicitly closed.

Since the SFML library has such objects, this binding adds a custom trait Resource, which enables either of the following paradigms:

  • manipulate these objects through the Using manager, as shown in the example above;
  • explicitly call the close method when appropriate.

Installation

Requirements

This library requires Scala Native 0.4.10 and therefore Scala 3.2.2.

It is recommended to use sbt to build a project, by setting up project/plugins.sbt and build.sbt as follows:

// project/plugins.sbt
addSbtPlugin("org.scala-native" % "sbt-scala-native" % "0.4.10")
// build.sbt
import scala.scalanative.build.*

name := "project"

scalaVersion := "3.2.2"
version := "0.1.0"

enablePlugins(ScalaNativePlugin)

Please refer to sbt settings and tasks for additional customisation.

Fetching the library

The library is not published on Maven, as it is not mature yet.

Nonetheless, packages are accessible via GitHub Packages and can be found here.

Access to GitHub Packages requires to generate a token with the read:packages scope and add it to ~/.config/git/config:

[github]
  token = TOKEN_DATA

The project sbt-github-packages enables to fetch the project. To use it, add the following to project/plugins.sbt:

// project/plugins.sbt
addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3")

The setup of the sbt-github-packages plugin is done in build.sbt, for example:

// build.sbt
githubSuppressPublicationWarning := true
githubTokenSource := TokenSource.GitConfig("github.token")

resolvers += Resolver.githubPackages("lafeychine")
libraryDependencies += "io.github.lafeychine" %%% "scala-native-sfml" % "x.x.x"

Notes

The typical way to use SFML in a foreign language is via the C ABI with CSFML. However, this project makes the choice to use the C++ ABI directly.

The use of the C++ API can be motivated by the fact that both C++ and Scala revolve around an object-oriented type system; binding the two directly is in that sense more straightforward than using the C API indirection, and allows to transport more features of the library into Scala.

On the other hand, the C++ ABI is largely unstable, and this project is also a pretext to experiment with the internals of the language.

As a result, this library is, for now, only compatible with the x86_64-linux-gnu platform.

About

Scala Native 3 bindings for SFML library

https://lafeychine.github.io/scala-native-sfml/

License:zlib License


Languages

Language:Scala 91.5%Language:C++ 5.7%Language:Shell 1.2%Language:Nix 1.0%Language:Makefile 0.7%