-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgba.ts
More file actions
131 lines (110 loc) · 2.43 KB
/
Copy pathgba.ts
File metadata and controls
131 lines (110 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { decode, GBACore } from "./deps.ts";
import { BIOS_BIN } from "./bios.js";
const BIOS = decode(BIOS_BIN);
export enum KeyCode {
A = 0,
B = 1,
SELECT = 2,
START = 3,
RIGHT = 4,
LEFT = 5,
UP = 6,
DOWN = 7,
R = 8,
L = 9,
}
Object.assign(globalThis, {
objwinActive: {},
tileRow: {},
addr: {},
});
export interface GameInfo {
title: string;
code: string;
maker: string;
saveType: string;
}
function queueFrame(fn: CallableFunction) {
setTimeout(() => {
fn();
queueFrame(fn);
}, 8);
}
export class GBA {
static WIDTH = 240;
static HEIGHT = 160;
core: any;
constructor(public ex: CallableFunction) {
this.core = new GBACore();
this.core.setBios(BIOS.buffer);
this.core.setCanvasMemory();
}
loadROM(rom: Uint8Array) {
this.core.setRom(rom.buffer);
}
getPixels(): Uint8Array | undefined {
return this.core.context?.pixelData?.data;
}
pressKey(key: KeyCode, time = 100) {
this.core.keypad.press(key, time);
}
keyDown(key: KeyCode) {
this.core.keypad.keydown(key);
}
keyUp(key: KeyCode) {
this.core.keypad.keyup(key);
}
run() {
if (this.core.interval) {
return; // Already running
}
const self = this.core;
this.core.paused = false;
this.core.audio.pause(false);
const runFunc = () => {
try {
if (self.paused) {
return;
} else {
queueFrame(runFunc);
}
self.advanceFrame();
this.ex();
} catch (exception) {
self.ERROR(exception);
if (exception.stack) {
self.logStackTrace(exception.stack.split("\n"));
}
throw exception;
}
}
queueFrame(runFunc);
}
pause() {
if (!this.core.paused) this.core.paused = true;
else this.run();
}
loadSaveData(data: Uint8Array) {
this.core.setSavedata(data.buffer);
}
getSaveData(): Uint8Array | undefined {
const data = this.core.mmu?.save?.buffer;
if (!data) return;
else return new Uint8Array(data);
}
getGameInfo(): GameInfo | undefined {
const info = this.core.mmu?.cart;
if (!info) return;
else {
return {
title: info.title,
code: info.code,
maker: info.maker,
saveType: info.saveType,
};
}
}
stop() {
this.core.reset();
}
}