| 1 |
/* Copyright (c) the Xinf contributors. |
|---|
| 2 |
see http://xinf.org/copyright for license. */ |
|---|
| 3 |
|
|---|
| 4 |
package xinf.erno; |
|---|
| 5 |
|
|---|
| 6 |
import xinf.event.SimpleEventDispatcher; |
|---|
| 7 |
import xinf.event.EventKind; |
|---|
| 8 |
import xinf.event.FrameEvent; |
|---|
| 9 |
import xinf.erno.Renderer; |
|---|
| 10 |
|
|---|
| 11 |
/** |
|---|
| 12 |
DOCME: out of date! |
|---|
| 13 |
|
|---|
| 14 |
The Runtime class has static functions to request the global |
|---|
| 15 |
Runtime instance (a singleton) and it's associated <a href="Renderer.html">Renderer</a>. |
|---|
| 16 |
<p> |
|---|
| 17 |
The Runtime represents the runtime environment (Flash, JavaScript, Xinfinity), |
|---|
| 18 |
there is only one global runtime object for a running Xinf application, |
|---|
| 19 |
an instance of a class deriving from xinf.erno.Runtime. The specific runtimes |
|---|
| 20 |
implement some 'abstract' functions defined here in their individual ways. |
|---|
| 21 |
</p> |
|---|
| 22 |
<p> |
|---|
| 23 |
The Runtime singleton (Runtime.runtime) is an EventDispatcher that dispatches |
|---|
| 24 |
all global low-level user-interface events. In Xinfony, some of these are |
|---|
| 25 |
listened for and dispatched to the corresponding target object (see |
|---|
| 26 |
<a href="../ony/Manager.html">xinf.ony.Manager</a>). |
|---|
| 27 |
For other events, you will have to register at the runtime (in particular, |
|---|
| 28 |
this regards MOUSE_UP, MOUSE_MOVED, KEY_UP, KEY_DOWN and STAGE_SCALED). |
|---|
| 29 |
You can register listeners either at the [Runtime.runtime] member or |
|---|
| 30 |
using the static [addEventListener] function. |
|---|
| 31 |
</p> |
|---|
| 32 |
<p> |
|---|
| 33 |
The Runtime is initialized automatically when you instantate an |
|---|
| 34 |
<a href="../ony/Application.html">xinf.ony.Application</a> object, or do anything |
|---|
| 35 |
with Runtime.runtime. Only if you want to use Xinferno directly you |
|---|
| 36 |
should probably care about initialization once at the beginning of your |
|---|
| 37 |
application. |
|---|
| 38 |
</p> |
|---|
| 39 |
**/ |
|---|
| 40 |
class Runtime extends SimpleEventDispatcher { |
|---|
| 41 |
|
|---|
| 42 |
/** |
|---|
| 43 |
A reference to the global Runtime singleton. |
|---|
| 44 |
If it is not initialized yet, requesting this will intialize |
|---|
| 45 |
the runtime. |
|---|
| 46 |
**/ |
|---|
| 47 |
static public var runtime(getRuntime,null):Runtime; |
|---|
| 48 |
static private var _runtime:Runtime; |
|---|
| 49 |
|
|---|
| 50 |
/** |
|---|
| 51 |
A reference to the Renderer associated to the global Runtime singleton. |
|---|
| 52 |
If it is not initialized yet, requesting this will intialize |
|---|
| 53 |
the runtime. |
|---|
| 54 |
**/ |
|---|
| 55 |
static public var renderer(getRenderer,null):Renderer; |
|---|
| 56 |
static private var _renderer:Renderer; |
|---|
| 57 |
|
|---|
| 58 |
/* global functions */ |
|---|
| 59 |
static private function getRuntime() :Runtime { |
|---|
| 60 |
if( _runtime==null ) initRuntime(); |
|---|
| 61 |
return _runtime; |
|---|
| 62 |
} |
|---|
| 63 |
|
|---|
| 64 |
static private function getRenderer() :Renderer { |
|---|
| 65 |
if( _renderer==null ) initRuntime(); |
|---|
| 66 |
return _renderer; |
|---|
| 67 |
} |
|---|
| 68 |
|
|---|
| 69 |
static private function initRuntime() :Runtime { |
|---|
| 70 |
#if neko |
|---|
| 71 |
_runtime = new xinf.inity.XinfinityRuntime(); |
|---|
| 72 |
// dynamically load renderer |
|---|
| 73 |
if( true ) { |
|---|
| 74 |
// _renderer = new xinf.inity.GLRenderer(); |
|---|
| 75 |
_renderer = new xinf.inity.GLVGRenderer(); |
|---|
| 76 |
} else { |
|---|
| 77 |
/* experimental. */ |
|---|
| 78 |
var name = "xinfinity-gl0"; |
|---|
| 79 |
try { |
|---|
| 80 |
var haxeLibPath = switch( neko.Sys.systemName() ) { |
|---|
| 81 |
case "Windows": |
|---|
| 82 |
neko.Sys.getEnv("HAXEPATH")+"\\lib\\"; |
|---|
| 83 |
default: |
|---|
| 84 |
neko.io.File.getContent( neko.Sys.getEnv("HOME")+"/.haxelib" ); |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
var libPath = haxeLibPath+"/"+name+"/"; |
|---|
| 88 |
var version = neko.io.File.getContent( libPath+".current" ); |
|---|
| 89 |
libPath += version.split(".").join(",")+"/ndll/"+neko.Sys.systemName()+"/"; |
|---|
| 90 |
|
|---|
| 91 |
// at least for windoze: add libPath to PATH, for loading DLLs |
|---|
| 92 |
//neko.Sys.putEnv("PATH",neko.Sys.getEnv("PATH")+":"+libPath ); |
|---|
| 93 |
|
|---|
| 94 |
// (try to) load the module |
|---|
| 95 |
var rClass:Dynamic; |
|---|
| 96 |
var loader = neko.vm.Loader.local(); |
|---|
| 97 |
loader.addPath( libPath ); |
|---|
| 98 |
rClass = loader.loadModule(name).getExports().get("Renderer__impl"); |
|---|
| 99 |
|
|---|
| 100 |
if( rClass==null ) throw("module does not export Renderer__impl"); |
|---|
| 101 |
|
|---|
| 102 |
_renderer = rClass.createRenderer(320,240); |
|---|
| 103 |
trace("Loaded Renderer "+name+" "+version ); |
|---|
| 104 |
} catch(e:Dynamic) { |
|---|
| 105 |
throw("unable to load Xinfinity Renderer '"+name+"': "+e ); |
|---|
| 106 |
} |
|---|
| 107 |
} |
|---|
| 108 |
#elseif js |
|---|
| 109 |
_renderer = new xinf.erno.js.JSRenderer(); |
|---|
| 110 |
_runtime = new xinf.erno.js.JSRuntime(); |
|---|
| 111 |
#elseif flash |
|---|
| 112 |
_renderer = new xinf.erno.flash9.Flash9Renderer(); |
|---|
| 113 |
_runtime = new xinf.erno.flash9.Flash9Runtime(); |
|---|
| 114 |
#end |
|---|
| 115 |
|
|---|
| 116 |
if( runtime==null ) throw("unable to create runtime environment"); |
|---|
| 117 |
|
|---|
| 118 |
return runtime; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
/** |
|---|
| 122 |
add an event listener to the Runtime singleton. This is a convenience |
|---|
| 123 |
function that has the same effect as Runtime.runtime.addEventListener. |
|---|
| 124 |
**/ |
|---|
| 125 |
static public function addEventListener<T>( type :EventKind<T>, h :T->Void ) :T->Void { |
|---|
| 126 |
return runtime.addEventListener(type,h); |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
/** |
|---|
| 130 |
remove an event listener from the Runtime singleton. This is a convenience |
|---|
| 131 |
function that has the same effect as Runtime.runtime.removeEventListener. |
|---|
| 132 |
**/ |
|---|
| 133 |
static public function removeEventListener<T>( type :EventKind<T>, h :T->Void ) :Bool { |
|---|
| 134 |
return runtime.removeEventListener(type,h); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
/** |
|---|
| 138 |
return a newly allocated numeric ID |
|---|
| 139 |
for use with <a href="Renderer.html">xinf.erno.Renderer</a>. |
|---|
| 140 |
The default implementation will throw an exception. |
|---|
| 141 |
**/ |
|---|
| 142 |
public function getNextId() :Int { |
|---|
| 143 |
throw("unimplemented"); |
|---|
| 144 |
return -1; |
|---|
| 145 |
} |
|---|
| 146 |
|
|---|
| 147 |
/** |
|---|
| 148 |
return the runtime's default Root <a href="NativeContainer.html">NativeContainer</a>. |
|---|
| 149 |
The default implementation will throw an exception. |
|---|
| 150 |
**/ |
|---|
| 151 |
public function getDefaultRoot() :NativeContainer { |
|---|
| 152 |
throw("unimplemented"); |
|---|
| 153 |
return null; |
|---|
| 154 |
} |
|---|
| 155 |
|
|---|
| 156 |
/** |
|---|
| 157 |
start the runtime main loop if such exists. |
|---|
| 158 |
From your application, you should call Runtime.runtime.run() once, at the end of your main() |
|---|
| 159 |
(<a href="../ony/Application.html">xinf.ony.Application</a>.run() does this for you). |
|---|
| 160 |
The function might return instantly, when the application quits, |
|---|
| 161 |
or never, depending on the runtime environment. |
|---|
| 162 |
The default implementation will throw an exception. |
|---|
| 163 |
**/ |
|---|
| 164 |
public function run() :Void { |
|---|
| 165 |
throw("unimplemented"); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
/** |
|---|
| 169 |
signal to the Runtime that some content in the display hierarchy has changed. |
|---|
| 170 |
This will trigger re-rendering of the default Root object. There is no need |
|---|
| 171 |
to ever call this if you use Xinfony (the Manager will take care of this). |
|---|
| 172 |
The default implementation does nothing. |
|---|
| 173 |
**/ |
|---|
| 174 |
public function changed() :Void { |
|---|
| 175 |
} |
|---|
| 176 |
|
|---|
| 177 |
public function setBackgroundColor( r:Float, g:Float, b:Float, ?a:Float ) :Void { |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
public function setFramerate( rate:Float ) :Void { |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
public function getFramerate() :Float { |
|---|
| 184 |
return 30; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
public function getMeasuredFramerate() :Float { |
|---|
| 188 |
return -1.; |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
} |
|---|