euv-engine 0.32.9

A high-performance 2D game engine built on the euv framework, featuring ECS, fixed-timestep game loop, canvas rendering, physics, collision detection, sprite animation, and audio.
Documentation
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
use super::*;

const CTX_METHODS: &str = r#"[
    "createShader","shaderSource","compileShader","getShaderParameter",
    "getShaderInfoLog","deleteShader","createProgram","attachShader",
    "linkProgram","getProgramParameter","getProgramInfoLog","deleteProgram",
    "getUniformLocation","uniform1f","uniform2f","uniform3f","uniform4f",
    "uniformMatrix4fv","uniform4fv","bindBuffer","bufferData","deleteBuffer"
]"#;

fn reflective(target: &Object, key: &str, value: &JsValue) {
    let installer: js_sys::Function = js_sys::Function::new_with_args(
        "target, key, value",
        "Object.defineProperty(target, key, { value: value, writable: true, configurable: true });",
    );
    let outcome: Result<JsValue, JsValue> = installer.call3(
        &JsValue::NULL,
        target.as_ref(),
        &JsValue::from_str(key),
        value,
    );
    assert!(outcome.is_ok(), "the stand-in must accept {key}");
}

fn recording_context(linked: bool, compiled: bool) -> (WebGl2RenderingContext, Object) {
    let context: Object = Object::new();
    reflective(context.as_ref(), "__log", Array::new().as_ref());
    let arm: js_sys::Function = js_sys::Function::new_with_args(
        "target, methods, linked, compiled",
        "for (const name of JSON.parse(methods)) { target[name] = function() { target.__log.push([name, Array.from(arguments)]); if (name === 'createShader') return { __gl: 'shader' }; if (name === 'createProgram') return { __gl: 'program' }; if (name === 'getShaderParameter') return compiled; if (name === 'getProgramParameter') return linked; if (name === 'getUniformLocation') { return arguments[1] === 'u_removed' ? null : { __gl: 'location' }; } if (name.indexOf('InfoLog') >= 0) return 'driver says no'; return null; }; }",
    );
    let armed: Result<JsValue, JsValue> = arm.call4(
        &JsValue::NULL,
        context.as_ref(),
        &JsValue::from_str(CTX_METHODS),
        &JsValue::from_bool(linked),
        &JsValue::from_bool(compiled),
    );
    assert!(armed.is_ok(), "the program recorder must install cleanly");
    let typed: WebGl2RenderingContext = context.clone().unchecked_into();
    (typed, context)
}

fn ops(context: &Object) -> Vec<String> {
    let log: Array = Reflect::get(context, &JsValue::from_str("__log"))
        .expect("log readable")
        .unchecked_into();
    (0..log.length())
        .map(|index: u32| {
            let entry: Array = log.get(index).unchecked_into();
            entry.get(0).as_string().unwrap_or_default()
        })
        .collect()
}

fn count_of(context: &Object, op: &str) -> usize {
    ops(context)
        .iter()
        .filter(|recorded: &&String| recorded.as_str() == op)
        .count()
}

fn args_at(context: &Object, op: &str, nth: usize) -> Array {
    nth_of(context, op, nth)
}

