File: test_rand.js

package info (click to toggle)
freej 0.10git20080824-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 13,504 kB
  • ctags: 19,398
  • sloc: ansic: 135,255; cpp: 32,550; sh: 9,318; perl: 2,932; asm: 2,355; yacc: 1,178; makefile: 1,119; java: 136; lex: 94; python: 16
file content (104 lines) | stat: -rw-r--r-- 1,707 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// freej script to test randomness
// this also shows keyboard interactivity
// and smart use of function pointers
// (C)2005 Denis Jaromil Rojo - GNU GPL 

width =  400;
height = 300;
drawer = draw_triangles;

function draw_pixels(geo) {
  var x, y;

  x = rand()%width;
  y = rand()%height;

  if(x<0) x = -x;
  if(y<0) y = -y;

  geo.pixel(x,y);
}


function draw_triangles(geo) {
  var x1, x2, x3;
  var y1, y2, y3;

  x1 = rand()%width;
  x2 = rand()%width;
  x3 = rand()%width;
  y1 = rand()%height;
  y2 = rand()%height;
  y3 = rand()%height;

  if(x1<0) x1 = -x1;
  if(x2<0) x2 = -x2;
  if(x3<0) x3 = -x3;
  if(y1<0) y1 = -y1;
  if(y2<0) y2 = -y2;
  if(y3<0) y3 = -y3;

  geo.trigon_fill(x1,y1,x2,y2,x3,y3);
}


function draw_ellipses(geo) {
  var x, y;
  var rx, ry;

  x = rand()%width;
  y = rand()%height;
  rx = rand()%(width/2);
  ry = rand()%(height/2);

  if(x<0) x = -x;
  if(y<0) y = -y;
  if(rx<0) rx = -rx;
  if(ry<0) ry = -ry;

  geo.ellipse_fill(x, y, rx, ry);
}

function randomize_color(geo) {
  var r, g, b;

  r = rand() % 255;
  g = rand() % 255;
  b = rand() % 255;

  if(r<0) r = -r;
  if(g<0) g = -g;
  if(b<0) b = -b;

  geo.color(r,g,b,150); 
}

geo = new GeometryLayer(400,300);
geo.set_blit("alpha");
geo.set_blit_value(0.5);
geo.activate(true);
geo.start();
geo.set_fps(24);
add_layer(geo);

running = true;
kbd = new KeyboardController();
kbd.released_q = function() { running = false; }
kbd.released_p = function() { drawer = draw_pixels; }
kbd.released_t = function() { drawer = draw_triangles; }
kbd.released_e = function() { drawer = draw_ellipses; }
register_controller( kbd );

while(running) {

  randomize_color(geo);

  drawer(geo);

  run(0.001);

}

quit();