-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
28b080f
commit b36c4ac
Showing
3 changed files
with
152 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Globals | ||
|
||
## Native | ||
|
||
The main native interface to the Switch hardware is provided by the `Switch` global: | ||
|
||
- [`Switch`](./switch.md) | ||
|
||
## ES2020 | ||
|
||
The QuickJS engine supports ES2020 features, so stuff like `Promise`, `Map`, `Set`, `async` functions, etc. are supported. | ||
For any non-supported features, it is recommended to polyfill those features by configuring a bundler, such as `esbuild`. | ||
|
||
## Web Standards | ||
|
||
Listed below are the officially supported Web interfaces: | ||
|
||
- [`console`](https://developer.mozilla.org/en-US/docs/Web/API/console) | ||
- Only `console.log()` is implemented at this time, and will enable the console renderer when invoked. | ||
- [`setTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/setTimeout) | ||
- [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/setInterval) | ||
- [`clearTimeout`](https://developer.mozilla.org/en-US/docs/Web/API/clearTimeout) | ||
- [`clearInterval`](https://developer.mozilla.org/en-US/docs/Web/API/clearInterval) | ||
- [`FontFace`](https://developer.mozilla.org/en-US/docs/Web/API/FontFace) | ||
- See the [`Switch.fonts`](./switch.md) property for more details. | ||
- [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) | ||
- [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL) | ||
- [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) | ||
- [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event) | ||
- [`UIEvent`](https://developer.mozilla.org/en-US/docs/Web/API/UIEvent) | ||
- [`TouchEvent`](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent) | ||
- [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent) | ||
- Only `utf-8` decoding is built-in. Use an npm module if you need a different encoding. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# `Switch` | ||
|
||
The `Switch` global object contains native interfaces to interact with the Switch hardware. | ||
|
||
## Properties | ||
|
||
#### `Switch.canvas` -> [`Canvas`](./canvas.md) | ||
|
||
Resembles the HTML [`Canvas`](./canvas.md) object which can have draw operations performed on it. | ||
The `width` and `height` properties are set to the Switch screen resolution. | ||
Use `getContext('2d')` to get the [canvas rendering context](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) instance. | ||
|
||
#### `Switch.fonts` -> [`FontFaceSet`](https://developer.mozilla.org/en-US/docs/Web/API/FontFaceSet) | ||
|
||
Contains the available fonts for use on the screen Canvas context. | ||
By default, there is only one font available, which is the system font provided by the Switch operating system. | ||
See the [ttf-fonts](../apps/ttf-font/) application for an example of using custom fonts. | ||
|
||
## Methods | ||
|
||
#### `Switch.cwd()` -> `URL` | ||
|
||
Returns the current working directory of the process, as a `URL` instance. Can be useful to | ||
create other `URL` instances relative to the location of the app's `.nro` file. | ||
|
||
```ts | ||
console.log(Switch.cwd()); | ||
// smdc:/switch/example/ | ||
|
||
console.log(new URL('assets/logo.png', Switch.cwd()); | ||
// smdc:/switch/example/assets/logo.png | ||
``` | ||
#### `Switch.readDirSync(path: string)` -> `string[]` | ||
Returns an array of file names that exist within the directory `path`. | ||
#### `Switch.readFileSync(path: string)` -> `Uint8Array` | ||
Reads the file at path `path` and returns a `Uint8Array` of the file contents. | ||
## Events | ||
The `Switch` global is also an `EventTarget` instance, and may emit the following events: | ||
#### `frame` event | ||
Emitted for every pass of the event loop (at around 60 frames per second). | ||
The nx.js runtime uses this internally to process interactions like timers, button presses and touchscreen events. | ||
Your application code may also use this event to update the UI or render a game frame at the highest frame rate. | ||
```ts | ||
let frameCount = 0; | ||
Switch.addEventListener('frame', () => { | ||
console.log(`frame: ${frameCount++}`); | ||
}); | ||
``` | ||
#### `exit` event | ||
Emitted exactly once, immediately before the application will exit. This event happens after the event loop has ended, | ||
so it is not possible to schedule any asynchronous work during this event. Can be used to clean up any resources | ||
or to save some state to the filesystem (synchronously). Note that this event will _not_ be emitted if the app is exited | ||
by hitting the Home button. | ||
```ts | ||
Switch.addEventListener('exit', () => { | ||
const logFile = new URL('debug.log', Switch.cwd()); | ||
Switch.writeFileSync(logFile, `Exited at ${new Date()}`); | ||
}); | ||
``` | ||
#### `buttondown` event | ||
Emitted when any button on the controller has been pressed down since the previous frame. | ||
The `event.detail` property describes all buttons which are currently being pressed. | ||
You may use `Button` enum from the `nxjs-constants` module with a bitwise `&` to check for a specific button. | ||
```ts | ||
import { Button } from 'nxjs-constants'; | ||
|
||
Switch.addEventListener('buttondown', (event) => { | ||
if (event.detail & Button.A) { | ||
console.log('A button pressed'); | ||
} | ||
}); | ||
``` | ||
#### `buttonup` event | ||
Emitted when any button on the controller has been released since the previous frame. | ||
The `event.detail` property describes all buttons which are currently being pressed. | ||
You may use `Button` enum from the `nxjs-constants` module with a bitwise `&` to check for a specific button. | ||
```ts | ||
import { Button } from 'nxjs-constants'; | ||
|
||
Switch.addEventListener('buttonup', (event) => { | ||
if (event.detail & Button.A) { | ||
console.log('A button was released'); | ||
} | ||
}); | ||
``` | ||
#### `touchstart`, `touchmove`, `touchend` events | ||
Emitted while interacting with the touchscreen. These events follow the Web [Touch events](https://developer.mozilla.org/en-US/docs/Web/API/Touch_events) standard. | ||
```ts | ||
Switch.addEventListener('touchmove', (e) => { | ||
for (const touch of e.changedTouches) { | ||
console.log(`${touch.identifier}: ${touch.screenX} x ${touch.screenY}`); | ||
} | ||
}); | ||
``` |