From 71fca9cfa7cccc51a42e73287954c9f3d34ce426 Mon Sep 17 00:00:00 2001 From: Xant Date: Sun, 13 Sep 2009 04:06:18 +0200 Subject: [PATCH] allow to get the default screen instance Instead of being forced to always specify the type The screen_js constructor now behaves properly (reusing already initialized screens properly ...so also the ones not created by the javascript, where jsobj is still NULL while initilized is true) --- scripts/javascript/examples/star.js | 10 ++++----- src/screen_js.cpp | 35 ++++++++++++++++------------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/scripts/javascript/examples/star.js b/scripts/javascript/examples/star.js index aecef378..4736ecc0 100644 --- a/scripts/javascript/examples/star.js +++ b/scripts/javascript/examples/star.js @@ -3,8 +3,6 @@ // it also shows the usage of keyboard controller // press 'q' to quit while running -W = get_width(); -H = get_height(); x = 100; y = 100; @@ -15,9 +13,11 @@ PI = 3.141592654; c = PI * 2; o = -PI / 2; -scr = new Screen("sdl"); -scr.init(W,H); -add_screen(scr); +scr = new Screen(); +W = get_width(); +H = get_height(); +//scr.init(W,H); +//add_screen(scr); function drawStar(lay, s_mul, s2_mul) { s = s_mul / 0.383; diff --git a/src/screen_js.cpp b/src/screen_js.cpp index a5ae8a76..d56dc76d 100644 --- a/src/screen_js.cpp +++ b/src/screen_js.cpp @@ -38,17 +38,20 @@ JSFunctionSpec screen_methods[] = { JS(screen_constructor) { func("%s",__PRETTY_FUNCTION__); - char *type; - ViewPort *screen; + char *type = NULL; + ViewPort *screen = NULL; - if(argc < 1) JS_ERROR("missing argument"); + if(argc >= 1) { + // a specific screen type has been requested + JS_ARG_STRING(type,0); + screen = Factory::get_instance( "Screen", type ); + } else { + // no screen type has been specified, return the default one + screen = Factory::get_instance( "Screen" ); + } - // recognize the type and instantiate the screen using the factory - JS_ARG_STRING(type,0); - - screen = Factory::get_instance( "Screen", type ); if(!screen) { - error("%s: cannot create a Screen of type %s",__FUNCTION__,type); + error("%s: cannot obtain current Screen",__FUNCTION__); JS_ReportErrorNumber(cx, JSFreej_GetErrorMessage, NULL, JSSMSG_FJ_CANT_CREATE, type, strerror(errno)); @@ -56,14 +59,16 @@ JS(screen_constructor) { } // if already existing, return the singleton - if(screen->initialized) obj = screen->jsobj; + if(screen->jsobj) { + obj = screen->jsobj; + } else { + if (!JS_SetPrivate(cx, obj, (void *) screen)) + JS_ERROR("internal error setting private value"); - if (!JS_SetPrivate(cx, obj, (void *) screen)) - JS_ERROR("internal error setting private value"); - - // assign the real js object - screen->jsclass = &screen_class; - screen->jsobj = obj; + // assign the real js object + screen->jsclass = &screen_class; + screen->jsobj = obj; + } *rval = OBJECT_TO_JSVAL(obj); return JS_TRUE; }