create-bloop 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-bloop",
3
- "version": "0.0.20",
3
+ "version": "0.0.21",
4
4
  "description": "Create a new Bloop game",
5
5
  "type": "module",
6
6
  "bin": "./dist/index.js",
@@ -13,8 +13,8 @@
13
13
  "vite": "^7.2.2"
14
14
  },
15
15
  "dependencies": {
16
- "@bloopjs/bloop": "^0.0.91",
16
+ "@bloopjs/bloop": "^0.0.92",
17
17
  "@bloopjs/toodle": "^0.1.3",
18
- "@bloopjs/web": "^0.0.91"
18
+ "@bloopjs/web": "^0.0.92"
19
19
  }
20
20
  }
@@ -15,8 +15,8 @@
15
15
  "vite": "^7.2.2"
16
16
  },
17
17
  "dependencies": {
18
- "@bloopjs/bloop": "^0.0.91",
18
+ "@bloopjs/bloop": "^0.0.92",
19
19
  "@bloopjs/toodle": "^0.1.3",
20
- "@bloopjs/web": "^0.0.91"
20
+ "@bloopjs/web": "^0.0.92"
21
21
  }
22
22
  }
@@ -1,4 +1,4 @@
1
- import { Bloop, Util } from "@bloopjs/bloop";
1
+ import { Bloop } from "@bloopjs/bloop";
2
2
  import * as cfg from "./config";
3
3
  import type { Flipbook } from "./flipbook";
4
4
  import { FLIPBOOKS } from "./sprites";
@@ -1,6 +1,68 @@
1
1
  import { Backends, Colors, type Toodle } from "@bloopjs/toodle";
2
2
 
3
- export function createChromaticAberrationEffect(toodle: Toodle): Backends.PostProcess {
3
+ export function setupGlitchEffect(toodle: Toodle): () => void {
4
+ let glitchEffect: Backends.PostProcess | null = null;
5
+ let glitchTimeout: number = -1;
6
+ let glitchQueued = false;
7
+
8
+ if (toodle.backend.type === "webgpu") {
9
+ glitchEffect = createChromaticAberrationEffect(toodle);
10
+ }
11
+
12
+ function doGlitchEffect() {
13
+ if (!glitchEffect) {
14
+ return console.warn(
15
+ `No glitch effect available - are we running on webgl2?`,
16
+ );
17
+ }
18
+
19
+ console.log("doGlitchEffect", performance.now(), {
20
+ hasFocus: document.hasFocus(),
21
+ });
22
+ // Queue effect if page isn't visible or window doesn't have focus (IDE covering browser)
23
+ if (document.visibilityState === "hidden" || !document.hasFocus()) {
24
+ console.log("queueing glitch");
25
+ glitchQueued = true;
26
+ return;
27
+ }
28
+ console.log("doing glitch immediately");
29
+ if (glitchTimeout > 0) {
30
+ clearTimeout(glitchTimeout);
31
+ }
32
+ toodle.postprocess = glitchEffect;
33
+ glitchTimeout = setTimeout(() => {
34
+ toodle.postprocess = null;
35
+ }, 1000);
36
+ }
37
+
38
+ document.addEventListener("visibilitychange", () => {
39
+ console.log(
40
+ "visibilityStateChange",
41
+ document.visibilityState,
42
+ performance.now(),
43
+ );
44
+ if (document.visibilityState === "visible" && glitchQueued) {
45
+ glitchQueued = false;
46
+ doGlitchEffect();
47
+ }
48
+ });
49
+
50
+ // Handle case where another app (like IDE) covers the browser window
51
+ // visibilitychange doesn't fire for this, but focus does when clicking back
52
+ window.addEventListener("focus", () => {
53
+ console.log("window focus", performance.now());
54
+ if (glitchQueued) {
55
+ glitchQueued = false;
56
+ doGlitchEffect();
57
+ }
58
+ });
59
+
60
+ return doGlitchEffect;
61
+ }
62
+
63
+ function createChromaticAberrationEffect(
64
+ toodle: Toodle,
65
+ ): Backends.PostProcess {
4
66
  if (!(toodle.backend instanceof Backends.WebGPUBackend)) {
5
67
  throw new Error("Post-processing requires WebGPU backend");
6
68
  }
@@ -1,9 +1,11 @@
1
1
  import "./style.css";
2
2
  import { Toodle } from "@bloopjs/toodle";
3
3
  import { start } from "@bloopjs/web";
4
- import { createChromaticAberrationEffect } from "./chromatic-aberration";
5
- import { draw } from "./draw";
4
+ import { draw as drawFn } from "./draw";
6
5
  import { game } from "./game";
6
+ import { setupGlitchEffect } from "./glitchEffect";
7
+
8
+ let draw = drawFn;
7
9
 
8
10
  // boot up the game
9
11
  const app = await start({
@@ -14,17 +16,6 @@ const app = await start({
14
16
  },
15
17
  });
16
18
 
17
- // HMR support
18
- if (import.meta.hot) {
19
- import.meta.hot.accept("./game", async (newModule) => {
20
- await app.acceptHmr(newModule?.game);
21
- });
22
-
23
- // import.meta.hot.accept("./draw", async (newModule) => {
24
- // draw = newModule?.draw;
25
- // });
26
- }
27
-
28
19
  const canvas = app.canvas;
29
20
  if (!canvas) throw new Error("No canvas element found");
30
21
 
@@ -87,14 +78,17 @@ window.addEventListener("keydown", (e) => {
87
78
  }
88
79
  });
89
80
 
90
- // Debug: Press G to toggle glitch effect on webgpu backend
91
- if (toodle.backend.type === "webgpu") {
92
- const glitchEffect = createChromaticAberrationEffect(toodle);
93
- let glitchEnabled = false;
94
- window.addEventListener("keydown", (e) => {
95
- if (e.key === "g") {
96
- glitchEnabled = !glitchEnabled;
97
- toodle.postprocess = glitchEnabled ? glitchEffect : null;
98
- }
81
+ const doGlitchEffect = setupGlitchEffect(toodle);
82
+
83
+ // HMR support
84
+ if (import.meta.hot) {
85
+ import.meta.hot.accept("./game", async (newModule) => {
86
+ doGlitchEffect();
87
+ await app.acceptHmr(newModule?.game);
88
+ });
89
+
90
+ import.meta.hot.accept("./draw", async (newModule) => {
91
+ doGlitchEffect();
92
+ draw = newModule?.draw;
99
93
  });
100
94
  }