appdelegatee applicationdidfinishlaunching怎么修改

本篇文章主要介绍一些UIApplicationDelegate中几个常用的回调方法的调用时机。 以帮助你判断哪些方法倒底放到哪个回调中去实现。
1. – (void)applicationDidFinishLaunching:(UIApplication *) 此方法基本已经弃用,改用第2个方法代替。 2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0); 当应用程序启动时(不包括已在后台的情况下转到前台),调用此回调。launchOptions是启动参数,假如用户通过点击push通知启动的应用,这个参数里会存储一些push通知的信息。
3. – (void)applicationDidBecomeActive:(UIApplication *) // 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. 当应用程序全新启动,或者在后台转到前台,完全激活时,都会调用这个方法。如果应用程序是以前运行在后台,这时可以选择刷新用户界面。
4. – (void)applicationWillResignActive:(UIApplication *) // 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. 当应用从活动状态主动到非活动状态的应用程序时会调用这个方法。这可导致产生某些类型的临时中断(如传入电话呼叫或SMS消息)。或者当用户退出应用程 序,它开始过渡到的背景状态。使用此方法可以暂停正在进行的任务,禁用定时器,降低OpenGL ES的帧速率。游戏应该使用这种方法来暂停游戏。 调用时机可能有以下几种:锁屏,按HOME键,下接状态栏,双击HOME键弹出低栏,等情况。
5. – (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *) // Will be deprecated at some point, please replace with application:openURL:sourceApplication:annotation: 这个方法已不再支持,可能会在以后某个版本中去掉。建议用下面第6个方法代替
6. – (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2); // no equiv. notification. return NO if the application can’t open for some reason 当用户通过其它应用启动本应用时,会回调这个方法,url参数是其它应用调用openURL:方法时传过来的。
7. – (void)applicationDidReceiveMemoryWarning:(UIApplication *) // try to clean up as much memory as possible. next step is to terminate app 当应用可用内存不足时,会调用此方法,在这个方法中,应该尽量去清理可能释放的内存。如果实在不行,可能会被强行退出应用。
8. – (void)applicationWillTerminate:(UIApplication *) // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 当应用退出,并且进程即将结束时会调到这个方法,一般很少主动调到,更多是内存不足时是被迫调到的,我们应该在这个方法里做一些数据存储操作。
9. // one of these will be called after calling -registerForRemoteNotifications - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0); - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0); 当客户端注册远程通知时,会回调上面两个方法。 如果成功,则回调第一个,客户端把deviceToken取出来发给服务端,push消息的时候要用。 如果失败了,则回调第二个,可以从error参数中看一下失败原因。 注:注册远程通知使用如下方法:
UIRemoteNotificationType t=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeS
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];
10. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0); 当应用在前台运行中,收到远程通知时,会回调这个方法。 当应用在后台状态时,点击push消息启动应用,也会回调这个方法。 11. – (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0); 当应用收到本地通知时会调这个方法,同上面一个方法类似。 如果在前台运行状态直接调用,如果在后台状态,点击通知启动时,也会回调这个方法 本地通知可见另一篇文章:http://bluevt.org/?p=70
12. – (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0); // 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, this method is called instead of applicationWillTerminate: when the user quits. 当用户从台前状态转入后台时,调用此方法。使用此方法来释放资源共享,保存用户数据,无效计时器,并储存足够的应用程序状态信息的情况下被终止后,将应用 程序恢复到目前的状态。如果您的应用程序支持后台运行,这种方法被调用,否则调用applicationWillTerminate:用户退出。
13. – (void)applicationWillEnterForeground:(UIApplication *)application NS_AVAILABLE_IOS(4_0); // Called as part of the transition from the background t here you can undo many of the changes made on entering the background. 当应用在后台状态,将要进行动前台运行状态时,会调用此方法。 如果应用不在后台状态,而是直接启动,则不会回调此方法。
浏览 28813
浏览: 78701 次
来自: 北京
lhmsweet1 写道谢谢啊,哥们,可是我现在遇到的问题主要 ...
谢谢啊,哥们,可是我现在遇到的问题主要是使用compile-s ...
lhmsweet1 写道可以详细说下,wax_stdlib 文 ...
可以详细说下,wax_stdlib 文件的生成过程吗?
请问这个返回upload页面之后怎么做呢?我的总是报错啊返回的 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'我的Cocos2d-x学习笔记(二)AppDelegate补充介绍
发布时间: 16:57:36
& & 上一篇中只介绍AppDelegate中applicationDidFinishLaunching()函数,这个函数是程序运行的关键,在CCApplicationProtocol中声明纯虚函数,在CCApplication中进行调用。在CCApplicationProtocol中与applicationDidFinishLaunching()类似的纯虚函数还有两个,分别是:applicationDidEnterBackground()和applicationWillEnterForeground(),在CCApplicationProtocol中声明如下:
class CC_DLL CCApplicationProtocol { public:
Implement CCDirector and CCScene init code here.
@return true
Initialize success, app continue.
@return false
Initialize failed, app terminate.
virtual bool applicationDidFinishLaunching() = 0;
The function be called when the application enter background
the pointer of the application
virtual void applicationDidEnterBackground() = 0;
The function be called when the application enter foreground
the pointer of the application
virtual void applicationWillEnterForeground() = 0; }在AppDelegate对其中类成员函数进行覆写,AppDelegate.h中的代码如下:
_APP_DELEGATE_H_ #define
_APP_DELEGATE_H_
#include &cocos2d.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();
Implement CCDirector and CCScene init code here.
@return true
Initialize success, app continue.
@return false
Initialize failed, app terminate.
virtual bool applicationDidFinishLaunching();
The function be called when the application enter background
the pointer of the application
virtual void applicationDidEnterBackground();
The function be called when the application enter foreground
the pointer of the application
virtual void applicationWillEnterForeground(); };
#endif // _APP_DELEGATE_H_ & & 由官方的注释就可知道applicationDidEnterBackground()和applicationWillEnterForeground()的作用,applicationDidEnterBackground()函数是游戏程序进入后台运行时候调用的,applicationWillEnterForeground()函数是游戏程序从后台恢复到前台运行时候调用的。
& & 再来看看AppDelegate.cpp中的内容,代码如下:
#include &AppDelegate.h& USING_NS_CC; AppDelegate::AppDelegate() {
} AppDelegate::~AppDelegate()
{ } bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector-&setOpenGLView(pEGLView);
// 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);
CCScene *pScene = HelloWorld::scene();
pDirector-&runWithScene(pScene); }
// 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()-&stopAnimation();
// 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()-&startAnimation();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()-&resumeBackgroundMusic(); }
AppDelegate的构造函数与析构函数是空的,构造函数运行时候通过执行父类构造函数初始化CCApplication单例对象,之后没有其他功能。
CCDirector* pDirector = CCDirector::sharedDirector();得到了一个CCDirector单例对象并把指针赋值给pDirector 。
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();Cocos2d-x对OpenGL进行了封装,Cocos2d-x对OpenGL进行了初始化。
pDirector-&setOpenGLView(pEGLView); 设置了场景的窗口。
pDirector-&setDisplayStats(true);启用FPS显示,设置是否显示游戏的帧数等调试信息。
pDirector-&setAnimationInterval(1.0 / 60);设置游戏的帧率,即每秒刷新画面次数,这里是60帧每秒。
其中重点关注以下两句:
CCScene *pScene = HelloWorld::scene();
pDirector-&runWithScene(pScene);
上面的第一句:CCScene *pScene = HelloWorld::scene();创建了一个场景,这个场景就是HelloWorld,然后把这个场景指针赋值给pScene,之后通过pDirector-&runWithScene(pScene);把这个HelloWorld场景运行起来。我们需要知道的是pDirector是一个CCDirector单例对象的指针,CCDirector是控制游戏中各种元素的类。
总结一下:CCApplicationProtocol中定义了运行时用到的几个接口,前台运行、进入后台运行、返回前台运行三个接口;CCApplication中包装了跨平台的实现;AppDelegate
则具体实现了CCApplicationProtocol定义的各个接口。
来源:http://www.cnblogs.com/gongyan/p/4539411.htmlobjective c - Cocoa: Pass arguments to NSApplicationDelegate - Stack Overflow
to customize your list.
Join Stack Overflow to learn, share knowledge, and build your career.
or sign in with
I have created the simple Cocoa application (Mac OS X 10.6) and there have appeared the entry point:
int main(int argc, char *argv[])
return NSApplicationMain(argc,
(const char **) argv);
and AppDelegate dummy:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// how to get argc and argv?
and some other. How could I pass the argc and argv to my AppDelegate right way?
8,71332242
user663896
Use +[NSProcessInfo processInfo] and -[NSProcessInfo arguments].
In your application delegate,
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
NSArray *args = [[NSProcessInfo processInfo] arguments];
// use -objectAtIndex: to obtain an element of the array
// and -count to obtain the number of elements in the array
user557219
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Post as a guest
Post as a guest
By posting your answer, you agree to the
Stack Overflow works best with JavaScript enabledcocos2dx学习笔记:CCGLView | cstriker1407的笔记本
当前位置&:& / / /cocos2dx学习笔记:CCGLView
最近下了cocos2dx3.0版本的代码,在windows下学习了它如何绘制openGL的,看了下它的代码调用,在windows下应该是使用了glfw库来进行的openGL的绘制。这里就笔记下其部分代码调用。
作者学习cocos2dx时间不长,理解的也不是太深刻。3.0版本的就使用的更少了,可能有部分笔记是错误的,还请路过的大牛们帮忙指正一下。
AppDelegate.cpp:
部分代码修改如下:
bool AppDelegate::applicationDidFinishLaunching() {
auto director = Director::getInstance();
auto glview = director-&getOpenGLView();
if(!glview) {
//这里采用默认的分辨率先创建出游戏窗口
glview = GLView::create("HelloWorld");
director-&setOpenGLView(glview);
#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
//通过函数将游戏窗口设置为640X1136,这里也可以通过GLView::create的重载函数来实现。不过为了跨平台,还是用宏包起来
glview-&setFrameSize(640,1136);
//由于作者的显示器的分辨率太小,为了能够完整显示游戏窗口,这里将其缩放一下,
//也可以通过GLView::create的重载函数来实现
glview-&setFrameZoomFactor(0.6f);
//设置游戏设计分辨率,通过修改改分辨率和后面的显示模式,就可以在Windows下方便的进行分辨率适配
glview-&setDesignResolutionSize(640,1136, ResolutionPolicy::SHOW_ALL);
director-&setAnimationInterval(1.0 / 60);
auto scene = HelloWorld::createScene();
director-&runWithScene(scene);
分辨率适配图像如下:
当FrameSize为(640,1136),DesignResolutionSize为(640,1136,SHOW_ALL)时,如下图显示,大小正好:
当FrameSize为(800,1136),DesignResolutionSize为(640,1136,SHOW_ALL)时,如下图显示,宽了一部分,由于是【 SHOW_ALL 】模式,这是两边就有了黑边:
CCGLView.cpp:
部分代码注释如下,由于作者是在windows下阅读的代码,因此这里的CCGLView的路径为【 cocos2d\cocos\platform\desktop\CCGLView.cpp 】:
// GLFWEventHandler
定义了一个管理GL回调函数的结构体,里面所有的函数都是静态的,由于是结构体,因此也全部都是默认public的,
这样便于管理,即这里的所有的函数都是用来处理openGL的回调的。
同时,这里所有的函数都没有实际的处理逻辑,全部是调用的GLView的函数,通过这种方式就可以通过编译时的函数指针检查
class GLFWEventHandler
static void onGLFWError(int errorID, const char* errorDesc)
if (_view)
_view-&onGLFWError(errorID, errorDesc);
static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
if (_view)
_view-&onGLFWMouseCallBack(window, button, action, modify);
static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
if (_view)
_view-&onGLFWMouseMoveCallBack(window, x, y);
static void onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
if (_view)
_view-&onGLFWMouseScrollCallback(window, x, y);
static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
if (_view)
_view-&onGLFWKeyCallback(window, key, scancode, action, mods);
static void onGLFWCharCallback(GLFWwindow* window, unsigned int character)
if (_view)
_view-&onGLFWCharCallback(window, character);
static void onGLFWWindowPosCallback(GLFWwindow* windows, int x, int y)
if (_view)
_view-&onGLFWWindowPosCallback(windows, x, y);
static void onGLFWframebuffersize(GLFWwindow* window, int w, int h)
if (_view)
_view-&onGLFWframebuffersize(window, w, h);
static void onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
if (_view)
_view-&onGLFWWindowSizeFunCallback(window, width, height);
通过这个函数和下面的静态私有变量,来给结构体中的_view赋值。
static void setGLView(GLView* view)
static GLView* _
//结构体静态变量初始化
GLView* GLFWEventHandler::_view =
////////////////////////////////////////////////////
struct keyCodeItem
int glfwKeyC
EventKeyboard::KeyCode keyC
static std::unordered_map&int, EventKeyboard::KeyCode& g_keyCodeM
static keyCodeItem g_keyCodeStructArray[] = {
/* The unknown key */
{ GLFW_KEY_UNKNOWN
, EventKeyboard::KeyCode::KEY_NONE
/* Printable keys */
{ GLFW_KEY_SPACE
, EventKeyboard::KeyCode::KEY_SPACE
。。。。。
。。。。。
{ GLFW_KEY_MENU
, EventKeyboard::KeyCode::KEY_MENU
{ GLFW_KEY_LAST
, EventKeyboard::KeyCode::KEY_NONE
//////////////////////////////////////////////////////////////////////////
// implement GLView
//////////////////////////////////////////////////////////////////////////
/* 默认构造函数 */
GLView::GLView()
: _captured(false)
, _supportTouch(false)
, _isInRetinaMonitor(false)
, _isRetinaEnabled(false)
, _retinaFactor(1)
, _frameZoomFactor(1.0f)
, _mainWindow(nullptr)
, _monitor(nullptr) //monitor默认是nullptr
, _mouseX(0.0f)
, _mouseY(0.0f)
_viewName = "cocos2dx";//先暂时命名GL窗口为cocos2dx
g_keyCodeMap.clear();//将键盘码映射到map中
for (auto& item : g_keyCodeStructArray)
g_keyCodeMap[item.glfwKeyCode] = item.keyC
//设置好回调函数的调用实例
GLFWEventHandler::setGLView(this);
//注册回调函数
glfwSetErrorCallback(GLFWEventHandler::onGLFWError);
//初始化GLFW
glfwInit();
//析构函数
GLView::~GLView()
CCLOGINFO("deallocing GLView: %p", this);//日志
GLFWEventHandler::setGLView(nullptr);//干掉回调函数响应
glfwTerminate();//关闭GLFW
//create函数,默认大小为960X640
GLView* GLView::create(const std::string& viewName)
auto ret = new GLV
if(ret && ret-&initWithRect(viewName, Rect(0, 0, 960, 640), 1)) {
ret-&autorelease();
//create函数
GLView* GLView::createWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
auto ret = new GLV
if(ret && ret-&initWithRect(viewName, rect, frameZoomFactor)) {
ret-&autorelease();
//create函数 全屏大小
GLView* GLView::createWithFullScreen(const std::string& viewName)
auto ret = new GLView();
if(ret && ret-&initWithFullScreen(viewName)) {
ret-&autorelease();
//create函数 全屏大小
GLView* GLView::createWithFullScreen(const std::string& viewName, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
auto ret = new GLView();
if(ret && ret-&initWithFullscreen(viewName, videoMode, monitor)) {
ret-&autorelease();
//初始化函数
bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
//更新下_viewName变量
setViewName(viewName);
//更新下缩放整体缩放系数
_frameZoomFactor = frameZoomF
//在调用 glfwCreateWindow 前更新glfw的属性
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
//调用 glfwCreateWindow,创建窗口
_mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor,
rect.size.height * _frameZoomFactor,
_viewName.c_str(),
//切换为当前上下文
glfwMakeContextCurrent(_mainWindow);
//将当前上下文的回调函数全部注册好
glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
//更新FrameSize大小,这里的FrameSize其实就是windows下游戏窗口的大小
setFrameSize(rect.size.width, rect.size.height);
//检查版本号码
// check OpenGL version at first
const GLubyte* glVersion = glGetString(GL_VERSION);
if ( atof((const char*)glVersion) & 1.5 )
char strComplain[256] = {0};
sprintf(strComplain,
"OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
glVersion);
MessageBox(strComplain, "OpenGL version too old");
//初始化Glew
initGlew();
//允许设置点的大小
// Enable point size by default.
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
bool GLView::initWithFullScreen(const std::string& viewName)
//首先获取当前的主显示器
//Create fullscreen window on primary monitor at its current video mode.
_monitor = glfwGetPrimaryMonitor();
if (nullptr == _monitor)
//然后根据当前的主显示器,获取当前显示器的宽 高 颜色位宽 等信息
const GLFWvidmode* videoMode = glfwGetVideoMode(_monitor);
//根据显示器的宽高初始化
return initWithRect(viewName, Rect(0, 0, videoMode-&width, videoMode-&height), 1.0f);
bool GLView::initWithFullscreen(const std::string &viewname, const GLFWvidmode &videoMode, GLFWmonitor *monitor)
//Create fullscreen on specified monitor at the specified video mode.
_monitor =
if (nullptr == _monitor)
//These are soft contraints. If the video mode is retrieved at runtime, the resulting window and context should match these exactly. If invalid attribs are passed (eg. from an outdated cache), window creation will NOT fail but the actual window/context may differ.
glfwWindowHint(GLFW_REFRESH_RATE, videoMode.refreshRate);
glfwWindowHint(GLFW_RED_BITS, videoMode.redBits);
glfwWindowHint(GLFW_BLUE_BITS, videoMode.blueBits);
glfwWindowHint(GLFW_GREEN_BITS, videoMode.greenBits);
return initWithRect(viewname, Rect(0, 0, videoMode.width, videoMode.height), 1.0f);
//检查GLEW是否就绪,通过判断窗口是否创建好
bool GLView::isOpenGLReady()
return nullptr != _mainW
//结束GLEW
void GLView::end()
if(_mainWindow)
//关闭GLEW的窗口
glfwSetWindowShouldClose(_mainWindow,1);
_mainWindow =
// Release self. Otherwise, GLView could not be freed.
release();//由于GLView继承自Ref,但是外部并不会在程序结束时释放GLView,因此需要自己掉用下Release,来减少引用计数
void GLView::swapBuffers()
{//交换前后buffer
if(_mainWindow)
glfwSwapBuffers(_mainWindow);
//判断是否需要关闭GLView
bool GLView::windowShouldClose()
if(_mainWindow)
return glfwWindowShouldClose(_mainWindow) ? true :
//类似于消息泵,用来进行消息循环
void GLView::pollEvents()
glfwPollEvents();
//支持Retina分辨率,主要用来在Mac平台上
void GLView::enableRetina(bool enabled)
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC)
_isRetinaEnabled =
if (_isRetinaEnabled)
_retinaFactor = 1;
_retinaFactor = 2;
updateFrameSize();
void GLView::setIMEKeyboardState(bool /*bOpen*/)
//设置窗口的缩放大小,由于有时候电脑显示器的分辨率不够,因此为了能够整屏幕
//的显示下GLView,需要再次缩放下窗口的大小
void GLView::setFrameZoomFactor(float zoomFactor)
CCASSERT(zoomFactor & 0.0f, "zoomFactor must be larger than 0");
if (fabs(_frameZoomFactor - zoomFactor) & FLT_EPSILON)
_frameZoomFactor = zoomF
updateFrameSize();
//获取窗口缩放大小
float GLView::getFrameZoomFactor()
return _frameZoomF
//更新游戏窗口大小,主要通过调用glfwSetWindowSize来实现
void GLView::updateFrameSize()
if (_screenSize.width & 0 && _screenSize.height & 0)
int w = 0, h = 0;
glfwGetWindowSize(_mainWindow, &w, &h);
int frameBufferW = 0, frameBufferH = 0;
glfwGetFramebufferSize(_mainWindow, &frameBufferW, &frameBufferH);
if (frameBufferW == 2 * w && frameBufferH == 2 * h)
if (_isRetinaEnabled)
_retinaFactor = 1;
_retinaFactor = 2;
glfwSetWindowSize(_mainWindow, _screenSize.width/2 * _retinaFactor * _frameZoomFactor, _screenSize.height/2 * _retinaFactor * _frameZoomFactor);
_isInRetinaMonitor =
if (_isInRetinaMonitor)
_retinaFactor = 1;
glfwSetWindowSize(_mainWindow, _screenSize.width * _retinaFactor * _frameZoomFactor, _screenSize.height *_retinaFactor * _frameZoomFactor);
_isInRetinaMonitor =
//设置游戏窗口大小
void GLView::setFrameSize(float width, float height)
GLViewProtocol::setFrameSize(width, height);
updateFrameSize();
//设置视点
void GLView::setViewPortInPoints(float x , float y , float w , float h)
glViewport((GLint)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
(GLint)(y * _scaleY * _retinaFactor
* _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
(GLsizei)(w * _scaleX * _retinaFactor * _frameZoomFactor),
(GLsizei)(h * _scaleY * _retinaFactor * _frameZoomFactor));
//设置裁剪
void GLView::setScissorInPoints(float x , float y , float w , float h)
glScissor((GLint)(x * _scaleX * _retinaFactor * _frameZoomFactor + _viewPortRect.origin.x * _retinaFactor * _frameZoomFactor),
(GLint)(y * _scaleY * _retinaFactor
* _frameZoomFactor + _viewPortRect.origin.y * _retinaFactor * _frameZoomFactor),
(GLsizei)(w * _scaleX * _retinaFactor * _frameZoomFactor),
(GLsizei)(h * _scaleY * _retinaFactor * _frameZoomFactor));
//以下为GLView的内部回调函数,通过上面的结构体的二次封装
//错误时直接输出
void GLView::onGLFWError(int errorID, const char* errorDesc)
CCLOGERROR("GLFWError #%d Happen, %s\n", errorID, errorDesc);
//鼠标按下时的相应函数
void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
//按下去的是左键
if(GLFW_MOUSE_BUTTON_LEFT == button)
if(GLFW_PRESS == action)//是按下
_captured =
if (this-&getViewPortRect().equals(Rect::ZERO) || this-&getViewPortRect().containsPoint(Vec2(_mouseX,_mouseY)))
intptr_t id = 0;
this-&handleTouchesBegin(1, &id, &_mouseX, &_mouseY);
else if(GLFW_RELEASE == action)//是抬起
if (_captured)
_captured =
intptr_t id = 0;
this-&handleTouchesEnd(1, &id, &_mouseX, &_mouseY);
//二次处理,不管是左键还是右键,直接将鼠标按下事件传入cocos2dx的事件队列中
if(GLFW_PRESS == action)
EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
event.setCursorPosition(_mouseX, this-&getViewPortRect().size.height - _mouseY);
event.setMouseButton(button);
Director::getInstance()-&getEventDispatcher()-&dispatchEvent(&event);
else if(GLFW_RELEASE == action)
//二次处理,不管是左键还是右键,直接将鼠标松开事件传入cocos2dx的事件队列中
EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
event.setCursorPosition(_mouseX, this-&getViewPortRect().size.height - _mouseY);
event.setMouseButton(button);
Director::getInstance()-&getEventDispatcher()-&dispatchEvent(&event);
//鼠标移动回调函数
void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y)
_mouseX = (float)x;
_mouseY = (float)y;
_mouseX /= this-&getFrameZoomFactor();
_mouseY /= this-&getFrameZoomFactor();
if (_isInRetinaMonitor)//鼠标移动回调函数,判断是否是视网膜屏幕
if (_retinaFactor == 1)
_mouseX *= 2;
_mouseY *= 2;
if (_captured)
intptr_t id = 0;
this-&handleTouchesMove(1, &id, &_mouseX, &_mouseY);
//将事件传入cocos2dx事件队列中
EventMouse event(EventMouse::MouseEventType::MOUSE_MOVE);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
event.setCursorPosition(_mouseX, this-&getViewPortRect().size.height - _mouseY);
Director::getInstance()-&getEventDispatcher()-&dispatchEvent(&event);
//鼠标滚动回调函数
void GLView::onGLFWMouseScrollCallback(GLFWwindow* window, double x, double y)
EventMouse event(EventMouse::MouseEventType::MOUSE_SCROLL);
//Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
event.setScrollData((float)x, -(float)y);
event.setCursorPosition(_mouseX, this-&getViewPortRect().size.height - _mouseY);
Director::getInstance()-&getEventDispatcher()-&dispatchEvent(&event);
void GLView::onGLFWKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
if (GLFW_REPEAT != action)
EventKeyboard event(g_keyCodeMap[key], GLFW_PRESS == action);
auto dispatcher = Director::getInstance()-&getEventDispatcher();
dispatcher-&dispatchEvent(&event);
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character)
IMEDispatcher::sharedDispatcher()-&dispatchInsertText((const char*) &character, 1);
void GLView::onGLFWWindowPosCallback(GLFWwindow *windows, int x, int y)
Director::getInstance()-&setViewport();
//设置FrameBufferSize回调函数,通过返回的宽和高设置游戏窗口大小
void GLView::onGLFWframebuffersize(GLFWwindow* window, int w, int h)
float frameSizeW = _screenSize.
float frameSizeH = _screenSize.
float factorX = frameSizeW / w * _retinaFactor * _frameZoomF
float factorY = frameSizeH / h * _retinaFactor * _frameZoomF
if (fabs(factorX - 0.5f) & FLT_EPSILON && fabs(factorY - 0.5f) & FLT_EPSILON )
_isInRetinaMonitor =
if (_isRetinaEnabled)
_retinaFactor = 1;
_retinaFactor = 2;
glfwSetWindowSize(window, static_cast&int&(frameSizeW * 0.5f * _retinaFactor * _frameZoomFactor) , static_cast&int&(frameSizeH * 0.5f * _retinaFactor * _frameZoomFactor));
else if(fabs(factorX - 2.0f) & FLT_EPSILON && fabs(factorY - 2.0f) & FLT_EPSILON)
_isInRetinaMonitor =
_retinaFactor = 1;
glfwSetWindowSize(window, static_cast&int&(frameSizeW * _retinaFactor * _frameZoomFactor), static_cast&int&(frameSizeH * _retinaFactor * _frameZoomFactor));
//设置WindowSize大小回调函数,当WindowSize大小确定之后,会在回调函数中自动更新下设计大小。
void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height)
if (_resolutionPolicy != ResolutionPolicy::UNKNOWN)
updateDesignResolutionSize();
Director::getInstance()-&setViewport();
//动态绑定内存操作函数
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
static bool glew_dynamic_binding()
const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS);
// If the current opengl driver doesn't have framebuffers methods, check if an extension exists
if (glGenFramebuffers == NULL)
log("OpenGL: glGenFramebuffers is NULL, try to detect an extension");
if (strstr(gl_extensions, "ARB_framebuffer_object"))
log("OpenGL: ARB_framebuffer_object is supported");
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer");
。。。。。
。。。。。
glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap");
if (strstr(gl_extensions, "EXT_framebuffer_object"))
log("OpenGL: EXT_framebuffer_object is supported");
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT");
。。。。。
。。。。。
glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT");
log("OpenGL: No framebuffers extension is supported");
log("OpenGL: Any call to Fbo will crash!");
bool GLView::initGlew()
#if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
GLenum GlewInitResult = glewInit();
if (GLEW_OK != GlewInitResult)
MessageBox((char *)glewGetErrorString(GlewInitResult), "OpenGL error");
if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)
log("Ready for GLSL");
log("Not totally ready :(");
if (glewIsSupported("GL_VERSION_2_0"))
log("Ready for OpenGL 2.0");
log("OpenGL 2.0 not supported");
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
if(glew_dynamic_binding() == false)
MessageBox("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error");
#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
NS_CC_END // end of namespace cocos2d;
这里我们再看一下GLView的继承关系:
class CC_DLL GLView : public GLViewProtocol, public Ref
这里【 Ref 】是用来管理引用计数的。
这里【 GLViewProtocol 】是一个基类,各个平台下会对其进行继承,继承之后的名称都为GLView。
这里就不对这两个类进行笔记了,以后再笔记吧。
本文链接地址:
2018年四月
9101112131415
16171819202122
23242526272829
天气预报来访者IP: 218.90.26.159来访者位置: 中国-江苏-无锡--电信操作系统信息: WindowsNT浏览器信息: Internet Explorer11

我要回帖

更多关于 application无法修改 的文章

 

随机推荐