MyNixOS website logo
Description

Game engine for Prototyping on a Grid.

Exposes a heavily constrained and simple API around SDL2 with builtin font and sound effects.

Grid Proto

Game Engine for Prototyping on a Grid

Quickly prototype a graphical program with basic input, colors, shapes, and text. Grid Proto follows an 'anti-polished' philosophy. It exposes a heavily limited API over SDL2 which doesn't allow for any user media. No images, fonts, or audio can be loaded. This usage is one step above prototyping within the terminal. So, just write a function for updating state, a pure function for rendering, and a pure function for sound effects.


Hello World

import GridProto

main :: IO ()
main = runGridProto helloWorld ()

helloWorld :: GridProto ()
helloWorld = defaultGridProto
  { title = "Hello World"
  , viewFn = \_ ->
      drawString wh1 (0,0) "hello" <> drawString wh1 (0,1) "world"
  }

API

-- The main type with config and callbacks
data GridProto state = GridProto
  -- Window name
  { title :: String
  -- Grid size
  , rows :: Int -- Default: 18
  , cols :: Int -- Default: 32
  -- Scaling and clear color
  , tilePixelSize :: Int -- Default: 24 minimum for readable text
  , backgroundColor :: Color -- Default: Black2
  -- Callbacks which are called each frame
  , updateFn :: Input -> state -> IO (state, [String]) -- [String] is user logging
  , viewFn :: state -> View
  , sfxFn :: state -> [Sfx]
  , quitFn :: state -> Bool
  }

defaultGridProto :: GridProto s -- Easy to update fields

runGridProto -- Loops until exited
  :: GridProto s -- Main type
  -> s -- Initial state
  -> IO ()

--
-- View
--

type View = Map (Int, Int) Tile -- Map (X,Y) Tile

emptyView :: View

data Tile = Tile
  { symbol :: Maybe (Char, Color) -- At front
  , shape :: Maybe (Shape, Color) -- In middle
  , fill :: Maybe Color -- At back
  }

data Color -- 15 base colors with 3 variations from tint, regular, and shaded. 45 Total.
-- Shorten constructors
-- Tint Reg. Shade
   rd0, rd1, rd2 -- Red
   or0, or1, or2 -- Orange
   yw0, yw1, yw2 -- Yellow
   ch0, ch1, ch2 -- Chartreuse
   gn0, gn1, gn2 -- Green
   sp0, sp1, sp2 -- Spring
   cn0, cn1, cn2 -- Cyan
   az0, az1, az2 -- Azure
   bu0, bu1, bu2 -- Blue
   vt0, vt1, vt2 -- Violet
   mg0, mg1, mg2 -- Magenta
   rs0, rs1, rs2 -- Rose
   br0, br1, br2 -- Brown
   gy0, gy1, gy2 -- Gray
   wh0, wh1, wh2 -- White
   bk0, bk1, bk2 -- Black

data Shape
  = Circle | FillCircle
  | Triangle | FillTriangle
  | Square | FillSquare
  | Plus | Dash
  | Bar | Cross

drawString :: Color -> (Int, Int) -> String -> View
drawTile :: View -> (Int, Int) -> Tile -> View

drawView :: View -> (Int, Int) -> View -> View
mergeViews :: View -> View -> View

rainbow, warms, cools, colorWheel0, colorWheel1, colorWheel2 :: [Color]
shade, tint :: Color -> Color

data Viewport = Viewport
  { vpView :: View
  , vpXY :: (Int, Int)
  , vpDim :: (Int, Int)
  }

mergeViewport :: View -> Viewport -> View
mergeViewports :: View -> [Viewport] -> View

-- instances of MapTile on Tile, View, Viewport
mapTile :: MapTile a
  => ((Char, Color) -> (Char, Color)) -> ((Shape, Color) -> (Shape, Color))
  -> (Color -> Color) -> a -> a
mapSymbol :: MapTile a => ((Char, Color) -> (Char, Color)) -> a -> a
mapShape :: MapTile a => ((Shape, Color) -> (Shape, Color)) -> a -> a
mapFill :: MapTile a => (Color -> Color) -> a -> a

--
-- Input
--

data Input = Input
  { mouse :: Mouse, keys :: Keys
  , controller1, controller2, controller3, controller4 :: Controller
  }

data Key
  = Char Char
  | UpArrow | DownArrow | LeftArrow | RightArrow
  | Enter | Escape
  | LeftShift | RightShift | LeftControl | RightControl
  | LeftAlt | RightAlt
  | Tab | Backspace | Meta

data KeyState = Pressed | Held | Released | Untouched

newtype Keys

lookupKey :: Keys -> Key -> KeyState

data Mouse = Mouse { mousePosition :: (Int, Int), mouseButton :: KeyState }

data Controller = Controller
  { isConnected :: Bool
  , startButton, backButton :: KeyState
  , dpadUp, dpadDown, dpadLeft, dpadRight :: KeyState
  , aButton, bButton, xButton, yButton :: KeyState
  , leftStick , rightStick :: KeyState
  , leftShoulder, rightShoulder :: KeyState
  , leftAxis, rightAxis :: Axis
  }

data Axis = Axis { xAxis :: Float, yAxis :: Float }

--
-- Sfx
--

data Sfx
  = SfxSuccess | SfxBell | SfxSelect
  | SfxNoSelect | SfxScroll | SfxChimes
  | SfxLaser | SfxPowerUp | SfxJump
  | SfxDamage | SfxExplosion | SfxNoise

Metadata

Version

0.2.0.1

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows