Asynchronously loading textures in cocos2d-x Lua

Source: Internet
Author: User
Tags lua
Preface


problem: recently the project needs to make a loading interface, in the middle of the interface there is a character moving around the animation, in the display of the loading interface when loading resources, the project is implemented with Cocos2d-x Lua, After the interface is done, the interface will be jammed when the resource is loaded.


cause: because it is not asynchronous loading, and Cocos2d-x does not have an API bound to the asynchronous load resource into Lua, it is not implemented asynchronously in Lua.


You want to load resources by starting a thread in Lua, but Lua does not support multithreading, only the co-threads, but not the real multi-threading, but is the reciprocal exchange of executive power between functions.


Solution Ideas:

Store the path of the resource that needs to be loaded into a ccarray, and through the tolua++ tool (view the use of the tolua++ tool), bind an interface in C + + that asynchronously loads the texture into LUA, which receives two parameters, one for the Ccarray (an array of resource paths), Parameter two is of type int (LUA callback function) (click to view C + + callback Lua function) to iterate over the resource path array in the binding interface method, load the texture using Cctexturecache's Addimageasync method, Callback Lua method when resource loading is complete



Sample Code


1. Writing an asynchronous resource loading interface class bound to Lua


AsynResLoader.h

#ifndef  _asynresloader_h_
#define _asynresloader_h_

#include "cocos2d.h"

using_ns_cc;

Asynchronous resource loader for LUA invocation
class Asynresloader:public Ccobject
{
private:	
	int count;
	int total;
	Lua callback method
	int mluacallback;
Public:
	//Create an asynchronous resource loader
	static asynresloader* create ();
	Asynchronously loads
	the texture void asynloadtexture (ccarray* paths, int luacallbck);
	Load callback
	void callback (ccobject* psender);
};

#endif

AsynResLoader.cpp

#include "AsynResLoader.h" #include "CCLuaEngine.h"//Create an Asynchronous resource loader asynresloader* asynresloader::create () {Asynresload
	er* instance = new Asynresloader;
		if (instance) {instance->autorelease ();
	return instance;
} return NULL;
	}//asynchronously load texture void asynresloader::asynloadtexture (ccarray* paths, int luacallback) {this->count = 0;
	This->total = Paths->count ();
	
	This->mluacallback = Luacallback; 		
		for (int idx = 0; idx <total; idx++) {Const char* Path = ((ccstring*) paths->objectatindex (idx))->getcstring ();
		Cclog ("Asynloadtexture PATH idx=%d:%s", Idx,path);
	Cctexturecache::sharedtexturecache ()->addimageasync (path, this, Callfunco_selector (Asynresloader::callback));
	}}//Texture load callback method void Asynresloader::callback (ccobject*) {this->count++;
		When the resource load is complete, the callback specifies the LUA function if (this->count >= this->total) {cclog ("Asyn load res completed."); if (this->mluacallback) {ccluastack* pstack = ccluaengine::d efaultengine ()->getluaStack ();
			The first parameter is the integer handle of the function, and the second parameter is the number of function arguments Pstack->executefunctionbyhandler (this->mluacallback,0);
			Pstack->clean ();
		Cclog ("Call Lua function.."); }
	}
}


2. Use the tolua++ tool to bind classes to LUA (click to see how to use them)


3. Create an array of resource paths in Lua, invoke an interface method, and pass an array of paths and callback functions in


Lua Code:

function Asynloadtexture (callback)
    Local pngs = Ccarray:create ()
    --Buff animation frame Path    
    for _, Anim in Ipairs (buff_ Anim_config.buff_anim_config) do
        pngs:addobject (Ccstring:create (anim.png))
    end
    --skill animation frame path
    for K, Anim in Ipairs (skill_anim_config.skill_anim_config) do
        pngs:addobject (Ccstring:create (anim.png))
    End
    Asynresloader:create (): Asynloadtexture (pngs,callback)
end


4. Operation effect


The essence is that Lua calls the C + + method to load resources asynchronously.


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.