(一) Android平台下:

cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml

  1. <application android:label="@string/app_name"
  2. android:icon="@drawable/icon">
  3.  
  4. <!-- Tell Cocos2dxActivity the name of our .so -->
  5. <meta-data android:name="android.app.lib_name"
  6. android:value="cocos2dcpp" />
  7.  
  8. <activity android:name="org.cocos2dx.cpp.AppActivity"
  9. android:label="@string/app_name"
  10. android:screenOrientation="landscape"
  11. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  12. android:configChanges="orientation">
  13.  
  14. <intent-filter>
  15. <action android:name="android.intent.action.MAIN" />
  16. <category android:name="android.intent.category.LAUNCHER" />
  17. </intent-filter>
  18. </activity>
  19. </application>

由此得知启动窗口类为 org.cocos2dx.cpp.AppActivity,并继承之 Cocos2dxActivity.

  1. package org.cocos2dx.cpp;
  2.  
  3. import org.cocos2dx.lib.Cocos2dxActivity;
  4.  
  5. public class AppActivity extends Cocos2dxActivity {
  6. }
  1. public abstract class Cocos2dxActivity extends Activity implements Cocos2dxHelperListener

看下 Cocos2dxActivity 的 onCreate

  1. @Override
  2. protected void onCreate(final Bundle savedInstanceState) {
  3. Log.i(TAG, "------onCreate----");
  4. super.onCreate(savedInstanceState);
  5. CocosPlayClient.init(this, false);
  6.  
  7. onLoadNativeLibraries();//加载了一些静态库
  8.  
  9. sContext = this;
  10. this.mHandler = new Cocos2dxHandler(this);
  11.  
  12. Cocos2dxHelper.init(this);
  13.  
  14. this.mGLContextAttrs = getGLContextAttrs();
  15. this.init();//初始化
  16.  
  17. if (mVideoHelper == null) {
  18. mVideoHelper = new Cocos2dxVideoHelper(this, mFrameLayout);
  19. }
  20.  
  21. if(mWebViewHelper == null){
  22. mWebViewHelper = new Cocos2dxWebViewHelper(mFrameLayout);
  23. }
  24. }

onCreate 调用了  init()  初始化

  1. public void init() {
  2.  
  3. // FrameLayout
  4. ViewGroup.LayoutParams framelayout_params =
  5. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  6. ViewGroup.LayoutParams.MATCH_PARENT);
  7. mFrameLayout = new FrameLayout(this);
  8. mFrameLayout.setLayoutParams(framelayout_params);
  9.  
  10. // Cocos2dxEditText layout
  11. ViewGroup.LayoutParams edittext_layout_params =
  12. new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  13. ViewGroup.LayoutParams.WRAP_CONTENT);
  14. Cocos2dxEditText edittext = new Cocos2dxEditText(this);
  15. edittext.setLayoutParams(edittext_layout_params);
  16.  
  17. // ...add to FrameLayout
  18. mFrameLayout.addView(edittext);
  19.  
  20. // Cocos2dxGLSurfaceView
  21. this.mGLSurfaceView = this.onCreateView();
  22.  
  23. // ...add to FrameLayout
  24. mFrameLayout.addView(this.mGLSurfaceView);
  25.  
  26. // Switch to supported OpenGL (ARGB888) mode on emulator
  27. if (isAndroidEmulator())
  28. this.mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
  29.  
  30. this.mGLSurfaceView.setCocos2dxRenderer(new Cocos2dxRenderer());
  31. this.mGLSurfaceView.setCocos2dxEditText(edittext);
  32.  
  33. // Set framelayout as the content view
  34. setContentView(mFrameLayout);
  35. }

最终显示的视图为 this.mGLSurfaceView = this.onCreateView();

  1. public Cocos2dxGLSurfaceView onCreateView() {
  2. Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
  3. //this line is need on some device if we specify an alpha bits
  4. if(this.mGLContextAttrs[3] > 0) glSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
  5.  
  6. Cocos2dxEGLConfigChooser chooser = new Cocos2dxEGLConfigChooser(this.mGLContextAttrs);
  7. glSurfaceView.setEGLConfigChooser(chooser);
  8.  
  9. return glSurfaceView;
  10. }

