void TexturePreLoad::timeoutCallback(float t) { // 不处理,不检测超时 return; if (mNeedLoadTaskNum <= 0) { TextAssert(false,"运行错误"); return; } int index = tasks.size() - mNeedLoadTaskNum; T_LOG("TexturePreLoad time out when load texture '%s'",tasks[index].c_str()); if (callback) { callback->textureTimeout(index,tasks[index]); } // 直接放弃,进入下一个纹理 mNeedLoadTaskNum -- ; if (mNeedLoadTaskNum > 0) { loadTexture(tasks[index+1].c_str(),true); } else { loadOver(); } }
void TexturePreLoad::timeToIgnoreTheRestImage(float t) { // 取消回调 CCDirector::sharedDirector()->getScheduler()->unscheduleSelector( schedule_selector(TexturePreLoad::timeToIgnoreTheRestImage),this); T_LOG("TexturePreLoad timeToIgnoreTheRestImage called ,the rest need load image num is %d",mNeedLoadTaskNum); loadOver(); }
bool SeimiServerHandler::handleRequest(Pillow::HttpConnection *connection){ QString method = connection->requestMethod(); QString path = connection->requestPath(); if(method == "GET"){ connection->writeResponse(405, Pillow::HttpHeaderCollection(),"Method 'GET' is not supprot,please use 'POST'"); return true; } if(path != "/doload"){ return false; } QEventLoop eventLoop; SeimiPage *seimiPage=new SeimiPage(this); QString url = connection->requestParamValue(urlP); int renderTime = connection->requestParamValue(renderTimeP).toInt(); QString proxyStr = connection->requestParamValue(proxyP); if(!proxyStr.isEmpty()){ QRegularExpression re("(?<protocol>http|https|socket)://(?:(?<user>\\w*):(?<password>\\w*)@)?(?<host>[\\w.]+)(:(?<port>\\d+))?"); QRegularExpressionMatch match = re.match(proxyStr); if(match.hasMatch()){ QNetworkProxy proxy; if(match.captured("protocol") == "socket"){ proxy.setType(QNetworkProxy::Socks5Proxy); }else{ proxy.setType(QNetworkProxy::HttpProxy); } proxy.setHostName(match.captured("host")); proxy.setPort(match.captured("port").toInt()==0?80:match.captured("port").toInt()); proxy.setUser(match.captured("user")); proxy.setPassword(match.captured("password")); seimiPage->setProxy(proxy); }else { qWarning("[seimi] proxy pattern error, proxy = %s",proxyStr.toUtf8().constData()); } } QString jscript = connection->requestParamValue(scriptP); QString postParamJson = connection->requestParamValue(postParamP); seimiPage->setScript(jscript); seimiPage->setPostParam(postParamJson); qInfo("[seimi] TargetUrl:%s ,RenderTime(ms):%d",url.toUtf8().constData(),renderTime); int useCookieFlag = connection->requestParamValue(useCookieP).toInt(); seimiPage->setUseCookie(useCookieFlag==1); QObject::connect(seimiPage,SIGNAL(loadOver()),&eventLoop,SLOT(quit())); seimiPage->toLoad(url,renderTime); eventLoop.exec(); Pillow::HttpHeaderCollection headers; headers << Pillow::HttpHeader("Content-Type", "text/html;charset=utf-8"); connection->writeResponse(200, headers,seimiPage->getContent().toUtf8()); seimiPage->deleteLater(); return true; }
void TexturePreLoad::textureLoadedCallback(CCObject* tex) { mNeedLoadTaskNum --; if (mNeedLoadTaskNum < 0) { mNeedLoadTaskNum = 0; // bug 修复 2013-4-17 // BUG : 修复了,纹理在home之后,又回来时,再次回调的bug。 // 应该是当时失败的纹理,在下次 // 这个完全是可以忽略的 TextAssert(false,"运行时出现问题了,多半是超时引起的"); } int index = tasks.size() - mNeedLoadTaskNum -1; T_LOG("texture loaded success --> [total = %d,left = %d]",tasks.size(),mNeedLoadTaskNum); if (callback) { callback->textureLoaded(mNeedLoadTaskNum,"NULL-Str"); } if (mNeedLoadTaskNum <= 0) { loadOver(); } // 看是否可以忽略剩下的图片 else if (mNeedLoadTaskNum <= IgnoreImageNum) { float delay = IgnorePerImageDelay*mNeedLoadTaskNum; T_LOG("TexturePreLoad will ignore the rest %d image(s) after %f seconds",mNeedLoadTaskNum,delay); // 取消回调 CCDirector::sharedDirector()->getScheduler()->unscheduleSelector( schedule_selector(TexturePreLoad::timeToIgnoreTheRestImage),this); // 回调 CCDirector::sharedDirector()->getScheduler()->scheduleSelector( schedule_selector(TexturePreLoad::timeToIgnoreTheRestImage),this,delay,false); } }
void TexturePreLoad::startAsync(TexturePreLoadCallBack* callback) { if (mBusy) { TextAssert(false,"任务还没有完成,不能再次开始"); return; } mBusy = true; // 不在一起载入了,而是一张一张的载入 this->callback = callback; //add by jj // 从第一张开始载入 // if ( tasks.size() > 0) // { // // 一次全部载入 // for (int i=0;i < tasks.size();i++) // { // loadTexture(tasks[i],true); // } // }else { loadOver(); } }
void GameScene::initGame() { blockVertexShader=new QGLShader(QGLShader::Vertex); blockVertexShader->compileSourceFile(QLatin1String(":/res/divinecraft/shader/block.vsh")); blockFragmentShader=new QGLShader(QGLShader::Fragment); blockFragmentShader->compileSourceFile(QLatin1String(":/res/divinecraft/shader/block.fsh")); blockProgram=new QGLShaderProgram; blockProgram->addShader(blockVertexShader); blockProgram->addShader(blockFragmentShader); if(!blockProgram->link()){ qWarning("Failed to compile and link shader program"); qWarning("Vertex shader log:"); qWarning() << blockVertexShader->log(); qWarning() << blockFragmentShader->log(); qWarning("Shader program log:"); qWarning() << blockProgram->log(); QMessageBox::warning(0,tr("错误"),tr("着色器程序加载失败造成游戏无法正常启动\n" "请联系开发者寻求解决方案"),QMessageBox::Ok); exit(1); } lineVertexShader=new QGLShader(QGLShader::Vertex); lineVertexShader->compileSourceFile(QLatin1String(":/res/divinecraft/shader/line.vsh")); lineFragmentShader=new QGLShader(QGLShader::Fragment); lineFragmentShader->compileSourceFile(QLatin1String(":/res/divinecraft/shader/line.fsh")); lineProgram=new QGLShaderProgram; lineProgram->addShader(lineVertexShader); lineProgram->addShader(lineFragmentShader); if(!lineProgram->link()){ qWarning("Failed to compile and link shader program"); qWarning("Vertex shader log:"); qWarning() << lineVertexShader->log(); qWarning() << lineFragmentShader->log(); qWarning("Shader program log:"); qWarning() << lineProgram->log(); QMessageBox::warning(0,tr("错误"),tr("着色器程序加载失败造成游戏无法正常启动\n" "请联系开发者寻求解决方案"),QMessageBox::Ok); exit(1); } //////////////////////////// camera=new Camera(QVector3D(0,4,0),QPointF(180.0,0.0)); world=new World; wThread=new QThread; world->moveToThread(wThread); connect(wThread,SIGNAL(finished()),world,SLOT(deleteLater())); //线程被销毁的同时销毁world connect(this,SIGNAL(reloadWorld()),world,SLOT(forcedUpdateWorld())); //强制进行世界刷新 connect(camera,SIGNAL(cameraMove(QVector3D)),world,SLOT(changeCameraPosition(QVector3D))); //连接camera移动与世界相机位移的槽 connect(this,SIGNAL(resetRenderLen()),world,SLOT(updateWorld())); connect(this,SIGNAL(addBlock()),camera,SLOT(addBlock())); connect(this,SIGNAL(removeBlock()),camera,SLOT(removeBlock())); connect(world,SIGNAL(loadOver()),this,SLOT(loadOverSlot())); wThread->start(); // world->setMaxRenderLen(maxRenderLen); world->setWorldName("new_world"); camera->setWorld(world); //传递世界指针 /////////////////////////// //这里是一个规定的加载顺序,后步骤会依赖于前步骤 world->loadBlockIndex(); //加载方块属性列表 loadTexture(); //加载纹理 //====================== line=new LineMesh(2); //十字准心 float lineLen=0.0004; line->addLine(QVector3D(-lineLen,0,-0.02),QVector3D(lineLen,0,-0.02)); line->addLine(QVector3D(0,-lineLen,-0.02),QVector3D(0,lineLen,-0.02)); lineQua=new LineMesh(12); //被选方块的包围线框 //======================= //数据面板 dataPanel=new DataPanel(0,0,200,100); addItem(dataPanel); glFps=0; drawCount=0; dataPanel->setRenderLen(maxRenderLen); connect(camera,SIGNAL(getPositions(QVector3D,QVector3D)),this,SLOT(dataShowPosition(QVector3D,QVector3D))); // dataPanel->hide(); //背包物品栏 backPackBar=new BackPackBar(this); hideBackPackBar(); backPackBar->setWorld(world); //传递world指针 //物品栏 itemBar=new ItemBar(this); connect(itemBar,SIGNAL(thingIndexChange(int)),camera,SLOT(setBlockId(int))); backPackBar->setPocket(itemBar); //======================= //消息面板 messagePanel=new MessagePanel; addItem(messagePanel); //=========================== //选项菜单 opWidget=new OptionsWidget(); opWidgetProxy=new QGraphicsProxyWidget(0); opWidgetProxy->setWidget(opWidget); this->addItem(opWidgetProxy); opWidgetProxy->hide(); inOpWidget=false; connect(opWidget,SIGNAL(continueGame()),this,SLOT(continueGame())); connect(opWidget,SIGNAL(mouseLevelValueChange(int)),camera,SLOT(setMouseLevel(int))); connect(opWidget,SIGNAL(renderValueChange(int)),this,SLOT(setRenderLen(int))); connect(opWidget,SIGNAL(quitClick()),gView,SLOT(close())); //============================= loadSettings(); camera->loadPosRot(); //加载位置视角信息 setRenderLen(maxRenderLen); //设置渲染距离并刷新整个世界 opWidget->setRenderLen(maxRenderLen); opWidget->setMouseLevel(camera->getMouseLevel()); }
bool SeimiServerHandler::handleRequest(Pillow::HttpConnection *connection){ QString method = connection->requestMethod(); QString path = connection->requestPath(); if(method == "GET"){ connection->writeResponse(405, Pillow::HttpHeaderCollection(),"Method 'GET' is not supprot,please use 'POST'"); return true; } if(path != "/doload"){ return false; } // QString url = QUrl::fromPercentEncoding(connection->requestParamValue(urlP).toUtf8()); QString url = connection->requestParamValue(urlP); int renderTime = connection->requestParamValue(renderTimeP).toInt(); QString proxyStr = connection->requestParamValue(proxyP); QString contentType = connection->requestParamValue(contentTypeP); QString outImgSizeStr = connection->requestParamValue(outImgSizeP); QString ua = connection->requestParamValue(uaP); // QString jscript = QUrl::fromPercentEncoding(connection->requestParamValue(scriptP).toUtf8()); QString jscript = connection->requestParamValue(scriptP); QString postParamJson = connection->requestParamValue(postParamP); int resourceTimeout = connection->requestParamValue(resourceTimeoutP).toInt(); Pillow::HttpHeaderCollection headers; headers << Pillow::HttpHeader("Pragma", "no-cache"); headers << Pillow::HttpHeader("Expires", "-1"); headers << Pillow::HttpHeader("Cache-Control", "no-cache"); try{ QEventLoop eventLoop; SeimiPage *seimiPage=new SeimiPage(this); if(!proxyStr.isEmpty()){ QRegularExpression reProxy("(?<protocol>http|https|socket)://(?:(?<user>\\w*):(?<password>\\w*)@)?(?<host>[\\w.]+)(:(?<port>\\d+))?"); QRegularExpressionMatch matchProxy = reProxy.match(proxyStr); if(matchProxy.hasMatch()){ QNetworkProxy proxy; if(matchProxy.captured("protocol") == "socket"){ proxy.setType(QNetworkProxy::Socks5Proxy); }else{ proxy.setType(QNetworkProxy::HttpProxy); } proxy.setHostName(matchProxy.captured("host")); proxy.setPort(matchProxy.captured("port").toInt()==0?80:matchProxy.captured("port").toInt()); proxy.setUser(matchProxy.captured("user")); proxy.setPassword(matchProxy.captured("password")); seimiPage->setProxy(proxy); }else { qWarning("[seimi] proxy pattern error, proxy = %s",proxyStr.toUtf8().constData()); } } seimiPage->setScript(jscript); seimiPage->setPostParam(postParamJson); qInfo("[seimi] TargetUrl:%s ,RenderTime(ms):%d",url.toUtf8().constData(),renderTime); int useCookieFlag = connection->requestParamValue(useCookieP).toInt(); seimiPage->setUseCookie(useCookieFlag==1); QObject::connect(seimiPage,SIGNAL(loadOver()),&eventLoop,SLOT(quit())); seimiPage->toLoad(url,renderTime,ua,resourceTimeout); eventLoop.exec(); if(contentType == "pdf"){ headers << Pillow::HttpHeader("Content-Type", "application/pdf"); QByteArray pdfContent = seimiPage->generatePdf(); QCryptographicHash md5sum(QCryptographicHash::Md5); md5sum.addData(pdfContent); QByteArray etag = md5sum.result().toHex(); headers << Pillow::HttpHeader("ETag", etag); connection->writeResponse(200,headers,pdfContent); }else if(contentType == "img"){ headers << Pillow::HttpHeader("Content-Type", "image/png"); QSize targetSize; if(!outImgSizeStr.isEmpty()){ QRegularExpression reImgSize("(?<xSize>\\d+)(?:x|X)(?<ySize>\\d+)"); QRegularExpressionMatch matchImgSize = reImgSize.match(outImgSizeStr); if(matchImgSize.hasMatch()){ targetSize.setWidth(matchImgSize.captured("xSize").toInt()); targetSize.setHeight(matchImgSize.captured("ySize").toInt()); } } QByteArray imgContent = seimiPage->generateImg(targetSize); QCryptographicHash md5sum(QCryptographicHash::Md5); md5sum.addData(imgContent); QByteArray etag = md5sum.result().toHex(); headers << Pillow::HttpHeader("ETag", etag); connection->writeResponse(200,headers,imgContent); }else{ headers << Pillow::HttpHeader("Content-Type", "text/html;charset=utf-8"); QString defBody = "<html>null</html>"; connection->writeResponse(200, headers,seimiPage->getContent().isEmpty()?defBody.toUtf8():seimiPage->getContent().toUtf8()); } seimiPage->deleteLater(); }catch (std::exception& e) { headers << Pillow::HttpHeader("Content-Type", "text/html;charset=utf-8"); QString errMsg = "<html>server error,please try again.</html>"; qInfo("[seimi error] Page error, url: %s, errorMsg: %s", url.toUtf8().constData(), QString(QLatin1String(e.what())).toUtf8().constData()); connection->writeResponse(500, headers, errMsg.toUtf8()); }catch (...) { qInfo() << "server error!"; headers << Pillow::HttpHeader("Content-Type", "text/html;charset=utf-8"); QString errMsg = "<html>server error,please try again.</html>"; connection->writeResponse(500, headers, errMsg.toUtf8()); } return true; }