|
| 1 | +# react-gbajs |
| 2 | + |
| 3 | +> GBA emulator on your React project - easy and powerful to use! |
| 4 | +
|
| 5 | +Just three steps to set it up ✨ |
| 6 | + |
| 7 | +1 - Apply `GbaProvider` |
| 8 | +```jsx |
| 9 | +import { GbaProvider } from 'react-gbajs' |
| 10 | + |
| 11 | +const App = () => ( |
| 12 | + <GbaProvider> |
| 13 | + ... |
| 14 | + </GbaProvider> |
| 15 | +) |
| 16 | +``` |
| 17 | + |
| 18 | +2 - Get `play` function from `GbaContext` and call it with the game ROM |
| 19 | +```jsx |
| 20 | +import { useContext } from 'react' |
| 21 | +import { GbaContext } from 'react-gbajs' |
| 22 | + |
| 23 | +const Component = () => { |
| 24 | + const { |
| 25 | + play, |
| 26 | + } = useContext(GbaContext) |
| 27 | + |
| 28 | + const onSomeHandle = () => { |
| 29 | + const gameRom = getGameRom() |
| 30 | + play(gameRom) |
| 31 | + } |
| 32 | + |
| 33 | + return ( |
| 34 | + <SomeComponent startGame={onSomeHandle}> |
| 35 | + ) |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +3 - And render the emulator using the default export |
| 40 | + |
| 41 | +```js |
| 42 | +import ReactGbaJs from 'react-gbajs' |
| 43 | + |
| 44 | +const Emulator = () => ( |
| 45 | + <ReactGbaJs volume={0} /> |
| 46 | +) |
| 47 | +``` |
| 48 | + |
| 49 | +Check a full example in [`/sample`](sample). |
| 50 | + |
| 51 | +## How it works |
| 52 | + |
| 53 | +This package is a React wrapper for [GBA.js](https://github.com/endrift/gbajs). |
| 54 | + |
| 55 | +`GBA.js` is vendored, with new features over the original version: |
| 56 | +- save and restore state |
| 57 | +- freeze address with a given value |
| 58 | + |
| 59 | +## Who is using |
| 60 | + |
| 61 | +<table align="center"> |
| 62 | + <tr> |
| 63 | + <td align="center" width="50%"> |
| 64 | + <img src="https://i.imgur.com/QxCoVPh.png"><br /> |
| 65 | + <strong><a href="https://github.com/macabeus/klo-gba.js">klo-gba.js</a></strong>, level editor for <strong>Klonoa: Empire of Dreams<strong> |
| 66 | + </td> |
| 67 | + <td align="center" width="50%"> |
| 68 | + <i><a href="https://github.com/macabeus/react-gbajs/issues/new">Add your project here</a></i> |
| 69 | + </td> |
| 70 | + </tr> |
| 71 | +</table> |
| 72 | + |
| 73 | +## Features |
| 74 | + |
| 75 | +`GbaContext` exports the following properties: |
| 76 | + |
| 77 | +#### `play(newRomBufferMemory)` |
| 78 | + |
| 79 | +Use this method to play the emulator, passing as its argument the game's ROM. Every time this function is called, it creates a new `GameBoyAdvance` instance, so all game progress is lost. |
| 80 | + |
| 81 | +#### `saveState()` |
| 82 | + |
| 83 | +Return the serializable state of the emulator. |
| 84 | + |
| 85 | +#### `updateState({ restoreState, newRomBuffer })` |
| 86 | + |
| 87 | +Use this function passing `restoreState` to restore to a previous state saved using `saveState`. You can also pass `newRomBuffer` to reset the emulator and then load a new ROM. If both are passed, the emulator will be reset with the new ROM and then restored to the given state. |
| 88 | + |
| 89 | +#### `addFreezeAddress({ address: number, size: 8 | 16 | 32, value: number })` |
| 90 | + |
| 91 | +Freeze an address with the given value. |
| 92 | + |
| 93 | +#### `removeFreezeAddress(address: number)` |
| 94 | + |
| 95 | +Remove the freeze value on the given address. |
| 96 | + |
| 97 | +#### `frozenAddresses()` |
| 98 | + |
| 99 | +Return the list of the addresses frozen in the following format: |
| 100 | + |
| 101 | +```ts |
| 102 | +{ |
| 103 | + [address in number]: { size: 8 | 16 | 32, value: number } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +# Contribution |
| 108 | + |
| 109 | +1 - Clone this repository |
| 110 | + |
| 111 | +``` |
| 112 | +> git clone git@github.com:macabeus/react-gbajs.git |
| 113 | +> cd react-gbajs |
| 114 | +``` |
| 115 | + |
| 116 | +2 - Build and watch the project |
| 117 | + |
| 118 | +``` |
| 119 | +> npm run start |
| 120 | +``` |
| 121 | + |
| 122 | +3 - In another terminal window, go to the [`/sample`](sample) project, then build and watch it |
| 123 | + |
| 124 | +``` |
| 125 | +> cd sample |
| 126 | +> npm run start |
| 127 | +``` |
| 128 | + |
| 129 | +Finished! Now you can develop the package and test it in real-time 🎉 |
0 commit comments