Permalink
Browse files

added support for top-level game.

--HG--
rename : examples/redstar/game.sml => game.sml
  • Loading branch information...
1 parent b64d710 commit 26f1b1e8e7435c409542b1344cd499e0eb3969dd @chrisamaphone chrisamaphone committed May 1, 2011
Showing with 62 additions and 7 deletions.
  1. +4 −6 examples/redstar/game.sml
  2. +4 −0 game.cm
  3. +50 −0 game.sml
  4. +4 −1 makefile
View
@@ -9,12 +9,6 @@ struct
val initstate = {xpos = width div 2, ypos = height div 2}
- fun requireimage s =
- case SDL.Image.load s of
- NONE => (print ("couldn't open " ^ s ^ "\n");
- raise Fail "image not found")
- | SOME p => p
-
fun initscreen screen =
(
SDL.blitall (star, screen, #xpos initstate, #ypos initstate);
@@ -39,6 +33,10 @@ struct
fun keyUp _ s = SOME s
+ fun handle_event (SDL.E_KeyDown k) s = keyDown k s
+ | handle_event (SDL.E_KeyUp k) s = keyUp k s
+ | handle_event _ s = SOME s
+
(* fun tick {xpos=x, ypos=y} = SOME {xpos=x+1, ypos=y} *)
fun tick s = SOME s
end
View
@@ -0,0 +1,4 @@
+Group is
+
+functioning/sources.cm
+game.sml
View
@@ -0,0 +1,50 @@
+structure Game :> GAME =
+struct
+ (* Types *)
+ type state = {xpos : int, ypos : int} (* position of controllable glyph *)
+ type screen = SDL.surface
+
+ (* Constants *)
+ val width = 800
+ val height = 600
+ val star = Graphics.requireimage "media/graphics/redstar.png"
+
+ (* Initialization *)
+ val initstate = {xpos = width div 2, ypos = height div 2}
+
+ fun initscreen screen =
+ (
+ SDL.blitall (star, screen, #xpos initstate, #ypos initstate);
+ SDL.flip screen
+ )
+
+ fun render screen {xpos=x, ypos=y} =
+ (
+ SDL.clearsurface (screen, SDL.color (0w0,0w0,0w0,0w0));
+ SDL.blitall (star, screen, x, y);
+ SDL.flip screen
+ )
+
+ val dpos = 5
+
+ fun keyDown (SDL.SDLK_ESCAPE) _ = NONE (* quit the game *)
+ | keyDown (SDL.SDLK_RIGHT) {xpos=x, ypos=y} = SOME {xpos=x+dpos, ypos=y}
+ | keyDown (SDL.SDLK_LEFT) {xpos=x, ypos=y} = SOME {xpos=x-dpos, ypos=y}
+ | keyDown (SDL.SDLK_UP) {xpos=x, ypos=y} = SOME {xpos=x, ypos=y-dpos}
+ | keyDown (SDL.SDLK_DOWN) {xpos=x, ypos=y} = SOME {xpos=x, ypos=y+dpos}
+ | keyDown _ s = SOME s
+
+ fun keyUp _ s = SOME s
+
+ fun handle_event (SDL.E_KeyDown {sym=k}) s = keyDown k s
+ | handle_event (SDL.E_KeyUp {sym=k}) s = keyUp k s
+ | handle_event _ s = SOME s
+
+ (* fun tick {xpos=x, ypos=y} = SOME {xpos=x+1, ypos=y} *)
+ fun tick s = SOME s
+end
+
+structure Main =
+struct
+ structure S = RunGame (Game)
+end
View
@@ -4,7 +4,10 @@ default : game
sdlml.o : sdlml/sdlml.c
gcc -DLINUX -O `sdl-config --cflags` -c sdlml/sdlml.c -o sdlml.o
-game : sdlml.o *.sml functioning/*.sml examples/starbot/*.sml makefile
+game : sdlml.o *.sml functioning/*.sml makefile
+ mlton -link-opt "-lSDL_image -ltiff -lpng -ljpeg -lz `sdl-config --libs`" -default-ann 'allowFFI true' -output game.exe game.cm sdlml.o
+
+starbot : sdlml.o *.sml functioning/*.sml examples/starbot/*.sml makefile
mlton -link-opt "-lSDL_image -ltiff -lpng -ljpeg -lz `sdl-config --libs`" -default-ann 'allowFFI true' -output game.exe examples/starbot/sources.cm sdlml.o
redstar : sdlml.o *.sml functioning/*.sml examples/redstar/*.sml makefile

0 comments on commit 26f1b1e

Please sign in to comment.