diff --git a/build/assets/include/math.h b/build/assets/include/math.h index 850253a..949f9e9 100644 --- a/build/assets/include/math.h +++ b/build/assets/include/math.h @@ -1,6 +1,7 @@ #pragma once #include +#include #define M_PI 3.14159265359 #define M_PI_2 1.57079632679 @@ -20,15 +21,10 @@ static inline bool isinf(double x) { return !isnan(x) && isnan(x - x); } -float sinf(float x); -static inline float cosf(float x) { - return sinf(x + M_PI_2); +IMPORT(Math_sin) double sin(double x); +static inline double cos(double x) { + return sin(x + M_PI_2); } -static inline float tanf(float x) { - return sinf(x) / cosf(x); -} - -float fmodf(float x, float y); #ifdef __cplusplus } diff --git a/build/assets/index.html b/build/assets/index.html index 3ab373d..2bac58c 100644 --- a/build/assets/index.html +++ b/build/assets/index.html @@ -22,11 +22,21 @@ /*$STYLE$*/ + + \ No newline at end of file diff --git a/build/assets/lib/math.c b/build/assets/lib/math.c deleted file mode 100644 index 2b000d9..0000000 --- a/build/assets/lib/math.c +++ /dev/null @@ -1,327 +0,0 @@ -#include -#include - -float sinf(float x) { - static const float lookup_table[256] = { - 0.0f, // 0 - 0.0061599463f, // 1 - 0.012319659f, // 2 - 0.018478904f, // 3 - 0.024637448f, // 4 - 0.030795058f, // 5 - 0.036951497f, // 6 - 0.043106537f, // 7 - 0.049259938f, // 8 - 0.055411477f, // 9 - 0.061560903f, // 10 - 0.067708001f, // 11 - 0.073852524f, // 12 - 0.079994254f, // 13 - 0.086132944f, // 14 - 0.092268363f, // 15 - 0.098400272f, // 16 - 0.10452846f, // 17 - 0.11065269f, // 18 - 0.1167727f, // 19 - 0.12288829f, // 20 - 0.12899922f, // 21 - 0.13510525f, // 22 - 0.14120616f, // 23 - 0.14730169f, // 24 - 0.15339166f, // 25 - 0.1594758f, // 26 - 0.16555387f, // 27 - 0.17162569f, // 28 - 0.17769097f, // 29 - 0.18374951f, // 30 - 0.1898011f, // 31 - 0.19584547f, // 32 - 0.20188241f, // 33 - 0.2079117f, // 34 - 0.21393308f, // 35 - 0.21994635f, // 36 - 0.2259513f, // 37 - 0.23194765f, // 38 - 0.2379352f, // 39 - 0.24391371f, // 40 - 0.24988301f, // 41 - 0.25584278f, // 42 - 0.26179287f, // 43 - 0.26773301f, // 44 - 0.27366298f, // 45 - 0.27958259f, // 46 - 0.28549159f, // 47 - 0.29138973f, // 48 - 0.29727685f, // 49 - 0.30315268f, // 50 - 0.309017f, // 51 - 0.31486961f, // 52 - 0.32071021f, // 53 - 0.32653871f, // 54 - 0.33235481f, // 55 - 0.33815828f, // 56 - 0.34394893f, // 57 - 0.3497265f, // 58 - 0.35549083f, // 59 - 0.36124167f, // 60 - 0.36697879f, // 61 - 0.372702f, // 62 - 0.37841105f, // 63 - 0.38410574f, // 64 - 0.38978586f, // 65 - 0.39545122f, // 66 - 0.40110153f, // 67 - 0.40673664f, // 68 - 0.41235632f, // 69 - 0.41796035f, // 70 - 0.42354852f, // 71 - 0.4291206f, // 72 - 0.43467644f, // 73 - 0.44021577f, // 74 - 0.44573835f, // 75 - 0.45124406f, // 76 - 0.45673263f, // 77 - 0.46220389f, // 78 - 0.4676576f, // 79 - 0.47309354f, // 80 - 0.47851157f, // 81 - 0.48391145f, // 82 - 0.48929292f, // 83 - 0.49465582f, // 84 - 0.5f, // 85 - 0.5053252f, // 86 - 0.5106312f, // 87 - 0.51591784f, // 88 - 0.52118486f, // 89 - 0.52643216f, // 90 - 0.53165948f, // 91 - 0.53686661f, // 92 - 0.54205334f, // 93 - 0.54721951f, // 94 - 0.55236501f, // 95 - 0.55748945f, // 96 - 0.56259274f, // 97 - 0.5676747f, // 98 - 0.57273513f, // 99 - 0.57777381f, // 100 - 0.58279061f, // 101 - 0.58778524f, // 102 - 0.59275758f, // 103 - 0.59770751f, // 104 - 0.60263467f, // 105 - 0.60753894f, // 106 - 0.6124202f, // 107 - 0.61727822f, // 108 - 0.62211281f, // 109 - 0.6269238f, // 110 - 0.63171101f, // 111 - 0.63647425f, // 112 - 0.6412133f, // 113 - 0.64592808f, // 114 - 0.65061831f, // 115 - 0.65528381f, // 116 - 0.65992457f, // 117 - 0.66454017f, // 118 - 0.66913062f, // 119 - 0.67369562f, // 120 - 0.67823511f, // 121 - 0.68274885f, // 122 - 0.68723667f, // 123 - 0.69169843f, // 124 - 0.69613391f, // 125 - 0.70054305f, // 126 - 0.70492554f, // 127 - 0.70928127f, // 128 - 0.71361017f, // 129 - 0.7179119f, // 130 - 0.72218645f, // 131 - 0.72643358f, // 132 - 0.73065311f, // 133 - 0.73484498f, // 134 - 0.7390089f, // 135 - 0.74314487f, // 136 - 0.74725252f, // 137 - 0.75133187f, // 138 - 0.75538272f, // 139 - 0.7594049f, // 140 - 0.76339829f, // 141 - 0.76736265f, // 142 - 0.77129793f, // 143 - 0.775204f, // 144 - 0.77908057f, // 145 - 0.78292763f, // 146 - 0.78674495f, // 147 - 0.79053241f, // 148 - 0.79428989f, // 149 - 0.7980172f, // 150 - 0.8017143f, // 151 - 0.80538094f, // 152 - 0.809017f, // 153 - 0.81262237f, // 154 - 0.81619692f, // 155 - 0.81974047f, // 156 - 0.82325292f, // 157 - 0.82673419f, // 158 - 0.83018404f, // 159 - 0.83360237f, // 160 - 0.8369891f, // 161 - 0.84034407f, // 162 - 0.84366715f, // 163 - 0.84695822f, // 164 - 0.8502171f, // 165 - 0.8534438f, // 166 - 0.85663807f, // 167 - 0.8597998f, // 168 - 0.86292899f, // 169 - 0.86602545f, // 170 - 0.86908895f, // 171 - 0.87211949f, // 172 - 0.875117f, // 173 - 0.87808126f, // 174 - 0.8810122f, // 175 - 0.8839097f, // 176 - 0.88677371f, // 177 - 0.88960397f, // 178 - 0.89240056f, // 179 - 0.8951633f, // 180 - 0.89789206f, // 181 - 0.90058666f, // 182 - 0.90324718f, // 183 - 0.90587342f, // 184 - 0.90846527f, // 185 - 0.91102266f, // 186 - 0.91354549f, // 187 - 0.91603357f, // 188 - 0.91848695f, // 189 - 0.92090553f, // 190 - 0.92328912f, // 191 - 0.92563766f, // 192 - 0.9279511f, // 193 - 0.93022931f, // 194 - 0.93247223f, // 195 - 0.93467975f, // 196 - 0.93685186f, // 197 - 0.93898833f, // 198 - 0.94108927f, // 199 - 0.94315445f, // 200 - 0.94518387f, // 201 - 0.94717735f, // 202 - 0.94913495f, // 203 - 0.95105654f, // 204 - 0.95294201f, // 205 - 0.95479131f, // 206 - 0.95660442f, // 207 - 0.95838124f, // 208 - 0.96012163f, // 209 - 0.96182567f, // 210 - 0.96349317f, // 211 - 0.96512407f, // 212 - 0.96671838f, // 213 - 0.96827602f, // 214 - 0.9697969f, // 215 - 0.97128105f, // 216 - 0.97272825f, // 217 - 0.97413862f, // 218 - 0.97551197f, // 219 - 0.9768483f, // 220 - 0.97814763f, // 221 - 0.97940975f, // 222 - 0.98063475f, // 223 - 0.98182255f, // 224 - 0.9829731f, // 225 - 0.98408633f, // 226 - 0.98516226f, // 227 - 0.98620075f, // 228 - 0.98720181f, // 229 - 0.9881655f, // 230 - 0.98909163f, // 231 - 0.98998022f, // 232 - 0.99083126f, // 233 - 0.99164468f, // 234 - 0.99242049f, // 235 - 0.99315864f, // 236 - 0.99385911f, // 237 - 0.99452192f, // 238 - 0.99514693f, // 239 - 0.99573416f, // 240 - 0.99628365f, // 241 - 0.9967953f, // 242 - 0.99726915f, // 243 - 0.99770516f, // 244 - 0.99810332f, // 245 - 0.99846363f, // 246 - 0.99878597f, // 247 - 0.99907047f, // 248 - 0.99931705f, // 249 - 0.99952573f, // 250 - 0.99969643f, // 251 - 0.99982923f, // 252 - 0.99992412f, // 253 - 0.99998105f, // 254 - 1.0f // 255 - }; - - x = fmodf(x, 2.0f * (float)M_PI); - if (x < 0) x += 2.0f * (float)M_PI; - - float reference_angle; - int sign = 1; - - if (x <= (float)M_PI_2) { - reference_angle = x; - } else if (x <= (float)M_PI) { - reference_angle = (float)M_PI - x; - } else if (x <= 3.0f * (float)M_PI_2) { - reference_angle = x - (float)M_PI; - sign = -1; - } else { - reference_angle = 2.0f * (float)M_PI - x; - sign = -1; - } - - float position = reference_angle * (255.0f / ((float)M_PI / 2.0f)); - int i = (int)position; - float interpolated; - - if (i >= 255) { - interpolated = lookup_table[255]; - } else { - float fractional = position - i; - interpolated = lookup_table[i] + fractional * (lookup_table[i + 1] - lookup_table[i]); - } - - return sign * interpolated; -} - - -float fmodf(float x, float y) { - if (y == 0.0 || isnan(x) || isnan(y)) { - return (0.0 / 0.0); - } - - bool neg = x < 0.0; - float ax = neg ? -x : x; - float ay = (y < 0.0) ? -y : y; - - if (ax < ay) { - return x; - } - - float tmp = ay; - while (tmp <= ax) { - tmp *= 2.0f; - } - if (isinf(tmp)) { - return 0; - } - - while (tmp > ay) { - tmp *= 0.5f; - if (ax >= tmp) { - ax -= tmp; - } - } - - return neg ? -ax : ax; -} - diff --git a/build/assets/pwa_icon.png b/build/assets/pwa_icon.png new file mode 100644 index 0000000..e38826d Binary files /dev/null and b/build/assets/pwa_icon.png differ diff --git a/build/build.ts b/build/build.ts index 34ff402..93dc29e 100644 --- a/build/build.ts +++ b/build/build.ts @@ -31,6 +31,6 @@ if (publish) { console.log(`Publishing ${game}...`); const result = await $`scp "${filePath}" "${publish}${game}.html"`; if (result.exitCode === 0) { - console.log(`Build successful: ${process.env.PUBLISH_URL}${game}.html`); + console.log(`Build successful: ${process.env.PUBLISH_URL}${game}`); } } \ No newline at end of file diff --git a/build/html.ts b/build/html.ts index 6b7a72c..e06727c 100644 --- a/build/html.ts +++ b/build/html.ts @@ -39,7 +39,7 @@ export async function buildHTML(game: string, { production = false, portable = f const scriptFile = bundle.outputs.find(a => a.kind === 'entry-point' && a.path.endsWith('.js')); if (!scriptFile) { console.error('No entry point found:', bundle.outputs); - return; + return; } const iconFile = Bun.file(path.resolve(import.meta.dir, '..', 'src', 'games', game, 'assets', 'favicon.ico')); let icon = ''; @@ -51,6 +51,29 @@ export async function buildHTML(game: string, { production = false, portable = f if (styleFile) { style = await styleFile.text(); } + const title = game[0].toUpperCase() + game.slice(1).toLowerCase(); + let pwaIconFile = Bun.file(path.resolve(import.meta.dir, '..', 'src', 'games', game, 'assets', 'pwa_icon.png')); + if (!await pwaIconFile.exists()) { + pwaIconFile = Bun.file(path.resolve(import.meta.dir, 'assets', 'pwa_icon.png')); + } + const pwaIcon = `data:;base64,${Buffer.from(await pwaIconFile.arrayBuffer()).toString('base64')}`; + const publishURL = process.env.PUBLISH_URL ? `${process.env.PUBLISH_URL}${game}`: '.'; + const manifestJSON = JSON.stringify({ + name: title, + short_name: title, + start_url: publishURL, + id: `/${game}`, + display_override: ["window-controls-overlay"], + display: "fullscreen", + background_color: "#ffffff", + theme_color: "#000000", + icons: [{ + src: pwaIcon, + sizes: '192x192', + type: 'image/png' + }] + }); + const manifest = ``; let script = await scriptFile.text(); const inits = new Set(); script = script.replace(/var (init_[^ ]+) = __esm\(\(\)/g, (_, $1) => { @@ -80,8 +103,9 @@ export async function buildHTML(game: string, { production = false, portable = f const resultHTML = html .replace('', () => `${scriptPrefix}`) - .replace('', game[0].toUpperCase() + game.slice(1).toLowerCase()) + .replace('', title) .replace('', icon) + .replace('', manifest) .replace('/*$STYLE$*/', style); return minify(resultHTML, { diff --git a/build/wasmPlugin.ts b/build/wasmPlugin.ts index 7ad66f2..c88ec4d 100644 --- a/build/wasmPlugin.ts +++ b/build/wasmPlugin.ts @@ -64,7 +64,12 @@ const wasmPlugin = ({ production, portable }: WasmLoaderConfig = {}): BunPlugin memory.grow(blocks); data = new DataView(memory.buffer); } - } + }, + Math_sin(x) { return Math.sin(x); }, + Math_log(x) { return Math.log(x); }, + Math_sqrt(x) { return Math.sqrt(x); }, + Math_pow(x,y) { return Math.pow(x, y); }, + Math_fmod(x,y) { return x % y; }, } }); diff --git a/bun.lock b/bun.lock index 5205958..ac5e76e 100644 --- a/bun.lock +++ b/bun.lock @@ -18,11 +18,10 @@ "@types/bun": "latest", "@types/html-minifier": "4.0.5", "@types/inquirer": "9.0.7", - "assemblyscript": "0.27.29", "browser-detect": "0.2.28", "eruda": "3.2.3", "html-minifier": "4.0.0", - "typescript": "^5.8.2", + "typescript": "5.8.2", "uglify-js": "3.19.3", }, }, @@ -112,7 +111,7 @@ "@protobufjs/utf8": ["@protobufjs/utf8@1.1.0", "", {}, "sha512-Vvn3zZrhQZkkBE8LSuW3em98c0FwgO4nxzv6OdSxPKJIEKY2bGbHn+mhGIPerzI4twdxaP8/0+06HBpwf345Lw=="], - "@types/bun": ["@types/bun@1.2.5", "", { "dependencies": { "bun-types": "1.2.5" } }, "sha512-w2OZTzrZTVtbnJew1pdFmgV99H0/L+Pvw+z1P67HaR18MHOzYnTYOi6qzErhK8HyT+DB782ADVPPE92Xu2/Opg=="], + "@types/bun": ["@types/bun@1.2.14", "", { "dependencies": { "bun-types": "1.2.14" } }, "sha512-VsFZKs8oKHzI7zwvECiAJ5oSorWndIWEVhfbYqZd4HI/45kzW7PN2Rr5biAzvGvRuNmYLSANY+H59ubHq8xw7Q=="], "@types/clean-css": ["@types/clean-css@4.2.11", "", { "dependencies": { "@types/node": "*", "source-map": "^0.6.0" } }, "sha512-Y8n81lQVTAfP2TOdtJJEsCoYl1AnOkqDqMvXb9/7pfgZZ7r8YrEyurrAvAoAjHOGXKRybay+5CsExqIH6liccw=="], @@ -132,8 +131,6 @@ "@types/wrap-ansi": ["@types/wrap-ansi@3.0.0", "", {}, "sha512-ltIpx+kM7g/MLRZfkbL7EsCEjfzCcScLpkg37eXEtx5kmrAKBkTJwd1GIAjDSL8wTpM6Hzn5YO4pSb91BEwu1g=="], - "@types/ws": ["@types/ws@8.5.10", "", { "dependencies": { "@types/node": "*" } }, "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A=="], - "ace-builds": ["ace-builds@1.36.3", "", {}, "sha512-YcdwV2IIaJSfjkWAR1NEYN5IxBiXefTgwXsJ//UlaFrjXDX5hQpvPFvEePHz2ZBUfvO54RjHeRUQGX8MS5HaMQ=="], "ansi-escapes": ["ansi-escapes@4.3.2", "", { "dependencies": { "type-fest": "^0.21.3" } }, "sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ=="], @@ -142,17 +139,13 @@ "ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="], - "assemblyscript": ["assemblyscript@0.27.29", "", { "dependencies": { "binaryen": "116.0.0-nightly.20240114", "long": "^5.2.1" }, "bin": { "asc": "bin/asc.js", "asinit": "bin/asinit.js" } }, "sha512-pH6udb7aE2F0t6cTh+0uCepmucykhMnAmm7k0kkAU3SY7LvpIngEBZWM6p5VCguu4EpmKGwEuZpZbEXzJ/frHQ=="], - "balanced-match": ["balanced-match@1.0.2", "", {}, "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw=="], - "binaryen": ["binaryen@116.0.0-nightly.20240114", "", { "bin": { "wasm2js": "bin/wasm2js", "wasm-opt": "bin/wasm-opt" } }, "sha512-0GZrojJnuhoe+hiwji7QFaL3tBlJoA+KFUN7ouYSDGZLSo9CKM8swQX8n/UcbR0d1VuZKU+nhogNzv423JEu5A=="], - "brace-expansion": ["brace-expansion@2.0.1", "", { "dependencies": { "balanced-match": "^1.0.0" } }, "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA=="], "browser-detect": ["browser-detect@0.2.28", "", { "dependencies": { "core-js": "^2.5.7" } }, "sha512-KeWGHqYQmHDkCFG2dIiX/2wFUgqevbw/rd6wNi9N6rZbaSJFtG5kel0HtprRwCGp8sqpQP79LzDJXf/WCx4WAw=="], - "bun-types": ["bun-types@1.2.5", "", { "dependencies": { "@types/node": "*", "@types/ws": "~8.5.10" } }, "sha512-3oO6LVGGRRKI4kHINx5PIdIgnLRb7l/SprhzqXapmoYkFl5m4j6EvALvbDVuuBFaamB46Ap6HCUxIXNLCGy+tg=="], + "bun-types": ["bun-types@1.2.14", "", { "dependencies": { "@types/node": "*" } }, "sha512-Kuh4Ub28ucMRWeiUUWMHsT9Wcbr4H3kLIO72RZZElSDxSu7vpetRvxIUDUaW6QtaIeixIpm7OXtNnZPf82EzwA=="], "camel-case": ["camel-case@3.0.0", "", { "dependencies": { "no-case": "^2.2.0", "upper-case": "^1.1.1" } }, "sha512-+MbKztAYHXPr1jNTSKQF52VpcFjwY5RkR7fxksV8Doo4KAYc5Fl4UJRgthBbTmEx8C54DqahhbLJkDwjI3PI/w=="], @@ -310,8 +303,6 @@ "@types/through/@types/node": ["@types/node@20.12.14", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="], - "@types/ws/@types/node": ["@types/node@20.12.14", "", { "dependencies": { "undici-types": "~5.26.4" } }, "sha512-scnD59RpYD91xngrQQLGkE+6UrHUPzeKZWhhjBSa3HSkwjbQc38+q3RoIVEwxQGRw3M+j5hpNAM+lgV3cVormg=="], - "html-minifier/uglify-js": ["uglify-js@3.18.0", "", { "bin": { "uglifyjs": "bin/uglifyjs" } }, "sha512-SyVVbcNBCk0dzr9XL/R/ySrmYf0s372K6/hFklzgcp2lBFyXtw4I7BOdDjlLhE1aVqaI/SHWXWmYdlZxuyF38A=="], "onnxruntime-web/onnxruntime-common": ["onnxruntime-common@1.20.0-dev.20241016-2b8fc5529b", "", {}, "sha512-KZK8b6zCYGZFjd4ANze0pqBnqnFTS3GIVeclQpa2qseDpXrCQJfkWBixRcrZShNhm3LpFOZ8qJYFC5/qsJK9WQ=="], diff --git a/src/games/playground/assets/pwa_icon.png b/src/games/playground/assets/pwa_icon.png new file mode 100644 index 0000000..b373c44 Binary files /dev/null and b/src/games/playground/assets/pwa_icon.png differ diff --git a/src/games/playground/awoo.cpp b/src/games/playground/awoo.cpp index cbc8975..301b233 100644 --- a/src/games/playground/awoo.cpp +++ b/src/games/playground/awoo.cpp @@ -3,19 +3,6 @@ #include static image_data_t image; -static image_color_t color; -static uint16_t current_x; -static uint16_t current_y; - -static void _moveto(uint16_t x, uint16_t y) { - current_x = x; - current_y = y; -} - -static void _lineto(uint16_t x, uint16_t y) { - image_draw_line(image, current_x, current_y, x, y, color); - _moveto(x, y); -} // D E F I N E S /////////////////////////////////////////////////////////// @@ -65,8 +52,8 @@ void Rotate_Object(object_ptr object, float angle) { float x_new, y_new, cs, sn; // pre-compute sin and cos - cs = cosf(angle); - sn = sinf(angle); + cs = cos(angle); + sn = sin(angle); // for each vertex rotate it by angle for (index = 0; index < object->num_vertices; index++) { @@ -94,16 +81,14 @@ void Create_Field(void) { asteroids[index].num_vertices = 6; asteroids[index].color = 0xFF000000 | (rand()); - asteroids[index].xo = 41 + rand() % (image.width - 82); - asteroids[index].yo = 41 + rand() % (image.height - 82); + asteroids[index].xo = 21 + rand() % (image.width - 42); + asteroids[index].yo = 21 + rand() % (image.height - 42); asteroids[index].x_velocity = -3 + rand() % 6; asteroids[index].y_velocity = -3 + rand() % 6; asteroids[index].scale = (float)(rand() % 30) / 10; asteroids[index].angle = (float)(-5 + (float)(rand() % 10)) / 100; - printf("xo=%f, yo=%f, xv=%f, yv=%f, r=%d\n", asteroids[index].xo, asteroids[index].yo, asteroids[index].x_velocity, asteroids[index].y_velocity, rand() % 6); - asteroids[index].vertices[0].x = 4.0; asteroids[index].vertices[0].y = 3.5; asteroids[index].vertices[1].x = 8.5; @@ -128,36 +113,32 @@ void Create_Field(void) { ////////////////////////////////////////////////////////////////////////////// void Draw_Asteroids(int erase) { - int index, vertex; - float xo, yo; + image_color_t color = {0xFF000000}; - for (index = 0; index < NUM_ASTEROIDS; index++) { + for (int index = 0; index < NUM_ASTEROIDS; index++) { // draw the asteroid - if (erase == ERASE) { - color.color = 0xFF000000; - } else { + if (erase == DRAW) { color.color = asteroids[index].color; } // get position of object - xo = asteroids[index].xo; - yo = asteroids[index].yo; + float xo = asteroids[index].xo; + float yo = asteroids[index].yo; // moveto first vertex - _moveto((int)(xo + asteroids[index].vertices[0].x), (int)(yo + asteroids[index].vertices[0].y)); - - for (vertex = 1; vertex < asteroids[index].num_vertices; vertex++) { - _lineto((int)(xo + asteroids[index].vertices[vertex].x), (int)(yo + asteroids[index].vertices[vertex].y)); + for (int startVertex = 0; startVertex < asteroids[index].num_vertices; startVertex++) { + int endVertex = (startVertex + 1) % asteroids[index].num_vertices; + int x1 = (int)(xo + asteroids[index].vertices[startVertex].x); + int y1 = (int)(yo + asteroids[index].vertices[startVertex].y); + int x2 = (int)(xo + asteroids[index].vertices[endVertex].x); + int y2 = (int)(yo + asteroids[index].vertices[endVertex].y); + image_draw_line(image, x1, y1, x2, y2, color); } // end for vertex - // close object - - _lineto((int)(xo + asteroids[index].vertices[0].x), (int)(yo + asteroids[index].vertices[0].y)); - } // end for index } // end Draw_Asteroids @@ -175,12 +156,12 @@ void Translate_Asteroids() { asteroids[index].yo += asteroids[index].y_velocity; // collision detection i.e. bounds check - if (asteroids[index].xo > image.width - 40 || asteroids[index].xo < 40) { + if (asteroids[index].xo > image.width - 20 || asteroids[index].xo < 20) { asteroids[index].x_velocity = -asteroids[index].x_velocity; asteroids[index].xo += asteroids[index].x_velocity; } - if (asteroids[index].yo > image.height - 40 || asteroids[index].yo < 40) { + if (asteroids[index].yo > image.height - 20 || asteroids[index].yo < 20) { asteroids[index].y_velocity = -asteroids[index].y_velocity; asteroids[index].yo += asteroids[index].y_velocity; } diff --git a/src/games/playground/index.tsx b/src/games/playground/index.tsx index 36738da..46f5084 100644 --- a/src/games/playground/index.tsx +++ b/src/games/playground/index.tsx @@ -3,6 +3,6 @@ import awoo from "./awoo.cpp"; export default function main() { const data = awoo.cpptest(window.innerWidth >> 2, window.innerHeight >> 2); - createWasmCanvas(awoo.data, data, awoo.step); - console.log(awoo); + const canvas = createWasmCanvas(awoo.data, data, awoo.step); + console.log(awoo, canvas.width, canvas.height); } \ No newline at end of file