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

Initial jsr.io release. #33

Merged
merged 7 commits into from
Jul 25, 2024
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

"editor.tabSize": 2,

"[josn,ts]": {
"[josn,typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
14 changes: 14 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@
},
"problemMatcher": ["$deno"]
},
{
"label": "Run Version",
"group": {
"kind": "none",
"isDefault": true
},
"type": "deno",
"command": "task",
"args": ["run:version"],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": ["$deno"]
},
{
"type": "deno",
"command": "task",
Expand Down
2 changes: 1 addition & 1 deletion License.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIT License
Copyright (c) 2022 Zachary Snow
Copyright (c) 2024 Zachary Snow

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
23 changes: 9 additions & 14 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ Currently working via [deno](https://deno.land/) on Windows, Linux, and mac OS u

## Non Goals

- Creating a higher level API or Game Framework. This library's only purpose is to expose the SDL API to deno.
- Creating a higher level API or Game Framework. This library's only purpose is to expose the SDL API to TypeScript.

## Versioning

This library's version number will match the SDL major and minor version against which it is currently tested. The patch
number will be specifici to this library. I.E.

## Getting Started

Expand Down Expand Up @@ -58,19 +63,9 @@ brew install sdl2 sdl2_image sdl2_ttf

### Creating a Project

To aid in creating a new project which uses `SDL_ts` there is an `init.ts` script to aid in getting your project set up:

```bash
deno run --allow-net --allow-read=/path/to/project --allow-write=/path/to/project --import-map https://deno.land/x/[email protected]/imports.deno.json https://deno.land/x/[email protected]/init.ts /path/to/project
```

Replace `/path/to/project` with the desired project path. After that the new project can be started via a deno task:

```bash
deno task start
```

You should be presented with a window titled "SDL_ts".
Located in the directory [examples/getting-started](examples/getting-started/) are a minimal set of files needed to get
a project up and running. Copy these files onto your computer and start the project with `deno task start`. You should
see a window with `SDL_ts` as the title.

### Loading only required functions

Expand Down
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

11 changes: 10 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "@smack0007/sdl-ts",
"version": "2.30.1",
"exports": "./mod.ts",
"compilerOptions": {
"lib": ["deno.window", "deno.unstable"]
},
Expand All @@ -14,7 +17,12 @@
"exclude": ["no-inferrable-types"]
}
},
"importMap": "imports.json",
"imports": {
"SDL_ts": "./mod.ts",
"@std/assert": "jsr:@std/[email protected]",
"@std/colors": "jsr:@std/[email protected]/colors",
"@std/path": "jsr:@std/[email protected]"
},
"tasks": {
"build": "cd ./scripts && deno run --allow-read --allow-run ./build.ts",
"build:ci": "deno task build --ci",
Expand All @@ -28,6 +36,7 @@
"run:play-wav": "cd ./examples/play-wav && SDL_TS_ENV_DIR=$INIT_CWD deno run --unstable-ffi --allow-env --allow-ffi --allow-read=../.. $DENO_FLAGS ./main.ts",
"run:renderer": "cd ./examples/renderer && SDL_TS_ENV_DIR=$INIT_CWD deno run --unstable-ffi --allow-env --allow-ffi --allow-read=../.. $DENO_FLAGS ./main.ts",
"run:same-game": "cd ./examples/same-game && SDL_TS_ENV_DIR=$INIT_CWD deno run --unstable-ffi --allow-env --allow-ffi --allow-read=../.. $DENO_FLAGS ./main.ts",
"run:version": "cd ./examples/version && SDL_TS_ENV_DIR=$INIT_CWD deno run --unstable-ffi --allow-env --allow-ffi --allow-read=../.. $DENO_FLAGS ./main.ts",
"test": "deno test --unstable-ffi --allow-ffi"
}
}
5 changes: 0 additions & 5 deletions deps.ts

This file was deleted.

80 changes: 32 additions & 48 deletions examples/doom-fire/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IMG, SDL } from "SDL_ts";
import { IMG_FUNCTIONS, SDL_FUNCTIONS } from "./sdlConfig.ts";
import { ASSETS_PATH } from "../../shared/constants.ts";
import { path } from "../../deps.ts";
import { join } from "@std/path";

const WINDOW_WIDTH = 1024;
const WINDOW_HEIGHT = 768;
Expand All @@ -11,43 +11,13 @@ const FIRE_WIDTH = 1024;
const FIRE_HEIGHT = 120;

