Merge pull request #321 from minggo/iss538

[iOS] fixed #538: add template of lua for xcode3
This commit is contained in:
minggo 2011-06-24 01:23:20 -07:00
commit ea0bf7cdd2
15 changed files with 545 additions and 9 deletions

View File

@ -2,7 +2,7 @@
echo 'cocos2d-x template installer'
COCOS2D_VER='cocos2d-0.99.5-x-0.8.4'
COCOS2D_VER='cocos2d-0.99.5-x-0.8.5'
BASE_TEMPLATE_DIR="/Library/Application Support/Developer/Shared/Xcode"
BASE_TEMPLATE_USER_DIR="$HOME/Library/Application Support/Developer/Shared/Xcode"
@ -141,7 +141,7 @@ print_template_banner(){
# copies project-based templates
copy_xcode3_project_templates(){
if [[ $user_dir ]]; then
if [[ $user_dir ]]; then
TEMPLATE_DIR="${BASE_TEMPLATE_USER_DIR}/Project Templates/${COCOS2D_VER}/"
else
TEMPLATE_DIR="${BASE_TEMPLATE_DIR}/Project Templates/${COCOS2D_VER}/"
@ -175,7 +175,7 @@ copy_xcode3_project_templates(){
check_dst_dir
echo ...copying template files
copy_files template/xcode3/cocos2d-x_box2d_app/ "$DST_DIR"
copy_files template/xcode3/cocos2d-x_box2d_app/ "$DST_DIR"
copy_base_files
@ -192,13 +192,28 @@ copy_xcode3_project_templates(){
check_dst_dir
echo ...copying template files
copy_files template/xcode3/cocos2d-x_chipmunk_app/ "$DST_DIR"
copy_files template/xcode3/cocos2d-x_chipmunk_app/ "$DST_DIR"
copy_base_files
echo ...coping chipmunk files
copy_files chipmunk "$LIBS_DIR"
print_template_banner "Installing cocos2d-x iOS + lua template"
DST_DIR="$TEMPLATE_DIR""cocos2d-x lua Application/"
LIBS_DIR="$DST_DIR"libs
check_dst_dir
echo ...copying template files
copy_files template/xcode3/cocos2d-x_lua_app/ "$DST_DIR"
copy_base_files
echo ...coping lua files
copy_files lua "$LIBS_DIR"
echo done!
}
@ -314,9 +329,27 @@ copy_xcode4_project_templates(){
}
# copy Xcode4 templates
copy_xcode4_project_templates
select_template_version(){
echo "select the template version to install"
echo "3 for xcode3"
echo "4 for xcode4"
echo "input nothing for all"
# copy Xcode3 templates
copy_xcode3_project_templates
# copy_xcode3_file_templates
read select
if [[ "$select" == 3 ]]; then
copy_xcode3_project_templates
fi
if [[ "$select" == 4 ]]; then
copy_xcode4_project_templates
fi
if [[ "$select""aaaa" == "aaaa" ]]; then
copy_xcode3_project_templates
copy_xcode4_project_templates
fi
}
select_template_version

View File

@ -0,0 +1,160 @@
#include "___PROJECTNAMEASIDENTIFIER___AppDelegate.h"
#include "cocos2d.h"
USING_NS_CC;
AppDelegate::AppDelegate()
:m_pLuaEngine(NULL)
{
}
AppDelegate::~AppDelegate()
{
CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine();
CC_SAFE_DELETE(m_pLuaEngine);
}
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"), 480, 320));
#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)
// OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp
// the default setting is to create a fullscreen view
// if you want to use auto-scale, please enable view->create(320,480) in main.cpp
#endif // CC_PLATFORM_ANDROID
#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, WM_WINDOW_ROTATE_MODE_CW));
#ifndef _TRANZDA_VM_
// on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file
cocos2d::CCFileUtils::setResource("HelloWorld.zip");
#endif
#endif // CC_PLATFORM_WOPHONE
#if (CC_TARGET_PLATFORM == CC_PLATFORM_AIRPLAY)
// MaxAksenov said it's NOT a very elegant solution. I agree, haha
CCDirector::sharedDirector()->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
#endif
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);
// turn on display FPS
pDirector->setDisplayFPS(true);
// pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// register lua engine
m_pLuaEngine = new LuaEngine;
CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
unsigned long size;
char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size);
if (pFileContent)
{
// copy the file contents and add '\0' at the end, or the lua parser can not parse it
char *pCodes = new char[size + 1];
pCodes[size] = '\0';
memcpy(pCodes, pFileContent, size);
delete[] pFileContent;
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes);
delete []pCodes;
}
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
// CCLuaScriptModule::sharedLuaScriptModule()->executeScriptFile("./../../HelloLua/Resource/hello.lua");
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile("./../../HelloLua/Resource/hello.lua");
/*
* Another way to run lua script.
* Load the file into memory and run it.
*
unsigned long size;
char *pFileContent = (char*)CCFileUtils::getFileData("./../../HelloLua/Resource/hello.lua", "r", &size);
if (pFileContent)
{
// copy the file contents and add '\0' at the end, or the lua parser can not parse it
char *pTmp = new char[size + 1];
pTmp[size] = '\0';
memcpy(pTmp, pFileContent, size);
delete[] pFileContent;
string code(pTmp);
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->excuteScriptFile(code);
delete []pTmp;
}
*/
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
printf("%s", path.c_str());
CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str());
#endif
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();
}