Cocos2dxGLSurfaceView 就是最终显示的视图,事件处理也在这个类中,包括 onResume,onPause,onSizeChanged,onKeyDown,onTouchEvent等 主要看下onTouchEvent事件的处理过程.

  1. @Override
  2. public boolean onTouchEvent(final MotionEvent pMotionEvent) {
  3.  
  4. //Log.d(TAG, "------onTouchEvent action=----"+pMotionEvent.getAction());
  5.  
  6. // these data are used in ACTION_MOVE and ACTION_CANCEL
  7. final int pointerNumber = pMotionEvent.getPointerCount();
  8. final int[] ids = new int[pointerNumber];
  9. final float[] xs = new float[pointerNumber];
  10. final float[] ys = new float[pointerNumber];
  11.  
  12. for (int i = 0; i < pointerNumber; i++) {
  13. ids[i] = pMotionEvent.getPointerId(i);
  14. xs[i] = pMotionEvent.getX(i);
  15. ys[i] = pMotionEvent.getY(i);
  16. }
  17.  
  18. switch (pMotionEvent.getAction() & MotionEvent.ACTION_MASK) {
  19. case MotionEvent.ACTION_POINTER_DOWN:
  20. final int indexPointerDown = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  21. final int idPointerDown = pMotionEvent.getPointerId(indexPointerDown);
  22. final float xPointerDown = pMotionEvent.getX(indexPointerDown);
  23. final float yPointerDown = pMotionEvent.getY(indexPointerDown);
  24.  
  25. this.queueEvent(new Runnable() {
  26. @Override
  27. public void run() {
  28. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idPointerDown, xPointerDown, yPointerDown);
  29. }
  30. });
  31. break;
  32.  
  33. case MotionEvent.ACTION_DOWN:
  34. // there are only one finger on the screen
  35. final int idDown = pMotionEvent.getPointerId(0);
  36. final float xDown = xs[0];
  37. final float yDown = ys[0];
  38.  
  39. this.queueEvent(new Runnable() {
  40. @Override
  41. public void run() {
  42. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown(idDown, xDown, yDown);
  43. }
  44. });
  45. break;
  46.  
  47. case MotionEvent.ACTION_MOVE:
  48. this.queueEvent(new Runnable() {
  49. @Override
  50. public void run() {
  51. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionMove(ids, xs, ys);
  52. }
  53. });
  54. break;
  55.  
  56. case MotionEvent.ACTION_POINTER_UP:
  57. final int indexPointUp = pMotionEvent.getAction() >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
  58. final int idPointerUp = pMotionEvent.getPointerId(indexPointUp);
  59. final float xPointerUp = pMotionEvent.getX(indexPointUp);
  60. final float yPointerUp = pMotionEvent.getY(indexPointUp);
  61.  
  62. this.queueEvent(new Runnable() {
  63. @Override
  64. public void run() {
  65. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idPointerUp, xPointerUp, yPointerUp);
  66. }
  67. });
  68. break;
  69.  
  70. case MotionEvent.ACTION_UP:
  71. // there are only one finger on the screen
  72. final int idUp = pMotionEvent.getPointerId(0);
  73. final float xUp = xs[0];
  74. final float yUp = ys[0];
  75.  
  76. this.queueEvent(new Runnable() {
  77. @Override
  78. public void run() {
  79. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionUp(idUp, xUp, yUp);
  80. }
  81. });
  82. break;
  83.  
  84. case MotionEvent.ACTION_CANCEL:
  85. this.queueEvent(new Runnable() {
  86. @Override
  87. public void run() {
  88. Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionCancel(ids, xs, ys);
  89. }
  90. });
  91. break;
  92. }
  93.  
  94. /*
  95. if (BuildConfig.DEBUG) {
  96. Cocos2dxGLSurfaceView.dumpMotionEvent(pMotionEvent);
  97. }
  98. */
  99. return true;
  100. }

看下 MotionEvent.ACTION_DOWN,调用了Cocos2dxGLSurfaceView.this.mCocos2dxRenderer.handleActionDown.

  1. public void handleActionDown(final int id, final float x, final float y) {
  2. Log.i("Cocos2dxRenderer","-----handleActionDown--");
  3. Cocos2dxRenderer.nativeTouchesBegin(id, x, y);
  4. }

