refactoring of garbage collection

testing that multiple scripts can be included from a main one
removed js garbage collection in controllers (was called after script load)
included JSON as a standard library (it works!)
modified example scripts to avoid namespace clash
This commit is contained in:
Jaromil
2009-05-16 17:00:37 +02:00
parent cb15d73d7c
commit a190f3f4e2
7 changed files with 578 additions and 62 deletions

View File

@@ -43,29 +43,29 @@ width=1024; height=768;
width=800; height=800;
width=400; height=300;
geo = new GeometryLayer();
geo.activate(true);
add_layer(geo);
cyno_geo = new GeometryLayer();
cyno_geo.activate(true);
add_layer(cyno_geo);
trigger = new TriggerController();
register_controller(trigger);
trigger.frame = function() { Draw(); };
cyno_trigger = new TriggerController();
register_controller(cyno_trigger);
cyno_trigger.frame = function() { Draw(); };
kbd = new KeyboardController();
register_controller(kbd);
kbd.released_q = function() { quit(); }
kbd.released_x = function() { Draw(); }
cyno_kbd = new KeyboardController();
register_controller(cyno_kbd);
cyno_kbd.released_q = function() { quit(); }
cyno_kbd.released_x = function() { Draw(); }
kbd.released_p = function() {
cyno_kbd.released_p = function() {
echo("ok, I shot myself now");
try{rem_controller(trigger);}catch(e){echo("tg nope: "+e);}
try{rem_layer(geo);}catch(e){echo("la nope: "+e);}
try{rem_controller(cyno_trigger);}catch(e){echo("tg nope: "+e);}
try{rem_layer(cyno_geo);}catch(e){echo("la nope: "+e);}
try{rem_controller(mc);}catch(e){echo("mc nope: "+e);}
try{rem_controller(kbd);}catch(e){echo("kb nope: "+e);}
delete geo;
try{rem_controller(cyno_kbd);}catch(e){echo("kb nope: "+e);}
delete cyno_geo;
delete mc;
delete kbd;
delete trigger;
delete cyno_kbd;
delete cyno_trigger;
gc(); // kbd-js not cleared within this call!
echo("Bullet arrived.");
}
@@ -132,7 +132,7 @@ function Draw() {
for (j=0; j<10; j++) {
if ((iterations > 0) && (++i >= iterations)) {
i = 0; // clearScreen
geo.rectangle_fill(0,0,width,height,0x000000FF);
cyno_geo.rectangle_fill(0,0,width,height,0x000000FF);
}
paint();
}
@@ -193,22 +193,22 @@ function paint() {
/* Draw the shadow */
if (elevation > 0)
geo.rectangle_fill(curX + elevation, curY + elevation,
cyno_geo.rectangle_fill(curX + elevation, curY + elevation,
curX + elevation + curWidth,
curY + elevation + curHeight,
0x00000060);
/* Draw the edge */
if (shadowWidth > 0)
geo.rectangle_fill(curX + shadowWidth, curY + shadowWidth,
cyno_geo.rectangle_fill(curX + shadowWidth, curY + shadowWidth,
curX + shadowWidth + curWidth,
curY + shadowWidth + curHeight,
0x000000ff);
geo.rectangle_fill(curX, curY, curX+curWidth, curY+curHeight, fg_gc)
cyno_geo.rectangle_fill(curX, curY, curX+curWidth, curY+curHeight, fg_gc)
/* Draw a 1-pixel black border around the rectangle */
geo.rectangle(curX, curY, curX+curWidth, curY+curHeight, 0x00000000);
cyno_geo.rectangle(curX, curY, curX+curWidth, curY+curHeight, 0x00000000);
}
}
}

View File

@@ -0,0 +1,31 @@
include("../lib/json2.js");
echo("JSON library included");
files = scandir(".");
echo("files found: " + files.length);
// stringify the array
json_enc = JSON.stringify(files);
// and parse it back
json_dec = JSON.parse(json_enc);
// then go through the array and load the scripts we like
var c;
for(c=0; c<json_dec.length; c++) {
if(json_dec[c] == "./star.js") {
echo("loading star.js");
include(json_dec[c]);
}
if(json_dec[c] == "./test_rand.js") {
echo("loading test_rand.js");
include(json_dec[c]);
}
if(json_dec[c] == "./cynosure.js") {
echo("loading cynoruse.js");
include(json_dec[c]);
}
}

View File

@@ -40,10 +40,13 @@ function drawStar(lay, s_mul, s2_mul) {
}
running = true;
kbd = new KeyboardController();
kbd.pressed_esc = function() { quit(); }
register_controller( kbd );
var kbd;
star_kbd = new KeyboardController();
star_kbd.pressed_esc = function() { quit(); }
register_controller( star_kbd );
var star;
star = new GeometryLayer();
star.color(255,255,255,255);
//star.set_blit("alpha");
@@ -59,9 +62,10 @@ srand();
cc = 1;
bang = new TriggerController();
register_controller(bang);
bang.frame = function() {
var star_bang;
star_bang = new TriggerController();
register_controller(star_bang);
star_bang.frame = function() {
cc += 0.05;

View File

@@ -6,7 +6,7 @@
width = 400;
height = 300;
function draw_pixels(geo) {
function draw_pixels(rand_geo) {
var x, y;
x = rand()%width;
@@ -15,11 +15,11 @@ function draw_pixels(geo) {
if(x<0) x = -x;
if(y<0) y = -y;
geo.pixel(x,y);
rand_geo.pixel(x,y);
}
function draw_triangles(geo) {
function draw_triangles(rand_geo) {
var x1, x2, x3;
var y1, y2, y3;
@@ -37,11 +37,11 @@ function draw_triangles(geo) {
if(y2<0) y2 = -y2;
if(y3<0) y3 = -y3;
geo.trigon_fill(x1,y1,x2,y2,x3,y3);
rand_geo.trigon_fill(x1,y1,x2,y2,x3,y3);
}
function draw_ellipses(geo) {
function draw_ellipses(rand_geo) {
var x, y;
var rx, ry;
@@ -55,10 +55,10 @@ function draw_ellipses(geo) {
if(rx<0) rx = -rx;
if(ry<0) ry = -ry;
geo.ellipse_fill(x, y, rx, ry);
rand_geo.ellipse_fill(x, y, rx, ry);
}
function randomize_color(geo) {
function randomize_color(rand_geo) {
var r, g, b;
r = rand() % 255;
@@ -69,33 +69,33 @@ function randomize_color(geo) {
if(g<0) g = -g;
if(b<0) b = -b;
geo.color(r,g,b,150);
rand_geo.color(r,g,b,150);
}
geo = new GeometryLayer(400,300);
//geo.set_blit("alpha");
//geo.set_blit_value(0.5);
geo.activate(true);
rand_geo = new GeometryLayer(400,300);
//rand_geo.set_blit("alpha");
//rand_geo.set_blit_value(0.5);
rand_geo.activate(true);
//geo.start();
//geo.set_fps(24);
add_layer(geo);
add_layer(rand_geo);
running = true;
kbd = new KeyboardController();
kbd.released_q = function() { quit(); }
kbd.released_p = function() { drawer = draw_pixels; }
kbd.released_t = function() { drawer = draw_triangles; }
kbd.released_e = function() { drawer = draw_ellipses; }
register_controller( kbd );
rand_kbd = new KeyboardController();
rand_kbd.released_q = function() { quit(); }
rand_kbd.released_p = function() { drawer = draw_pixels; }
rand_kbd.released_t = function() { drawer = draw_triangles; }
rand_kbd.released_e = function() { drawer = draw_ellipses; }
register_controller( rand_kbd );
drawer = draw_triangles;
bang = new TriggerController();
bang.frame = function() {
randomize_color(geo);
drawer(geo);
rand_bang = new TriggerController();
rand_bang.frame = function() {
randomize_color(rand_geo);
drawer(rand_geo);
}
register_controller(bang);
register_controller(rand_bang);