Permalink
Browse files

Initial checkin

  • Loading branch information...
0 parents commit 737772964f1cf3140743a0b6c7190c4addf9fff1 @clawoo committed Sep 8, 2011
Showing 895 changed files with 259,875 additions and 0 deletions.
@@ -0,0 +1,2 @@
+build
+.DS_Store
@@ -0,0 +1,110 @@
+//
+// CuteAPultCocos2D_xAppDelegate.cpp
+// CuteAPultCocos2D-x
+//
+// Created by Clawoo on 9/8/11.
+// Copyright __MyCompanyName__ 2011. All rights reserved.
+//
+
+#include "AppDelegate.h"
+
+#include "cocos2d.h"
+#include "HelloWorldScene.h"
+
+USING_NS_CC;
+
+AppDelegate::AppDelegate()
+{
+
+}
+
+AppDelegate::~AppDelegate()
+{
+}
+
+bool AppDelegate::initInstance()
+{
+ bool bRet = false;
+ do
+ {
+#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
+
+ // Initialize OpenGLView instance, that release by CCDirector when application terminate.
+ // The HelloWorld is designed as HVGA.
+ CCEGLView * pMainWnd = new CCEGLView();
+ CC_BREAK_IF(! pMainWnd
+ || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 320, 480));
+
+#endif // CC_PLATFORM_WIN32
+
+#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
+ // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here.
+#endif // CC_PLATFORM_IOS
+
+#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
+ // android does not do anything
+#endif
+
+#if (CC_TARGET_PLATFORM == CC_PLATFORM_WOPHONE)
+
+ // Initialize OpenGLView instance, that release by CCDirector when application terminate.
+ // The HelloWorld is designed as HVGA.
+ CCEGLView* pMainWnd = new CCEGLView(this);
+ CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480));
+
+#ifndef _TRANZDA_VM_
+ // on wophone emulator, we copy resources files to Work7/TG3/APP/ folder instead of zip file
+ cocos2d::CCFileUtils::setResource("HelloWorld.zip");
+#endif
+
+#endif // CC_PLATFORM_WOPHONE
+
+ bRet = true;
+ } while (0);
+ return bRet;
+}
+
+bool AppDelegate::applicationDidFinishLaunching()
+{
+ // initialize director
+ CCDirector *pDirector = CCDirector::sharedDirector();
+ pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
+
+ // enable High Resource Mode(2x, such as iphone4) and maintains low resource on other devices.
+ // pDirector->enableRetinaDisplay(true);
+
+ // sets landscape mode
+ // pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
+
+ // turn on display FPS
+ pDirector->setDisplayFPS(true);
+
+ // set FPS. the default value is 1.0/60 if you don't call this
+ pDirector->setAnimationInterval(1.0 / 60);
+
+ // create a scene. it's an autorelease object
+ CCScene *pScene = HelloWorld::scene();
+
+ // run
+ pDirector->runWithScene(pScene);
+
+ return true;
+}
+
+// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
+void AppDelegate::applicationDidEnterBackground()
+{
+ CCDirector::sharedDirector()->pause();
+
+ // if you use SimpleAudioEngine, it must be pause
+ // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
+}
+
+// this function will be called when the app is active again
+void AppDelegate::applicationWillEnterForeground()
+{
+ CCDirector::sharedDirector()->resume();
+
+ // if you use SimpleAudioEngine, it must resume here
+ // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
+}
@@ -0,0 +1,51 @@
+//
+// CuteAPultCocos2D_xAppDelegate.h
+// CuteAPultCocos2D-x
+//
+// Created by Clawoo on 9/8/11.
+// Copyright __MyCompanyName__ 2011. All rights reserved.
+//
+
+#ifndef _APP_DELEGATE_H_
+#define _APP_DELEGATE_H_
+
+#include "CCApplication.h"
+
+/**
+@brief The cocos2d Application.
+
+The reason for implement as private inheritance is to hide some interface call by CCDirector.
+*/
+class AppDelegate : private cocos2d::CCApplication
+{
+public:
+ AppDelegate();
+ virtual ~AppDelegate();
+
+ /**
+ @brief Implement for initialize OpenGL instance, set source path, etc...
+ */
+ virtual bool initInstance();
+
+ /**
+ @brief Implement CCDirector and CCScene init code here.
+ @return true Initialize success, app continue.
+ @return false Initialize failed, app terminate.
+ */
+ virtual bool applicationDidFinishLaunching();
+
+ /**
+ @brief The function be called when the application enter background
+ @param the pointer of the application
+ */
+ virtual void applicationDidEnterBackground();
+
+ /**
+ @brief The function be called when the application enter foreground
+ @param the pointer of the application
+ */
+ virtual void applicationWillEnterForeground();
+};
+
+#endif // _APP_DELEGATE_H_
+
@@ -0,0 +1,215 @@
+//
+// HelloWorldScene.cpp
+// CuteAPultCocos2D-x
+//
+// Created by Clawoo on 9/8/11.
+// Copyright __MyCompanyName__ 2011. All rights reserved.
+//
+#include "HelloWorldScene.h"
+
+using namespace cocos2d;
+
+#define PTM_RATIO 32
+enum
+{
+ kTagTileMap = 1,
+ kTagSpriteManager = 1,
+ kTagAnimation1 = 1,
+};
+
+HelloWorld::HelloWorld()
+{
+ setIsTouchEnabled( true );
+ setIsAccelerometerEnabled( true );
+
+ CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
+ //UXLOG(L"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);
+
+ // Define the gravity vector.
+ b2Vec2 gravity;
+ gravity.Set(0.0f, -10.0f);
+
+ // Do we want to let bodies sleep?
+ bool doSleep = true;
+
+ // Construct a world object, which will hold and simulate the rigid bodies.
+ world = new b2World(gravity, doSleep);
+
+ world->SetContinuousPhysics(true);
+
+ /*
+ m_debugDraw = new GLESDebugDraw( PTM_RATIO );
+ world->SetDebugDraw(m_debugDraw);
+
+ uint flags = 0;
+ flags += b2DebugDraw::e_shapeBit;
+ flags += b2DebugDraw::e_jointBit;
+ flags += b2DebugDraw::e_aabbBit;
+ flags += b2DebugDraw::e_pairBit;
+ flags += b2DebugDraw::e_centerOfMassBit;
+ m_debugDraw->SetFlags(flags);
+ */
+
+ // Define the ground body.
+ b2BodyDef groundBodyDef;
+ groundBodyDef.position.Set(0, 0); // bottom-left corner
+
+ // Call the body factory which allocates memory for the ground body
+ // from a pool and creates the ground box shape (also from a pool).
+ // The body is also added to the world.
+ b2Body* groundBody = world->CreateBody(&groundBodyDef);
+
+ // Define the ground box shape.
+ b2PolygonShape groundBox;
+
+ // bottom
+ groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
+ groundBody->CreateFixture(&groundBox, 0);
+
+ // top
+ groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
+ groundBody->CreateFixture(&groundBox, 0);
+
+ // left
+ groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(0,0));
+ groundBody->CreateFixture(&groundBox, 0);
+
+ // right
+ groundBox.SetAsEdge(b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,0));
+ groundBody->CreateFixture(&groundBox, 0);
+
+
+ //Set up sprite
+
+ CCSpriteBatchNode *mgr = CCSpriteBatchNode::batchNodeWithFile("blocks.png", 150);
+ addChild(mgr, 0, kTagSpriteManager);
+
+ addNewSpriteWithCoords( CCPointMake(screenSize.width/2, screenSize.height/2) );
+
+ CCLabelTTF *label = CCLabelTTF::labelWithString("Tap screen", "Marker Felt", 32);
+ addChild(label, 0);
+ label->setColor( ccc3(0,0,255) );
+ label->setPosition( CCPointMake( screenSize.width/2, screenSize.height-50) );
+
+ schedule( schedule_selector(HelloWorld::tick) );
+}
+
+HelloWorld::~HelloWorld()
+{
+ delete world;
+ world = NULL;
+
+ //delete m_debugDraw;
+}
+
+void HelloWorld::draw()
+{
+ // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
+ // Needed states: GL_VERTEX_ARRAY,
+ // Unneeded states: GL_TEXTURE_2D, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
+ glDisable(GL_TEXTURE_2D);
+ glDisableClientState(GL_COLOR_ARRAY);
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+
+ //world->DrawDebugData();
+
+ // restore default GL states
+ glEnable(GL_TEXTURE_2D);
+ glEnableClientState(GL_COLOR_ARRAY);
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+}
+
+void HelloWorld::addNewSpriteWithCoords(CCPoint p)
+{
+ //UXLOG(L"Add sprite %0.2f x %02.f",p.x,p.y);
+ CCSpriteBatchNode* sheet = (CCSpriteBatchNode*)getChildByTag(kTagSpriteManager);
+
+ //We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
+ //just randomly picking one of the images
+ int idx = (CCRANDOM_0_1() > .5 ? 0:1);
+ int idy = (CCRANDOM_0_1() > .5 ? 0:1);
+ CCSprite *sprite = CCSprite::spriteWithBatchNode(sheet, CCRectMake(32 * idx,32 * idy,32,32));
+ sheet->addChild(sprite);
+
+ sprite->setPosition( CCPointMake( p.x, p.y) );
+
+ // Define the dynamic body.
+ //Set up a 1m squared box in the physics world
+ b2BodyDef bodyDef;
+ bodyDef.type = b2_dynamicBody;
+ bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
+ bodyDef.userData = sprite;
+ b2Body *body = world->CreateBody(&bodyDef);
+
+ // Define another box shape for our dynamic body.
+ b2PolygonShape dynamicBox;
+ dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
+
+ // Define the dynamic body fixture.
+ b2FixtureDef fixtureDef;
+ fixtureDef.shape = &dynamicBox;
+ fixtureDef.density = 1.0f;
+ fixtureDef.friction = 0.3f;
+ body->CreateFixture(&fixtureDef);
+}
+
+
+void HelloWorld::tick(ccTime dt)
+{
+ //It is recommended that a fixed time step is used with Box2D for stability
+ //of the simulation, however, we are using a variable time step here.
+ //You need to make an informed choice, the following URL is useful
+ //http://gafferongames.com/game-physics/fix-your-timestep/
+
+ int velocityIterations = 8;
+ int positionIterations = 1;
+
+ // Instruct the world to perform a single step of simulation. It is
+ // generally best to keep the time step and iterations fixed.
+ world->Step(dt, velocityIterations, positionIterations);
+
+ //Iterate over the bodies in the physics world
+ for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
+ {
+ if (b->GetUserData() != NULL) {
+ //Synchronize the AtlasSprites position and rotation with the corresponding body
+ CCSprite* myActor = (CCSprite*)b->GetUserData();
+ myActor->setPosition( CCPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO) );
+ myActor->setRotation( -1 * CC_RADIANS_TO_DEGREES(b->GetAngle()) );
+ }
+ }
+}
+
+void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
+{
+ //Add a new body/atlas sprite at the touched location
+ CCSetIterator it;
+ CCTouch* touch;
+
+ for( it = touches->begin(); it != touches->end(); it++)
+ {
+ touch = (CCTouch*)(*it);
+
+ if(!touch)
+ break;
+
+ CCPoint location = touch->locationInView(touch->view());
+
+ location = CCDirector::sharedDirector()->convertToGL(location);
+
+ addNewSpriteWithCoords( location );
+ }
+}
+
+CCScene* HelloWorld::scene()
+{
+ // 'scene' is an autorelease object
+ CCScene *scene = CCScene::node();
+
+ // add layer as a child to scene
+ CCLayer* layer = new HelloWorld();
+ scene->addChild(layer);
+ layer->release();
+
+ return scene;
+}
Oops, something went wrong.

0 comments on commit 7377729

Please sign in to comment.