-
Notifications
You must be signed in to change notification settings - Fork 0
/
Console.as
338 lines (297 loc) · 10.7 KB
/
Console.as
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* =BEGIN MIT LICENSE
*
* The MIT License (MIT)
*
* Copyright (c) 2014 The CrossBridge Team
* https://github.com/crossbridge-community
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* =END MIT LICENSE
*
*/
package com.adobe.flascc {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.display.Stage3D;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display3D.Context3D;
import flash.display3D.Context3DRenderMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.SampleDataEvent;
import flash.geom.Rectangle;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.text.TextField;
import flash.utils.ByteArray;
import com.adobe.flascc.*;
import com.adobe.flascc.vfs.*;
import GLS3D.GLAPI;
public class Console extends Sprite implements ISpecialFile {
// CORE
private var _frameCounter:TextField;
private var _textField:TextField;
private var _mainloopTickPtr:int;
private var _inputContainer;
// STAGE3D
private var _stage3D:Stage3D;
private var _context3D:Context3D;
// BLITTING
private var _bitmapData:BitmapData;
private var _bitmap:Bitmap;
private var _bitmapRectangle:Rectangle;
// VFS
private var _fs:InMemoryBackingStore;
// FLAGS
private var _isRendered:Boolean;
private var _isInitialized:Boolean;
private var _useStage3D:Boolean = true;
private var _useBlitting:Boolean = !_useStage3D;
// INPUT
private var _keyBA:ByteArray = new ByteArray()
private var _keyX:int = 0;
private var _keyY:int = 0;
// SOUND
private var _soundBuffer:ByteArray;
private var _soundPos:int = 0;
private var _sound:Sound = null;
private var _soundChannel:SoundChannel;
// DISPLAY
private var _frameCount:int;
private var _vglttyargs:Vector.<int> = new Vector.<int>();
private var _vbuffer:int;
private var _vgl_mx:int;
private var _vgl_my:int;
private var _kp:int;
// ISpecialFile
public var exitHook:Function;
// CONSTS
private const EMPTY_VECTOR:Vector.<int> = new Vector.<int>();
// CONSTRUCTOR
public function Console(container:DisplayObjectContainer = null) {
CModule.rootSprite = container ? container.root : this;
if (CModule.runningAsWorker()) {
return;
}
if (container) {
container.addChild(this);
onAdded(null);
} else {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
}
}
// EVENT HANDLERS
private function onAdded(e:Event):void {
log("onAdded");
// STAGE
stage.frameRate = 60;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
// STAGE3d
_stage3D = stage.stage3Ds[0];
_stage3D.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
_stage3D.requestContext3D(Context3DRenderMode.AUTO);
}
private function createChildren():void {
log("createChildren");
// ROOT
_inputContainer = new Sprite()
addChild(_inputContainer);
// BLITTING
if(_useBlitting) {
_bitmapData = new BitmapData(800, 600, false);
_bitmap = new Bitmap(_bitmapData);
_bitmapRectangle = new Rectangle(0, 0, _bitmapData.width, _bitmapData.height);
_bitmapData.fillRect(_bitmapData.rect, 0);
_inputContainer.addChild(_bitmap);
}
// LOGGING
_textField = new TextField;
_textField.multiline = true;
_textField.selectable = false;
_textField.width = 800;
_textField.height = 600;
_textField.textColor = 0x666666;
_inputContainer.addChild(_textField);
_textField.border = true;
// FRAME COUNTER
_frameCounter = new TextField;
_frameCounter.width = 40;
_frameCounter.height = 20;
_frameCounter.textColor = 0x666666;
_frameCounter.x = 760;
_inputContainer.addChild(_frameCounter);
}
private function onContextCreated(event:Event):void {
log("onContextCreated");
createChildren();
if(_useStage3D) {
_context3D = _stage3D.context3D
_context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 2, true /*enableDepthAndStencil*/)
_context3D.enableErrorChecking = true;
log("Stage3D context: " + _context3D.driverInfo);
if (_context3D.driverInfo.indexOf("Software") != -1) {
log("Software mode unsupported...");
return;
}
GLAPI.init(_context3D, null, stage);
var gl:GLAPI = GLAPI.instance;
gl.context.clear(0, 0, 0);
gl.context.present();
}
// file system
CModule.vfs.console = this;
_fs = new myfs();
CModule.vfs.addBackingStore(_fs, null);
// starting c code
CModule.startAsync(this, new <String>["/main.swf"]);
// VIDEO
_vbuffer = CModule.getPublicSymbol("__avm2_vgl_argb_buffer");
_vgl_mx = CModule.getPublicSymbol("vgl_cur_mx");
_vgl_my = CModule.getPublicSymbol("vgl_cur_my");
// INPUT
stage.addEventListener(KeyboardEvent.KEY_DOWN, onBufferKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onBufferKeyUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onBufferMouseMove);
stage.addEventListener (Event.ENTER_FRAME, onFrameEnter);
stage.addEventListener(Event.RESIZE, onStageResized);
//onFrameEnter(null);
}
private function initialize():void {
log("initialize");
_isInitialized = true;
_mainloopTickPtr = CModule.getPublicSymbol("_Z4drawv");
_sound = new Sound();
_sound.addEventListener( SampleDataEvent.SAMPLE_DATA, onSoundData);
_soundPos = 0;
_soundChannel = _sound.play()
_soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
}
private function onFrameEnter(event:Event):void {
if (!_isInitialized) {
initialize();
}
// CModule.serviceUIRequests();
CModule.write32(_vgl_mx, _keyX);
CModule.write32(_vgl_my, _keyY);
if(_useStage3D) {
GLAPI.instance.context.clear(1, 1, 1);
CModule.callI(_mainloopTickPtr, EMPTY_VECTOR);
GLAPI.instance.context.present();
}
if(_useBlitting) {
var ram:ByteArray = CModule.ram;
ram.position = CModule.read32(_vbuffer)
if (ram.position != 0) {
_frameCount++;
_frameCounter.text = String(_frameCount);
_bitmapData.setPixels(_bitmapRectangle, ram)
}
}
}
private function onStageResized(event:Event):void {
log("onStageResized");
// need to reconfigure back buffer
_context3D.configureBackBuffer(stage.stageWidth, stage.stageHeight, 2, true /*enableDepthAndStencil*/)
}
private function onError(event:Event):void {
log(event.toString());
}
// Keyboard
public function onBufferMouseMove(event:MouseEvent) {
event.stopPropagation();
_keyX = event.stageX;
_keyY = event.stageY;
}
public function onBufferKeyDown(event:KeyboardEvent) {
_keyBA.writeByte(int(event.keyCode & 0x7F));
}
public function onBufferKeyUp(event:KeyboardEvent) {
_keyBA.writeByte(int(event.keyCode | 0x80));
}
// Sound
public function onSoundComplete(e:Event):void {
_soundChannel.removeEventListener(Event.SOUND_COMPLETE, onSoundComplete)
_soundChannel = _sound.play()
_soundChannel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete)
}
public function onSoundData(event:SampleDataEvent):void {
event.data.length = 0
_soundBuffer = event.data
if(_frameCount == 0)
return;
/* if(engineticksoundptr == 0)
engineticksoundptr = CModule.getPublicSymbol("engineTickSound")
if(engineticksoundptr)
CModule.callI(engineticksoundptr, emptyArgs)*/
}
// ISpecialFile
public function exit(code:int):Boolean {
// default to unhandled
return exitHook ? exitHook(code) : false;
}
public function write(fd:int, bufPtr:int, nbyte:int, errnoPtr:int):int {
var str:String = CModule.readString(bufPtr, nbyte)
log(str)
return nbyte
}
/**
* libVGL expects to be able to read Keyboard input from
* file descriptor zero using normal C IO.
*/
public function read(fd:int, bufPtr:int, nbyte:int, errnoPtr:int):int {
if (fd == 0 && nbyte == 1) {
_keyBA.position = _kp++
if (_keyBA.bytesAvailable) {
CModule.write8(bufPtr, _keyBA.readUnsignedByte())
return 1
} else {
_keyBA.length = 0
_keyBA.position = 0
_kp = 0
}
}
return 0
}
public function fcntl(fd:int, com:int, data:int, errnoPtr:int):int {
return 0
}
public function ioctl(fd:int, com:int, data:int, errnoPtr:int):int {
_vglttyargs[0] = fd
_vglttyargs[1] = com
_vglttyargs[2] = data
_vglttyargs[3] = errnoPtr
return CModule.callI(CModule.getPublicSymbol("vglttyioctl"), _vglttyargs);
}
// HELPERS
private function log(message:String):void {
trace(message);
if (_textField) {
_textField.appendText(message + "\n");
_textField.scrollV = _textField.maxScrollV;
}
}
}
}