Cocos2d-x C + + Gets the network picture cache and shows

Source: Internet
Author: User
Tags sprintf

#ifndef __httpgetimg__

#define __httpgetimg__

#include "Cocos2d.h"

#include "HttpRequest.h"

#include "HttpClient.h"

Using_ns_cc_ext;

using namespace Std;

USING_NS_CC;

Class Httpgetimg:public Cocos2d::cclayer

{

Public

Httpgetimg ();

~httpgetimg ();

Get network picture, need address

static void Gethttpimg (ccobject* obj, string imgurl,int number);

void Onhttprequestrptimg (Cocos2d::extension::cchttpclient *sender, Cocos2d::extension::cchttpresponse *response);

Set new picture size by original default avatar scale

static void setimgsize (int oldhead_w,int oldhead_h,ccsprite* sprite);

Create a picture to overwrite an existing picture

static void Createimg (ccsprite* tagsprite,ccsprite* oldsprite,cctexture2d* texture,int newtag);

Private

};

#endif/* httpgetimg */

#include "HttpGetImg.h"

#include ". /datamodel/globalinfo.h "

#include ". /gameconfig.h "

#include ". /commen/publicdofunc.h "

Httpgetimg::httpgetimg (void)

{

}

void Httpgetimg::gethttpimg (ccobject* obj, string imgurl,int number)

{

cchttprequest* request = new Cchttprequest ();

Required Fields

Request->seturl (Imgurl.c_str ());

Request->setrequesttype (Cchttprequest::khttpget);

Request->setresponsecallback (obj, Httpresponse_selector (httpgetimg::onhttprequestrptimg));

Optional fields

Char thisnumber[10] = "";

sprintf (Thisnumber, "%d", number);

Request->settag (Thisnumber);

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

Request->release ();

}

void Httpgetimg::onhttprequestrptimg (Cchttpclient *sender, Cchttpresponse *response)

{

Char c_tag[20]= "";

sprintf (C_tag, "%s", Response->gethttprequest ()->gettag ());

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

string str_tag = C_tag;

if (!response)

{

Return

}

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

if (0! = strlen (Response->gethttprequest ()->gettag ()))

{

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

}

if (!response->issucceed ())

{

Cclog ("Response failed");

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

Return

}

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

Create image

ccimage* img = new Ccimage;

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

Create texture

cctexture2d* texture = new cctexture2d ();

BOOL isimg = Texture->initwithimage (IMG);

Img->release ();

Save the texture.

Texture

It is the part of your own encapsulated publicdofunc that asynchronously calls the main thread to perform a swap operation, and you can encapsulate it yourself.

SEL_CALLFUNCN pfncallback =callfuncn_selector (view_room::upplayerhead);

ccnode* nd = Ccnode::create ();

Buffer->clear ();

Nd->settag (Ccstring::create (Str_tag)->intvalue ());

PUBLICDOFUNC::TODOFUNCN (Layer_room, pfncallback,nd);

}

void httpgetimg::setimgsize (int oldhead_w,int oldhead_h,ccsprite* sprite)

{

Float s_x=oldhead_w/sprite->getcontentsize (). width;

Float s_y=oldhead_h/sprite->getcontentsize (). Height;

Sprite->setscalex (s_x);

Sprite->setscaley (s_y);

}

void Httpgetimg::createimg (ccsprite* tagsprite,ccsprite* oldsprite,cctexture2d* texture,int newTag)

{

int OLDW =oldsprite->getcontentsize (). width;

int OLDH =oldsprite->getcontentsize (). Height;

if (texture!=null)

{

ccsprite* Sprite = ccsprite::createwithtexture (texture);

Set by original scale

Setimgsize (Oldw,oldh,sprite);

Sprite->setposition (Oldsprite->getposition ());

Tagsprite->addchild (Sprite,1,newtag);

Texture->release ();

}

}

Httpgetimg::~httpgetimg (void)

{

}

Cocos2d-x C + + Gets the network picture cache and shows

Related Article

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.