Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Windows x64 support #64

Merged
merged 1 commit into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.bmp
*.dll

# VS code
.vscode/
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,27 +55,26 @@ 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.

<!--
Windows (x64):

Grab prebuilt libraries from all of:
Grab prebuilt libraries from:

- https://buildbot.libsdl.org/sdl-builds/sdl-visualstudio-amd64/
- https://www.libsdl.org/projects/SDL_image/
- https://github.com/libsdl-org/SDL/releases/tag/release-2.28.5
- https://github.com/libsdl-org/SDL_image/releases/tag/release-2.8.1
- https://github.com/libsdl-org/SDL_ttf/releases/tag/release-2.0.18

Take `SDL2.dll`, `SDL2_image.dll` and `SDL2_ttf.dll` from each respectively and
put them into `C:\Windows\System32\`.
-->
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):

```shell
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
Expand Down
35 changes: 29 additions & 6 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -282,7 +282,22 @@ const sdl2Font = Deno.dlopen(getLibraryPath("SDL2_ttf"), {
"parameters": [],
"result": "i32",
},
});
} as const;

let sdl2Image: Deno.DynamicLibrary<typeof SDL2_Image_symbols>,
sdl2Font: Deno.DynamicLibrary<typeof SDL2_TTF_symbols>;

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() {
Expand Down Expand Up @@ -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
{
Expand All @@ -344,7 +359,7 @@ function init() {
}
// TTF_Init
{
sdl2Font.symbols.TTF_Init();
sdl2Font?.symbols.TTF_Init();
}
}

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Loading