[Cocos2dx] Build a dashboard

Source: Internet
Author: User
Tags connect socket htons

For a while, cocos2dx has not been touched. Today, I want to feel warm again. Otherwise, I will be ruined by my previous studies. I 'd like to be a landlord. I will write online files on the server end later, create an interface today!

:
Code of the created interface:
# Include "HelloWorldScene. h" # include "SimpleAudioEngine. h" # include
 
  
# Include
  
   
Using namespace cocos2d; using namespace CocosDenshion; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} int socketHandle = 0; // create a BSD connection // ip and portint HelloWorld: connect (const cha R * ip, unsigned short port) {struct sockaddr_in sa; struct hostent * hp; hp = gethostbyname (ip); if (! Hp) {return-1;} memset (& sa, 0, sizeof (sa); memcpy (char *) & sa. sin_addr, hp-> h_addr, hp-> h_length); sa. sin_family = hp-> h_addrtype; sa. sin_port = HTONS (port); socketHandle = socket (sa. sin_family, SOCK_STREAM, 0); if (socketHandle <0) {printf ("failed to create socket \ n"); return-1;} if (: connect (socketHandle, (sockaddr *) & sa, sizeof (sa) {printf ("failed to connect socket \ n"); return-1;} CCLog (" Client connect OK! IP: % s: % d ", ip, port); return 0 ;}// create thread int HelloWorld: threadStart () {int errCode = 0; do {pthread_attr_t tAttr; errCode = pthread_attr_init (& tAttr); CC_BREAK_IF (errCode! = 0); errCode = pthread_attr_setdetachstate (& tAttr, PTHREAD_CREATE_DETACHED); if (errCode! = 0) {pthread_attr_destroy (& tAttr); break;} // errCode = pthread_create (& threadHimi, & tAttr, thread_function, this);} while (0); return errCode ;} // on "init" you need to initialize your instancebool HelloWorld: init () {// 1. super init first if (! CCLayer: init () {return false;} // char * ip = "192.168.1.24"; // unsigned short port = 5005; // int con = HelloWorld :: connect (ip, port); // if (con> 0) {// printf ("error "); //} // else // {// printf ("Login successful"); // ask director the window size CCSize size = CCDirector ctor: shareddire () -> getWinSize (); CCSprite * bg = CCSprite: create ("bg.jpg"); bg-> setAnchorPoint (CCPointMake (0, 1); bg-> setPosi Tion (CCPointMake (0, size. height); bg-> setScaleX (0.58); this-> addChild (bg); CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFramesWithFile ("card. plist "); CCSpriteBatchNode * batchNode = CCSpriteBatchNode: create (" card. pvr. ccz "); batchNode-> setPosition (CCPointZero); this-> addChild (batchNode); for (int I = 1; I <= 13; I ++) {CCString * name = CCString: createWithFormat ("hongw.d.png", I); CCSprite * ca Rd1 = CCSprite: createWithSpriteFrameName (name-> getCString (); card1-> setScale (0.7); card1-> setPosition (CCPointMake (140 + I * 15, size. height/2-95); batchNode-> addChild (card1);} CCSprite * role = CCSprite: createWithSpriteFrameName ("role.png"); role-> setScale (0.6 ); role-> setPosition (CCPointMake (60, 80); batchNode-> addChild (role); CCSprite * role1 = CCSprite: createWithSpriteFrameName ("role1.png"); r Ole1-> setScale (0.5); role1-> setPosition (CCPointMake (60,220); batchNode-> addChild (role1); CCSprite * role2 = CCSprite :: createWithSpriteFrameName ("role1.png"); role2-> setScale (0.5); role2-> setPosition (CCPointMake (420,220); batchNode-> addChild (role2 ); // left card for (int I = 2; I <= 13; I ++) {CCSprite * card1 = CCSprite: createWithSpriteFrameName ("SmallCardBack1.png "); card1-& gt; setScale (0.6); card1-& gt; setRotation (-82); card1-> setPosition (CCPointMake (115 + I, size. height/2-13 + I * 11); batchNode-> addChild (card1);} // right card for (int I = 2; I <= 13; I ++) {CCSprite * card1 = CCSprite: createWithSpriteFrameName ("SmallCardBack1.png"); card1-> setScale (0.6); card1-> setRotation (82 ); card1-> setPosition (CCPointMake (365-i, size. height/2-13 + I * 11); batchNode-> addChild (card1);} CCMenuItemImage * pCloseItem = CCMenuItemImage: create ("CloseNormal.png", "CloseSelected.png", this, menu_selector (HelloWorld: menuCloseCallback); pCloseItem-> setPosition (ccp (CCDirector: shareddire()-> getWinSize (). width/2, size. height-20); CCMenu * pMenu = CCMenu: create (pCloseItem, NULL); pMenu-> setPosition (CCPointZero); this-> addChild (pMenu, 1 ); bgMusic = true; return true;} void HelloWorld: menuCloseCallback (CCObject * pSender) {// CCDirect Or: sharedDirector ()-> end (); // # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) // exit (0); // # endif bgMusic =! BgMusic; if (bgMusic) {SimpleAudioEngine: sharedEngine ()-> pauseBackgroundMusic ();} else {// play scene music SimpleAudioEngine: sharedEngine () -> playBackgroundMusic ("bg.mp3", true );}}
  
 



The following sections describe Socket and multithreading in order to prepare the network end in the future. We will post them first. If you are interested in your own research, you can refer to the Himi blog on this topic: Prepare:
Int socketHandle = 0; // create a BSD connection // ip and portint HelloWorld: connect (const char * ip, unsigned short port) {struct sockaddr_in sa; struct hostent * hp; hp = gethostbyname (ip); if (! Hp) {return-1;} memset (& sa, 0, sizeof (sa); memcpy (char *) & sa. sin_addr, hp-> h_addr, hp-> h_length); sa. sin_family = hp-> h_addrtype; sa. sin_port = HTONS (port); socketHandle = socket (sa. sin_family, SOCK_STREAM, 0); if (socketHandle <0) {printf ("failed to create socket \ n"); return-1;} if (: connect (socketHandle, (sockaddr *) & sa, sizeof (sa) {printf ("failed to connect socket \ n"); return-1;} CCLog (" Client connect OK! IP: % s: % d ", ip, port); return 0 ;}

Multi-threaded Code:
// Create thread int HelloWorld: threadStart () {int errCode = 0; do {pthread_attr_t tAttr; errCode = pthread_attr_init (& tAttr); CC_BREAK_IF (errCode! = 0); errCode = pthread_attr_setdetachstate (& tAttr, PTHREAD_CREATE_DETACHED); if (errCode! = 0) {pthread_attr_destroy (& tAttr); break;} // errCode = pthread_create (& threadHimi, & tAttr, thread_function, this);} while (0); return errCode ;}



The above is a very simple setup of interface code, but it has been tuned for a while, and later continue with the new logic code!

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.