Refactoring rendering/window logic into their respective classes, ordering them better for abstractions

This commit is contained in:
underscorediscovery
2013-06-27 05:45:53 -02:30
parent 3467104504
commit 0ba5e04e6f
3 changed files with 111 additions and 99 deletions

View File

@@ -24,9 +24,9 @@ class LiME {
//nme specifics
//the handle to the window from nme
public var mainframe_handle : Dynamic;
public var window_handle : Dynamic;
//the handle to the nme stage
public var stage_handle : Dynamic;
public var view_handle : Dynamic;
//flags
@@ -46,65 +46,27 @@ class LiME {
_debug(':: lime :: initializing -');
_debug(':: lime :: Creating window at ' + config.width + 'x' + config.height);
#if lime_native
nme_create_main_frame(
on_main_frame_created,
config.width, //width
config.height, //height
Window.RESIZABLE |
Window.HARDWARE |
Window.VSYNC |
Window.HW_AA |
Window.HW_AA_HIRES |
Window.ALLOW_SHADERS |
Window.REQUIRE_SHADERS |
Window.DEPTH_BUFFER |
Window.STENCIL_BUFFER , //flags
config.title, //title
null //icon
); //nme_create_main_frame
#end
#if lime_html5
var handle = null;
untyped {
js.Browser.document.body.onload = function (_) {
var handle = js.Browser.document.getElementById('lime_canvas');
on_main_frame_created( handle );
}
}
#end //lime_html5
//Create our window handler class,
//this will handle initialization
window = new WindowHandler( this );
window.startup();
} //init
//This gets called once the create_main_frame call inside new()
//comes back with our window
private function on_main_frame_created( handle:Dynamic ) {
public function on_window_ready( handle:Dynamic ) {
//Store a reference to the handle
mainframe_handle = handle;
#if lime_native
stage_handle = nme_get_frame_stage( mainframe_handle );
//Set the stage handler for NME to send us events
nme_set_stage_handler(stage_handle, on_stage_event, config.width, config.height);
#end
window_handle = handle;
//do any on ready initialization for the window
window.ready();
//Create our input message handler class
input = new InputHandler( this );
input.startup();
//Create our window message handler class
window = new WindowHandler( this );
window.startup();
input.startup();
//Create our render message handler class
render = new RenderHandler( this );
@@ -120,12 +82,11 @@ class LiME {
host.ready(this);
}
#if lime_html5
//start the run loop
js.Browser.window.requestAnimationFrame(on_update);
#end
//if any post ready things that need
//to happen, do it here.
window.post_ready();
} //on_main_frame_created
} //on_window_ready
public function shutdown() {
@@ -145,13 +106,6 @@ class LiME {
input.shutdown();
window.shutdown();
//Ok kill it!
//Order matters for events coming
//when things below are set to null.
#if lime_native
nme_close();
#end
//Flag it
has_shutdown = true;
@@ -164,7 +118,7 @@ class LiME {
window = null;
}
private function on_stage_event( _event:Dynamic ) : Dynamic {
public function on_lime_event( _event:Dynamic ) : Dynamic {
var event_type:Int = Std.int(Reflect.field(_event, "type"));
@@ -272,7 +226,7 @@ class LiME {
return null;
} //on_stage_event
} //on_lime_event
//Handle system/window messages
public function on_syswm(ev:Dynamic) {
@@ -287,24 +241,19 @@ class LiME {
//Called when updated by the nme/sdl runtime
public function on_update(_event) {
#if lime_html5
js.Browser.window.requestAnimationFrame(on_update);
#end
_debug('on_update ' + Timer.stamp(), true, true);
if(!has_shutdown) {
if(host.update != null) {
host.update();
}
do_render(_event);
}
#if lime_html5
return true;
#end
return true;
} //on_update
//Render the window
@@ -320,29 +269,12 @@ class LiME {
window.invalidated = false;
}
render.on_render();
#if lime_native
nme_render_stage( stage_handle );
#end //lime_native
render.render();
} //do_render
//Noisy stuff
#if lime_native
//import nme_library functions
private static var nme_render_stage = Libs.load("nme","nme_render_stage", 1);
private static var nme_set_stage_handler = Libs.load("nme","nme_set_stage_handler", 4);
private static var nme_get_frame_stage = Libs.load("nme","nme_get_frame_stage", 1);
private static var nme_close = Libs.load("nme","nme_close", 0);
private static var nme_create_main_frame = Libs.load("nme","nme_create_main_frame", -1);
#end //lime_native
//temporary debugging with verbosity options
public var log : Bool = false;

View File

@@ -39,7 +39,7 @@ class RenderHandler {
direct_renderer_handle = nme_direct_renderer_create();
//Add this to the main stage, so it will render
nme_doc_add_child( lib.stage_handle, direct_renderer_handle );
nme_doc_add_child( lib.view_handle, direct_renderer_handle );
//Set this handle to the real view with a render function
nme_direct_renderer_set( direct_renderer_handle, on_render );
@@ -48,7 +48,7 @@ class RenderHandler {
#if lime_html5
direct_renderer_handle = lib.mainframe_handle.getContext('webgl');
direct_renderer_handle = lib.window_handle.getContext('webgl');
lime.gl.GL.nmeContext = direct_renderer_handle;
#end //lime_html5
@@ -57,19 +57,36 @@ class RenderHandler {
lib._debug(':: lime :: \t RenderHandler Initialized.');
}
public function shutdown() {
lib._debug(':: lime :: \t RenderHandler shut down.');
}
public function on_render() {
public function render() {
#if lime_html5
on_render();
js.Browser.window.requestAnimationFrame(lib.on_update);
return true;
#end
#if lime_native
nme_render_stage( lib.view_handle );
#end //lime_native
}
public function on_render() {
if( lib.host.render != null ) {
lib.host.render();
}
}
//nme functions
#if lime_native
private static var nme_render_stage = Libs.load("nme","nme_render_stage", 1);
private static var nme_doc_add_child = Libs.load("nme","nme_doc_add_child", 2);
private static var nme_direct_renderer_create = Libs.load("nme","nme_direct_renderer_create", 0);
private static var nme_direct_renderer_set = Libs.load("nme","nme_direct_renderer_set", 2);

View File

@@ -2,6 +2,7 @@ package lime;
import lime.LiME;
import lime.utils.Libs;
import lime.Constants;
class WindowHandler {
@@ -14,15 +15,75 @@ class WindowHandler {
public var invalidated : Bool = false;
public function startup() {
lib._debug(':: lime :: \t WindowHandler Initialized.');
lib._debug(':: lime :: \t WindowHandler Initializing...');
#if lime_native
nme_create_main_frame(
lib.on_window_ready,
lib.config.width, //width
lib.config.height, //height
Window.RESIZABLE |
Window.HARDWARE |
Window.VSYNC |
Window.HW_AA |
Window.HW_AA_HIRES |
Window.ALLOW_SHADERS |
Window.REQUIRE_SHADERS |
Window.DEPTH_BUFFER |
Window.STENCIL_BUFFER , //flags
lib.config.title, //title
null //icon
); //nme_create_main_frame
#end
#if lime_html5
var handle = null;
untyped {
js.Browser.document.body.onload = function (_) {
var handle = js.Browser.document.getElementById('lime_canvas');
lib.on_window_ready( handle );
}
}
#end //lime_html5
}
public function shutdown() {
#if lime_native
nme_close();
#end
lib._debug(':: lime :: \t WindowHandler shut down.');
}
public function process() {
public function ready() {
//todo, neaten
#if lime_native
lib.view_handle = nme_get_frame_stage( lib.window_handle );
#end //lime_native
}
public function post_ready() {
#if lime_native
//Set the stage handler for NME to send us events
nme_set_stage_handler(lib.view_handle, lib.on_lime_event, lib.config.width, lib.config.height);
#end
#if lime_html5
//start the run loop on html5, as there is no
//window creation callback there.
js.Browser.window.requestAnimationFrame(lib.on_update);
#end
lib._debug(':: lime :: \t WindowHandler Initialized.');
}
//Invalidate the window (forcing a redraw on next update)
@@ -82,10 +143,12 @@ class WindowHandler {
//nme functions
private static var nme_set_stage_handler = Libs.load("nme","nme_set_stage_handler", 4);
private static var nme_get_frame_stage = Libs.load("nme","nme_get_frame_stage", 1);
private static var nme_create_main_frame = Libs.load("nme","nme_create_main_frame", -1);
private static var nme_pause_animation = Libs.load("nme","nme_pause_animation", 0);
private static var nme_resume_animation = Libs.load("nme","nme_resume_animation", 0);
private static var nme_terminate = Libs.load("nme","nme_terminate", 0);
private static var nme_close = Libs.load("nme","nme_close", 0);
}