Skip to content

Commit

Permalink
Initial commit of HelloLua to git
Browse files Browse the repository at this point in the history
  • Loading branch information
ciaranj committed Sep 14, 2011
0 parents commit d2e2937
Show file tree
Hide file tree
Showing 53 changed files with 5,147 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/android/bin
/android/gen
/android/libs
/android/obj
/android/assets

# xcode noise.
/ios/build/*
*.pbxuser
*.mode1v3
xcuserdata

# osx noise
.DS_Store
profile
136 changes: 136 additions & 0 deletions Classes/AppDelegate.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "AppDelegate.h"

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
using namespace CocosDenshion;

AppDelegate::AppDelegate()
:m_pLuaEngine(NULL)
{
}

AppDelegate::~AppDelegate()
{
// end simple audio engine here, or it may crashed on win32
SimpleAudioEngine::sharedEngine()->end();
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) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
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();
}
47 changes: 47 additions & 0 deletions Classes/AppDelegate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_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 // _APP_DELEGATE_H_

Binary file added Resource/Default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/background.mp3
Binary file not shown.
Binary file added Resource/crop.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/dog.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/effect1.wav
Binary file not shown.
Binary file added Resource/farm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
160 changes: 160 additions & 0 deletions Resource/hello.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
-- create scene & layer
layerFarm = cocos2d.CCLayer:node()
layerFarm:setIsTouchEnabled(true)

layerMenu = cocos2d.CCLayer:node()

sceneGame = cocos2d.CCScene:node()
sceneGame:addChild(layerFarm)
sceneGame:addChild(layerMenu)

winSize = cocos2d.CCDirector:sharedDirector():getWinSize()

-- add in farm background
spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg")
spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2))
layerFarm:addChild(spriteFarm)

-- touch handers
pointBegin = nil

function btnTouchMove(e)
cocos2d.CCLuaLog("btnTouchMove")
if pointBegin ~= nil then
local v = e[1]
local pointMove = v:locationInView(v:view())
pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove)
local positionCurrent = layerFarm:getPosition()
layerFarm:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y))
pointBegin = pointMove
end
end

function btnTouchBegin(e)
cocos2d.CCLuaLog("btnTouchBegin")
for k,v in ipairs(e) do
pointBegin = v:locationInView(v:view())
pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin)
end
end

function btnTouchEnd(e)
cocos2d.CCLuaLog("btnTouchEnd")
touchStart = nil
end

-- regiester touch handlers
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin")
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove")
layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd")


-- add land sprite
for i=0,3,1 do
for j=0,1,1 do
spriteLand = cocos2d.CCSprite:spriteWithFile("land.png")
layerFarm:addChild(spriteLand)
spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2))
end
end

-- add crop

for i=0,3,1 do
for j=0,1,1 do

textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png")
frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95))
spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop);

layerFarm:addChild(spriteCrop)

spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2))

end
end

-- add the moving dog

FrameWidth = 105
FrameHeight = 95

textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png")
frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight))
frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight))

spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0)
spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3))
layerFarm:addChild(spriteDog)

animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2)
animFrames:addObject(frame0)
animFrames:addObject(frame1)

animation = cocos2d.CCAnimation:animationWithFrames(animFrames, 0.5)

animate = cocos2d.CCAnimate:actionWithAnimation(animation, false);
spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate))


-- add a popup menu

function menuCallbackClosePopup()
-- stop test sound effect
CocosDenshion.SimpleAudioEngine:sharedEngine():stopEffect(effectID)
menuPopup:setIsVisible(false)
end

menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png")
menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) )
menuPopupItem:registerScriptHandler("menuCallbackClosePopup")
menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem)
menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) )
menuPopup:setIsVisible(false)
layerMenu:addChild(menuPopup)

-- add the left-bottom "tools" menu to invoke menuPopup

function menuCallbackOpenPopup()
-- loop test sound effect
-- NOTE: effectID is global, so it can be used to stop
effectID = CocosDenshion.SimpleAudioEngine:sharedEngine():playEffect("effect1.wav")
menuPopup:setIsVisible(true)
end

menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png")
menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) )
menuToolsItem:registerScriptHandler("menuCallbackOpenPopup")
menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem)
menuTools:setPosition( cocos2d.CCPoint(30, 40) )
layerMenu:addChild(menuTools)


function tick()

point = spriteDog:getPosition();

if point.x > winSize.width then
point.x = 0
spriteDog:setPosition(point)
else
point.x = point.x + 1
spriteDog:setPosition(point)
end

if point.y > winSize.height then
point.y = 0
else
point.y = point.y + 1
end
spriteDog:setPosition(point)
end

cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false)

-- play background music
CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true);
-- preload effect
CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav");
-- run
cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame)
Binary file added Resource/land.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/menu1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Resource/menu2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions android/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="output" path="bin"/>
</classpath>
8 changes: 8 additions & 0 deletions android/.externalToolBuilders/BuildNative.launch
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<launchConfiguration type="org.eclipse.ui.externaltools.ProgramBuilderLaunchConfigurationType">
<booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${workspace_loc:/EscapeVelocity/build_native.sh}"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,"/>
<booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/>
<stringAttribute key="org.eclipse.ui.externaltools.ATTR_WORKING_DIRECTORY" value="${workspace_loc:/EscapeVelocity}"/>
</launchConfiguration>
Loading

0 comments on commit d2e2937

Please sign in to comment.