It’s with great pleasure that on behalf of the haskell-game group, I’d like to announce the release of a new set of high-level bindings to the SDL library. SDL is a C library providing a set of cross-platform functions for handling graphics, window management, audio, joystick/gamepad interaction, and more.
For a while, we’ve had bindings to SDL 2 on Hackage, but these bindings are as close to 1:1 as you can get in Haskell. This results in a library that certainly can be used in Haskell, but does not feel particularly like writing ordinary Haskell! A real concern here is that this raises the barrier to entry for those new to either game programming or writing games in Haskell (or both!) - a barrier that I would certainly like to see lowered. To address this, myself and many others have spent the last year working on high-level bindings to abstract away the C-like feel of the existing library, and to present a more Haskell interface.
To give you an idea of how things look, here’s a basic application that opens a window, clears the screen, and quits when the user presses ‘q’:
{-# LANGUAGE OverloadedStrings #-}
module Main where
import SDL
import Linear (V4(..))
import Control.Monad (unless)
main :: IO ()
= do
main InitEverything]
initialize [<- createWindow "My SDL Application" defaultWindow
window <- createRenderer window (-1) defaultRenderer
renderer
appLoop renderer
appLoop :: Renderer -> IO ()
= do
appLoop renderer <- pollEvents
events let eventIsQPress event =
case eventPayload event of
KeyboardEvent keyboardEvent ->
== Pressed &&
keyboardEventKeyMotion keyboardEvent == KeycodeQ
keysymKeycode (keyboardEventKeysym keyboardEvent) -> False
_ = not (null (filter eventIsQPress events))
qPressed $= V4 0 0 255 255
rendererDrawColor renderer
clear renderer
present renderer unless qPressed (appLoop renderer)
Hopefully you’ll agree that the code above is close to idiomatic Haskell.
We’ve tried to be extensive with the bindings, and at the moment the following should (!) all be working:
OpenGL
and gl
libraries).The bindings are not 100% exhaustive - we’ve omitted some routines that are already provided by the Haskell runtime, but we also currently lack bindings to the following:
SDL2_net
and SDL2_ttf
. We’d love for these projects to have the same treatment, and are more than happy to host them under the haskell-game
Github account.We hope this enables more people to begin building interactive software and games in Haskell. It’s still early days for these bindings, so if you find any bugs (runtime problems or API bugs), or if you find the bindings lacking in anyway, please don’t hesitate to open an issue on our issue tracker.
Happy hacking!
You can contact me via email at ollie@ocharles.org.uk or tweet to me @acid2. I share almost all of my work at GitHub. This post is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.