标签:
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include <stdio.h> #include "external/curl/include/win32/curl/curl.h" Scene* HelloWorld::createScene() { // ‘scene‘ is an autorelease object auto scene = Scene::create(); // ‘layer‘ is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } size_t process_data(void* buffer, size_t size, size_t nmemb, void* uer_p) { FILE* fp = (FILE*)uer_p; size_t retsize = fwrite(buffer, size, nmemb, fp); return retsize; } size_t progress_back(void* tp, double dltotal, double dlnow, double ultotal, double ulnow) { int per = (int)(dlnow * 100.0 / dltotal); CCLOG("now load:%d", per); Label* lbl = (Label*)tp; String str; str.initWithFormat("load.......:%d", per); lbl->setString(str.getCString()); return 0; } void HelloWorld::mythread() { CURL* curl; CURLcode retCode; curl = curl_easy_init(); if (curl) { FILE* fp = fopen("data.html", "ab+"); curl_easy_setopt(curl, CURLOPT_URL, "http://www.360kan.com/"); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &process_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L); curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &progress_back); curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this->lbl); retCode = curl_easy_perform(curl); if (retCode != 0) { CCLOG("curl error,errorcode:%d", retCode); } fclose(fp); curl_easy_cleanup(curl); } } void HelloWorld::onEnter() { Layer::onEnter(); std::thread t(&HelloWorld::mythread, this); t.detach(); } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } this->lbl = Label::create(); this->addChild(this->lbl); this->lbl->setPosition(Vec2(100, 100)); lbl->setString(""); return true; }
标签:
原文地址:http://www.cnblogs.com/OrangeLife/p/5699334.html