使用cocos2d-x开发各种编辑器
来源:互联网 发布:宏编程鼠标压枪教程 编辑:程序博客网 时间:2024/05/16 04:08
感觉cocos2d/x的各种编辑器还是挺丰富的,但由于都是免费的软件,其中有各种隐患,cocos2d/x在不断的更新,但如果相应的编辑器没更新的话,这软件就不能用了,或者有些软件的bug层出不穷,所以自己能动手开发相应的编辑器还是有必要的。一开始想用windows的GDI开发,后来再想了一下为啥不直接用cocos2d-x来开发呢,将cocos2d-x嵌入到MFC里,就可以将windows各种强大的控件跟cocos2d-x各种动画结合起来了。
原理很简单,cocos2d-x在windows上运行的时候其实也是依赖于窗口的,但那是一个顶层窗口,要稍微修改一下,将cocos2d-x运行在一个子窗口里,这样就能嵌入到编辑器里。
打开cocos2d-x的libcoco2d库里的platform/win32/CCEGLView.cpp,替换boolCCEGLView::Create()的代码:
bool bRet = false; do { CC_BREAK_IF(m_hWnd); HINSTANCE hInstance = GetModuleHandle( NULL ); WNDCLASS wc; // Windows Class Structure // Redraw On Size, And Own DC For Window. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = _WindowProc; // WndProc Handles Messages wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data wc.hInstance = hInstance; // Set The Instance wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); // Load The Default Icon wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load The Arrow Pointer wc.hbrBackground = NULL; // No Background Required For GL wc.lpszMenuName = m_menu; // wc.lpszClassName = kWindowClassName; // Set The Class Name CC_BREAK_IF(! RegisterClass(&wc) && 1410 != GetLastError()); // center window position RECT rcDesktop; GetWindowRect(GetDesktopWindow(), &rcDesktop); WCHAR wszBuf[50] = {0}; MultiByteToWideChar(CP_UTF8, 0, m_szViewName, -1, wszBuf, sizeof(wszBuf)); // create window m_hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, // Extended Style For The Window kWindowClassName, // Class Name wszBuf, // Window Title WS_CAPTION | WS_POPUPWINDOW | WS_MINIMIZEBOX, // Defined Window Style 0, 0, // Window Position //TODO: Initializing width with a large value to avoid getting a wrong client area by 'GetClientRect' function. 1000, // Window Width 1000, // Window Height NULL, // No Parent Window NULL, // No Menu hInstance, // Instance NULL ); CC_BREAK_IF(! m_hWnd); bRet = initGL();if(!bRet) destroyGL(); CC_BREAK_IF(!bRet); s_pMainWindow = this; bRet = true; } while (0);#if(_MSC_VER >= 1600) m_bSupportTouch = CheckTouchSupport(); if(m_bSupportTouch){ m_bSupportTouch = (s_pfRegisterTouchWindowFunction(m_hWnd, 0) != 0); }#endif /* #if(_MSC_VER >= 1600) */ return bRet;
替换为
bool bRet = false; do { CC_BREAK_IF(m_hWnd); HINSTANCE hInstance = GetModuleHandle( NULL ); WNDCLASS wc; // Windows Class Structure // Redraw On Size, And Own DC For Window. wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = _WindowProc; // WndProc Handles Messages wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data wc.hInstance = hInstance; // Set The Instance wc.hIcon = LoadIcon( NULL, IDI_WINLOGO ); // Load The Default Icon wc.hCursor = LoadCursor( NULL, IDC_ARROW ); // Load The Arrow Pointer wc.hbrBackground = NULL; // No Background Required For GL wc.lpszMenuName = NULL; // wc.lpszClassName = kWindowClassName; // Set The Class Name CC_BREAK_IF(! RegisterClass(&wc) && 1410 != GetLastError()); // center window position RECT rcDesktop; //GetWindowRect(GetDesktopWindow(), &rcDesktop);GetClientRect(mParentHwnd,&rcDesktop); WCHAR wszBuf[50] = {0}; MultiByteToWideChar(CP_UTF8, 0, m_szViewName, -1, wszBuf, sizeof(wszBuf)); // create window m_hWnd = CreateWindowEx( WS_EX_APPWINDOW | WS_EX_WINDOWEDGE, // Extended Style For The Window kWindowClassName, // Class Name wszBuf, // Window Title WS_CHILDWINDOW|WS_VISIBLE, // Defined Window Style 0, 0, // Window Position //TODO: Initializing width with a large value to avoid getting a wrong client area by 'GetClientRect' function. 1000, // Window Width 1000, // Window Height mParentHwnd, // No Parent Window NULL, // No Menu hInstance, // Instance NULL ); CC_BREAK_IF(! m_hWnd); bRet = initGL();if(!bRet) destroyGL(); CC_BREAK_IF(!bRet); s_pMainWindow = this; bRet = true; } while (0);#if(_MSC_VER >= 1600) m_bSupportTouch = CheckTouchSupport(); if(m_bSupportTouch){ m_bSupportTouch = (s_pfRegisterTouchWindowFunction(m_hWnd, 0) != 0); }#endif /* #if(_MSC_VER >= 1600) */ return bRet;
在libcocos2d项目下增加一个文件,里面存放MFC跟cocos2d-x交互的C函数。
extern "C"{CC_DLL bool M2InitializeApplication( HWND hwnd ){cocos2d::CCEGLView::SetParentHwnd(hwnd);// create the application instanceAppDelegate app;cocos2d::CCEGLView* eglView = cocos2d::CCEGLView::sharedOpenGLView();eglView->setFrameSize(480, 320);// The resolution of ipad3 is very large. In general, PC's resolution is smaller than it.// So we need to invoke 'setFrameZoomFactor'(only valid on desktop(win32, mac, linux)) to make the window smaller.//eglView->setFrameZoomFactor(0.4f);cocos2d::CCApplication::sharedApplication()->run();return true;}CC_DLL bool M2GameCleanUp(){cocos2d::CCDirector::sharedDirector()->end();cocos2d::CCEGLView* eglView = cocos2d::CCEGLView::sharedOpenGLView();eglView->Destroy();return true;}CC_DLL bool M2GameLoop( float interval ){cocos2d::CCDirector::sharedDirector()->mainLoop();return true;}}
后在MFC的对话框或View的初始化函数里增加如下代码,以绑定cocos2d的句柄并设置cocos2d视口的大小:
HWND hCocos = ::GetDlgItem(this->m_hWnd, IDC_PANEL);CRect rect;::GetWindowRect(hCocos, &rect);ScreenToClient(&rect);::MoveWindow(hCocos, rect.left, rect.top, 480, 320, TRUE);::M2InitializeApplication(hCocos);
增加一个定时器辅助cocos2d进行重绘:
SetTimer(1,16,NULL);
在OnTimer函数里刷新cocos2d-x:
::M2GameLoop(16);
DEMO可以在这里下载:http://download.csdn.net/detail/visualcatsharp/4947482
- 使用cocos2d-x开发各种编辑器
- Cocos2d-x游戏开发之一 CocosBuilder编辑器使用
- cocos2d-x 使用地图编辑器
- wxPython结合cocos2d-x开发编辑器
- 【Cocos2d-X开发学习笔记】开发工具之Tiled地图编辑器的使用
- cocos2d-x中使用TileMap地图编辑器
- cocos2d-x 3.2 |Tiled地图编辑器使用
- 使用 Cocos2d-x 和粒子编辑器实现
- cocos2d-x 粒子编辑器和使用
- Cocos2d-x 3.0 开发(十)使用CocoStudio场景编辑器关联组件
- Cocos2d-x 3.0 开发(十)使用CocoStudio场景编辑器关联组件
- cocos2d-x各种Size
- cocos2d-x粒子编辑器
- cocos2d-x 游戏开发工具,各种官方地址
- 搭建cocos2d-x-andriod开发环境遇到的各种问题
- [Cocos2d-x开发问题-2] 有关Sprite的各种操作
- cocos2d-x+lua开发模式下编辑器的选择
- cocos2d-x+lua开发模式下编辑器的选择
- 线程实现的两种方式——用户空间和内核中
- Ajax Control Toolkit中CalendarExtender(日期)控件显示中文的设定
- 主从或者主主手工维护同步数据
- C++中如何使用gsoap开发WebService
- 不错的向下缓冲式菜单特效
- 使用cocos2d-x开发各种编辑器
- Perl 中有趣的函数
- VS中Debug与Release的区别
- Ubuntu下交叉编译 win32 vlc-2.1.0-git
- 使用VMware运行最简单的操作系统
- Oracle数据库与MySQL数据库的区别
- [Spring3.x源码]AOP(三)代理类的实例化
- MFC获取本地IP地址
- 自评的程序员级别,你到了哪级?