1 /************************************************************************ 2 3 Test the libgfx scripting facility. 4 5 by Michael Garland, 2000. 6 7 $Id: t-script.cxx 426 2004-09-27 04:34:55Z garland $ 8 9 ************************************************************************/ 10 11 #include <gfx/gfx.h> 12 #include <gfx/script.h> 13 #include <gfx/vec3.h> 14 15 using namespace std; 16 17 // usage: add <x>* 18 // Adds all numbers listed on the line and prints the result 19 // 20 // usage: avg <x>* 21 // Averages all numbers listed on the line and prints the result 22 // proc_add(const CmdLine & cmd)23int proc_add(const CmdLine &cmd) 24 { 25 std::vector<double> values; 26 double sum = 0.0; 27 28 cmd.collect_as_numbers(values); 29 std::vector<double>::size_type count; 30 for(count=0; count<values.size(); count++) 31 sum += values[count]; 32 33 if( cmd.opname() == "avg" && count>0 ) 34 sum /= (double)count; 35 36 cout << sum << endl; 37 return SCRIPT_OK; 38 } 39 40 // usage: vec3 <x> <y> <z> 41 // Constructs a 3-vector and prints the result proc_vec3(const CmdLine & cmd)42int proc_vec3(const CmdLine &cmd) 43 { 44 if( cmd.argcount() != 3 ) return SCRIPT_ERR_SYNTAX; 45 46 Vec3 v; 47 cmd.collect_as_numbers(v, 3); 48 49 cout << v << endl; 50 return SCRIPT_OK; 51 } 52 53 // usage: echo <msg> 54 // Prints all the text following the command name verbatim proc_echo(const CmdLine & cmd)55int proc_echo(const CmdLine &cmd) 56 { 57 cout << cmd.argline() << endl; 58 return SCRIPT_OK; 59 } 60 main(int argc,char * argv[])61int main(int argc, char *argv[]) 62 { 63 CmdEnv env; 64 65 env.register_command("add", proc_add); 66 env.register_command("avg", proc_add); 67 env.register_command("echo", proc_echo); 68 env.register_command("vec3", proc_vec3); 69 70 for(int i=1; i<argc; i++) 71 env.do_file(argv[i]); 72 73 return 0; 74 } 75