Exemple #1
0
int Scenario::loadMapBuf(string mapn) {
    mapname=mapn;
    ifstream mapfile;
    string tmpline;
    string loadfile=config.datadir+mapname;

    mapfile.open(loadfile.c_str());
    if (mapfile) {
        cout << "Loading new map: " << loadfile << endl;
    } else {
        cout << "Map loading failed: " << loadfile << " not found!\n";
        return 2;
    }

    mapbuf.clear();

    /* We parse the parameters to add a name if it was missing
       This whole file reading looks much more complicated because
       of this (otherwise there would only be mapbuf.push_back(tmpline) */
    string cname;
    Uint16 x,y;
    ParameterMap parameters;
    bool header=true;
    std::map<string,Uint16> namecount;
    while (getline(mapfile,tmpline)) {
        if (header) {
            mapbuf.push_back(tmpline);
            std::istringstream tmpstream(tmpline);
            tmpstream >> cname;
            if (cname[0]=='#') {
                if (cname=="#ENDHEADER") header=false;
                continue;
            }
        } else {
            cname.erase();
            x=y=0;
            std::istringstream tmpstream(tmpline);
            tmpstream >> cname >> x >> y;

            if (cname.empty() || cname[0]=='#') {
                mapbuf.push_back(tmpline);
                continue;
            } else {
                namecount[cname]++;
            }

            string parameterlist((istreambuf_iterator<char>(tmpstream)), istreambuf_iterator<char>());
            parameters=getParameters(parameterlist);
            if (!hasParam(parameters,"name")) {
                if (parameters.empty()) tmpline+=" name="+cname+itos(namecount[cname]);
                else tmpline+=", name="+cname+itos(namecount[cname]);
            }
            mapbuf.push_back(tmpline);
        }
    }
EmptyAnimationPtr Object::loadAnimation(string anim_name,
                                        double scale_factor,
                                        BasePointType abp_type,
                                        Uint16 aanimation_type,
                                        double afps,
                                        AllignType aallign_type) {
    /* Parse animation data file */
    ifstream file;
    string tmpline;
    string loadfile=config.datadir+config.anim_file;

    file.open(loadfile.c_str());
    if (!file) {
        cout << "Failed to open the animation data file: " << loadfile << " => Couldn't load " << anim_name << " animation!\n" << endl;
        return EmptyAnimationPtr(new EmptyAnimation());
    } else {
        string arg1,arg2,arg3,arg4,arg5,arg6;
        string tmp_anim_name="";
        string imagename="";
        Uint16 astart_pos=0;
        Uint16 aframes=1;

        Uint16 description_type=DESC_NONE;

        while (getline(file,tmpline)) {
            arg1=arg2=arg3=arg4=arg5=arg6="";
            std::istringstream tmpstream(tmpline);
            tmpstream >> arg1 >> arg2 >> arg3 >> arg4 >> arg5 >> arg6;

            if        (arg1 == "DESCRIPTION") {
                description_type=DESC_NONE;  
            } else if (arg1 == "ANIMATION") {
                if (arg2 == "LVLANIM") {
                    description_type=DESC_ANIM_LVLANIM;
                } else {
                    description_type=DESC_NONE;
                }
            }
     
            if (description_type==DESC_ANIM_LVLANIM) {
                if (arg1.empty() || arg2.empty() || arg3.empty() || arg4.empty()) {
                } else {
                    tmp_anim_name=arg1;
                    imagename=arg2;
                    astart_pos=(Uint16)atoi(arg3.c_str());
                    aframes=(Uint16)atoi(arg4.c_str());

                    if (anim_name==tmp_anim_name) {
                        return loadAnimation(imgcache->loadImage(imagename,scale_factor),aframes,abp_type,aanimation_type,afps,astart_pos,aallign_type);
                    }
                }
            }
        }
    }
    cout << "Animation " << anim_name << " not found!" << endl;
    return EmptyAnimationPtr(new EmptyAnimation());
}
Exemple #3
0
KWMailMergeDataSource *KWMailMergeDataBase::loadPlugin(const QString& name)
{
  if (!name.isEmpty())
  {
      // get the library loader instance

      KLibLoader *loader = KLibLoader::self();

      // try to load the library
      QString libname=name;
//      QString libname("lib%1");
      KLibrary *lib = loader->library(QFile::encodeName(libname));
      if (lib) {
          // get the create_ function
          QString factory=QString("create_%1").arg(name);
          void *create = lib->symbol(QFile::encodeName(factory));

          if (create)
          {
              // create the module
              KWMailMergeDataSource * (*func)(KInstance*,QObject*);
              func = (KWMailMergeDataSource* (*)(KInstance*,QObject*)) create;
              KWMailMergeDataSource *tmpsource =func(KWFactory::instance(),this);
              if (tmpsource)
              {
                  QDataStream tmpstream(tmpsource->info,IO_WriteOnly);
                  tmpstream<<name;
              }
              return tmpsource;
          }
      }
      kdWarning() << "Couldn't load plugin " << name <<  endl;
  }
  else
      kdWarning()<< "No plugin name specified" <<endl;
  return 0;
}
std::shared_ptr<ObjectFileNormalFormat>
ObjectFileFormatNormalReader::read( const QString & fileName ){
    std::shared_ptr<ObjectFileNormalFormat> ans ;

    QFile file( fileName );

    if ( file.open( QIODevice::ReadOnly |QIODevice::Text ) ) {
        QTextStream stream( &file );

        /*read off*/
        while ( stream.atEnd()==false ) {
            QString line_=stream.readLine();
            line_=line_.trimmed().toLower();
            if (line_.isEmpty()) { continue; }
            if (line_=="off") { break; }
            return std::move(ans);
        }

        std::size_t points_size_=0;
        std::size_t faces_size_=0;

        {
            QString line_=stream.readLine();
            QTextStream tmpstream(&line_);
            tmpstream>>points_size_;
            tmpstream>>faces_size_;
        }

        if ((stream.atEnd()==false)
            &&(faces_size_>0)
            &&(points_size_>0)) {
            ans=std::make_shared<ObjectFileNormalFormat>();
            auto * ansPointer=ans.get();
            {/*read points*/
                auto & points_=ans->points;
                points_.reserve(points_size_);
                {/*read points*/
                    GLfloat x,y,z;

                    bool xMax_set=false;bool xMin_set=false;
                    bool zMax_set=false;bool zMin_set=false;
                    bool yMax_set=false;bool yMin_set=false;

                    std::size_t current_point_=0;
                    while ((stream.atEnd()==false)&&(current_point_<points_size_)) {
                        stream>>x;
                        stream>>y;
                        stream>>z;
                        if (stream.status()==QTextStream::ReadCorruptData) {
                            return nullptr;
                        }

                        if (zMax_set) { if (z>ansPointer->zMax) { ansPointer->zMax=z; } }
                        else { ansPointer->zMax=z; zMax_set=true; }
                        if (zMin_set) { if (z<ansPointer->zMin) { ansPointer->zMin=z; } }
                        else { ansPointer->zMin=z; zMin_set=true; }
                        if (yMax_set) { if (y>ansPointer->yMax) { ansPointer->yMax=y; } }
                        else { ansPointer->yMax=y; yMax_set=true; }
                        if (yMin_set) { if (y<ansPointer->yMin) { ansPointer->yMin=y; } }
                        else { ansPointer->yMin=y; yMin_set=true; }
                        if (xMax_set) { if (x>ansPointer->xMax) { ansPointer->xMax=x; } }
                        else { ansPointer->xMax=x; xMax_set=true; }
                        if (xMin_set) { if (x<ansPointer->xMin) { ansPointer->xMin=x; } }
                        else { ansPointer->xMin=x; xMin_set=true; }

                        points_.emplace_back(x,y,z);
                        ++current_point_;
                    }
                }
            }/*read points*/

            {/*read faces*/
                auto & faces_=ans->faces;
                faces_.reserve(faces_size_);
                {/*read faces*/
                    std::vector<GLuint> line_face_;
                    std::size_t current_face_=0;
                    GLuint tmp;
                    while ((stream.atEnd()==false)&&(current_face_<faces_size_)) {
                        line_face_.clear();
                        GLuint n_points=0;
                        stream>>n_points;
                        if (stream.status()==QTextStream::ReadCorruptData) {
                            return nullptr;
                        }
                        if (n_points<3) { return nullptr; }
                        line_face_.reserve(n_points);
                        {/*read npoints*/
                            std::size_t c_point=0;
                            while ((stream.atEnd()==false)&&(c_point<n_points)) {
                                stream>>tmp;
                                if (stream.status()==QTextStream::ReadCorruptData) {
                                    return nullptr;
                                }
                                line_face_.push_back(tmp);
                                ++c_point;
                            }
                        }

                        if (n_points==3) {/*a triangle */
                            faces_.emplace_back(line_face_[0],line_face_[1],line_face_[2]);
                        }
                        else {/*a polygon */
                            const auto first_point_=line_face_.begin();
                            auto second_point_=first_point_+1;
                            auto third_point_=second_point_+1;
                            const auto end_point_=line_face_.end();
                            for (; third_point_!=end_point_; second_point_=third_point_++) {
                                faces_.emplace_back(*first_point_,*second_point_,*third_point_);
                            }

                        }

                        ++current_face_;
                    }
                }
            }/*read faces*/
        }
int readConfig(const string& filename) {
    ifstream configfile;
    string tmpline, option, arg1;
    
    //default values
    config.width=640;
    config.height=480;
    config.full=false;
    config.audio_rate = MIX_DEFAULT_FREQUENCY;
    config.audio_format = MIX_DEFAULT_FORMAT;
    config.audio_channels = 2;
    config.datadir="data/";
    config.anim_file="animation_data.anim";
    config.map="";
    config.scenario="lv1.sce";
    config.lvlscale=2;
    config.onlymap=false;

    //key bindings
    config.keybind[KEY_LEFT]    = SDLK_LEFT;
    config.keybind[KEY_RIGHT]   = SDLK_RIGHT;
    config.keybind[KEY_UP]      = SDLK_UP;
    config.keybind[KEY_DOWN]    = SDLK_DOWN;
    config.keybind[KEY_SP1]     = SDLK_SPACE;
    config.keybind[KEY_SP2]     = SDLK_LSHIFT;
    config.keybind[KEY_ACT]     = SDLK_RETURN;
    config.keybind[KEY_USE]     = SDLK_INSERT;
    config.keybind[KEY_DROP]    = SDLK_DELETE;
    config.keybind[KEY_SWITCH]  = SDLK_LCTRL;
    config.keybind[KEY_PAUSE]   = SDLK_TAB;
    config.keybind[KEY_MENU]    = SDLK_ESCAPE;
    config.keybind[KEY_DEBUG]   = SDLK_F4;
    config.keybind[KEY_NOANIM]  = SDLK_F3;
    config.keybind[KEY_FPS]     = SDLK_F2;
    config.keybind[KEY_BAR]     = SDLK_F1;
    config.keybind[KEY_FULL]    = SDLK_f;
    config.keybind[KEY_QUIT]    = SDLK_q;
    config.keybind[KEY_ADD_SEL] = SDLK_LSHIFT;
    config.keybind[KEY_RM_SEL]  = SDLK_LCTRL;
    config.keybind[KEY_DEL]     = SDLK_DELETE;

    configfile.open(filename.c_str());
    if (!configfile) return 0;

    //check every line
    while (getline(configfile,tmpline)) {
        std::istringstream tmpstream(tmpline);
        tmpstream >> option >> arg1;

        //Skip empty lines
        if (option.empty()) continue;
        //Skip comments
        if (option[0]=='#') continue;
        if (option=="datadir") {
            config.datadir=arg1;
            if (config.datadir.substr(config.datadir.size() - 1, 1)!="/") config.datadir+="/";
        } else if (option=="animfile") {
            config.anim_file=arg1;
        } else if (option=="width") {
            config.width=atoi(arg1.c_str());
        } else if (option=="height") {
            config.height=atoi(arg1.c_str());
        } else if (option=="full") {
            if (arg1=="1") config.full=true;
            else config.full=false;
        } else if (option=="map") {
            config.map=arg1;
            config.onlymap=true;
        } else if (option=="scenario") {
            config.scenario=arg1;
        //TODO: add keybindings
        } else {
            cout << "Unknown option: " << option << endl;
        }
    }

    return 1;
}