Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am using curl in C++ to get an Image from a URL for a Cocos2dx Game. The Code is Working Fine. And using CCImage And cocos2d::CCTexture2D I create a Sprite and Add it on my Layer.

The Sprite is definitely getting added on the Layer, (I Know it because if i try to Add the Same Sprite It is getting crashed Stating Child already Added But its is no where to be found on Layer. I tried changing the BG Color, ZIndex of Sprite But the sprite is not visible.

here is the code:

void HelloWorld::getImageFromURL(const char* url)
{
    CURL *curl;       // CURL objects
    CURLcode res;
    MemoryStruct buffer; // memory buffer

    curl = curl_easy_init(); // init CURL library object/structure

    if(curl) {

        // set up the write to memory buffer
        // (buffer starts off empty)

        buffer.memory = NULL;
        buffer.size = 0;

        stringstream urlString;
        urlString<<url;
        // (N.B. check this URL still works in browser in case image has moved)

        CCLog("%s", urlString.str().c_str());

        curl_easy_setopt(curl, CURLOPT_URL, urlString.str().c_str());
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); // tell us what is happening

        // tell libcurl where to write the image (to a dynamic memory buffer)

        curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION, Utility::WriteMemoryCallback);
        curl_easy_setopt(curl,CURLOPT_WRITEDATA, (void *) &buffer);

        // get the image from the specified URL

        res = curl_easy_perform(curl);
        CCLog("Curl code: %i", res);

        CCImage* img = new CCImage();
        img->initWithImageData((void*)buffer.memory, (long)buffer.size, CCImage::kFmtPng);
        cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D(); //TODO:: leak
        texture->initWithImage(img);


        CCSprite* sprite = CCSprite::createWithTexture(texture, CCRectMake(0, 0, 256, 256));
        sprite->setPosition(CENTRE_OF_SCREEN);
        sprite->setContentSize(CCSize(256,256));
        sprite->setAnchorPoint(ccp(0, 0));
        this->addChild(sprite,1000,1000);

        curl_easy_cleanup(curl);
        free(buffer.memory);


    }

}

Any Help will be Greatly Appreciated.

Thanks a ton W

share|improve this question

put on hold as off-topic by Josh Petrie 2 days ago

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about debugging a problem in your project must present a concise selection of code and context so as to allow a reader to diagnose the issue without needing to read all of your code or to engage in extensive back-and-forth dialog. For more information, see this meta thread." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

    
it's just a hunch but check if either img or texture are getting deleted before you start any drawing operation. –  Ali.S May 24 '13 at 7:32

Browse other questions tagged or ask your own question.