View File

@ -0,0 +1,47 @@
#ifndef ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__
#define ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__
#include "CCApplication.h"
#include "LuaEngine.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();
private:
LuaEngine* m_pLuaEngine;
};
#endif // ___PROJECTNAMEASIDENTIFIER___AppDelegate_H__

View File

@ -0,0 +1,22 @@
cocos2d-x http://www.cocos2d-x.org
Copyright (c) 2008-2010 - (see each file to see the different copyright owners)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@ -0,0 +1,10 @@
License for Lua 5.0 and later versions
http://www.lua.org/license.html
Copyright © 1994Ð2011 Lua.org, PUC-Rio.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -0,0 +1 @@
d7290c34702d1c6bdb368acb060d93b42d5deff8

View File

@ -0,0 +1,127 @@
g_Scene = cocos2d.CCScene:node()
pSprite = cocos2d.CCSprite:spriteWithFile("90001.jpg")
pSprite:setPosition(cocos2d.CCPoint(300, 400))
pLayer = cocos2d.CCLayer:node()
pLayer:setIsTouchEnabled(true)
pLayer:setAnchorPoint(cocos2d.CCPoint(0,0))
pLayer:setPosition( cocos2d.CCPoint(0, -300) )
pLayer:addChild(pSprite)
g_Scene:addChild(pLayer)
function btnTouchMove(e)
cocos2d.CCLuaLog("mousemove")
end
function btnTouchBegin(e)
cocos2d.CCLuaLog("btnTouchBegin")
end
function btnTouchEnd(e)
cocos2d.CCLuaLog("btnTouchEnd")
end
-- regiester touch handler
pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
pLayer.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")
-- add a menu
menuItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png")
menuItem:setAnchorPoint(cocos2d.CCPoint(0,0))
menuItem:setPosition( cocos2d.CCPoint(100, 200) )
menuItem:registerScriptHandler("CloseMenu")
pMenu = cocos2d.CCMenu:menuWithItem(menuItem)
pMenu:setPosition( cocos2d.CCPoint(1000, 200) )
g_Scene:addChild(pMenu)
function CloseMenu()
pMenu:setPosition(cocos2d.CCPoint(1000, 200) )
end
function PopMenu()
pMenu:setPosition( cocos2d.CCPoint(0, -100) )
end
pCloseItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png")
pCloseItem:setPosition( cocos2d.CCPoint(30, 40) )
pCloseItem:registerScriptHandler("PopMenu")
pcloseMenu = cocos2d.CCMenu:menuWithItem(pCloseItem)
pcloseMenu:setPosition( cocos2d.CCPoint(30, 40) )
g_Scene:addChild(pcloseMenu)
for i=0,3,1 do
for j=0,1,1 do
landSprite = cocos2d.CCSprite:spriteWithFile("land1.png")
pLayer:addChild(landSprite)
landSprite:setAnchorPoint(cocos2d.CCPoint(0,0))
landSprite:setPosition(cocos2d.CCPoint(90+j*180 - i%2*90, 200+i*95/2))
end
end
--crop
for i=0,3,1 do
for j=0,1,1 do
texturecrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop1.png")
framecrop = cocos2d.CCSpriteFrame:frameWithTexture(texturecrop, cocos2d.CCRectMake(0, 0, 105, 95))
spritecrop = cocos2d.CCSprite:spriteWithSpriteFrame(framecrop);
pLayer:addChild(spritecrop)
spritecrop:setAnchorPoint(cocos2d.CCPoint(0,0))
spritecrop:setPosition(cocos2d.CCPoint(45+90+j*180 - i%2*90, 25+200+i*95/2))
end
end
nFrameWidth = 105
nFrameHeight = 95
texture = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog1.png")
frame0 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(0, 0, nFrameWidth, nFrameHeight))
frame1 = cocos2d.CCSpriteFrame:frameWithTexture(texture, cocos2d.CCRectMake(nFrameWidth*1, 0, nFrameWidth, nFrameHeight))
spritedog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
spritedog:setPosition(cocos2d.CCPoint(300, 500))
pLayer:addChild(spritedog)
animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
animFrames:addObject(frame0)
animFrames:addObject(frame1)
animation = cocos2d.CCAnimation:animationWithName("wait", 0.5, animFrames)
animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
spritedog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))
cocos2d.CCDirector:sharedDirector():runWithScene(g_Scene)
function tick()
point = cocos2d.CCPoint(300, 500);
point = spritedog:getPosition();
if point.x > 600 then
point.x = 0
spritedog:setPosition(point)
else
point.x = point.x + 1
spritedog:setPosition(point)
end
end
cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)

