Windows cocos2d-x integrated luasocket Module

Source: Internet
Author: User

This afternoon I spent some time on vs2010 to do the cocos2d-x integration luasocket, just a simple integration of the luasocket module,

I haven't done a decent test yet. I'm a little tired. My classmates are playing games and can't sleep. Forget it. Write a blog first and go back to the dormitory to bed.

 

Cocos2d-x in the previous version is supporting luasocket, but later versions disappear. I also went to the Forum to see, cocos2d-x

Maintenance staff said to keep the Development Kit clean so it removes the cocos2d-x, also, the previous version also supports LUA-llthread

But now there is no more, but there is nothing to do with it. We can integrate it by ourselves when necessary.

 

When I integrate the luasocket module by myself, I first roughly browsed the luasocket source code, which also provides a method for subsequent integration.

The following function is provided in luasocket. h:

 
1Luasocket_apiIntLuaopen_socket_core (lua_state * l) // socket module

Of course, there are other modules:

 1   Int Tcp_open (lua_state * l ); //  TCP Module  2   3 Mime_api Int Luaopen_mime_core (lua_state * l ); //  Mime Module  4   5   Int Udp_open (lua_state * l ); //  UDP Module  6   7   Int Select_open (lua_state * l ); //  Select Module  8   9   Int Inet_open (lua_state * l ); //  Inet Mode 

The idea is from here, luasocket source code has been registered tasks are encapsulated, so the next task is mainly in the source code of the cocos2d-x

Find the registration entry, and I do not need to explain the mutual calls between Lua and C/C ++. As we all know, this is directly included here.

 

Cocos2d-x for the Lua engine encapsulation management, the source code are in the scrpiting/Lua/cocos2dx_support folder.

  Class Ccluaengine: Public  Ccscriptengineprotocol { Public  :  Static Ccluaengine * defaultengine ( Void ); //  Engine Creation Method        Static Ccluaengine * Create ( Void  );  Virtual ~ Ccluaengine ( Void  );....  //  The intermediate method is omitted.    Private : Ccluaengine (  Void  ): M_state (null ){}  Bool Init ( Void  );  Bool Pushfunction ( Int  Nhandler); lua_state * M_state;  Static Ccluaengine * m_defaultengine; //  Static private variable };

 

1 Ccluaengine * ccluaengine: m_defaultengine = NULL;  2   3   //  Singleton, cocos2d-x source code in a large number of use Singleton.  4 Ccluaengine * ccluaengine: defaultengine ( Void  )  5   {  6       If (! M_defaultengine)  7  {  8 M_defaultengine = Ccluaengine: Create ();  9   }  10       Return  M_defaultengine;  11   }  12   13 Ccluaengine * ccluaengine: Create ( Void  )  14   { 15 Ccluaengine * pengine = New  Ccluaengine ();  16 Pengine-> Init (); //  Call initialization Registration  17       Return  Pengine;  18   }  19   20 Ccluaengine ::~ Ccluaengine ( Void  ) 21   {  22   Lua_close (m_state );  23       If ( This = M_defaultengine)  24   {  25 M_defaultengine = NULL;  26   }  27 }

So what needs to be done is to call the registration method provided by the luasocket module that I mentioned earlier in the init method.

 1   Bool Ccluaengine: Init ( Void  )  2   {  3 M_state = Lua_open ();  4   Lual_openlibs (m_state );  5   Tolua_cocos2d_open (m_state );  6   Toluafix_open (m_state ); 7 Luaopen_socket_core (m_state ); //  Here is the test I added.  8   # If (Cc_target_platform = cc_platform_android) 9   Addlualoader (loader_android );  10   # Endif  11       Return   True  ;  12 }

Note that you need to add the following header files in extern "C:

1   Extern   "  C  "  {  2 # Include "  Lualib. h  "  3 # Include "  Lauxlib. h  "  4 # Include "  Tolua_fix.h "  5 # Include "  Luasocket. h  "   //  Header files for testing  6 }

Then, just copy the remaining source files to the project.

You don't need to talk about anything else. Just copy the Lua module file in the source code to the DEBUG directory. Take the hellolua instance as an example. Below we can simply try to use luasocket in hello. Lua.

Module.

 

Here we provide a method to integrate other parts. My idea is to write and encapsulate all the sub-modules in the luasocket, saving the trouble of messy source code. next time.

 

In addition, I deleted the source code that uses the BSD style in the source code, because it is always unable to compile in. in principle, cross-platform compilation and macro control are adopted. I don't know why an error occurs. This is my first time using

For a long time, I am not familiar with this heavy guy. So simply just take out those files.

 

Then, add the following in the wsocket. h file header:

 
1# Include <Winsock. h>2 3 # PragmaComment (Lib, "wsock32.lib ")//Please do not reverse the order. This is common sense.

Configure the path of the database to be linked in the project properties, that is, under the System32 path. If you do not know this, you can also find it.

 

After that, I recently considered integrating and encapsulating luasocket, and then looking for a better thread module in Lua.

I don't know how it will work if I use the game fully developed by Lua.

 

Tired.

 

 

 

 

 

 

+

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.