diff --git a/scripts/javascript/examples/processing_test.js b/scripts/javascript/examples/processing_test.js deleted file mode 100644 index 7fd14f89..00000000 --- a/scripts/javascript/examples/processing_test.js +++ /dev/null @@ -1,20 +0,0 @@ -// simple loader of a processing script - -include("processing.js"); - -scr = new Screen("sdl"); -if (!scr.is_initialized()) { - scr.init(800,600); - add_screen(scr); -} - -script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/ventaglio.pjs"); -//script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/simple.processing"); -//script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/recursion.pjs"); -//script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/sinewave.pjs"); -//script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/colorwheel.pjs"); -//script = read_file("/home/jaromil/devel/freej/scripts/javascript/examples/setupdraw.pjs"); - - -Processing(script); - diff --git a/scripts/javascript/examples/setupdraw.pjs b/scripts/javascript/examples/setupdraw.pjs deleted file mode 100644 index 347a309d..00000000 --- a/scripts/javascript/examples/setupdraw.pjs +++ /dev/null @@ -1,26 +0,0 @@ -// All Examples Written by Casey Reas and Ben Fry -// unless otherwise stated. -// The statements in the setup() function -// execute once when the program begins -void setup() -{ - size(200, 200); // Size should be the first statement - stroke(255); // Set line drawing color to white - frameRate(30); -} - -float y = 100; - -// The statements in draw() are executed until the -// program is stopped. Each statement is executed in -// sequence and after the last line is read, the first -// line is executed again. -void draw() -{ - background(0); // Set the background to black - y = y - 1; - if (y < 0) { y = height; } - line(0, y, width, y); - echo("y = " + y); - -} diff --git a/scripts/processing/BIG_BANG.pde b/scripts/processing/BIG_BANG.pde new file mode 100644 index 00000000..0440b31a --- /dev/null +++ b/scripts/processing/BIG_BANG.pde @@ -0,0 +1,55 @@ +/* BIG BANG(2.0) +por Bernardo Silva C. +*/ +float margen, sp,x,y,d,m,M;//variables + + +void setup(){ + margen = 1; + sp =150; + size(500, 500);//tamaño + smooth();//suavizado + stroke(255,100);//linea + fill(0,100);//relleno negro con transparencia + strokeWeight(3.7);//grosor de la linea + m=0.000005;// aumenta en ese valor + background(0);//fondo negro + + +} + + +void draw(){ + if(M<0||M>=0.004){//si M es menor que 0 o mayor que 0.0003 + m=-m;/* m es -m.entonces matematicamente hablando cuando sea + postivo y sea mayor o igual que 0.0003,m se volvera negativo,por + lo cual regrasa a comprimirse el big bang + */ + } + fill(0,15);//transparencia para logra el difuminado + rect(0,0,height,width);// cuadrado como fondo + + M+=m;//variable en la que incrementa(escala) + + + translate(height/2,width/2);//posición de la figura + for(y=margen ; y < height; y += sp/2){ + for(x = margen; x < width; x += sp/12){ + //esta es la distancia + d = 100+sqrt((mouseX - x)*(mouseX - x) + (mouseY - y)*(mouseY - y)); + scale(d * M);//se increneta la distancia en m + quad(x,y,x,y,x,d,x,d);//cuadrilateros + quad(x/12,y+6,x,d,x,d,x*2,d); + rotate(100);//la figura se rota en torno a su posicion en x e y + + } + } + } + + +void keyPressed(){//si se aprieta el boton(f) se graba la foto + saveFrame("BIG BANG.jpg"); + println("fotograma grabado"); + + +} diff --git a/scripts/javascript/examples/recursion.pjs b/scripts/processing/recursion.pde similarity index 100% rename from scripts/javascript/examples/recursion.pjs rename to scripts/processing/recursion.pde diff --git a/scripts/javascript/examples/sinewave.pjs b/scripts/processing/sinewave.pde similarity index 98% rename from scripts/javascript/examples/sinewave.pjs rename to scripts/processing/sinewave.pde index 3aff3db9..aef332f1 100644 --- a/scripts/javascript/examples/sinewave.pjs +++ b/scripts/processing/sinewave.pde @@ -10,7 +10,7 @@ float dx; // Value for incrementing X, to be calculated as a fun float[] yvalues; // Using an array to store height values for the wave (not entirely necessary) void setup() { - size(200,200); + size(400,300); frameRate(30); colorMode(RGB,255,255,255,100); smooth(); diff --git a/scripts/javascript/examples/ventaglio.pjs b/scripts/processing/ventaglio.pde similarity index 100% rename from scripts/javascript/examples/ventaglio.pjs rename to scripts/processing/ventaglio.pde diff --git a/src/context_js.cpp b/src/context_js.cpp index cfed0080..0ca18524 100644 --- a/src/context_js.cpp +++ b/src/context_js.cpp @@ -696,45 +696,21 @@ JS(entry_select) { JS(include_javascript_file) { func("%u:%s:%s",__LINE__,__FILE__,__FUNCTION__); - char *jscript; - const char *debian_path = "share/doc/freej/scripts/javascript/lib"; - char temp[512]; - FILE *fd; if(argc<1) JS_ERROR("missing argument"); + + char *jscript; jscript = js_get_string(argv[0]); + JsParser *js = (JsParser *)JS_GetContextPrivate(cx); - snprintf(temp,512,"%s",jscript); - fd = fopen(temp,"r"); - if(!fd) { - snprintf(temp,511,"%s/%s/%s",PREFIX,debian_path,jscript); - fd = fopen(temp,"r"); - if(!fd) { - error("included file %s not found", jscript); - error("locations checked: current and %s/%s", - PREFIX,debian_path); - error("javascript include('%s') failed", jscript); - - return JS_FALSE; - } - } - - fclose(fd); - - if (js->open(cx, obj, temp) == 0) { - // all errors already reported, - // js->open talks too much - error("JS include('%s') failed", jscript); - return JS_FALSE; - } - - func("JS: included %s", jscript); - + if( ! js->include(jscript) ) + error("javascript include not found: \"%s\"",jscript); + // if its the first script loaded, save it as main one // this is then used in reset to return at beginning stage - if(global_environment->main_javascript[0] == 0x0) - memcpy(global_environment->main_javascript, temp, 512); + // if(global_environment->main_javascript[0] == 0x0) + // memcpy(global_environment->main_javascript, temp, 512); return JS_TRUE; } diff --git a/src/freej.cpp b/src/freej.cpp index 4b4d7efb..37f5973c 100644 --- a/src/freej.cpp +++ b/src/freej.cpp @@ -70,10 +70,11 @@ static const char *help = " . -g experimental opengl engine! (better pow(2) res as 256x256)\n" #endif " . -j execute a javascript file\n" +" . -p execute a processing script (experimental)\n" " .\n"; // we use only getopt, no _long -static const char *short_options = "-hvD:gas:S:nj:cgf:F"; +static const char *short_options = "-hvD:gas:S:nj:p:cgf:F"; /* this is the global FreeJ context */ Context *freej = NULL; @@ -89,6 +90,7 @@ int width = 400; int height = 300; int magn = 0; char javascript[512]; // script filename +char processing[512]; // script filename int fps = 25; @@ -102,10 +104,12 @@ bool opengl = false; void cmdline(int argc, char **argv) { int res, optlen; + FILE *fd; // tmp fd for checks /* initializing defaults */ char *p = layer_files; javascript[0] = 0; + processing[0] = 0; debug_level = 0; @@ -185,7 +189,6 @@ void cmdline(int argc, char **argv) { break; case 'j': - FILE *fd; fd = fopen(optarg,"r"); if(!fd) { error("can't open JS file '%s': %s", optarg, strerror(errno)); @@ -198,6 +201,16 @@ void cmdline(int argc, char **argv) { } break; + case 'p': + fd = fopen(optarg,"r"); + if(!fd) { + error("processing script file not found '%s': %s", optarg, strerror(errno)); + } else { + snprintf(processing,512,"%s",optarg); + fclose(fd); + } + break; + #ifdef WITH_OPENGL case 'g': opengl = true; @@ -345,6 +358,21 @@ int main (int argc, char **argv) { } else freej->interactive = true; } + /* execute processing */ + if( processing[0] ) { + freej->interactive = false; + + if( freej->js->include("processing.js") ) { + // correctly included our extra library + + char tmp[1024]; + snprintf(tmp,1023,"script = read_file(\"%s\");Processing(script);", processing); + freej->js->parse(tmp); + } + + if(freej->quit) exit(1); + else freej->interactive = true; + } diff --git a/src/include/jsparser.h b/src/include/jsparser.h index fd544633..b1e0fdf6 100644 --- a/src/include/jsparser.h +++ b/src/include/jsparser.h @@ -47,9 +47,11 @@ class JsParser { public: JsParser(Context *_env); ~JsParser(); + int include(const char* jscript); ///< include javascript libraries from known path (current or PREFIX) int open(const char* script_file); int open(JSContext *cx, JSObject *obj, const char* script_file); int use(JSContext *cx, JSObject *obj, const char* script_file); + int parse(const char *command); void stop(); void gc(); diff --git a/src/jsparser.cpp b/src/jsparser.cpp index 044e4478..56f00789 100644 --- a/src/jsparser.cpp +++ b/src/jsparser.cpp @@ -384,6 +384,43 @@ void JsParser::init_class(JSContext *cx, JSObject *obj) { return; } +int JsParser::include(const char* jscript) { + func("%u:%s:%s",__LINE__,__FILE__,__FUNCTION__); + + const char *debian_path = "share/doc/freej/scripts/javascript/lib"; + int res = 0; + char temp[512]; + FILE *fd; + + snprintf(temp,512,"%s",jscript); + fd = fopen(temp,"r"); + if(!fd) { + snprintf(temp,511,"%s/%s/%s",PREFIX,debian_path,jscript); + fd = fopen(temp,"r"); + if(!fd) { + error("included file %s not found", jscript); + error("locations checked: current and %s/%s", + PREFIX,debian_path); + error("javascript include('%s') failed", jscript); + + return res; + } + } + + fclose(fd); + + res = this->open(temp); + if (!res) { + // all errors already reported, + // js->open talks a lot + error("JS include('%s') failed", jscript); + return res; + } + + func("JS: included %s", jscript); + return res; +} + /* return lines read, or 0 on error */ int JsParser::open(const char* script_file) { return open(global_context, global_object, script_file);