这里的nativeTouchesBegin 是一个jni方法,是现在cocos2d\cocos\platform\android\jni\TouchesJni.cpp里,

  1. private static native void nativeTouchesBegin(final int id, final float x, final float y);
  1. JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloat x, jfloat y) {
  2. intptr_t idlong = id;
  3. log("----Info:nativeTouchesBegin id = %d, x=%f, y=%f",id,x,y);
  4. cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(1, &idlong, &x, &y);
  5. }

最后调用GLView::handleTouchesBegin (其实GLView并没有重写父类的handleTouchesBegin方法,所以android下的触发事件,最后是通过GLViewProtocol类的handleTouchesBegin方法进行处理的). 通过 EventDispatcher::dispatchEvent进行事件的分发,调用事件响应函数,都是C++里完成的,就不再往下分析了。

附注:

C++里对事件的分发机制是通过Event,EventListener和EventDispatcher三个主要类完成的,具体详见cocos2d-x 源码分析 : EventDispatcher、EventListener、Event 源码分析 (新触摸机制,新的NotificationCenter机制).

[转自]cocos2dx android平台事件系统解析

(二) 上面针对的是Android平台下,其他系统事件通知到cocos2dx的流程,在desktop和ios下的流程类似,都有相应平台下的处理文件接收系统事件,如下:

  1. /home/yangxt/document/cocos2d-x-3.2/cocos/platform/CCGLViewProtocol.cpp:
  2. : void GLViewProtocol::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
  3.  
  4. /home/yangxt/document/cocos2d-x-3.2/cocos/platform/CCGLViewProtocol.h:
  5. : virtual void handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[]);
  6.  
  7. /home/yangxt/document/cocos2d-x-3.2/cocos/platform/android/jni/TouchesJni.cpp:
  8. : cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(, &id, &x, &y);
  9.  
  10. /home/yangxt/document/cocos2d-x-3.2/cocos/platform/desktop/CCGLView.cpp:
  11. : this->handleTouchesBegin(, &id, &_mouseX, &_mouseY);
  12.  
  13. /home/yangxt/document/cocos2d-x-3.2/cocos/platform/ios/CCEAGLView.mm:
  14. : glview->handleTouchesBegin(i, (intptr_t*)ids, xs, ys);

从上面可以看出,android下的系统调用最后是调用到了TouchesJni.cpp文件下的触摸事件.也即本文第(一)部分所分析的结果.如果在ios平台下,可以看到是通过CCEAGLView.mm文件处理的.ios下这里不做过多分析. 而desktop平台下,直接通过CCGLView.cpp处理.下面看看desktop平台下的处理过程:

我们可以在GLView.cpp下找到下面的方法:

  1. bool GLView::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
  2. {
  3. setViewName(viewName);
  4.  
  5. _frameZoomFactor = frameZoomFactor;
  6.  
  7. glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
  8.  
  9. _mainWindow = glfwCreateWindow(rect.size.width * _frameZoomFactor,
  10. rect.size.height * _frameZoomFactor,
  11. _viewName.c_str(),
  12. _monitor,
  13. nullptr);
  14. glfwMakeContextCurrent(_mainWindow);
  15.  
  16. glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);
  17. glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack);
  18. glfwSetScrollCallback(_mainWindow, GLFWEventHandler::onGLFWMouseScrollCallback);
  19. glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback);
  20. glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback);
  21. glfwSetWindowPosCallback(_mainWindow, GLFWEventHandler::onGLFWWindowPosCallback);
  22. glfwSetFramebufferSizeCallback(_mainWindow, GLFWEventHandler::onGLFWframebuffersize);
  23. glfwSetWindowSizeCallback(_mainWindow, GLFWEventHandler::onGLFWWindowSizeFunCallback);
  24.  
  25. setFrameSize(rect.size.width, rect.size.height);
  26.  
  27. // check OpenGL version at first
  28. const GLubyte* glVersion = glGetString(GL_VERSION);
  29.  
  30. if ( utils::atof((const char*)glVersion) < 1.5 )
  31. {
  32. char strComplain[] = {};
  33. sprintf(strComplain,
  34. "OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.",
  35. glVersion);
  36. MessageBox(strComplain, "OpenGL version too old");
  37. return false;
  38. }
  39.  
  40. initGlew();
  41.  
  42. // Enable point size by default.
  43. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
  44.  
  45. return true;
  46. }