View File

@ -0,0 +1,8 @@
//
// Prefix header for all source files of the '___PROJECTNAME___' target in the '___PROJECTNAME___' project
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#endif

View File

@ -0,0 +1 @@
15ab4b09f881ec0585d0a25e7f4adb771a2f5881

View File

@ -0,0 +1,14 @@
//
// ___PROJECTNAMEASIDENTIFIER___AppController.h
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//
@interface ___PROJECTNAMEASIDENTIFIER___AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate, UITextFieldDelegate,UIApplicationDelegate> {
UIWindow *window;
}
@end

View File

@ -0,0 +1,97 @@
//
// ___PROJECTNAMEASIDENTIFIER___AppController.mm
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "___PROJECTNAMEASIDENTIFIER___AppController.h"
#import "cocos2d.h"
#import "EAGLView.h"
#import "___PROJECTNAMEASIDENTIFIER___AppDelegate.h"
@implementation ___PROJECTNAMEASIDENTIFIER___AppController
#pragma mark -
#pragma mark Application lifecycle
// cocos2d application instance
static AppDelegate s_sharedApplication;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Add the view controller's view to the window and display.
window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
pixelFormat: kEAGLColorFormatRGBA8
depthFormat: GL_DEPTH_COMPONENT16_OES
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0 ];
[window addSubview: __glView];
[window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarHidden: YES];
cocos2d::CCApplication::sharedApplication().run();
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
/*
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
*/
cocos2d::CCDirector::sharedDirector()->pause();
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
cocos2d::CCDirector::sharedDirector()->resume();
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
/*
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
*/
cocos2d::CCDirector::sharedDirector()->stopAnimation();
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
/*
Called as part of transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
*/
cocos2d::CCDirector::sharedDirector()->startAnimation();
}
- (void)applicationWillTerminate:(UIApplication *)application {
/*
Called when the application is about to terminate.
See also applicationDidEnterBackground:.
*/
}
#pragma mark -
#pragma mark Memory management
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
/*
Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
*/
}
- (void)dealloc {
[super dealloc];
}
@end

View File

@ -0,0 +1,16 @@
//
// main.m
// ___PROJECTNAME___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [NSAutoreleasePool new];
int retVal = UIApplicationMain(argc, argv, nil, @"___PROJECTNAMEASIDENTIFIER___AppController");
[pool release];
return retVal;
}