/
game.sml
53 lines (42 loc) · 1.39 KB
/
game.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 use_gl = false
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
val ticks_per_second = 60
end
structure Main =
struct
structure S = RunGame (Game)
end