Source formatting, clean up.

This commit is contained in:
NathanSweet 2013-03-25 22:27:00 +01:00
parent 1f991ac234
commit ec8509b511
8 changed files with 129 additions and 172 deletions

View File

@ -9,86 +9,67 @@
USING_NS_CC; USING_NS_CC;
using namespace std; using namespace std;
AppDelegate::AppDelegate() { AppDelegate::AppDelegate () {
} }
AppDelegate::~AppDelegate() AppDelegate::~AppDelegate () {
{
} }
bool AppDelegate::applicationDidFinishLaunching() { bool AppDelegate::applicationDidFinishLaunching () {
// initialize director CCDirector* director = CCDirector::sharedDirector();
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); CCEGLView* view = CCEGLView::sharedOpenGLView();
director->setOpenGLView(view);
pDirector->setOpenGLView(pEGLView); view->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder);
// Set the design resolution // In this demo, we select resource according to the frame's height.
pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder); // If the resource size is different from design resolution size, you need to set contentScaleFactor.
// We use the ratio of resource's height to the height of design resolution,
CCSize frameSize = pEGLView->getFrameSize(); // this can make sure that the resource's height could fit for the height of design resolution.
vector<string> searchPath; vector<string> searchPath;
CCSize frameSize = view->getFrameSize();
// In this demo, we select resource according to the frame's height. if (frameSize.height > mediumResource.size.height) {
// If the resource size is different from design resolution size, you need to set contentScaleFactor. // if the frame's height is larger than the height of medium resource size, select large resource.
// We use the ratio of resource's height to the height of design resolution, searchPath.push_back(largeResource.directory);
// this can make sure that the resource's height could fit for the height of design resolution.
director->setContentScaleFactor( //
// if the frame's height is larger than the height of medium resource size, select large resource. MIN(largeResource.size.height / designResolutionSize.height, //
if (frameSize.height > mediumResource.size.height) largeResource.size.width / designResolutionSize.width));
{ } else if (frameSize.height > smallResource.size.height) {
searchPath.push_back(largeResource.directory); // if the frame's height is larger than the height of small resource size, select medium resource.
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width));
director->setContentScaleFactor( //
MIN(mediumResource.size.height / designResolutionSize.height, //
mediumResource.size.width / designResolutionSize.width));
} else {
// if the frame's height is smaller than the height of medium resource size, select small resource.
searchPath.push_back(smallResource.directory);
director->setContentScaleFactor( //
MIN(smallResource.size.height / designResolutionSize.height, //
smallResource.size.width / designResolutionSize.width));
} }
// if the frame's height is larger than the height of small resource size, select medium resource.
else if (frameSize.height > smallResource.size.height) searchPath.push_back("common");
{ CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
searchPath.push_back(mediumResource.directory);
director->setDisplayStats(true);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width)); director->setAnimationInterval(1.0 / 60);
} CCScene *pScene = ExampleScene::scene();
// if the frame's height is smaller than the height of medium resource size, select small resource. director->runWithScene(pScene);
else
{ return true;
searchPath.push_back(smallResource.directory);
pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width));
}
searchPath.push_back("common");
// set searching path
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn on display FPS
pDirector->setDisplayStats(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 = ExampleScene::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 () {
void AppDelegate::applicationDidEnterBackground() { CCDirector::sharedDirector()->stopAnimation();
CCDirector::sharedDirector()->stopAnimation(); // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
// 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 () {
void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->startAnimation();
CCDirector::sharedDirector()->startAnimation(); // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
} }

View File

@ -1,38 +1,16 @@
#ifndef _APP_DELEGATE_H_ #ifndef _APPDELEGATE_H_
#define _APP_DELEGATE_H_ #define _APPDELEGATE_H_
#include "cocos2d.h" #include "cocos2d.h"
/** class AppDelegate: private cocos2d::CCApplication {
@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: public:
AppDelegate(); AppDelegate ();
virtual ~AppDelegate(); virtual ~AppDelegate ();
/** virtual bool applicationDidFinishLaunching ();
@brief Implement CCDirector and CCScene init code here. virtual void applicationDidEnterBackground ();
@return true Initialize success, app continue. virtual void applicationWillEnterForeground ();
@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_ #endif // _APPDELEGATE_H_

View File

@ -1,26 +1,26 @@
#ifndef __APPMACROS_H__ #ifndef _APPMACROS_H_
#define __APPMACROS_H__ #define _APPMACROS_H_
#include "cocos2d.h" #include "cocos2d.h"
/* For demonstrating using one design resolution to match different resources, /* For demonstrating using one design resolution to match different resources,
or one resource to match different design resolutions. or one resource to match different design resolutions.
[Situation 1] Using one design resolution to match different resources. [Situation 1] Using one design resolution to match different resources.
Please look into Appdelegate::applicationDidFinishLaunching. Please look into Appdelegate::applicationDidFinishLaunching.
We check current device frame size to decide which resource need to be selected. We check current device frame size to decide which resource need to be selected.
So if you want to test this situation which said in title '[Situation 1]', So if you want to test this situation which said in title '[Situation 1]',
you should change ios simulator to different device(e.g. iphone, iphone-retina3.5, iphone-retina4.0, ipad, ipad-retina), you should change ios simulator to different device(e.g. iphone, iphone-retina3.5, iphone-retina4.0, ipad, ipad-retina),
or change the window size in "proj.XXX/main.cpp" by "CCEGLView::setFrameSize" if you are using win32 or linux plaform or change the window size in "proj.XXX/main.cpp" by "CCEGLView::setFrameSize" if you are using win32 or linux plaform
and modify "proj.mac/AppController.mm" by changing the window rectangle. and modify "proj.mac/AppController.mm" by changing the window rectangle.
[Situation 2] Using one resource to match different design resolutions. [Situation 2] Using one resource to match different design resolutions.
The coordinates in your codes is based on your current design resolution rather than resource size. The coordinates in your codes is based on your current design resolution rather than resource size.
Therefore, your design resolution could be very large and your resource size could be small. Therefore, your design resolution could be very large and your resource size could be small.
To test this, just define the marco 'TARGET_DESIGN_RESOLUTION_SIZE' to 'DESIGN_RESOLUTION_2048X1536' To test this, just define the marco 'TARGET_DESIGN_RESOLUTION_SIZE' to 'DESIGN_RESOLUTION_2048X1536'
and open iphone simulator or create a window of 480x320 size. and open iphone simulator or create a window of 480x320 size.
[Note] Normally, developer just need to define one design resolution(e.g. 960x640) with one or more resources. [Note] Normally, developer just need to define one design resolution(e.g. 960x640) with one or more resources.
*/ */
#define DESIGN_RESOLUTION_480X320 0 #define DESIGN_RESOLUTION_480X320 0
@ -31,16 +31,15 @@
/* If you want to switch design resolution, change next line */ /* If you want to switch design resolution, change next line */
#define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_480X320 #define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_480X320
typedef struct tagResource typedef struct tagResource {
{ cocos2d::CCSize size;
cocos2d::CCSize size; char directory[100];
char directory[100]; } Resource;
}Resource;
static Resource smallResource = { cocos2d::CCSizeMake(480, 320), "iphone" }; static Resource smallResource = {cocos2d::CCSizeMake(480, 320), "iphone"};
static Resource mediumResource = { cocos2d::CCSizeMake(960, 640), "iphone-retina" }; static Resource mediumResource = {cocos2d::CCSizeMake(960, 640), "iphone-retina"};
static Resource largeResource = { cocos2d::CCSizeMake(1024, 768), "ipad" }; static Resource largeResource = {cocos2d::CCSizeMake(1024, 768), "ipad"};
static Resource extralargeResource = { cocos2d::CCSizeMake(2048, 1536), "ipadhd" }; static Resource extralargeResource = {cocos2d::CCSizeMake(2048, 1536), "ipadhd"};
#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320) #if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320)
static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320); static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320);
@ -57,4 +56,4 @@ static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(2048, 1536);
// The font size 24 is designed for small resolution, so we should change it to fit for current design resolution // The font size 24 is designed for small resolution, so we should change it to fit for current design resolution
#define TITLE_FONT_SIZE (cocos2d::CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width / smallResource.size.width * 24) #define TITLE_FONT_SIZE (cocos2d::CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width / smallResource.size.width * 24)
#endif /* __APPMACROS_H__ */ #endif /* _APPMACROS_H_ */

