Skip to content

Commit

Permalink
Add some initial docs
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 25, 2023
1 parent 28b080f commit b36c4ac
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
**nx.js** is a framework that enables the development of Nintendo Switch
homebrew applications using JavaScript. Powered by the [QuickJS][] engine,
nx.js provides a streamlined environment for writing homebrew applications
on the Nintendo Switch console.
for the Nintendo Switch console.

With nx.js, developers can leverage their JavaScript skills and tools to
create engaging and interactive experiences for the Nintendo Switch platform.
Expand All @@ -12,7 +12,7 @@ high-level JavaScript API that simplifies the development process.

nx.js is designed with Web standards in mind, so familiar APIs like
`setTimeout()`, `fetch()`, `new URL()`, `Canvas` and much more are
supported. So if you are familar with web development then you should feel
supported. If you are familar with web development then you should feel
right at home.

## Features
Expand All @@ -21,7 +21,7 @@ right at home.
- **High-Level API**: Benefit from a high-level JavaScript API designed specifically for the Nintendo Switch platform, providing easy access to console-specific features and functionality.
- **Input Handling**: Capture and process user input with ease, including buttons, touch screen, and motion controls, to create engaging gameplay experiences.
- **Graphics and UI**: Create visually appealing and interactive user interfaces using the web [`Canvas`](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API) API.
- **Audio Support**: Integrate audio playback and sound effects into your applications using the provided web [`Audio`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio) API.
- **Audio Support**: Integrate audio playback and sound effects into your applications using the web [`Audio`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLAudioElement/Audio) API.
- **No Compilation Necessary**: Just drop a `.js` file with the name matching the nx.js `.nro` file onto your SD card.

## Getting Started
Expand All @@ -35,7 +35,7 @@ right at home.
1. Launch the app from the homebrew loader.
1. Profit!

See the [API docs](./api.md) for further details, and check out the [`apps`](./apps) directory for examples.
See the [API docs](./api/globals.md) for further details, and check out the [`apps`](./apps) directory for examples.

## Contributing

Expand Down
33 changes: 33 additions & 0 deletions api/globals.md
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.
115 changes: 115 additions & 0 deletions api/switch.md
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}`);
}
});
```

0 comments on commit b36c4ac

Please sign in to comment.