main.cpp
#include "main.h"
#include "../Classes/AppDelegate.h"
#include "CCEGLView.h"
USING_NS_CC;
int APIENTRY _tWinMain(HINSTANCE hInstance, //HINSTANCE标识一个应用程序模块的一个实例
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, //lpCmdLine是命令行参数, 在windows的命令行工具cmd中启动程序的话, 有时能带上其它参数
int nCmdShow) //nCmdShow表示窗口的初始状态, 最大化, 最小化, 隐藏, 还是一般大小的显示
{
{
UNREFERENCED_PARAMETER(hPrevInstance); //UNREFERENCED_PARAMETER 让编译器不必检测你的警告
UNREFERENCED_PARAMETER(lpCmdLine);
AppDelegate app; //创建应用程序实例,这里的AppDelegate与后面AppDelegate.h这一文件中设置的 类 的名称对应
CCEGLView& eglView = CCEGLView::sharedOpenGLView(); //创建主窗体的实例
eglView.setViewName("Hello World"); //设置主窗体是名称
eglView.setFrameSize(1024, 700); //创建主窗体窗口
return CCApplication::sharedApplication().run();
//CCApplication::sharedApplication()获取当前应用程序实例,CCApplication::run()运行应用程序消息循环 , 程序入口为 cocos2d:: CCApplication::run()
//CCApplication应用程序平台实现类
}
}
AppDelegate.h
/**AppDelegate是应用真正的入口,在这里有平台的适配参数,找到导演类,启动我们的场景*/
#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual bool applicationDidFinishLaunching(); //处理导演类和场景的开始
virtual void applicationDidEnterBackground(); //暂停
virtual void applicationWillEnterForeground(); //重新开始
};
AppDelegate.cpp
#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate() {
}
bool AppDelegate::applicationDidFinishLaunching() {
CCDirector *pDirector = CCDirector::sharedDirector(); //初始化导演类
pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView()); //导演类中有创建窗口的功能
pDirector->setDisplayStats(true); //导演类中 有setDisplayFPS这个函数的功能
pDirector->setAnimationInterval(1.0 / 60); //设置调试窗口中显示的"帧每秒"参数,本语句的意思是 导演类中 有setAnimationInterval这个函数的功能
CCScene *pScene = HelloWorld::scene(); //创建一个场景
pDirector->runWithScene(pScene); //开始场景
return true;
}
void AppDelegate::applicationDidEnterBackground() { //暂停
CCDirector::sharedDirector()->stopAnimation();
}
void AppDelegate::applicationWillEnterForeground() { //重新开始
CCDirector::sharedDirector()->startAnimation();
}
HelloWorldScene.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create(); //创建一个场景
HelloWorld *layer = HelloWorld::create(); //创建一个HelloWorld层
scene->addChild(layer); //把层放到场景上
return scene; //返回场景
}
bool HelloWorld::init() //创建HelloWorl层的时候,调用了init()函数
{
if ( !CCLayer::init() )
{
return false;
}
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png", //点击 关闭按钮 前后的图片替换
this,
menu_selector(HelloWorld::menuCloseCallback) );
//“menuCloseCallback”为构建关掉程序的方法,即实现 点击 关闭按钮(图片) 能关闭窗口的功能
pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) ); //按钮精灵的位置
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition( CCPointZero ); //按钮精灵的有无
this->addChild(pMenu, 1); //每次创建元素后将其使用this-〉addchild函数插入,即放入此场景,第二个参数就是绘制的顺序
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); //在层上添加文字,设置其字体及大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition( ccp(size.width / 2, size.height - 50) ); //精灵位置
this->addChild(pLabel, 1); //每次创建元素后将其使用this-〉addchild函数插入,即放入此场景,第二个参数就是绘制的顺序
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); //在层上添加人物("HelloWorld.png"图片)
pSprite->setPosition( ccp(size.width/2, size.height/2) ); //图片位置
this->addChild(pSprite, 0); //每次创建元素后将其使用this-〉addchild函数插入,即放入此场景,第二个参数就是绘制的顺序
CCSize s = CCDirector::sharedDirector()->getWinSize(); //创建一个窗口实例
//各种精灵以及动画都在窗口上显示,所以每一个cpp文件中都需要创建一个这样的实例
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender) //实现“关闭按钮”的功能对应的函数
{
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}