From a74758b81414d1d9524201bbb1f9b034a159e55c Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Sat, 23 Dec 2023 19:04:01 +0530 Subject: [PATCH] Enable Windows x64 support --- .gitignore | 1 + README.md | 15 +++++++-------- mod.ts | 35 +++++++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 9f932f0..9440612 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.bmp +*.dll # VS code .vscode/ diff --git a/README.md b/README.md index d1d9753..7e80639 100644 --- a/README.md +++ b/README.md @@ -55,18 +55,19 @@ sudo ln -s /opt/homebrew/lib/libSDL2_ttf.dylib /usr/local/lib/ Additionally, you can set `DENO_SDL2_PATH` to point to the directory where these three libraries are located. - +put them into cwd or `C:\Windows\System32\`. + +> Windows is expected to work but I do not accept Windows-specifc issues. Open a +> PR directly with a fix. Linux (x64): @@ -74,8 +75,6 @@ Linux (x64): sudo apt install libsdl2-dev libsdl2-image-dev libsdl2-ttf-dev ``` -Windows is not supported, it might work but I can't maintain it. - ### security you need `--allow-ffi --unstable` to use SDL2. `deno_sdl2` needs access to diff --git a/mod.ts b/mod.ts index 5ad95cc..101df61 100644 --- a/mod.ts +++ b/mod.ts @@ -242,7 +242,7 @@ const sdl2 = Deno.dlopen(getLibraryPath("SDL2"), { }, }); -const sdl2Image = Deno.dlopen(getLibraryPath("SDL2_image"), { +const SDL2_Image_symbols = { "IMG_Init": { "parameters": ["u32"], "result": "u32", @@ -251,9 +251,9 @@ const sdl2Image = Deno.dlopen(getLibraryPath("SDL2_image"), { "parameters": ["buffer"], "result": "pointer", }, -}); +} as const; -const sdl2Font = Deno.dlopen(getLibraryPath("SDL2_ttf"), { +const SDL2_TTF_symbols = { "TTF_Init": { "parameters": [], "result": "u32", @@ -282,7 +282,22 @@ const sdl2Font = Deno.dlopen(getLibraryPath("SDL2_ttf"), { "parameters": [], "result": "i32", }, -}); +} as const; + +let sdl2Image: Deno.DynamicLibrary, + sdl2Font: Deno.DynamicLibrary; + +try { + sdl2Image = Deno.dlopen(getLibraryPath("SDL2_image"), SDL2_Image_symbols); +} catch (_e) { + console.log("SDL2_image not loaded. Some features will not be available."); +} + +try { + sdl2Font = Deno.dlopen(getLibraryPath("SDL2_ttf"), SDL2_TTF_symbols); +} catch (_e) { + console.log("SDL2_ttf not loaded. Some features will not be available."); +} let context_alive = false; function init() { @@ -331,7 +346,7 @@ function init() { // IMG_Init { // TIF = 4, WEBP = 8 - sdl2Image.symbols.IMG_Init(1 | 2); // png and jpg + sdl2Image?.symbols.IMG_Init(1 | 2); // png and jpg } // SDL_INIT_TTF { @@ -344,7 +359,7 @@ function init() { } // TTF_Init { - sdl2Font.symbols.TTF_Init(); + sdl2Font?.symbols.TTF_Init(); } } @@ -927,6 +942,10 @@ export class Surface { * @returns a Surface */ static fromFile(path: string): Surface { + if (!sdl2Image) { + throw new Error("SDL2_image was not loaded"); + } + const raw = sdl2Image.symbols.IMG_Load(asCString(path)); if (raw === null) { throwSDLError(); @@ -938,6 +957,10 @@ export class Surface { * @returns a Surface */ static loadBmp(path: string): Surface { + if (!sdl2Image) { + throw new Error("SDL2_image was not loaded"); + } + const raw = sdl2.symbols.SDL_LoadBMP_RW(asCString(path)); if (raw === null) { throwSDLError();