const FIRE_COLORS = [
0x00000000,
0xC0070707,
0xC007071F,
0xC0070F2F,
0xC0070F47,
0xC0071757,
0xC0071F67,
0xC0071F77,
0xC007278F,
0xC0072F9F,
0xC0073FAF,
0xC00747BF,
0xC00747C7,
0xC0074FDF,
0xC00757DF,
0xC00757DF,
0xC0075FD7,
0xC00F67D7,
0xC00F6FCF,
0xC00F77CF,
0xC00F7FCF,
0xC01787CF,
0xC01787C7,
0xC0178FC7,
0xC01F97C7,
0xC01F9FBF,
0xC01F9FBF,
0xC027A7BF,
0xC027A7BF,
0xC02FAFBF,
0xC02FAFB7,
0xC02FB7B7,
0xC037B7B7,
0xC06FCFCF,
0xC09FDFDF,
0xC0C7EFEF,
0xC0FFFFFF,
0x00000000, 0xc0070707, 0xc007071f, 0xc0070f2f, 0xc0070f47, 0xc0071757,
0xc0071f67, 0xc0071f77, 0xc007278f, 0xc0072f9f, 0xc0073faf, 0xc00747bf,
0xc00747c7, 0xc0074fdf, 0xc00757df, 0xc00757df, 0xc0075fd7, 0xc00f67d7,
0xc00f6fcf, 0xc00f77cf, 0xc00f7fcf, 0xc01787cf, 0xc01787c7, 0xc0178fc7,
0xc01f97c7, 0xc01f9fbf, 0xc01f9fbf, 0xc027a7bf, 0xc027a7bf, 0xc02fafbf,
0xc02fafb7, 0xc02fb7b7, 0xc037b7b7, 0xc06fcfcf, 0xc09fdfdf, 0xc0c7efef,
0xc0ffffff,
];

