| |
| |
| |
| |
|
|
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <unistd.h> |
| #include <string> |
|
|
| #include "cocos2d.h" |
|
|
| #include <emscripten.h> |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #define DESIGN_RESOLUTION_480X320 0 |
| #define DESIGN_RESOLUTION_1024X768 1 |
| #define DESIGN_RESOLUTION_2048X1536 2 |
|
|
| |
| #define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_480X320 |
|
|
| typedef struct tagResource |
| { |
| cocos2d::Size size; |
| char directory[100]; |
| }Resource; |
|
|
| static Resource smallResource = { cocos2d::Size(480, 320), "iphone" }; |
| static Resource mediumResource = { cocos2d::Size(1024, 768), "ipad" }; |
| static Resource largeResource = { cocos2d::Size(2048, 1536), "ipadhd" }; |
|
|
| #if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320) |
| static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320); |
| #elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768) |
| static cocos2d::Size designResolutionSize = cocos2d::Size(1024, 768); |
| #elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536) |
| static cocos2d::Size designResolutionSize = cocos2d::Size(2048, 1536); |
| #else |
| #error unknown target design resolution! |
| #endif |
|
|
| |
| #define TITLE_FONT_SIZE (cocos2d::EGLView::getInstance()->getDesignResolutionSize().width / smallResource.size.width * 24) |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| class AppDelegate : private cocos2d::Application |
| { |
| public: |
| AppDelegate(); |
| virtual ~AppDelegate(); |
|
|
| |
| |
| |
| |
| |
| virtual bool applicationDidFinishLaunching(); |
|
|
| |
| |
| |
| |
| virtual void applicationDidEnterBackground(); |
|
|
| |
| |
| |
| |
| virtual void applicationWillEnterForeground(); |
| }; |
|
|
| |
| |
| |
|
|
| class HelloWorld : public cocos2d::Layer |
| { |
| public: |
| |
| virtual bool init(); |
|
|
| |
| static cocos2d::Scene* scene(); |
| |
| |
| void menuCloseCallback(Object* sender); |
| |
| |
| CREATE_FUNC(HelloWorld); |
| }; |
|
|
| USING_NS_CC; |
|
|
| |
| |
| |
|
|
| Scene* HelloWorld::scene() |
| { |
| |
| Scene *scene = Scene::create(); |
| |
| |
| HelloWorld *layer = HelloWorld::create(); |
|
|
| |
| scene->addChild(layer); |
|
|
| |
| return scene; |
| } |
|
|
| |
| bool HelloWorld::init() |
| { |
| |
| |
| if ( !Layer::init() ) |
| { |
| return false; |
| } |
| |
| Size visibleSize = Director::getInstance()->getVisibleSize(); |
| Point origin = Director::getInstance()->getVisibleOrigin(); |
|
|
| |
| |
| |
|
|
| |
| MenuItemImage *closeItem = MenuItemImage::create( |
| "CloseNormal.png", |
| "CloseSelected.png", |
| CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)); |
| |
| closeItem->setPosition(origin + Point(visibleSize) - Point(closeItem->getContentSize() / 2)); |
|
|
| |
| Menu* menu = Menu::create(closeItem, nullptr); |
| menu->setPosition(Point::ZERO); |
| this->addChild(menu, 1); |
|
|
| |
| |
|
|
| |
| |
| |
| LabelTTF* label = LabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); |
| |
| |
| label->setPosition(Point(origin.x + visibleSize.width/2, |
| origin.y + visibleSize.height - label->getContentSize().height)); |
|
|
| |
| this->addChild(label, 1); |
|
|
| |
| Sprite* sprite = Sprite::create("HelloWorld.png"); |
|
|
| |
| sprite->setPosition(Point(visibleSize / 2) + origin); |
|
|
| |
| this->addChild(sprite, 0); |
| |
| return true; |
| } |
|
|
|
|
| void HelloWorld::menuCloseCallback(Object* sender) |
| { |
| Director::getInstance()->end(); |
|
|
| #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) |
| exit(0); |
| #endif |
| } |
|
|
| |
| |
| |
|
|
| using namespace std; |
|
|
| AppDelegate::AppDelegate() { |
|
|
| } |
|
|
| AppDelegate::~AppDelegate() |
| { |
| } |
|
|
| bool AppDelegate::applicationDidFinishLaunching() { |
| |
| Director* director = Director::getInstance(); |
| EGLView* glView = EGLView::getInstance(); |
|
|
| director->setOpenGLView(glView); |
| |
| Size size = director->getWinSize(); |
|
|
| |
| glView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER); |
|
|
| Size frameSize = glView->getFrameSize(); |
| |
| vector<string> searchPath; |
|
|
| |
| |
| |
| |
|
|
| |
| if (frameSize.height > mediumResource.size.height) |
| { |
| searchPath.push_back(largeResource.directory); |
|
|
| director->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height, largeResource.size.width/designResolutionSize.width)); |
| } |
| |
| else if (frameSize.height > smallResource.size.height) |
| { |
| searchPath.push_back(mediumResource.directory); |
| |
| director->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height, mediumResource.size.width/designResolutionSize.width)); |
| } |
| |
| else |
| { |
| searchPath.push_back(smallResource.directory); |
|
|
| director->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height, smallResource.size.width/designResolutionSize.width)); |
| } |
| |
| |
| FileUtils::getInstance()->setSearchPaths(searchPath); |
| |
| |
| director->setDisplayStats(false); |
|
|
| |
| director->setAnimationInterval(1.0 / 60); |
|
|
| |
| Scene *scene = HelloWorld::scene(); |
|
|
| |
| director->runWithScene(scene); |
|
|
| return true; |
| } |
|
|
| |
| void AppDelegate::applicationDidEnterBackground() { |
| Director::getInstance()->stopAnimation(); |
|
|
| |
| |
| } |
|
|
| |
| void AppDelegate::applicationWillEnterForeground() { |
| Director::getInstance()->startAnimation(); |
|
|
| |
| |
| } |
|
|
|
|
| |
| |
| |
|
|
| int main(int argc, char **argv) |
| { |
| |
| AppDelegate app; |
| EM_ASM({ |
| Browser.setCanvasSize(640, 480); |
| }); |
| return Application::getInstance()->run(); |
| } |
|
|