1
1
#include "gunfight.h"
2
2
3
- // ENV IMPORTS
4
- void logFloat32 (f32 val );
5
-
6
- extern u8 * __heap_base ;
7
-
8
3
// NOTE(fede): We define everything statically here because we already have all the memory
9
4
// we will use (it is allocated and passed from js as a WebAssembly.Memory object.
10
5
// Defining variables used globally in the environment seems not worth it here
@@ -13,35 +8,98 @@ extern u8 *__heap_base;
13
8
// GLOBAL GAME STATE
14
9
global_variable bool32 globalIsInitialized = 0 ;
15
10
global_variable f64 globalLastTimestamp ;
11
+
16
12
global_variable GameState globalGameState ;
17
- global_variable GameControllerInput globalGameControllerInput = {};
18
- global_variable GameControllerInput globalLastFrameControllerInput = {};
13
+
14
+ global_variable GameControllerInput globalGameControllerInputCurrent = {};
15
+ global_variable GameControllerInput globalGameControllerInputLastFrame = {};
16
+
17
+ global_variable u32 globalColorShaderTrianglesCount = 0 ;
18
+ global_variable u32 globalTextureShaderTrianglesCount = 0 ;
19
19
20
20
// GLOBAL BUFFERS
21
- // 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
22
- // 4 * 4 * 3 * 100 = 4800 bytes =~ 4.6KB
23
- global_variable u32 globalVertexBufferSize = 4800 ;
24
- // 4 f32 (4 bytes) per color, 100 triangles
25
- // 4 * 4 * 100 = 1600 bytes =~ 1.6KB
26
- global_variable u32 globalColorBufferSize = 1600 ;
27
21
28
- // GLOBAL FRAME STATE
29
- global_variable u32 globalTrianglesCount = 0 ;
30
22
31
- export u8 * getBufferBase () {
23
+ // Define indices for each buffer
24
+ enum bufferIndex {INDEX_COLOR_SHADER_VERTICES , INDEX_COLOR_SHADER_COLORS , INDEX_TEXTURE_SHADER_VERTICES };
25
+
26
+ global_variable u32 globalBufferSizes [3 ] = {
27
+ // ColorShaderVertices: 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
28
+ 4800 ,
29
+ // ColorShaderColor: 4 f32 (4 bytes) per color, 100 triangles
30
+ 1600 ,
31
+ // TextureShaderVertices: 4 f32 (4 bytes) per vertex, 3 vertices per triangle, 100 triangles
32
+ 4800
33
+ };
34
+
35
+
36
+ // GLOBAL FRAME STATE
37
+ export u8 * getHeapBase () {
32
38
return __heap_base ;
33
39
}
34
40
41
+ export u8 * getBufferBase (index ) {
42
+ u8 * ret = __heap_base ;
43
+
44
+ assert (index < arrayLength (globalBufferSizes ));
45
+
46
+ for (int i = 0 ; i < index ; i ++ ) {
47
+ ret = ret + globalBufferSizes [i ];
48
+ }
49
+ return ret ;
50
+ }
51
+
52
+ export u32 colorShaderGetTrianglesCount () {
53
+ return globalColorShaderTrianglesCount ;
54
+ }
55
+
56
+ internal
57
+ void bufferPushf32 (Buffer * buffer , f32 value ) {
58
+ * ((f32 * )(buffer -> current + buffer -> offset )) = value ;
59
+ buffer -> offset += sizeof (f32 );
60
+ }
61
+
62
+ ColorShaderFrame colorShaderFrameInit () {
63
+ ColorShaderFrame ret = {};
64
+ ret .trianglesCount = 0 ;
65
+
66
+ Buffer verticesBuffer = {};
67
+ verticesBuffer .current = getBufferBase (INDEX_COLOR_SHADER_VERTICES );
68
+ verticesBuffer .offset = 0 ;
69
+
70
+ ret .verticesBuffer = verticesBuffer ;
71
+
72
+ Buffer colorsBuffer = {};
73
+ colorsBuffer .current = getBufferBase (INDEX_COLOR_SHADER_COLORS );
74
+ colorsBuffer .offset = 0 ;
75
+
76
+ ret .colorsBuffer = colorsBuffer ;
77
+
78
+ return ret ;
79
+ }
80
+
81
+ TextureShaderFrame textureShaderFrameInit () {
82
+ TextureShaderFrame ret = {};
83
+ ret .trianglesCount = 0 ;
84
+
85
+ Buffer verticesBuffer = {};
86
+ verticesBuffer .current = getBufferBase (INDEX_COLOR_SHADER_VERTICES );
87
+ verticesBuffer .offset = 0 ;
88
+
89
+ ret .verticesBuffer = verticesBuffer ;
90
+
91
+ return ret ;
92
+ }
93
+
35
94
// CONTROLLER
36
95
export void processControllerInput (u32 keyIndex , bool32 isDown ) {
37
- GameButtonState * buttonState = & globalGameControllerInput .buttons [keyIndex ];
96
+ GameButtonState * buttonState = & globalGameControllerInputCurrent .buttons [keyIndex ];
38
97
buttonState -> isDown = isDown ;
39
98
}
40
99
41
100
// DRAW
42
101
internal void drawRectangle (
43
- Buffer * vertexBuffer ,
44
- Buffer * colorBuffer ,
102
+ ColorShaderFrame * colorShaderFrame ,
45
103
Color color ,
46
104
f32 minX ,
47
105
f32 minY ,
@@ -57,54 +115,29 @@ internal void drawRectangle(
57
115
maxX , maxY ,
58
116
};
59
117
60
- f32 * vertexPointer = ( f32 * ) vertexBuffer -> current ;
61
- f32 * colorPointer = ( f32 * ) colorBuffer -> current ;
118
+ Buffer * verticesBuffer = & colorShaderFrame -> verticesBuffer ;
119
+ Buffer * colorsBuffer = & colorShaderFrame -> colorsBuffer ;
62
120
63
121
for (int i = 0 ; i < arrayLength (vertices ); i ++ ) {
64
- * vertexPointer ++ = vertices [i ];
122
+ bufferPushf32 ( verticesBuffer , vertices [i ]) ;
65
123
}
66
124
67
125
// every three vertices pairs (triangle) we need to specify color and increment triangles
68
126
// count
69
127
for (int i = 0 ; i < arrayLength (vertices )/6 ; i ++ ) {
70
- * colorPointer ++ = color .r ;
71
- * colorPointer ++ = color .g ;
72
- * colorPointer ++ = color .b ;
73
- * colorPointer ++ = color .a ;
128
+ bufferPushf32 ( colorsBuffer , color .r ) ;
129
+ bufferPushf32 ( colorsBuffer , color .g ) ;
130
+ bufferPushf32 ( colorsBuffer , color .b ) ;
131
+ bufferPushf32 ( colorsBuffer , color .a ) ;
74
132
75
- ++ globalTrianglesCount ;
133
+ colorShaderFrame -> trianglesCount ++ ;
76
134
}
77
-
78
- vertexBuffer -> current = (u8 * )vertexPointer ;
79
- colorBuffer -> current = (u8 * )colorPointer ;
80
-
81
-
82
- }
83
-
84
-
85
- // NOTE(fede)
86
- // from base we store in order:
87
- // 1) vertices
88
- // 2) colors
89
- export u8 * getVertexBufferBase () {
90
- return __heap_base ;
91
135
}
92
136
93
- export u8 * getColorBufferBase () {
94
- return (__heap_base + globalVertexBufferSize );
95
- }
96
-
97
- export u32 getTrianglesCount () {
98
- return globalTrianglesCount ;
99
- }
100
137
101
138
// RENDER
102
- internal void resetFrameState () {
103
- globalTrianglesCount = 0 ;
104
- }
105
139
106
140
export void updateAndRender (f64 timestamp ) {
107
- resetFrameState ();
108
141
//TODO(fede): This values should be more dynamic. Also how to achieve this while
109
142
// preserving aspect ratio?
110
143
f32 levelHeightInPixels = 960 ;
@@ -131,16 +164,16 @@ export void updateAndRender(f64 timestamp) {
131
164
f32 vx = 0.0f ; // meters per second
132
165
f32 vy = 0.0f ;
133
166
134
- if (globalGameControllerInput .moveUp .isDown ) {
167
+ if (globalGameControllerInputCurrent .moveUp .isDown ) {
135
168
vy = 1.5f ;
136
169
}
137
- if (globalGameControllerInput .moveDown .isDown ) {
170
+ if (globalGameControllerInputCurrent .moveDown .isDown ) {
138
171
vy = -1.5f ;
139
172
}
140
- if (globalGameControllerInput .moveRight .isDown ) {
173
+ if (globalGameControllerInputCurrent .moveRight .isDown ) {
141
174
vx = 1.5f ;
142
175
}
143
- if (globalGameControllerInput .moveLeft .isDown ) {
176
+ if (globalGameControllerInputCurrent .moveLeft .isDown ) {
144
177
vx = -1.5f ;
145
178
}
146
179
@@ -168,8 +201,8 @@ export void updateAndRender(f64 timestamp) {
168
201
globalGameState .playerPosition .y = playerMinY ;
169
202
}
170
203
171
- Buffer verticesBuffer = { getVertexBufferBase (), getVertexBufferBase () };
172
- Buffer colorsBuffer = { getColorBufferBase (), getColorBufferBase () } ;
204
+ //TODO: initialize shader frames
205
+ ColorShaderFrame colorShaderFrame = colorShaderFrameInit () ;
173
206
174
207
Position positionInPixels ;
175
208
positionInPixels .x = globalGameState .playerPosition .x * metersToPixels ;
@@ -187,8 +220,7 @@ export void updateAndRender(f64 timestamp) {
187
220
color .a = 1.0f ;
188
221
189
222
drawRectangle (
190
- & verticesBuffer ,
191
- & colorsBuffer ,
223
+ & colorShaderFrame ,
192
224
color ,
193
225
minX ,
194
226
minY ,
@@ -197,5 +229,7 @@ export void updateAndRender(f64 timestamp) {
197
229
);
198
230
199
231
232
+ globalColorShaderTrianglesCount = colorShaderFrame .trianglesCount ;
200
233
globalLastTimestamp = timestamp ;
234
+ globalGameControllerInputLastFrame = globalGameControllerInputCurrent ;
201
235
}
0 commit comments