glfwCreateWindow方法里面就完成了窗口的创建以及其他的一些初始化工作,并且也设置了鼠标,键盘等一下响应回调函数,这里,我们看看glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);的回调函数:

  1. void GLView::onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify)
  2. {
  3. if(GLFW_MOUSE_BUTTON_LEFT == button)
  4. {
  5. if(GLFW_PRESS == action)
  6. {
  7. _captured = true;
  8. if (this->getViewPortRect().equals(Rect::ZERO) || this->getViewPortRect().containsPoint(Vec2(_mouseX,_mouseY)))
  9. {
  10. intptr_t id = ;
  11. this->handleTouchesBegin(, &id, &_mouseX, &_mouseY);
  12. }
  13. }
  14. else if(GLFW_RELEASE == action)
  15. {
  16. if (_captured)
  17. {
  18. _captured = false;
  19. intptr_t id = ;
  20. this->handleTouchesEnd(, &id, &_mouseX, &_mouseY);
  21. }
  22. }
  23. }
  24.  
  25. //Because OpenGL and cocos2d-x uses different Y axis, we need to convert the coordinate here
  26. float cursorX = (_mouseX - _viewPortRect.origin.x) / _scaleX;
  27. float cursorY = (_viewPortRect.origin.y + _viewPortRect.size.height - _mouseY) / _scaleY;
  28.  
  29. if(GLFW_PRESS == action)
  30. {
  31. EventMouse event(EventMouse::MouseEventType::MOUSE_DOWN);
  32. event.setCursorPosition(cursorX, cursorY);
  33. event.setMouseButton(button);
  34. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  35. }
  36. else if(GLFW_RELEASE == action)
  37. {
  38. EventMouse event(EventMouse::MouseEventType::MOUSE_UP);
  39. event.setCursorPosition(cursorX, cursorY);
  40. event.setMouseButton(button);
  41. Director::getInstance()->getEventDispatcher()->dispatchEvent(&event);
  42. }
  43. }

可以看到,在GLView里面直接处理窗口鼠标事件,并通过dispatchEvent将事件分发给cocos2dx. cocos2dx里面的事件分发这里也不作过多阐述.

附注:

上面对事件分发机制的分析中,我们可以看到,GLViewProtocol类实际负责了窗口级别的功能管理和实现, 包括:坐标和缩放管理, 画图工具,按键事件,而这些正是cocos2d-x游戏引擎核心(3.x)----启动渲染流程 博文分析中可以看到的.

cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析的更多相关文章

  1. cocos2d-x 事件分发机制 ——加速计事件监听

    加速计事件监听机制 在上一篇中介绍了cocos2d-x中的触摸事件机制,这篇来介绍下游戏中也常常常使用到的加速计事件,这些都是游戏中的常常要用到的. 移动设备上一个非常重要的输入源是设备的方向.大多数 ...

  2. cocos2d-x 事件分发机制 ——触摸事件监听

    cocos2d-x 3.0 出来已经好久了,也已经用3.0写了几个小游戏,感觉3.0的事件触发机制太赞了,随这里总结一下.也算是对知识的一种回顾和加深理解. 3.0的事件分发机制中.须要也只须要通过创 ...

  3. 【Unity游戏开发】用C#和Lua实现Unity中的事件分发机制EventDispatcher

    一.简介 最近马三换了一家大公司工作,公司制度规范了一些,因此平时的业余时间多了不少.但是人却懒了下来,最近这一个月都没怎么研究新技术,博客写得也是拖拖拉拉,周六周天就躺尸在家看帖子.看小说,要么就是 ...

  4. 本以为精通Android事件分发机制,没想到被面试官问懵了

    文章中出现的源码均基于8.0 前言 事件分发机制不仅仅是核心知识点更是难点,并且还是View的一大难题滑动冲突解决方法的理论基础,因此掌握好View的事件分发机制是十分重要的. 一.基本认识 1. 事 ...

  5. Android事件分发机制源码分析

    Android事件分发机制源码分析 Android事件分发机制源码分析 Part1事件来源以及传递顺序 Activity分发事件源码 PhoneWindow分发事件源码 小结 Part2ViewGro ...

  6. Android面试收集录6 事件分发机制

    转自:秋招面试宝典. 一. 基础认知 1.1 事件分发的对象是谁? 答:事件 当用户触摸屏幕时(View或ViewGroup派生的控件),将产生点击事件(Touch事件). Touch事件相关细节(发 ...

  7. Android进阶——Android事件分发机制之dispatchTouchEvent、onInterceptTouchEvent、onTouchEvent

    Android事件分发机制可以说是我们Android工程师面试题中的必考题,弄懂它的原理是我们避不开的任务,所以长痛不如短痛,花点时间干掉他,废话不多说,开车啦 Android事件分发机制的发生在Vi ...

  8. 浅谈Android中的事件分发机制

    View事件分发机制的本质就是就是MotionEvent事件的分发过程,即MotionEvent产生后是怎样在View之间传递及处理的. 首先介绍一下什么是MotionEvent.所谓MotionEv ...

  9. cocos2d-x游戏引擎核心之五——触摸事件和触摸分发器机制

    一.触摸事件 为了处理屏幕触摸事件,Cocos2d-x 提供了非常方便.灵活的支持.在深入研究 Cocos2d-x 的触摸事件分发机制之前,我们利用 CCLayer 已经封装好的触摸接口来实现对简单的 ...

随机推荐

  1. 咖啡之约--体验 SourceAnywhere

    http://www.damingsoft.com/campaign/school-campaign-coffee-code.aspx 必备技能:代码版本控制 不论你是菜鸟还是大牛,想要获得高薪水和高 ...

  2. 微信小程序无法获取UnionId的情况及处理

    问题背景:做了微信小程序,一切都还正常,但是最后体验版放出去时,却发现很多用户无法绑定用户,后台返回:参数非法.经过多方排查,发现是微信拿到的code请求返回的数据里没有UnionId,也就是接口返回 ...

  3. Morris图表使用小记

    挺好用的,碰到几个问题,有的是瞎试解决了的: 1.我想折线图能够响应单击事件,即点击某个节点后,就能加载进一步的信息,帮助没找到,参照另外一个地方的写法,居然支持事件 Morris.Line({ el ...

  4. 微软BI 之SSRS 系列 - 如何在 MDX 查询中获取有效的 MEMBER 成员属性作为参数传递

    这篇小文章的来源是 天善问答,比如在报表中要根据点击某一个成员名称然后作为参数传递给自身报表或者下一张报表,这个在普通的 SQL 查询中没有任何问题.但是在 MDX 中查询是有区别的,比如在 MDX ...

  5. Ubuntu中安装和配置 Java JDK,并卸载自带OpenJDK(以Ubuntu 14.04为例)

    1.下载jdk-7u67-linux-x64.tar.gz 2.用ftp客户端工具filezilla上传到ubuntu的合适文件夹.如果如果不能上传到指定文件夹可能是文件夹权限不足,修改文件夹可执行权 ...

  6. 配置Windows Server 2008/2012/2016允许2个用户同时远程桌面

    Windows Server 系列服务器默认情况下只能支持一个用户远程,如果第二个人远程上去之后会直接把前面一个登录用户踢掉.在日常工作中如果有多个人需要同时远程过去工作,会很不方面. 网上很多教程讲 ...

  7. 服务端怎样暴露IBinder接口对象

    服务端怎样暴露IBinder接口对象: package com.example.mydownload; import android.app.Service; import android.conte ...

  8. C# 重启exe

    休夸此地分天下 c# 关闭和重启.exe程序 Process[] myprocess = Process.GetProcessesByName("a"); )//判断如果存在 { ...

  9. JAVA和JAVAC 命令行

    转自:http://www.blogjava.net/pdw2009/archive/2008/06/12/207413.html?opt=admin javac和java命令行中的-classpat ...

  10. Fluent动网格【4】:DEFINE_CG_MOTION宏实例

    DEFINE_CG_MOTION宏通常用于定义刚体部件的运动.本文以一个简单的案例描述DEFINE_CG_MOTION的使用方法. 案例描述 本次计算的案例如图所示.在计算域中有一个刚体块(图中的小正 ...