Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ibc
4 changes: 1 addition & 3 deletions sdl.ipkg
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package sdl

sourcedir = src
modules = Graphics.SDL, Graphics.Config, Effect.SDL
opts = "-p effects"
opts = "-p effects"

makefile = MakefileSDLC

objs = sdlrun.o, sdlrun.h
libs = SDL, SDL_gfx

48 changes: 33 additions & 15 deletions src/Effect/SDL.idr
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,45 @@ module Effect.SDL
import Effects
import public Graphics.SDL

Srf : Type
Srf = SDLSurface

public export
data Colour = MkCol Int Int Int Int

export
black : Colour
black = MkCol 0 0 0 255

export
white : Colour
white = MkCol 255 255 255 255

export
red : Colour
red = MkCol 255 0 0 255

export
green : Colour
green = MkCol 0 255 0 255

export
blue : Colour
blue = MkCol 0 0 255 255

export
yellow : Colour
yellow = MkCol 255 255 0 255

export
cyan : Colour
cyan = MkCol 0 255 255 255

export
magenta : Colour
magenta = MkCol 255 0 255 255

Srf : Type
Srf = SDLSurface

export
data Sdl : Effect where
Initialise : Int -> Int -> Sdl () () (\v => Srf)
Quit : Sdl () Srf (\v => ())
Expand All @@ -46,39 +56,47 @@ Handler Sdl IO where

handle s Flip k = do flipBuffers s; k () s
handle s Poll k = do x <- pollEvent; k x s
handle s (WithSurface f) k = do r <- f s; k r s
handle s (WithSurface f) k = do r <- f s; k r s

public export
SDL : Type -> EFFECT
SDL res = MkEff res Sdl

public export
SDL_ON : EFFECT
SDL_ON = SDL SDLSurface

initialise : Int -> Int -> { [SDL ()] ==> [SDL_ON] } Eff ()
export
initialise : Int -> Int -> { [SDL ()] ==> [SDL_ON] } Eff ()
initialise x y = call $ Initialise x y

quit : { [SDL_ON] ==> [SDL ()] } Eff ()
export
quit : { [SDL_ON] ==> [SDL ()] } Eff ()
quit = call Quit

export
flip : { [SDL_ON] } Eff ()
flip = call Flip

poll : { [SDL_ON] } Eff (Maybe Event)
export
poll : { [SDL_ON] } Eff (Maybe Event)
poll = call Poll

export
getSurface : { [SDL_ON] } Eff SDLSurface
getSurface = call $ WithSurface (\s => return s)

rectangle : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
rectangle (MkCol r g b a) x y w h
export
rectangle : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
rectangle (MkCol r g b a) x y w h
= call $ WithSurface (\s => filledRect s x y w h r g b a)

ellipse : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
ellipse (MkCol r g b a) x y rx ry
export
ellipse : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
ellipse (MkCol r g b a) x y rx ry
= call $ WithSurface (\s => filledEllipse s x y rx ry r g b a)

line : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
line (MkCol r g b a) x y ex ey
export
line : Colour -> Int -> Int -> Int -> Int -> { [SDL_ON] } Eff ()
line (MkCol r g b a) x y ex ey
= call $ WithSurface (\s => drawLine s x y ex ey r g b a)


2 changes: 2 additions & 0 deletions src/Graphics/Config.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
%flag C "-L/usr/local/Cellar/sdl_gfx/2.0.25/lib -L/usr/local/lib -lSDL_gfx -lSDLmain -lSDL -Wl,-framework,Cocoa"
%flag C "-D_GNU_SOURCE=1 -D_THREAD_SAFE -I/usr/local/Cellar/sdl_gfx/2.0.25/include/SDL -I/usr/local/include/SDL"
25 changes: 11 additions & 14 deletions src/Graphics/SDL.idr
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,48 @@ module Graphics.SDL
import Graphics.Config

%include C "sdlrun.h"
%include C "SDL/SDL.h"
%link C "sdlrun.o"
%lib C "SDL_gfx"

