bool load_scene( Scene* scene, const char* filename ) { TiXmlDocument doc( filename ); const TiXmlElement* root = 0; const TiXmlElement* elem = 0; MaterialMap materials; MeshMap meshes; TriVertMap triverts; assert( scene ); // load the document if ( !doc.LoadFile() ) { std::cout << "ERROR, " << doc.ErrorRow() << ":" << doc.ErrorCol() << "; " << "parse error: " << doc.ErrorDesc() << "\n"; return false; } // check for root element root = doc.RootElement(); if ( !root ) { std::cout << "No root element.\n"; return false; } // reset the scene scene->reset(); try { // parse the camera elem = get_unique_child( root, true, STR_CAMERA ); parse_camera( elem, &scene->camera ); // parse background color parse_elem( root, true, STR_BACKGROUND, &scene->background_color ); // parse refractive index parse_elem( root, true, STR_REFRACT, &scene->refractive_index ); // parse ambient light parse_elem( root, false, STR_AMLIGHT, &scene->ambient_light ); // parse the lights elem = root->FirstChildElement( STR_PLIGHT ); while ( elem ) { PointLight pl; parse_point_light( elem, &pl ); scene->add_light( pl ); elem = elem->NextSiblingElement( STR_PLIGHT ); } // parse the materials elem = root->FirstChildElement( STR_MATERIAL ); while ( elem ) { Material* mat = new Material(); check_mem( mat ); scene->add_material( mat ); const char* name = parse_material( elem, mat ); assert( name ); // place each material in map by it's name, so we can associate geometries // with them when loading geometries // check for repeat name if ( !materials.insert( std::make_pair( name, mat ) ).second ) { print_error_header( elem ); std::cout << "Material '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_MATERIAL ); } // parse the meshes elem = root->FirstChildElement( STR_MESH ); while ( elem ) { Mesh* mesh = new Mesh(); check_mem( mesh ); scene->add_mesh( mesh ); const char* name = parse_mesh( elem, mesh ); assert( name ); // place each mesh in map by it's name, so we can associate geometries // with them when loading geometries if ( !meshes.insert( std::make_pair( name, mesh ) ).second ) { print_error_header( elem ); std::cout << "Mesh '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_MESH ); } // parse vertices (used by triangles) elem = root->FirstChildElement( STR_VERTEX ); while ( elem ) { Triangle::Vertex v; const char* name = parse_triangle_vertex( materials, elem, &v ); assert( name ); // place each vertex in map by it's name, so we can associate triangles // with them when loading geometries if ( !triverts.insert( std::make_pair( name, v ) ).second ) { print_error_header( elem ); std::cout << "Triangle vertex '" << name << "' multiply defined.\n"; throw std::exception(); } elem = elem->NextSiblingElement( STR_VERTEX ); } // parse the geometries // spheres elem = root->FirstChildElement( STR_SPHERE ); while ( elem ) { Sphere* geom = new Sphere(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_sphere( materials, elem, geom ); elem = elem->NextSiblingElement( STR_SPHERE ); } // triangles elem = root->FirstChildElement( STR_TRIANGLE ); while ( elem ) { Triangle* geom = new Triangle(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_triangle( materials, triverts, elem, geom ); elem = elem->NextSiblingElement( STR_TRIANGLE ); } // models elem = root->FirstChildElement( STR_MODEL ); while ( elem ) { Model* geom = new Model(); check_mem( geom ); scene->add_geometry( geom ); parse_geom_model( materials, meshes, elem, geom ); elem = elem->NextSiblingElement( STR_MODEL ); } // TODO add you own geometries here } catch ( std::bad_alloc const& ) { std::cout << "Out of memory error while loading scene\n."; scene->reset(); return false; } catch ( ... ) { scene->reset(); return false; } return true; }
int parse_data_module( DATAModule *module ) { int data_count; bool fatalError = TRUE; // if true, catch leads to exit(-1) try { data_count = module->datas_count; for ( int i = 0; i < module->datas_count; i++ ) { DataContext ctx; data_context = &ctx; const char *tagName = module->datas[i]->tag->image; const int tagId = find_tag( tagName ); if ( DATA_VERBOSE > 4 ) { printf("tagName: %s tagId: %d\n\r", tagName, tagId ); } switch ( tagId ) { case TAG_Prereq: parse_prerequisite( module->datas[i] ); break; case TAG_BrewFormula: create_brew_formula_table( module->datas_count ); parse_brew_formula( module->datas[i] ); break; case TAG_Ban: parse_ban( module->datas[i] ); break; case TAG_Command: create_command_table( module->datas_count ); parse_command( module->datas[i] ); break; case TAG_Unique: parse_unique( module->datas[i] ); break; case TAG_Disable: parse_disabled( module->datas[i] ); break; case TAG_Material: create_material_table( module->datas_count ); parse_material( module->datas[i] ); break; case TAG_Liquid: create_liquid_table( module->datas_count ); parse_liquid( module->datas[i] ); break; case TAG_God: create_god_table( module->datas_count ); parse_god( module->datas[i] ); break; case TAG_Clan: parse_clan( module->datas[i] ); break; case TAG_Faction: create_faction_table( module->datas_count, module ); parse_faction( module->datas[i] ); break; case TAG_Race: create_races_table( module->datas_count ); parse_race( module->datas[i] ); break; case TAG_PCRace: create_pcraces_table( module->datas_count ); parse_pcrace( module->datas[i] ); break; case TAG_Sphere: create_spheres_table( module->datas_count ); parse_sphere( module->datas[i] ); break; case TAG_Group: create_groups_table( module->datas_count ); parse_groups( module->datas[i] ); break; case TAG_Ability: parse_abilities( module->datas[i] ); break; case TAG_Class: create_classes_table( module->datas_count ); parse_classes( module->datas[i] ); break; case TAG_Player: fatalError = FALSE; parse_player( module->datas[i] ); fatalError = TRUE; break; case TAG_Area: parse_area( module->datas[i] ); break; case TAG_Config: if ( parse_config( module->datas[i] ) ) // once we have found the right config: skip others return data_count; break; case TAG_AreaState: parse_area_state( module->datas[i] ); break; case TAG_Time: parse_time( module->datas[i] ); break; case TAG_Hometown: create_hometown_table( module->datas_count ); parse_hometown( module->datas[i] ); break; case TAG_School: create_school_table( module->datas_count ); parse_school( module->datas[i] ); break; case TAG_Super_Race: create_super_race_table( module->datas_count ); parse_super_race( module->datas[i] ); break; default: p_error("Invalid Tag: %s", tagName ); break; } } } catch (ScriptException e) { bug("Error while parsing datas. %s", e.msg ); if ( fatalError ) { bug("FATAL error. Bye!"); exit(-1); } else return -1; } dump_GC_info(); return data_count; }