https://github.com/rblsb/kharec
Simple g2 command recorder
https://github.com/rblsb/kharec
Last synced: 5 months ago
JSON representation
Simple g2 command recorder
- Host: GitHub
- URL: https://github.com/rblsb/kharec
- Owner: RblSb
- Created: 2019-07-28T12:34:14.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-28T16:16:56.000Z (almost 7 years ago)
- Last Synced: 2025-02-16T17:56:12.488Z (over 1 year ago)
- Language: Haxe
- Homepage:
- Size: 13.7 KB
- Stars: 5
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## KhaRec
Simple way to write graphics2 commands to file and play it.
Only for fun, html5 and g2 on g4.
### Implementation details
`Graphics2` is shadowed with almost same code, but has `Recorder.drawImage(img, x, y)` call in `drawImage` function, same for other commands from `rec.Command` (no font/drawString currently). `Recorder` does nothing if not activated in user code. After activation it start writing some commands into `FastFloat` array and then saves it to a file with compression. `Player` just reads that array and run a record in the loop.
### Usage
First of all, add [pako.min.js](https://raw.githubusercontent.com/hamaluik/haxe-pako/master/libs/pako.min.js) to `Assets` and to [index.html](https://github.com/Kode/Kha/wiki/HTML5#custom-indexhtml-and-js-libraries). 30s 60fps record with pako compression can have 900 kb size for one million graphic commands. Don't ask how fat it can be without compression.
Lets add code in game for Recorder/Player activation in `onKeyDown` event:
```haxe
if (key == Zero) {
// Start/Stop recording on `0` key
if (!rec.Recorder.isActive) rec.Recorder.enable();
else rec.Recorder.save();
return;
}
if (key == Nine) {
// Start/Stop playing from file on `9` key
// Recorder writes all renderTargets as one image,
// so it supports only one renderTarget
// We can send it to Player
if (!rec.Player.isActive) rec.Player.enable(optionalAtlasImage);
else rec.Player.disable();
return;
}
```
Code for `Player` in render loop:
```haxe
if (rec.Player.isActive) {
rec.Player.render(framebuffer.g2);
return;
}
```
And code for `Recorder` in render loop:
```haxe
final g = framebuffer.g2;
rec.Recorder.frameBegin(g);
// actual render code
// g.begin();
// ...
// g.end();
rec.Recorder.frameEnd();
```
That's all folks! Beware, `Recorder` writes image ids to data array, so any new image in `Assets` will break recordings. Can be avoided with `ImageName => IntId` map in data file, but this is just concept, feel free to hack or contribute.