-
Notifications
You must be signed in to change notification settings - Fork 0
/
game-init.sml
128 lines (109 loc) · 4.53 KB
/
game-init.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
structure GameInit =
struct
open GameTypes
fun posToBoxVertexData (x, y) =
let
val x = x - 10
val x = Real32.fromInt x / 10.0
val y = y - 10
val y = ~(Real32.fromInt y / 10.0)
in
#[
x, y, (* tl *)
x + 0.1, y, (* tr *)
x, y - 0.1, (* bl *)
x, y - 0.1, (* bl *)
x + 0.1, y, (* tr *)
x + 0.1, y - 0.1 (* br *)
]
end
fun initBall (player, xPos, yPos, xMove, yMove) : ball =
{player = player, xPos = xPos, yPos = yPos, xMove = xMove, yMove = yMove}
fun initBlock (curLine, positionInRow) : block =
let
val block = if positionInRow < 10 then LIGHT else DARK
val vertexData = posToBoxVertexData (positionInRow, curLine)
in
{block = block, vertexData = vertexData}
end
(* Creates a 10x10 grid of blocks, with initial state. *)
fun initBlocks () : block vector vector =
Vector.tabulate (20, fn lineNum =>
Vector.tabulate (20, fn positionInRow =>
initBlock (lineNum, positionInRow)))
fun initBoard () : game_board =
let
(* Initialise vertex buffers and shaders. *)
val dayVertexBuffer = Gles3.createBuffer ()
val dayVertexShader = Gles3.createShader (Gles3.VERTEX_SHADER ())
val _ = Gles3.shaderSource
(dayVertexShader, Constants.boxVertexShaderString)
val _ = Gles3.compileShader dayVertexShader
val nightVertexBuffer = Gles3.createBuffer ()
val nightVertexShader = Gles3.createShader (Gles3.VERTEX_SHADER ())
val _ = Gles3.shaderSource
(nightVertexShader, Constants.boxVertexShaderString)
val _ = Gles3.compileShader nightVertexShader
val ballVertexBuffer = Gles3.createBuffer ()
val ballVertexShader = Gles3.createShader (Gles3.VERTEX_SHADER ())
val _ = Gles3.shaderSource
(ballVertexShader, Constants.boxVertexShaderString)
val _ = Gles3.compileShader ballVertexShader
(* Initialise fragment buffer and shader. *)
val dayFragmentBuffer = Gles3.createBuffer ()
val dayFragmentShader = Gles3.createShader (Gles3.FRAGMENT_SHADER ())
val _ = Gles3.shaderSource
(dayFragmentShader, Constants.boxFragmentShaderString)
val _ = Gles3.compileShader dayFragmentShader
val nightFragmentBuffer = Gles3.createBuffer ()
val nightFragmentShader = Gles3.createShader (Gles3.FRAGMENT_SHADER ())
val _ = Gles3.shaderSource
(nightFragmentShader, Constants.boxFragmentShaderString)
val _ = Gles3.compileShader nightFragmentShader
val ballFragmentBuffer = Gles3.createBuffer ()
val ballFragmentShader = Gles3.createShader (Gles3.FRAGMENT_SHADER ())
val _ = Gles3.shaderSource
(ballFragmentShader, Constants.boxFragmentShaderString)
val _ = Gles3.compileShader ballFragmentShader
(* Create and compile programs. *)
val dayProgram = Gles3.createProgram ()
val _ = Gles3.attachShader (dayProgram, dayVertexShader)
val _ = Gles3.attachShader (dayProgram, dayFragmentShader)
val _ = Gles3.linkProgram dayProgram
val nightProgram = Gles3.createProgram ()
val _ = Gles3.attachShader (nightProgram, nightVertexShader)
val _ = Gles3.attachShader (nightProgram, nightFragmentShader)
val _ = Gles3.linkProgram nightProgram
val ballProgram = Gles3.createProgram ()
val _ = Gles3.attachShader (ballProgram, ballVertexShader)
val _ = Gles3.attachShader (ballProgram, ballFragmentShader)
val _ = Gles3.linkProgram ballProgram
in
{ dayBall = initBall (DAY, ~275, 25, 11, ~11)
, nightBall = initBall (NIGHT, 225, 25, ~11, 11)
, blocks = initBlocks ()
, dr = Constants.initialDr
, dg = Constants.initialDg
, db = Constants.initialDb
, nr = Constants.initialNr
, ng = Constants.initialNg
, nb = Constants.initialNb
(* OpenGL buffers/shaders/programs below. *)
, dayVertexBuffer = dayVertexBuffer
, dayVertexShader = dayVertexShader
, dayFragmentBuffer = dayFragmentBuffer
, dayFragmentShader = dayFragmentShader
, dayProgram = dayProgram
, nightVertexBuffer = nightVertexBuffer
, nightVertexShader = nightVertexShader
, nightFragmentBuffer = nightFragmentBuffer
, nightFragmentShader = nightFragmentShader
, nightProgram = nightProgram
, ballVertexBuffer = ballVertexBuffer
, ballVertexShader = ballVertexShader
, ballFragmentBuffer = ballFragmentBuffer
, ballFragmentShader = ballFragmentShader
, ballProgram = ballProgram
}
end
end