COCOS2DX 3.x (load Web custom avatar)

Source: Internet
Author: User

//

Connection.h

Xxdemo

//

Created by Leehongee on 14-9-4.

//

//

#ifndef __xxdemo__connection__

#define __XXDEMO__CONNECTION__

#include <iostream>

#include "Cocos2d.h"

#include "Network/httpclient.h"

USING_NS_CC;

using namespace Std;

using namespace Cocos2d::network;

Class connection{

Public

void Dohttprequest (const char* url,const char* requestdata,const char* tag,const cchttprequestcallback& callback);

void onhttprequestimagecompleted (HttpClient *sender, HttpResponse *response);

Private

Get Request Network Picture Resource

texture2d* texture;

};

#endif/* Defined (__xxdemo__connection__) */

//

Connection.cpp

Xxxdemo

//

Created by Leehongee on 14-9-4.

//

//

#include "Connection.h"

/**

After compiling to Android platform, pay attention to adding network permissions

**/

void Connection::d ohttprequest (const char* url,const char* requestdata,const char* tag,const cchttprequestcallback & callback) {

httprequest* request = new HttpRequest ();

Request->seturl (URL);

Request->setrequesttype (httprequest::type::P ost);

This is the callback object and the callback function

Request->setresponsecallback (callback);

Request->settag (tag);

The requested data

if (strlen (requestData)!=0) {

Request->setrequestdata (Requestdata,strlen (requestData));

}

Httpclient::getinstance ()->send (request);

Freeing memory

Request->release ();

}

void connection::onhttprequestimagecompleted (HttpClient *sender, HttpResponse *response) {

if (!response)

{

Return

}

You can get original request type From:response->request->reqtype

Const char* tag = Response->gethttprequest ()->gettag ();

if (0! = strlen (tag))

{

Log ("%s completed", Response->gethttprequest ()->gettag ());

}

if (!response->issucceed ())

{

Log ("Error buffer:%s", Response->geterrorbuffer ());

Return

}

std::vector<char> *buffer = Response->getresponsedata ();

Create image

image* image = new Image;

Image->initwithimagedata (unsigned char*) buffer->data (), buffer->size ());

Create texture

Texture = new Texture2d ();

BOOL Isimage = texture->initwithimage (image);

if (isimage) {

Log ("Texture2d initialization!");

}

Image->release ();

}

COCOS2DX 3.x (load Web custom avatar)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.