View File

@ -7,19 +7,19 @@ using namespace cocos2d;
using namespace spine; using namespace spine;
using namespace std; using namespace std;
CCScene* ExampleScene::scene() { CCScene* ExampleScene::scene () {
CCScene *scene = CCScene::create(); CCScene *scene = CCScene::create();
ExampleScene *layer = ExampleScene::create(); ExampleScene *layer = ExampleScene::create();
scene->addChild(layer); scene->addChild(layer);
return scene; return scene;
} }
bool ExampleScene::init() { bool ExampleScene::init () {
if (!CCLayer::init()) return false; if (!CCLayer::init()) return false;
Atlas *atlas = new Atlas("spineboy.txt"); Atlas *atlas = new Atlas("spineboy.txt");
SkeletonJson json(atlas); SkeletonJson json(atlas);
json.scale = 0.5; json.scale = 0.5;
SkeletonData *skeletonData = json.readSkeletonData("spineboy-skeleton.json"); SkeletonData *skeletonData = json.readSkeletonData("spineboy-skeleton.json");
Animation *animation = json.readAnimation("spineboy-walk.json", skeletonData); Animation *animation = json.readAnimation("spineboy-walk.json", skeletonData);

View File

@ -3,13 +3,13 @@
#include "cocos2d.h" #include "cocos2d.h"
class ExampleScene : public cocos2d::CCLayer { class ExampleScene: public cocos2d::CCLayer {
public: public:
static cocos2d::CCScene* scene(); static cocos2d::CCScene* scene ();
virtual bool init(); virtual bool init ();
CREATE_FUNC(ExampleScene); CREATE_FUNC (ExampleScene);
}; };
#endif // SPINE_EXAMPLESCENE_H_ #endif // _EXAMPLESCENE_H_

View File

@ -39,8 +39,8 @@ AtlasPage::~AtlasPage () {
Atlas::Atlas (const std::string &path) { Atlas::Atlas (const std::string &path) {
unsigned long size; unsigned long size;
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData( char *data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size)); CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
if (!data) throw std::runtime_error("Error reading atlas file: " + path); if (!data) throw std::runtime_error("Error reading atlas file: " + path);
load(data, data + size); load(data, data + size);
} }

View File

@ -1,27 +1,27 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2013, Esoteric Software * Copyright (c) 2013, Esoteric Software
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met: * modification, are permitted provided that the following conditions are met:
* *
* 1. Redistributions of source code must retain the above copyright notice, this * 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer. * list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice, * 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation * this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution. * and/or other materials provided with the distribution.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/ ******************************************************************************/
#include <spine-cocos2dx/CCSkeleton.h> #include <spine-cocos2dx/CCSkeleton.h>
#include <stdexcept> #include <stdexcept>
@ -38,8 +38,7 @@ using namespace spine;
USING_NS_CC; USING_NS_CC;
CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) : CCSkeleton::CCSkeleton (SkeletonData *skeletonData, AnimationStateData *stateData) :
debug(false) debug(false) {
{
if (!skeletonData) throw std::invalid_argument("skeletonData cannot be null."); if (!skeletonData) throw std::invalid_argument("skeletonData cannot be null.");
skeleton = new Skeleton(skeletonData); skeleton = new Skeleton(skeletonData);
state = new AnimationState(stateData); state = new AnimationState(stateData);
@ -57,12 +56,12 @@ void CCSkeleton::update (float deltaTime) {
skeleton->update(deltaTime); skeleton->update(deltaTime);
state->update(deltaTime); state->update(deltaTime);
state->apply(skeleton); state->apply(skeleton);
skeleton->updateWorldTransform(); skeleton->updateWorldTransform();
} }
void CCSkeleton::draw () { void CCSkeleton::draw () {
CC_NODE_DRAW_SETUP(); CC_NODE_DRAW_SETUP();
glBlendFunc(GL_ONE,GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
skeleton->draw(); skeleton->draw();
if (debug) { if (debug) {

View File

@ -43,16 +43,16 @@ SkeletonJson::SkeletonJson (Atlas *atlas) :
SkeletonData* SkeletonJson::readSkeletonData (const std::string &path) const { SkeletonData* SkeletonJson::readSkeletonData (const std::string &path) const {
unsigned long size; unsigned long size;
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData( char *data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size)); CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
if (!data) throw runtime_error("Error reading skeleton file: " + path); if (!data) throw runtime_error("Error reading skeleton file: " + path);
return BaseSkeletonJson::readSkeletonData(data, data + size); return BaseSkeletonJson::readSkeletonData(data, data + size);
} }
Animation* SkeletonJson::readAnimation (const std::string &path, const SkeletonData *skeletonData) const { Animation* SkeletonJson::readAnimation (const std::string &path, const SkeletonData *skeletonData) const {
unsigned long size; unsigned long size;
char* data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData( char *data = reinterpret_cast<char*>(CCFileUtils::sharedFileUtils()->getFileData(
CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size)); CCFileUtils::sharedFileUtils()->fullPathForFilename(path.c_str()).c_str(), "r", &size));
if (!data) throw runtime_error("Error reading animation file: " + path); if (!data) throw runtime_error("Error reading animation file: " + path);
return BaseSkeletonJson::readAnimation(data, data + size, skeletonData); return BaseSkeletonJson::readAnimation(data, data + size, skeletonData);
} }