Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added support for top-level game.

--HG--
rename : examples/redstar/game.sml => game.sml
  • Loading branch information...
commit 26f1b1e8e7435c409542b1344cd499e0eb3969dd 1 parent b64d710
authored April 30, 2011
10  examples/redstar/game.sml
@@ -9,12 +9,6 @@ struct
9 9
 
10 10
   val initstate = {xpos = width div 2, ypos = height div 2}
11 11
   
12  
-  fun requireimage s =
13  
-      case SDL.Image.load s of
14  
-            NONE => (print ("couldn't open " ^ s ^ "\n");
15  
-                      raise Fail "image not found")
16  
-          | SOME p => p
17  
-
18 12
   fun initscreen screen =
19 13
   (
20 14
     SDL.blitall (star, screen, #xpos initstate, #ypos initstate); 
@@ -39,6 +33,10 @@ struct
39 33
 
40 34
   fun keyUp _ s = SOME s
41 35
 
  36
+  fun handle_event (SDL.E_KeyDown k) s = keyDown k s
  37
+    | handle_event (SDL.E_KeyUp k) s = keyUp k s
  38
+    | handle_event _ s = SOME s
  39
+
42 40
   (* fun tick {xpos=x, ypos=y} = SOME {xpos=x+1, ypos=y} *)
43 41
   fun tick s = SOME s
44 42
 end
4  game.cm
... ...
@@ -0,0 +1,4 @@
  1
+Group is
  2
+
  3
+functioning/sources.cm
  4
+game.sml
50  game.sml
... ...
@@ -0,0 +1,50 @@
  1
+structure Game :> GAME =
  2
+struct
  3
+  (* Types *)
  4
+  type state = {xpos : int, ypos : int} (* position of controllable glyph *)
  5
+  type screen = SDL.surface
  6
+
  7
+  (* Constants *)
  8
+  val width = 800
  9
+  val height = 600
  10
+  val star = Graphics.requireimage "media/graphics/redstar.png"
  11
+
  12
+  (* Initialization *)
  13
+  val initstate = {xpos = width div 2, ypos = height div 2}
  14
+  
  15
+  fun initscreen screen =
  16
+  (
  17
+    SDL.blitall (star, screen, #xpos initstate, #ypos initstate); 
  18
+    SDL.flip screen
  19
+  )
  20
+
  21
+  fun render screen {xpos=x, ypos=y} =
  22
+  (
  23
+    SDL.clearsurface (screen, SDL.color (0w0,0w0,0w0,0w0));
  24
+    SDL.blitall (star, screen, x, y);
  25
+    SDL.flip screen
  26
+  )
  27
+
  28
+  val dpos = 5
  29
+
  30
+  fun keyDown (SDL.SDLK_ESCAPE) _ = NONE (* quit the game *)
  31
+    | keyDown (SDL.SDLK_RIGHT) {xpos=x, ypos=y} = SOME {xpos=x+dpos, ypos=y}
  32
+    | keyDown (SDL.SDLK_LEFT)  {xpos=x, ypos=y} = SOME {xpos=x-dpos, ypos=y}
  33
+    | keyDown (SDL.SDLK_UP)    {xpos=x, ypos=y} = SOME {xpos=x, ypos=y-dpos}
  34
+    | keyDown (SDL.SDLK_DOWN)  {xpos=x, ypos=y} = SOME {xpos=x, ypos=y+dpos}
  35
+    | keyDown _ s = SOME s
  36
+
  37
+  fun keyUp _ s = SOME s
  38
+
  39
+  fun handle_event (SDL.E_KeyDown {sym=k}) s = keyDown k s
  40
+    | handle_event (SDL.E_KeyUp {sym=k}) s = keyUp k s
  41
+    | handle_event _ s = SOME s
  42
+
  43
+  (* fun tick {xpos=x, ypos=y} = SOME {xpos=x+1, ypos=y} *)
  44
+  fun tick s = SOME s
  45
+end
  46
+
  47
+structure Main =
  48
+struct
  49
+  structure S = RunGame (Game)
  50
+end
5  makefile
@@ -4,7 +4,10 @@ default : game
4 4
 sdlml.o : sdlml/sdlml.c
5 5
 	gcc -DLINUX -O `sdl-config --cflags` -c sdlml/sdlml.c -o sdlml.o
6 6
 
7  
-game : sdlml.o *.sml functioning/*.sml examples/starbot/*.sml makefile
  7
+game    : sdlml.o *.sml functioning/*.sml makefile
  8
+	mlton -link-opt "-lSDL_image -ltiff -lpng -ljpeg -lz `sdl-config --libs`" -default-ann 'allowFFI true' -output game.exe game.cm sdlml.o
  9
+
  10
+starbot : sdlml.o *.sml functioning/*.sml examples/starbot/*.sml makefile
8 11
 	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
9 12
 
10 13
 redstar : sdlml.o *.sml functioning/*.sml examples/redstar/*.sml makefile

0 notes on commit 26f1b1e

Please sign in to comment.
Something went wrong with that request. Please try again.