-- Set up a window

abstract
export
data SDLSurface = MkSurface Ptr

public
export
startSDL : Int -> Int -> IO SDLSurface
startSDL x y = do ptr <- do_startSDL
return (MkSurface ptr)
where do_startSDL = foreign FFI_C "startSDL" (Int -> Int -> IO Ptr) x y

public
export
endSDL : IO ()
endSDL = foreign FFI_C "SDL_Quit" (IO ())

public
export
flipBuffers : SDLSurface -> IO ();
flipBuffers (MkSurface ptr)
= foreign FFI_C "flipBuffers" (Ptr -> IO ()) ptr


-- Some drawing primitives

public
export
filledRect : SDLSurface -> Int -> Int -> Int -> Int ->
Int -> Int -> Int -> Int -> IO ()
filledRect (MkSurface ptr) x y w h r g b a
= foreign FFI_C "filledRect"
(Ptr -> Int -> Int -> Int -> Int ->
Int -> Int -> Int -> Int -> IO ()) ptr x y w h r g b a

public
export
filledEllipse : SDLSurface -> Int -> Int -> Int -> Int ->
Int -> Int -> Int -> Int -> IO ()
filledEllipse (MkSurface ptr) x y rx ry r g b a
= foreign FFI_C "filledEllipse"
(Ptr -> Int -> Int -> Int -> Int ->
Int -> Int -> Int -> Int -> IO ()) ptr x y rx ry r g b a

public
export
drawLine : SDLSurface -> Int -> Int -> Int -> Int ->
Int -> Int -> Int -> Int -> IO ()
drawLine (MkSurface ptr) x y ex ey r g b a
Expand All @@ -57,7 +55,7 @@ drawLine (MkSurface ptr) x y ex ey r g b a
-- TODO: More keys still to add... careful to do the right mappings in
-- KEY in sdlrun.c

public
public export
data Key = KeyUpArrow
| KeyDownArrow
| KeyLeftArrow
Expand Down Expand Up @@ -120,7 +118,7 @@ Eq Key where
(KeyAny x) == (KeyAny y) = x == y
_ == _ = False

public
public export
data Button = Left | Middle | Right | WheelUp | WheelDown

Eq Button where
Expand All @@ -131,7 +129,7 @@ Eq Button where
WheelDown == WheelDown = True
_ == _ = False

public
public export
data Event = KeyDown Key
| KeyUp Key
| MouseMotion Int Int Int Int
Expand All @@ -152,10 +150,9 @@ Eq Event where
= b == b' && x == x' && y == y'
_ == _ = False

public
export
pollEvent : IO (Maybe Event)
pollEvent
= do MkRaw e <-
foreign FFI_C "pollEvent" (Ptr -> IO (Raw (Maybe Event))) prim__vm
return e

6 changes: 3 additions & 3 deletions src/MakefileSDLC
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CC = gcc
CFLAGS = `idris --include` `sdl-config --cflags`
CFLAGS = `idris --include` `pkg-config --cflags SDL_gfx`

sdlrun.o: config sdlrun.c sdlrun.h

config: .PHONY
echo "%flag C \"`sdl-config --libs`\"" > Graphics/Config.idr
echo "%flag C \"`sdl-config --cflags`\"" >> Graphics/Config.idr
echo "%flag C \"`pkg-config --libs SDL_gfx`\"" > Graphics/Config.idr
echo "%flag C \"`pkg-config --cflags SDL_gfx`\"" >> Graphics/Config.idr

clean: .PHONY
rm sdlrun.o
Expand Down
4 changes: 1 addition & 3 deletions src/sdlrun.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#ifndef __SDLRUN_H
#define __SDLRUN_H

#include <idris_rts.h>

// Start SDL, open a window with dimensions (x,y)
void* startSDL(int x, int y);

Expand All @@ -19,6 +17,6 @@ void drawLine(void* s_in,
int r, int g, int b, int a);

