Esempio n. 1
0
bool Sprite3D::initWithFile(const std::string &path)
{
    _meshes.clear();
    _meshVertexDatas.clear();
    CC_SAFE_RELEASE_NULL(_skeleton);
    removeAllAttachNode();
    
    if (loadFromCache(path))
        return true;
    
    //load from file
    std::string ext = path.substr(path.length() - 4, 4);
    std::transform(ext.begin(), ext.end(), ext.begin(), tolower);
    
    if (ext == ".obj")
    {
        return loadFromObj(path);
    }
    else if (ext == ".c3b" || ext == ".c3t")
    {
        return loadFromC3x(path);
    }
    
    return false;
}
Esempio n. 2
0
bool PlanetSprite::init()
{
    if(!Sprite3D::init())
        return false;

    //Loads the mesh from cache
    loadFromCache(PLANET_MESH_MARS);
    //Loads the texture
    setTexture(PLANET_TEXTURE_MARS);

    //Calculates a random rotation speed and Z axis inclination
    int rotationSpeed = RandomHelper::random_int(1, 3);
    unsigned int axisInclinitation = RandomHelper::random_int(-90, 90);

    //Calculates a random sacale
    float scale = RandomHelper::random_real(1.f, 3.f);

    //Calculates a irregular scale
    float scaleX = RandomHelper::random_real(1.0f, 1.25f) * scale;
    float scaleY = RandomHelper::random_real(1.0f, 1.25f) * scale;
    float scaleZ = RandomHelper::random_real(1.0f, 1.25f) * scale;

    setScaleX(scaleX);
    setScaleY(scaleY);
    setScaleZ(scaleZ);

    setRotation3D(Vec3(0, 0, axisInclinitation));

    runAction(RepeatForever::create(RotateBy::create(rotationSpeed, Vec3(0, 360, 0))));

    return true;
}
Esempio n. 3
0
void ModelManager::loadList()
{
	fs::path ModelPath(L"./Data/Models/"), CacheFilePath(L"./Data/ModelCache.dat");
	if (!fs::exists(ModelPath)) throw std::ios_base::failure("Model directory not found");

	if (loadFromCache(CacheFilePath, ModelPath))
		return;

	// Rebuild the cache file, since it is invalid
	loadInternal(ModelPath);
	saveToCache(CacheFilePath, ModelPath);
}
Esempio n. 4
0
bool Sprite3D::initWithFile(const std::string& path)
{
    _aabbDirty = true;
    _meshes.clear();
    _meshVertexDatas.clear();
    CC_SAFE_RELEASE_NULL(_skeleton);
    removeAllAttachNode();
    
    if (loadFromCache(path))
        return true;
    
    MeshDatas* meshdatas = new (std::nothrow) MeshDatas();
    MaterialDatas* materialdatas = new (std::nothrow) MaterialDatas();
    NodeDatas* nodeDatas = new (std::nothrow) NodeDatas();
    if (loadFromFile(path, nodeDatas, meshdatas, materialdatas))
    {
        if (initFrom(*nodeDatas, *meshdatas, *materialdatas))
        {
            //add to cache
            auto data = new (std::nothrow) Sprite3DCache::Sprite3DData();
            data->materialdatas = materialdatas;
            data->nodedatas = nodeDatas;
            data->meshVertexDatas = _meshVertexDatas;
            for (const auto mesh : _meshes) {
                data->glProgramStates.pushBack(mesh->getGLProgramState());
            }
            
            Sprite3DCache::getInstance()->addSprite3DData(path, data);
            CC_SAFE_DELETE(meshdatas);
            _contentSize = getBoundingBox().size;
            return true;
        }
    }
    CC_SAFE_DELETE(meshdatas);
    CC_SAFE_DELETE(materialdatas);
    CC_SAFE_DELETE(nodeDatas);
    
    return false;
}
Esempio n. 5
0
void AssetRequest::start() {
    if (QThread::currentThread() != thread()) {
        QMetaObject::invokeMethod(this, "start", Qt::AutoConnection);
        return;
    }

    if (_state != NotStarted) {
        qCWarning(asset_client) << "AssetRequest already started.";
        return;
    }

    // in case we haven't parsed a valid hash, return an error now
    if (!isValidHash(_hash)) {
        _error = InvalidHash;
        _state = Finished;

        emit finished(this);
        return;
    }
    
    // Try to load from cache
    _data = loadFromCache(getUrl());
    if (!_data.isNull()) {
        _error = NoError;

        _loadedFromCache = true;

        _state = Finished;
        emit finished(this);

        return;
    }

    _state = WaitingForData;

    auto assetClient = DependencyManager::get<AssetClient>();
    auto that = QPointer<AssetRequest>(this); // Used to track the request's lifetime
    auto hash = _hash;

    _assetRequestID = assetClient->getAsset(_hash, _byteRange.fromInclusive, _byteRange.toExclusive,
        [this, that, hash](bool responseReceived, AssetServerError serverError, const QByteArray& data) {

        if (!that) {
            qCWarning(asset_client) << "Got reply for dead asset request " << hash << "- error code" << _error;
            // If the request is dead, return
            return;
        }
        _assetRequestID = INVALID_MESSAGE_ID;

        if (!responseReceived) {
            _error = NetworkError;
        } else if (serverError != AssetServerError::NoError) {
            switch (serverError) {
                case AssetServerError::AssetNotFound:
                    _error = NotFound;
                    break;
                case AssetServerError::InvalidByteRange:
                    _error = InvalidByteRange;
                    break;
                default:
                    _error = UnknownError;
                    break;
            }
        } else {
            if (!_byteRange.isSet() && hashData(data).toHex() != _hash) {
                // the hash of the received data does not match what we expect, so we return an error
                _error = HashVerificationFailed;
            }

            if (_error == NoError) {
                _data = data;
                _totalReceived += data.size();
                emit progress(_totalReceived, data.size());

                if (!_byteRange.isSet()) {
                    saveToCache(getUrl(), data);
                }
            }
        }
        
        if (_error != NoError) {
            qCWarning(asset_client) << "Got error retrieving asset" << _hash << "- error code" << _error;
        }
        
        _state = Finished;
        emit finished(this);
    }, [this, that](qint64 totalReceived, qint64 total) {
        if (!that) {
            // If the request is dead, return
            return;
        }
        emit progress(totalReceived, total);
    });
}
Esempio n. 6
0
// -------------------------------------------------------------
bool Webpage::open(string url, bool wellFormed, bool useCache)
{	
	if(Webpage::cacheDirectory.empty())
	{
		useCache=false;
	}
	
	bool loaded=false;
	if(useCache)
	{
		locale loc;
		const collate<char>& coll = use_facet<collate<char> >(loc);
		long myhash = coll.hash(url.data(),url.data()+url.length());
		sprintf(cachefile, "%s/%ld.cache", Webpage::cacheDirectory.c_str(), myhash);
		
		loaded = loadFromCache();
	}
	
	if(!loaded)
	{
		contents = download(url, verbose);
		if(contents.empty())
		{
			if(verbose) cerr << "No contents downloaded." << endl;
			return false;
		}
		if(!wellFormed)
		{
			tidy_me();
		}
	}
	
	// get rid of doctype line.  It messes up the parser
	string prefix = "<!DOCTYPE";
	if(contents.compare(0, prefix.size(), prefix)==0)
	{
		size_t pos = contents.find(">");
		contents.erase(0, pos+1);
	}
	
	// HACK:  I think tidySaveString puts some extra junk at the end of the document
	// if there is anything after </html>, get rid of it.
	size_t end = contents.find("</html>");
	if(end!=string::npos)
	{
		contents.erase(end+7);
	}

	if(useCache)
	{
		saveToCache();
	}
	
	if(verbose)
		cerr << "Parsing document. Length: " << contents.length() << endl;
	
	
	doc = xmlParseMemory(contents.c_str(), contents.length());
	if (doc == NULL) {
		if(verbose)
			cerr << "ERROR: Unable to parse HTML" << endl;
		return false;
	}
	
	xpathCtx = xmlXPathNewContext(doc);
	if(xpathCtx == NULL) {
		xmlFreeDoc(doc);
		if(verbose)
			cerr << "ERROR: Unable to create new XPath context" << endl;
		return false;
	}
	
	return true;
}