1 | /********************************************************************** <BR>
|
---|
2 | This file is part of Crack dot Com's free source code release of
|
---|
3 | Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
|
---|
4 | information about compiling & licensing issues visit this URL</a>
|
---|
5 | <PRE> If that doesn't help, contact Jonathan Clark at
|
---|
6 | golgotha_source@usa.net (Subject should have "GOLG" in it)
|
---|
7 | ***********************************************************************/
|
---|
8 |
|
---|
9 | // new_type.cc : adds a new type "vector" into the li_ system
|
---|
10 | // mainly a new type is responsible for
|
---|
11 | // - printing
|
---|
12 | // - loading & saving
|
---|
13 | // - marking for garbage collection if it contains references to other li_object's
|
---|
14 | // - freeing memory when gc says you are no longer referenced
|
---|
15 |
|
---|
16 | #include "main/main.hh"
|
---|
17 | #include "init/init.hh"
|
---|
18 | #include "lisp/lisp.hh"
|
---|
19 | #include "math/vector.hh"
|
---|
20 | #include "file/file.hh"
|
---|
21 | #include "loaders/dir_save.hh"
|
---|
22 | #include "loaders/dir_load.hh"
|
---|
23 |
|
---|
24 | // this is the global type number assigned by the li system for li_vector
|
---|
25 | li_type_number li_vector_type;
|
---|
26 |
|
---|
27 | class li_vector : public li_object
|
---|
28 | {
|
---|
29 | i4_3d_vector *vector;
|
---|
30 | public:
|
---|
31 | li_vector(const i4_3d_vector &v)
|
---|
32 | : li_object(li_vector_type)
|
---|
33 | {
|
---|
34 | vector=new i4_3d_vector(v.x, v.y, v.z);
|
---|
35 | }
|
---|
36 |
|
---|
37 | // used by load_object
|
---|
38 | li_vector(i4_file_class *fp)
|
---|
39 | : li_object(li_vector_type)
|
---|
40 | {
|
---|
41 | float x=fp->read_float();
|
---|
42 | float y=fp->read_float();
|
---|
43 | float z=fp->read_float();
|
---|
44 | vector=new i4_3d_vector(x,y,z);
|
---|
45 | }
|
---|
46 |
|
---|
47 | // free memory assocaited with vector during gc()
|
---|
48 | void free() { delete vector; }
|
---|
49 |
|
---|
50 | // print the object in a english form
|
---|
51 | void print(i4_file_class *fp)
|
---|
52 | {
|
---|
53 | fp->printf("<vector %f %f %f>", vector->x, vector->y, vector->z);
|
---|
54 | }
|
---|
55 |
|
---|
56 | // write to disk
|
---|
57 | void save(i4_file_class *fp, li_environment *env)
|
---|
58 | {
|
---|
59 | fp->write_float(vector->x);
|
---|
60 | fp->write_float(vector->y);
|
---|
61 | fp->write_float(vector->z);
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | i4_3d_vector value() { return *vector; }
|
---|
66 | static li_vector *get(li_object *o, li_environment *env)
|
---|
67 | { check_type(o, li_vector_type, env); return ((li_vector *)o); }
|
---|
68 | };
|
---|
69 |
|
---|
70 | // if you want to add a new type into the system, implement one of these
|
---|
71 | // and call li_add_type
|
---|
72 | class li_vector_function_table : public li_type_function_table
|
---|
73 | {
|
---|
74 | public:
|
---|
75 | // free data associated with an instance of this type
|
---|
76 | virtual void free(li_object *o) { li_vector::get(o,0)->free(); }
|
---|
77 |
|
---|
78 | // compare 2 objects
|
---|
79 | virtual int equal(li_object *o1, li_object *o2)
|
---|
80 | {
|
---|
81 | i4_3d_vector v1=li_vector::get(o1,0)->value();
|
---|
82 | i4_3d_vector v2=li_vector::get(o2,0)->value();
|
---|
83 |
|
---|
84 | return v1.x==v2.x && v1.y==v2.y && v1.z==v2.z;
|
---|
85 | }
|
---|
86 |
|
---|
87 | // print the object
|
---|
88 | virtual void print(li_object *o, i4_file_class *stream)
|
---|
89 | {
|
---|
90 | li_vector::get(o,0)->print(stream);
|
---|
91 | }
|
---|
92 |
|
---|
93 | virtual char *name() { return "vector"; }
|
---|
94 |
|
---|
95 | // create a new object
|
---|
96 | virtual li_object *create(li_object *params, li_environment *env)
|
---|
97 | {
|
---|
98 | if (params) // with parameters?
|
---|
99 | {
|
---|
100 | float x=li_get_float(li_eval(li_first(params,env), env),env);
|
---|
101 | float y=li_get_float(li_eval(li_second(params,env), env),env);
|
---|
102 | float z=li_get_float(li_eval(li_third(params,env), env),env);
|
---|
103 |
|
---|
104 | return new li_vector(i4_3d_vector(x,y,z));
|
---|
105 | }
|
---|
106 | else
|
---|
107 | return new li_vector(i4_3d_vector(0,0,0)); // default vector
|
---|
108 | }
|
---|
109 |
|
---|
110 | // write to disk
|
---|
111 | virtual void save_object(i4_saver_class *fp, li_object *o, li_environment *env)
|
---|
112 | { li_vector::get(o,env)->save(fp,env); }
|
---|
113 |
|
---|
114 | // load from disk
|
---|
115 | virtual li_object *load_object(i4_loader_class *fp, li_type_number *type_remap, li_environment *env)
|
---|
116 | {
|
---|
117 | return new li_vector(fp);
|
---|
118 | }
|
---|
119 | };
|
---|
120 |
|
---|
121 |
|
---|
122 |
|
---|
123 | void i4_main(w32 argc, i4_const_str *argv)
|
---|
124 | {
|
---|
125 | i4_init();
|
---|
126 |
|
---|
127 | // add the type into the system
|
---|
128 | li_vector_type=li_add_type(new li_vector_function_table, 0,0);
|
---|
129 |
|
---|
130 |
|
---|
131 | li_load("new_type.scm");
|
---|
132 |
|
---|
133 | i4_uninit();
|
---|
134 | }
|
---|
135 |
|
---|