// Events
void* pollEvent(VM* vm); // builds an Idris value
void* pollEvent(void* vm); // builds an Idris value

#endif
3 changes: 3 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
etest
simple
test
14 changes: 14 additions & 0 deletions test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.PHONY: all
all: etest test

.PHONY: etest
etest:
idris --build etest.ipkg

.PHONY: simple
simple:
idris --build simple.ipkg

.PHONY: test
test:
idris --build test.ipkg
35 changes: 17 additions & 18 deletions test/etest.idr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Main

{- Test program for SDL effect - draws a rectangle and an ellipse on a
scrolling starfield background, with the position of the ellipse
scrolling starfield background, with the position of the ellipse
controlled by the arrow keys -}

import Effects
Expand All @@ -26,28 +26,28 @@ data Vars = Position -- position of ellipse
-- generation and console I/O

Prog : Type -> Type -> Type
Prog i t = Eff IO [SDL i,
Position ::: STATE (Int, Int),
XMove ::: STATE Int,
YMove ::: STATE Int,
Frames ::: STATE Integer,
Starfield ::: STATE (List (Int, Int)),
RND,
STDIO] t
Prog i t = Eff t [SDL i,
Position ::: STATE (Int, Int),
XMove ::: STATE Int,
YMove ::: STATE Int,
Frames ::: STATE Integer,
Starfield ::: STATE (List (Int, Int)),
RND,
STDIO]

-- Convenient shorthand for initialised SDL
Running : Type -> Type
Running t = Prog SDLSurface t

initStarfield : List (Int, Int) -> Int -> Eff m [RND] (List (Int, Int))
initStarfield : List (Int, Int) -> Int -> Eff (List (Int, Int)) [RND]
initStarfield acc 0 = return acc
initStarfield acc n = do x <- rndInt 0 639
y <- rndInt 0 479
initStarfield ((fromInteger x, fromInteger y) :: acc) (n - 1)

updateStarfield : List (Int, Int) -> Eff m [RND] (List (Int, Int))
updateStarfield : List (Int, Int) -> Eff (List (Int, Int)) [RND]
updateStarfield xs = upd [] xs where
upd : List (Int, Int) -> List (Int, Int) -> Eff m [RND] (List (Int, Int))
upd : List (Int, Int) -> List (Int, Int) -> Eff (List (Int, Int)) [RND]
upd acc [] = return acc
upd acc ((x, y) :: xs)
= if (y > 479) then do
Expand All @@ -56,7 +56,7 @@ updateStarfield xs = upd [] xs where
else
upd ((x, y+1) :: acc) xs

drawStarfield : List (Int, Int) -> Eff IO [SDL_ON] ()
drawStarfield : List (Int, Int) -> Eff () [SDL_ON]
drawStarfield [] = return ()
drawStarfield ((x, y) :: xs) = do line white x y x y
drawStarfield xs
Expand Down Expand Up @@ -132,12 +132,11 @@ emain = do initialise 640 480
when continue eventLoop

main : IO ()
main = run [(), Position := (320,200),
XMove := 0,
YMove := 0,
main = runInit [SDL (),
Position := (320,200),
XMove := 0,
YMove := 0,
Frames := 0,
Starfield := List.Nil,
1234567890,
()] emain


6 changes: 6 additions & 0 deletions test/etest.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package etest

pkgs = effects, sdl

main = etest
executable = etest
18 changes: 18 additions & 0 deletions test/simple.idr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Main

import Graphics.SDL

main : IO ()
main = (do
surface <- startSDL 640 480
rest surface)
where
rest : SDLSurface -> IO ()
rest surface = do
filledRect surface 100 100 50 50 255 0 0 128
flipBuffers surface
event <- pollEvent
case event of
Just (KeyDown KeyEscape) => pure ()
Just (AppQuit) => pure ()
_ => rest surface
6 changes: 6 additions & 0 deletions test/simple.ipkg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test

pkgs = effects, sdl

main = simple
executable = simple
Loading