From db4a0f5f6f8b87e57a1eaeb65d01abbd2efd150d Mon Sep 17 00:00:00 2001 From: Pabloader Date: Thu, 8 May 2025 23:06:55 +0000 Subject: [PATCH] Add import for shaders --- build/assets/stdlib.h | 9 +++++++++ build/filePlugin.ts | 22 ++++++++++++++++++++++ build/html.ts | 4 +++- src/games/playground/assets/shader.glsl | 9 +++++++++ src/games/playground/awoo.cpp | 16 ++++++++++++++++ src/games/playground/index.tsx | 7 ++++--- src/types.d.ts | 6 ++++++ 7 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 build/filePlugin.ts create mode 100644 src/games/playground/assets/shader.glsl create mode 100644 src/games/playground/awoo.cpp diff --git a/build/assets/stdlib.h b/build/assets/stdlib.h index 63587aa..98ec64b 100644 --- a/build/assets/stdlib.h +++ b/build/assets/stdlib.h @@ -1,4 +1,9 @@ #pragma once + +#ifdef __cplusplus +extern "C" { +#endif + #include #include extern unsigned char __heap_base; @@ -22,3 +27,7 @@ IMPORT(log) void print_int(int64_t); EXPORT(__srand) void srand(uint64_t seed); uint64_t rand(void); + +#ifdef __cplusplus +} +#endif diff --git a/build/filePlugin.ts b/build/filePlugin.ts new file mode 100644 index 0000000..a206288 --- /dev/null +++ b/build/filePlugin.ts @@ -0,0 +1,22 @@ +import { plugin, type BunPlugin } from "bun"; + +const filePlugin: BunPlugin = { + name: "File loader", + async setup(build) { + build.onLoad({ filter: /\.glsl$/ }, async (args) => { + console.log(args); + const text = await Bun.file(args.path).text(); + return { + contents: ` + const text = (${JSON.stringify(text)}); + export default text; + `, + loader: 'js', + }; + }); + } +}; + +plugin(filePlugin); + +export default filePlugin; \ No newline at end of file diff --git a/build/html.ts b/build/html.ts index 3f0a5c5..6b7a72c 100644 --- a/build/html.ts +++ b/build/html.ts @@ -5,9 +5,10 @@ import UglifyJS from 'uglify-js'; import wasmPlugin from './wasmPlugin'; import imagePlugin from './imagePlugin'; import fontPlugin from './fontPlugin'; +import audioPlugin from './audioPlugin'; +import filePlugin from './filePlugin'; import { getGames } from './isGame'; -import audioPlugin from './audioPlugin'; interface Args { production?: boolean; @@ -30,6 +31,7 @@ export async function buildHTML(game: string, { production = false, portable = f audioPlugin, fontPlugin, wasmPlugin({ production, portable }), + filePlugin, ] }); diff --git a/src/games/playground/assets/shader.glsl b/src/games/playground/assets/shader.glsl new file mode 100644 index 0000000..dd8df6c --- /dev/null +++ b/src/games/playground/assets/shader.glsl @@ -0,0 +1,9 @@ +attribute vec4 a_position; +attribute vec4 a_color; + +varying vec4 v_color; + +void main() { + gl_Position = a_position; + v_color = a_color; +} \ No newline at end of file diff --git a/src/games/playground/awoo.cpp b/src/games/playground/awoo.cpp new file mode 100644 index 0000000..f715b59 --- /dev/null +++ b/src/games/playground/awoo.cpp @@ -0,0 +1,16 @@ +#include + +struct awoo { + int a; + + awoo operator+(const awoo& other) const { return awoo{a + other.a}; } + + operator int() const { return a * 2; } +}; + +EXPORT(cpptest) uint32_t cpptest() { + awoo a{42}; + awoo b{69}; + + return a + b; +} \ No newline at end of file diff --git a/src/games/playground/index.tsx b/src/games/playground/index.tsx index 5bc7cd7..da43e6e 100644 --- a/src/games/playground/index.tsx +++ b/src/games/playground/index.tsx @@ -1,10 +1,11 @@ import { render } from "preact"; -import attack from './assets/attack.wav'; +import awoo from "./awoo.cpp"; +import shader from './assets/shader.glsl'; export default function main() { - + console.log(shader); render( - , document.body diff --git a/src/types.d.ts b/src/types.d.ts index ad4676c..2f8610a 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -46,6 +46,7 @@ declare module "*.c" { data: DataView; malloc: (size: number) => number; free: (ptr: number) => void; + realloc: (ptr: number, size: number) => number; [x: string]: (...args: any) => any; }; @@ -57,8 +58,13 @@ declare module "*.cpp" { data: DataView; malloc: (size: number) => void; free: (ptr: number) => void; + realloc: (ptr: number, size: number) => number; [x: string]: (...args: any) => any; }; export default instance; +} +declare module "*.glsl" { + const content: string; + export default content; } \ No newline at end of file