Skip to content

Commit

Permalink
Export StructArray and update Readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
smack0007 committed Sep 22, 2024
1 parent 370b996 commit e71d89c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
28 changes: 28 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@ SDL.Init(SDL.InitFlags.VIDEO, {
});
```

## Concepts

- `AllocatableStruct`: is a `Struct` that can be allocated from JavaScript and will be backed by a `Uint8Array`. The
constructor allows all properties to be set in a few ways.

- `OpaqueStruct`: is a `Struct` where no properties are known to JavaScript (C code would also know about the fields)
and can only be created by the native side. The `Struct` can only be passed back to the native side and usally to a
function that will return information related to the `Struct`. The best example of this concept is `SDL.Window`.

- `Pointer<T>`: represents a pointer to something and is explicitly typed as `unknown` by SDL_ts. `Pointer<T>` can only
be returned from functions and will never be created by the JavaScript side. The concept of pointers can't completely
be hidden when interfacing with SDL but SDL_ts attempts to constrain pointers to a type level concept. SDL_ts will
take care of passing pointers to the native side as part of the marshalling process. See `PointerLike<T>`.

- `PointerLike<T>`: is used only as an input to functions and is used to represent anything to which a pointer can be
created for. This includes `Pointer<T>`, `TypedArray`(s), `Struct`(s) and `StructArray`(s).

- `Struct`: the JavaScript representation of structs from the native side. `Struct`(s) are special classes because their
data is either a `Pointer<T>` or a `Uint8Array`. If the `Struct` data is a `Pointer<T>` the `Struct` it was allocated
on the native side and is read only (see `OpaqueStruct`). If the `Struct` data is an instance of `Uint8Array` it was
allocated on the JavaScript side and can be written to as well as read from (see `AllocatableStruct`).

- `StructArray`: Use the `StructArray` class to allocate an array on `Struct`(s). All `Struct`(s) in the the array will
be backed by a shared `Uint8Array` allowing the entire array to be passed to the native side. Normal JavaScript arrays
are not guarenteed to be laid out contiguously in memory. The `Struct` type passed to the constructor of the array
must be an `AllocatableStruct`. All `Struct`(s) passed into the the constructor will have their data copied into the
shared `Uint8Array`.

## Credits

Deno images taken from https://deno.land/artwork.
Expand Down
5 changes: 2 additions & 3 deletions examples/renderer/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SDL } from "SDL_ts";
import { SDL, StructArray } from "SDL_ts";
import { SDL_FUNCTIONS } from "./sdlConfig.ts";
import { ASSETS_PATH } from "../../shared/constants.ts";
import { join } from "@std/path";
import { StructArray } from "../../src/structs.ts";

const WINDOW_WIDTH = 1024;
const WINDOW_HEIGHT = 768;
Expand Down Expand Up @@ -82,7 +81,7 @@ function main(): void {
textureCenter,
SDL.RendererFlip.NONE,
);

SDL.SetRenderDrawColor(renderer, 255, 0, 0, 255);
const rect = new SDL.Rect(100, 100, 200, 400);
SDL.RenderDrawLine(renderer, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
Expand Down
1 change: 1 addition & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * as IMG from "./mod.SDL_image.ts";
export * as TTF from "./mod.SDL_ttf.ts";
export * from "./src/error.ts";
export * from "./src/events.ts";
export * from "./src/structs.ts";
export * from "./src/types.ts";
4 changes: 1 addition & 3 deletions src/_boxes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import {
import { PlatformDataView } from "./_types.ts";
import { sizeof } from "./_utils.ts";

export type BoxValue = TypedNumber | Pointer<unknown>;

type BoxValue = TypedNumber | Pointer<unknown>;
export type BoxValueConstructor<T extends BoxValue> = Constructor<T>;

export type BoxValueFactory<T extends BoxValue> = Factory<T>;

type BoxValueTransformer<T extends BoxValue> = (
Expand Down
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// This file is for types exposed as part of the API.

import Platform from "./_platform.ts";
import { Box, BoxValue } from "./_boxes.ts";
import { PlatformPointer } from "./_types.ts";
import { StructArray } from "./structs.ts";

Expand Down Expand Up @@ -64,9 +63,8 @@ export type Callback = Function;
// deno-lint-ignore ban-types
export type FunctionWithSymbolName = Function & { symbolName: string };

type PointerBoxValue<T> = T extends BoxValue ? Box<T> : never;
type PointerStructArray<T> = T extends AllocatableStruct ? StructArray<T> : never;
export type PointerLike<T> = Pointer<T> | TypedArray | Struct | PointerBoxValue<T> | PointerStructArray<T>;
export type PointerLike<T> = Pointer<T> | TypedArray | Struct | PointerStructArray<T>;

//
// Complex types
Expand Down

0 comments on commit e71d89c

Please sign in to comment.