fn nth_of(context: &Object, op: &str, nth: usize) -> Array {
    let log: Array = Reflect::get(context, &JsValue::from_str("__log"))
        .expect("log readable")
        .unchecked_into();
    let mut seen: usize = 0;
    for index in 0..log.length() {
        let entry: Array = log.get(index).unchecked_into();
        if entry.get(0).as_string().as_deref() == Some(op) {
            if seen == nth {
                return entry.get(1).unchecked_into();
            }
            seen += 1;
        }
    }
    panic!(
        "{op} call #{nth} was never made; recorded ops were {:?}",
        ops(context)
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_linked_program_compiles_both_stages_attaches_them_and_frees_the_shaders() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);

    let program: Result<GlProgram, WebGlProgramError> =
        GlProgram::create(&context, "vertex body", "fragment body");

    assert!(
        program.is_ok(),
        "a shader that compiles and links produces a program"
    );
    assert_eq!(
        count_of(&log, "createShader"),
        2,
        "a vertex stage and a fragment stage, so two shader objects"
    );
    assert_eq!(
        count_of(&log, "attachShader"),
        2,
        "both stages are attached to the program"
    );
    assert_eq!(
        count_of(&log, "linkProgram"),
        1,
        "linking happens exactly once"
    );
    assert_eq!(
        count_of(&log, "deleteShader"),
        2,
        "the shader objects are intermediate products and are freed once linked"
    );
    assert_eq!(
        count_of(&log, "deleteProgram"),
        0,
        "a successful program must survive for later draws"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn the_shader_source_reaches_the_driver_verbatim_for_each_stage() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);

    GlProgram::create(&context, "vertex body", "fragment body").expect("linked");

    assert_eq!(
        args_at(&log, "shaderSource", 0).get(1).as_string(),
        Some(String::from("vertex body")),
        "the vertex source is forwarded to the shader object unchanged"
    );
    assert_eq!(
        args_at(&log, "shaderSource", 1).get(1).as_string(),
        Some(String::from("fragment body")),
        "the fragment source is forwarded to its own shader object unchanged"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_failed_link_returns_the_driver_log_and_frees_the_program() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(false, true);

    let outcome: Result<GlProgram, WebGlProgramError> =
        GlProgram::create(&context, "vertex body", "fragment body");

    match outcome {
        Err(WebGlProgramError::ProgramLink(log_text)) => assert_eq!(
            log_text,
            String::from("driver says no"),
            "a GLSL diagnostic is only actionable in the driver's own wording"
        ),
        Err(other) => panic!("a failed link must report ProgramLink, got {other:?}"),
        Ok(_) => panic!("a driver that refuses to link must not yield a program"),
    }
    assert_eq!(
        count_of(&log, "deleteProgram"),
        1,
        "a program that failed to link is unusable and has to be released"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_shader_that_fails_to_compile_stops_before_the_program_is_ever_created() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, false);

    let outcome: Result<GlProgram, WebGlProgramError> =
        GlProgram::create(&context, "broken vertex", "fragment body");

    match outcome {
        Err(WebGlProgramError::ShaderCompile(log_text)) => assert_eq!(
            log_text,
            String::from("driver says no"),
            "the compile log is what the caller needs, not a generic failure"
        ),
        Err(other) => panic!("a failed compile must report ShaderCompile, got {other:?}"),
        Ok(_) => panic!("a shader the driver refused must not yield a program"),
    }
    assert_eq!(
        count_of(&log, "createProgram"),
        0,
        "a failed compile must not reach program creation, or the fragment stage would leak"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_uniform_location_is_resolved_once_and_then_served_from_cache() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let after_link: usize = count_of(&log, "getUniformLocation");

    program.set_uniform_1f(&context, "u_alpha", 0.5);
    program.set_uniform_1f(&context, "u_alpha", 0.75);

    assert_eq!(
        count_of(&log, "getUniformLocation"),
        after_link + 1,
        "a repeated name is served from the cache instead of a second lookup"
    );
    assert_eq!(
        count_of(&log, "uniform1f"),
        2,
        "each upload still reaches the driver, cache or not"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_uniform_the_compiler_removed_is_looked_up_once_and_its_miss_is_cached() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let after_link: usize = count_of(&log, "getUniformLocation");
    program.set_uniform_1f(&context, "u_removed", 0.5);
    program.set_uniform_1f(&context, "u_removed", 0.75);
    assert_eq!(
        count_of(&log, "getUniformLocation"),
        after_link + 1,
        "a uniform the compiler removed is resolved once, because the miss is cached like a hit"
    );
    assert_eq!(
        count_of(&log, "uniform1f"),
        2,
        "each write still reaches the driver, cache or not"
    );
    let upload: Array = args_at(&log, "uniform1f", 1);
    assert!(
        upload.get(0).is_null() || upload.get(0).is_undefined(),
        "the cached miss hands the driver no location, which is what an optimized out uniform gets"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_repeated_uniform_write_reuses_the_cached_location_and_still_carries_the_new_value() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let after_link: usize = count_of(&log, "getUniformLocation");
    program.set_uniform_1f(&context, "u_alpha", 0.5);
    program.set_uniform_1f(&context, "u_alpha", 0.75);
    assert_eq!(
        count_of(&log, "getUniformLocation"),
        after_link + 1,
        "the second write of the same name is served from the cache"
    );
    assert_eq!(
        count_of(&log, "uniform1f"),
        2,
        "both writes reach the driver"
    );
    let second: Array = args_at(&log, "uniform1f", 1);
    assert_eq!(
        second.get(1).as_f64(),
        Some(0.75),
        "the second write carries its own value, so the cache holds the location and not the value"
    );
    assert!(
        !second.get(0).is_undefined(),
        "the cached location is what the second write uploads through"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn each_uniform_width_forwards_exactly_its_own_component_count() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");

    program.set_uniform_1f(&context, "a", 1.0);
    program.set_uniform_2f(&context, "b", 1.0, 2.0);
    program.set_uniform_3f(&context, "c", 1.0, 2.0, 3.0);
    program.set_uniform_4f(&context, "d", 1.0, 2.0, 3.0, 4.0);

    assert_eq!(
        args_at(&log, "uniform1f", 0).length(),
        2,
        "uniform1f passes the location then the single value"
    );
    assert_eq!(
        args_at(&log, "uniform2f", 0).length(),
        3,
        "uniform2f passes the location then x and y, not a third component"
    );
    assert_eq!(
        args_at(&log, "uniform3f", 0).length(),
        4,
        "uniform3f passes the location then x, y and z"
    );
    assert_eq!(
        args_at(&log, "uniform4f", 0).length(),
        5,
        "uniform4f passes the location then the whole vector including w"
    );
    assert_eq!(
        args_at(&log, "uniform2f", 0).get(2).as_f64(),
        Some(2.0),
        "the second component is y, so it has to arrive second after the location"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_matrix_upload_passes_sixteen_lanes_untransposed() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let matrix: Matrix4x4 = Matrix4x4::identity();

    program.set_uniform_mat4(&context, "u_model", &matrix);

    let upload: Array = args_at(&log, "uniformMatrix4fv", 0);
    assert_eq!(
        upload.get(1).as_bool(),
        Some(false),
        "the transpose flag is false, so the engine matrix goes across in the order GL expects"
    );
    let lanes: Array = upload.get(2).unchecked_into();
    assert_eq!(
        lanes.length(),
        16,
        "a mat4 is sixteen floats, converted into a fixed stack array rather than a heap Vec"
    );
    let observed: Vec<f64> = (0..lanes.length())
        .map(|index: u32| lanes.get(index).as_f64().unwrap_or(-1.0))
        .collect();
    assert_eq!(
        observed.len(),
        16,
        "the engine's own Matrix4x4 is sixteen elements, so all of them are converted"
    );
    assert!(
        observed.contains(&1.0),
        "an identity matrix carries ones, so the f64 to f32 conversion preserved a value: {observed:?}"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn a_vec4_array_upload_hands_the_callers_slice_straight_to_the_driver() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let data: Vec<f32> = vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0];

    program.set_uniform_vec4_array(&context, "u_offsets[0]", &data);

    let upload: Array = args_at(&log, "uniform4fv", 0);
    assert!(
        !upload.get(0).is_undefined(),
        "the resolved location is what makes the upload land on the right uniform"
    );
    let forwarded: Array = upload.get(1).unchecked_into();
    assert_eq!(
        forwarded.length(),
        8,
        "the slice is handed over whole; two packed vec4 elements is eight floats"
    );
    assert_eq!(
        forwarded.get(7).as_f64(),
        Some(8.0),
        "the tail of the caller slice reaches the driver, so nothing is truncated on the way"
    );
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
#[cfg_attr(
    not(target_arch = "wasm32"),
    ignore = "records a WebGl2RenderingContext stand-in, which only exists under wasm"
)]
fn deleting_a_program_releases_the_handle() {
    let (context, log): (WebGl2RenderingContext, Object) = recording_context(true, true);
    let mut program: GlProgram =
        GlProgram::create(&context, "vertex body", "fragment body").expect("linked");
    let before: usize = count_of(&log, "deleteProgram");

    program.delete(&context);

    assert_eq!(
        count_of(&log, "deleteProgram"),
        before + 1,
        "the driver handle is released exactly once"
    );
}