function main(): number {
Expand All @@ -61,15 +31,17 @@ function main(): number {
return 1;
}

console.info(`SDL_image Version: ${version.major}.${version.minor}.${version.patch}`);
console.info(
`SDL_image Version: ${version.major}.${version.minor}.${version.patch}`
);

const window = SDL.CreateWindow(
"Doom Fire",
SDL.WindowPos.CENTERED,
SDL.WindowPos.CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL.WindowFlags.SHOWN,
SDL.WindowFlags.SHOWN
);

if (window == null) {
Expand All @@ -84,14 +56,20 @@ function main(): number {
return 1;
}

const denoSurfaceUnoptimized = IMG.Load(path.join(ASSETS_PATH, "jurassicDeno.png"));
const denoSurfaceUnoptimized = IMG.Load(
join(ASSETS_PATH, "jurassicDeno.png")
);

if (denoSurfaceUnoptimized == null) {
console.error("Failed to load jurassicDeno.png.");
return 1;
}

const denoSurface = SDL.ConvertSurface(denoSurfaceUnoptimized, frontBuffer.format, 0);
const denoSurface = SDL.ConvertSurface(
denoSurfaceUnoptimized,
frontBuffer.format,
0
);

if (denoSurface == null) {
console.error("Failed to convert surface format of jurassicDeno.png.");
Expand All @@ -107,23 +85,29 @@ function main(): number {
FIRE_HEIGHT,
32,
4 * FIRE_WIDTH,
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000,
0x000000ff,
0x0000ff00,
0x00ff0000,
0xff000000
);

if (fireSurface == null) {
console.error("Failed to create fireSurface.");
return 1;
}

const flamesRect = new SDL.Rect(0, HALF_WINDOW_HEIGHT, frontBuffer.w, HALF_WINDOW_HEIGHT);
const flamesRect = new SDL.Rect(
0,
HALF_WINDOW_HEIGHT,
frontBuffer.w,
HALF_WINDOW_HEIGHT
);

firePixels.fill(0x00000000);

for (let x = 0; x < FIRE_WIDTH; x += 1) {
firePixels[(FIRE_HEIGHT - 1) * FIRE_WIDTH + x] = FIRE_COLORS[FIRE_COLORS.length - 1];
firePixels[(FIRE_HEIGHT - 1) * FIRE_WIDTH + x] =
FIRE_COLORS[FIRE_COLORS.length - 1];
}

let lastFrame = SDL.GetTicks64();
Expand Down
6 changes: 6 additions & 0 deletions examples/getting-started/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"imports": { "@smack0007/sdl-ts": "jsr:@smack0007/sdl-ts" },
"tasks": {
"start": "deno run --unstable-ffi --allow-env --allow-ffi --allow-read=. ./main.ts"
}
}
16 changes: 13 additions & 3 deletions tools/init/main.ts → examples/getting-started/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SDL } from "SDL_ts";
import { SDL } from "@smack0007/sdl-ts";

function main(): number {
SDL.Init(SDL.InitFlags.VIDEO);
Expand All @@ -9,14 +9,24 @@ function main(): number {
SDL.WindowPos.CENTERED,
1024,
768,
SDL.WindowFlags.SHOWN | SDL.WindowFlags.RESIZABLE,
SDL.WindowFlags.SHOWN | SDL.WindowFlags.RESIZABLE
);

if (window == null) {
console.error(`Failed to create window: ${SDL.GetError()}`);
Deno.exit(1);
return 1;
}

const renderer = SDL.CreateRenderer(window, 0, SDL.RendererFlags.ACCELERATED);

if (renderer === null) {
console.error(`Failed to create window: ${SDL.GetError()}`);
return 1;
}

SDL.RenderClear(renderer);
SDL.RenderPresent(renderer);

const event = new SDL.Event();

let done = false;
Expand Down
5 changes: 0 additions & 5 deletions examples/hello-world/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ SDL.Init(SDL.InitFlags.VIDEO, { functions: SDL_FUNCTIONS });

console.info("SDL Initialized.");

const version = new SDL.version();
SDL.GetVersion(version);
console.info(`SDL Version: ${version.major}.${version.minor}.${version.patch}`);
console.info(`SDL Revision: ${SDL.GetRevision()}`);

console.info(`${SDL.GetSystemRAM()} MB RAM available.`);

const createWindowResult = SDL.CreateWindow(
Expand Down
2 changes: 0 additions & 2 deletions examples/hello-world/sdlConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ export const SDL_FUNCTIONS = [
SDL.DestroyWindow,
SDL.FillRect,
SDL.GetError,
SDL.GetRevision,
SDL.GetScancodeName,
SDL.GetSystemRAM,
SDL.GetVersion,
SDL.GetWindowSurface,
SDL.Init,
SDL.MapRGB,
Expand Down
6 changes: 3 additions & 3 deletions examples/play-wav/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Adapted from https://gigi.nullneuron.net/gigilabs/playing-a-wav-file-using-sdl2/

import { Box, Pointer, SDL, U32, U8, u32, u8 } from "SDL_ts";
import { Box, Pointer, SDL, U32, u32, u8 } from "SDL_ts";
import { SDL_FUNCTIONS } from "./sdlConfig.ts";
import { ASSETS_PATH } from "../../shared/constants.ts";
import { path } from "../../deps.ts";
import { join } from "@std/path";

const main = (): number => {
if (SDL.Init(SDL.InitFlags.AUDIO, { functions: SDL_FUNCTIONS }) < 0) {
Expand All @@ -18,7 +18,7 @@ const main = (): number => {

if (
SDL.LoadWav(
path.join(ASSETS_PATH, "powerup.wav"),
join(ASSETS_PATH, "powerup.wav"),
wavSpec,
wavBufferBox,
wavLengthBox
Expand Down
4 changes: 2 additions & 2 deletions examples/renderer/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Box, BoxArray, Int, int, Memory, Pointer, SDL } from "SDL_ts";
import { SDL_FUNCTIONS } from "./sdlConfig.ts";
import { ASSETS_PATH } from "../../shared/constants.ts";
import { path } from "../../deps.ts";
import { join } from "@std/path";

const WINDOW_WIDTH = 1024;
const WINDOW_HEIGHT = 768;
Expand Down Expand Up @@ -46,7 +46,7 @@ function main(): number {
SDL.RenderPresent(renderer);
SDL.RenderFlush(renderer);

const denoSurface = SDL.LoadBMP(path.join(ASSETS_PATH, "jurassicDeno.bmp"));
const denoSurface = SDL.LoadBMP(join(ASSETS_PATH, "jurassicDeno.bmp"));

if (denoSurface == null) {
console.error("Failed to load jurassicDeno.bmp.");
Expand Down
3 changes: 3 additions & 0 deletions examples/renderer/sdlConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { SDL } from "SDL_ts";
export const SDL_FUNCTIONS = [
SDL.CreateTextureFromSurface,
SDL.CreateWindowAndRenderer,
SDL.DestroyRenderer,
SDL.DestroyTexture,
SDL.DestroyWindow,
SDL.Delay,
SDL.FreeSurface,
SDL.GetError,
Expand Down
Loading
Loading