コード例 #1
0
ファイル: LevelParser.cpp プロジェクト: iiechapman/Sandman2D
//For extracting objects from object layers
void LevelParser::parseObjectLayer
(TiXmlElement *pObjectElement, vector<ILayer *> *pLayers,
 string layerType, Level* pLevel ,PlayState* newState){
    
    //Create an object layer
    ObjectLayer* pObjectLayer = new ObjectLayer();
    pObjectLayer->setType(layerType);
    cout << "Created new " << pObjectLayer->getType() << " layer\n";
    //cout << "Current Value: ";
    //cout << pObjectElement->FirstChildElement()->Value() << "\n";
    
    for (TiXmlElement* e = pObjectElement->FirstChildElement();
         e != NULL; e = e->NextSiblingElement()){
        cout << "Checking " << e->Value() << "\n";
        if (e->Value() == string("object")){
            int x(0), y(0), width(0), height(0), numFrames(1), callbackID(0), animSpeed(1);
            
            SDL_Color color;
            SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
            
            string textureID(""),name(""),lockTo(""),scrollLock(""),ai("");
            
            
            //Get initial values
            e->Attribute("x", &x);
            e->Attribute("y", &y);
            //cout << "Y: " << x << "\nY: " << y << "\n";
            name = e->Attribute("name");
            
            int GID = 0;
            e->Attribute("gid",&GID);
            
            
            //Check if object exists in element library
            IGameObject* pGameObject;
            if ((*newState->getElements())[name]){
                GameObjectParams* elementParams = (*newState->getElements())[name];
                cout << "Loaded from library: " << elementParams->getName() << "\n";
                pGameObject = GameObjectFactory::Instance()->create(elementParams->getType());
            } else {
                cout << "Object \"" << e->Attribute("name") << "\" not found in library!\n";
                pGameObject = GameObjectFactory::Instance()->create(e->Attribute("type"));
            }
            
            
            //Prep game object params
            pGameObject->load
            (*new GameObjectParams
             (name, (x), (y-height),width, height, textureID,callbackID,animSpeed));
            
            //Load default params per element library
            if ((*newState->getElements())[name]){
                pGameObject->GetParams() = *(*newState->getElements())[name];
            }
            
            
            pGameObject->GetParams().getPosition().setX(x);
            pGameObject->GetParams().getPosition().setY(y);
            
            //Overload properties based on unique object params IN TILED
            for (TiXmlElement* properties = e->FirstChildElement();
                 properties != NULL; properties = properties->NextSiblingElement()){
                if (properties->Value() == string("properties")){
                    
                    for (TiXmlElement* property = properties->FirstChildElement();
                         property!=NULL; property = property->NextSiblingElement()){
                        
                        if (property->Value() == string("property")){
                            
                            if (property->Attribute("name") == string("numFrames")){
                                property->Attribute("value",&numFrames);
                                pGameObject->GetParams().setMaxFrames(numFrames);
                                cout << "Set frames to " << numFrames << "\n";
                                
                            } else if (property->Attribute("name") == string("lockTo")){
                                lockTo = property->Attribute("value");
                                pGameObject->GetParams().setLockTo(lockTo);
                                
                            } else if (property->Attribute("name") == string("ai")){
                                ai = property->Attribute("value");
                                
                                cout << "checking ai flag\n";
                                
                                if (ai == string("true")){
                                    pGameObject->GetParams().setAI(true);
                                } else {
                                    pGameObject->GetParams().setAI(false);
                                }
                                
                            } else if (property->Attribute("name") == string("textureHeight")){
                                property->Attribute("value",&height);
                                pGameObject->GetParams().setHeight(height);
                                
                            } else if (property->Attribute("name") == string("scrollLock")){
                                scrollLock = property->Attribute("value");
                                pGameObject->GetParams().setScrolling(scrollLock != string("true"));
                                
                            } else if (property->Attribute("name") == string("textureWidth")){
                                property->Attribute("value",&width);
                                pGameObject->GetParams().setWidth(width);
                                
                            } else if (property->Attribute("name") == string("animSpeed")){
                                property->Attribute("value",&animSpeed);
                                pGameObject->GetParams().setAnimSpeed(animSpeed);
                                
                            } else if (property->Attribute("name") == string("callbackID")){
                                property->Attribute("value",&callbackID);
                                pGameObject->GetParams().setCallBackID(callbackID);
                                
                            } else if (property->Attribute("name") == string("textureID")){
                                textureID = property->Attribute("value");
                                pGameObject->GetParams().setTextureID(textureID);
                                
                            } else if (property->Attribute("name") == string("blendMode")){
                                if (property->Attribute("value") == string("add")){
                                    blendMode = SDL_BLENDMODE_ADD;
                                } else if (property->Attribute("value") == string("blend")){
                                    blendMode = SDL_BLENDMODE_BLEND;
                                } else if (property->Attribute("value") == string("mod")){
                                    blendMode = SDL_BLENDMODE_MOD;
                                }
                                pGameObject->GetParams().setBlendMode(blendMode);
                                
                            } else if (property->Attribute("name") == string("alpha")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.a = temp;
                                
                            }  else if (property->Attribute("name") == string("red")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.r = temp;
                                
                            }  else if (property->Attribute("name") == string("green")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.g = temp;
                                
                            }  else if (property->Attribute("name") == string("blue")){
                                int temp;
                                property->Attribute("value",&temp);
                                color.b = temp;
                            }
                            pGameObject->GetParams().setColor(color);
                            
                        }
                    }
                }
            }
            

            //If object is player set game player pointer accordingly
            if (pObjectLayer->getType() == string("player")){
                cout << "Adding player to object spot "
                << pObjectLayer->getGameObjects()->size() << "\n";
                
                if (Game::Instance()->isLiveModeOn()){
                    pGameObject = 0;
                } else {
                    pLevel->setPlayer(dynamic_cast<Player*>(pGameObject));
                    Game::Instance()->setPlayer(dynamic_cast<Player*>(pGameObject));
                }
            }
            
            if (pGameObject){
                if(pGameObject->GetParams().getType() == string("Light")){
                    pGameObject->GetParams().getPosition().setX
                    (pGameObject->GetParams().getPosition().getX() - pGameObject->GetParams().getWidth()/2);
                    
                    pGameObject->GetParams().getPosition().setY
                    (pGameObject->GetParams().getPosition().getY() - pGameObject->GetParams().getHeight()/2);
                }
            }
            
            if (pGameObject){
                pObjectLayer->getGameObjects()->push_back(pGameObject);
                cout << "Created new " << pGameObject->GetParams().getType() << "\n";
            }
        }
    }
    pLayers->push_back(pObjectLayer);
}
コード例 #2
0
	void CamSettingsPage::OnHScroll( PropertyType property, Gtk::Adjustment* pAdjustment )
	{
        Glib::Mutex::Lock scrollLock( m_hScrollMutex, Glib::NOT_LOCK );
        if ( scrollLock.try_acquire() != true )
        {
            return;
        }

		double value = pAdjustment->get_value();
        unsigned int valueReg = static_cast<unsigned int>(value);
		
        Error error;
		Property camProp;
		PropertyInfo camPropInfo;
		
		camProp.type = property;
		camPropInfo.type = property;
		
        error = m_pCamera->GetProperty( &camProp );
        if ( error != PGRERROR_OK )
        {
            // Error
            ShowErrorMessageDialog( "Error getting camera property", error );
        }

		error = m_pCamera->GetPropertyInfo( &camPropInfo );
        if ( error != PGRERROR_OK )
        {
            // Error
            ShowErrorMessageDialog( "Error getting camera property information", error );
        }

        // Clamp the abs value
        if ( value > camPropInfo.absMax )
        {
            value = camPropInfo.absMax;
        }

        if ( value < camPropInfo.absMin )
        {
            value = camPropInfo.absMin;
        }

        // Clamp the relative value
        if ( valueReg > camPropInfo.max )
        {
            valueReg = camPropInfo.max;
        }

        if ( valueReg < camPropInfo.min )
        {
            valueReg = camPropInfo.min;
        }
        
        if ( property == WHITE_BALANCE )
        {          
            if ( pAdjustment == m_widgetPropArray[property].pAdjustment1 )
            {
                camProp.valueA = valueReg;
            }
            else if ( pAdjustment == m_widgetPropArray[property].pAdjustment2 )
            {
                camProp.valueB = valueReg;
            }
            else
            {
                // Error
                return;
            }

            camProp.absControl = false;
        }
        else
        {
            if ( camPropInfo.absValSupported == true && 
                m_absMode == true )
            {
                if (property == BRIGHTNESS)
                {
                    // The brightness abs register sometimes starts drifting
                    // due to a rounding error between the camera and the
                    // actual value being held by the adjustment. To prevent
                    // this, only apply the change to the camera if the
                    // difference is greater than a specified amount.

                    // Check if the difference is greater than 0.005f.
                    const float difference = static_cast<float>(value - camProp.absValue);
                    if ( fabs(difference) <= 0.005f )
                    {
                        // The difference is too small, don't do anything
                        return;
                    }
                }

                camProp.absValue = static_cast<float>(value);
                camProp.absControl = true;
            }
            else
            {			
                camProp.valueA = valueReg;
                camProp.absControl = false;
            }
        }		
		
		error = m_pCamera->SetProperty( &camProp, false );
        if ( error != PGRERROR_OK )
        {
            // Error
            ShowErrorMessageDialog( "Error setting camera property", error );
        }
	}