1 /*********************************************************************** 2 * z:/wata/src/a/csmash/loadparts.h 3 * $Id: loadparts.h,v 1.7 2002/03/05 14:21:21 yotsuya Exp $ 4 * 5 * Copyright by ESESoft. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer 16 * in the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * The name of the author may not be used to endorse or promote 20 * products derived from this software without specific prior written 21 * permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 24 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 27 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 29 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 * 35 ***********************************************************************/ 36 #ifndef __Yotsuya_ESESoft_07897981__loadparts_h__INCLUDED__ 37 #define __Yotsuya_ESESoft_07897981__loadparts_h__INCLUDED__ 38 /***********************************************************************/ 39 #include <stdarg.h> 40 #include <map> 41 #include <list> 42 #include <string> 43 /* __BEGIN__BEGIN__ */ 44 45 /*********************************************************************** 46 * Class parts (root) 47 ***********************************************************************/ 48 class texture_parts; 49 class polyhedron_parts; 50 class anim_parts; 51 class body_parts; 52 53 class parts 54 { 55 public: 56 typedef std::basic_string<char> string; 57 class error { 58 public: 59 const char* str; 60 error(const char * str)61 inline error(const char *str) : str(str) {} what()62 const char* what() const { return str; } 63 }; 64 class verror : public error 65 { 66 public: 67 char buf[508]; // 512 - sizeof(char*) 68 verror(int lineno,const char * fmt,...)69 inline verror(int lineno, const char *fmt, ...) : error(buf) { 70 int l = snprintf(buf, sizeof(buf), "%d: ", lineno); 71 va_list arg; 72 va_start(arg, fmt); 73 vsnprintf(&buf[l], sizeof(buf)-l, fmt, arg); 74 va_end(arg); 75 } verror(const char * fmt,...)76 inline verror(const char *fmt, ...) : error(buf) { 77 va_list arg; 78 va_start(arg, fmt); 79 vsnprintf(buf, sizeof(buf), fmt, arg); 80 va_end(arg); 81 } 82 }; 83 84 enum symbol_t { 85 sym_unknown = 0, 86 sym_null, 87 sym_load, sym_create, 88 sym_polyhedron, sym_anim, sym_texture, 89 sym_body, 90 }; 91 parts(const char * str)92 inline parts(const char *str) : name(str) {} ~parts()93 virtual ~parts() {} 94 95 virtual bool load(const char*) = 0; 96 virtual symbol_t type() const = 0; typestr()97 virtual const char *typestr() const { return sym2str(type()); } assign(parts * obejct)98 virtual bool assign(parts* obejct) { return false; } 99 realize()100 virtual bool realize() { return true; } unrealize()101 virtual void unrealize() {} 102 103 static symbol_t getsym(const char *str); 104 static const char* sym2str(symbol_t); 105 106 static parts* getobject(const char* name); 107 static void clearobjects(); 108 static bool loadobjects(const char *str); 109 110 static bool realizeobjects(); 111 static void unrealizeobjects(); 112 113 protected: 114 static bool addobject(const char* name, parts*); 115 static bool delobject(const char *name); 116 static bool loadfile(const char *str); 117 118 private: 119 static bool load_create(int lineno, int ac, const char *av[], int *optind); 120 static bool load_load(int lineno, int ac, const char *av[], int *optind); 121 static bool load_polyhedron(int lineno, polyhedron_parts*, 122 int ac, const char *av[], int *ind); 123 static bool load_anim(int lineno, anim_parts*, 124 int ac, const char *av[], int *ind); 125 public: 126 string name; 127 }; 128 129 /*********************************************************************** 130 * Class parts_map 131 ***********************************************************************/ 132 class parts_map : public std::map<parts::string, parts*> 133 { 134 public: 135 typedef std::map<parts::string, parts*> super; 136 ~parts_map()137 ~parts_map() { 138 clear(); 139 } clear()140 void clear() { 141 for (iterator i = begin(); i != end(); ++i) delete i->second; 142 super::clear(); 143 } 144 }; 145 146 /*********************************************************************** 147 * Class texture_parts 148 ***********************************************************************/ 149 class texture_parts : public parts 150 { 151 public: 152 typedef GLuint object_t; 153 GLuint object; 154 string filename; 155 texture_parts(const char * name)156 inline texture_parts(const char *name) : parts(name), object(0) {} ~texture_parts()157 virtual ~texture_parts() { unrealize(); } type()158 virtual symbol_t type() const { return sym_texture; } 159 virtual bool load(const char *str); 160 161 public: 162 // Textures must be realized before glBindTextures(). 163 // realize() will fail if GL library is not initialized yet. 164 virtual bool realize(); 165 virtual void unrealize(); 166 }; 167 168 /*********************************************************************** 169 * Class polyhedron_parts 170 ***********************************************************************/ 171 class polyhedron_parts : public parts 172 { 173 public: 174 typedef polyhedron object_t; 175 polyhedron *object; 176 texture_parts *tex; 177 polyhedron_parts(const char * name)178 inline polyhedron_parts(const char *name) : parts(name), object(NULL), tex(NULL) {} ~polyhedron_parts()179 virtual ~polyhedron_parts() { delete object; } type()180 virtual symbol_t type() const { return sym_polyhedron; } 181 virtual bool assign(parts*); 182 virtual bool load(const char* str); 183 184 public: 185 void render() const; 186 void renderWire(const vector3F &origin) const; 187 }; 188 189 /*********************************************************************** 190 * Class anim_parts 191 ***********************************************************************/ 192 class anim_parts : public parts 193 { 194 public: 195 typedef affineanim object_t; 196 affineanim *object; 197 std::list<polyhedron_parts*> poly; 198 anim_parts(const char * name)199 inline anim_parts(const char *name) : parts(name), object(NULL) {} ~anim_parts()200 virtual ~anim_parts() { delete object; } type()201 virtual symbol_t type() const { return sym_anim; } 202 virtual bool assign(parts*); 203 virtual bool load(const char* str); 204 205 public: 206 void render(int frame) const; 207 void renderWire(int frame) const; 208 }; 209 210 /*********************************************************************** 211 * Class body_parts 212 ***********************************************************************/ 213 class body_parts : public parts 214 { 215 public: 216 typedef std::list<affinemotion*> object_t; 217 std::list<anim_parts*> object; 218 body_parts(const char * name)219 inline body_parts(const char *name) : parts(name) {} ~body_parts()220 virtual ~body_parts() {} type()221 inline symbol_t type() const { return sym_body; } 222 virtual bool assign(parts*); load(const char * str)223 virtual bool load(const char* str) { return false; } 224 225 public: 226 void render(int frame) const; 227 void renderWire(int frame) const; 228 }; 229 230 /* __END__END__ */ 231 /***********************************************************************/ 232 #endif 233 /*********************************************************************** 234 * END OF loadparts.h 235 ***********************************************************************/ 236