Cocos2d-x Maker Notes (i)

Source: Internet
Author: User

With Cocos for 4 years, from the beginning of the cocos2d to today's cocos2d-x, quietly watching the touch of procrastination forward. Along the Cocos Code 1.1 point Learning game development, the feelings of cocos is difficult to say, although Cocos has such a flaw, unity has such an advantage, still holding Cocos refused to give up. Long, long time ago, I wanted to write a Cocos-based editor, but a little fart programmer how to do, a moment to think about cocosstudio hundreds of people's team to do, a moment to think of such a no money way things, do have what meaning, a moment to think about their lazy kind, How can can afford so big a project, abruptly always dragged. But recently some figured out, usually extra time desperately thinking about how to make money pressure and really do not earn anything, hair daze and some waste, then the development of interest and hobbies, just see "Red Babe" blog written "a diligent person can go beyond a lazy company", really is a life model. Of course, I am very self-aware, I am not diligent people, usually can save a point of power will not waste a lot of a. Pick up a long time ago interest, start DIY a Cocos editor. (the Cocos that appear later in this article are generally referred to as cocos2d-x)

Step into the topic, about the editor, I choose to develop on the Mac platform, purely personal hobbies. In the implementation, I chose to reuse most of the Cocos Engine Code, intercept events on top of the engine code, and re-provide a set of Mainloop and event handling schemes used by the editor.

So, the first thing is to rewrite the editor-specific Glviewimpl, look at the Cocos code, almost spit blood, on Mac and Windows is completely using GLFW create a non-resizable window, and then look at the GLFW code, Stop ObjC Runloop, himself, and then cocos in the code wrote a

 while (! _exit ) {    mainloop ();    Sleep ();}

, which let me just simply modify the extended dream on the Mac version completely dashed. Although GLFW did a lot of convenient processing, but I do not have a way to GLFW and Mac app together. Had to do their own, step by step to achieve a glview. In view of the convenience, my code only uses COCOS2DX 3.8 to create an empty project, and then in the project generated by the IOS_MAC project, the first step is to create a new editor dedicated target, I'm going to rewrite it from MAIN.M, to build a basic Mac app that needs the content, the directory is roughly the following:

Where Cxmmainview will be the main window of the editor (temporarily without him), CCGLVIEWIMPL-CXM is the focus of this time, rewrite the Glview.

Overriding the Create method, the original ccglviewimple-desktop in Create 3 main things, set the GLFW window required parameters, set GLFW window event callback, and finally create OpenGL window, What we are going to do is from the GLFW source code to the relevant 1.1 points dug out, in fact, is still very simple, directly paste some code:

Add some property methods in. h:

BOOL initwithrect (const std::string& ViewName, rect rect, float Framezoomfactor);

_glview ; void _glpixelformat ; void _glcontext;

In. mm add-on implementations:

BOOLCxmglviewimpl::initwithrect (ConstSTD::string& ViewName, rect rect,floatframezoomfactor)    {setviewname (viewName); _screensize=rect.size; _designresolutionsize=rect.size; _framezoomfactor=Framezoomfactor; _viewportrect=rect; //Init View_glview = [[NSView alloc] Initwithframe:nsmakerect (0,0, _screensize.width, _screensize.height)]; //init pixel format#defineAdd_attr (x) {attributes[attributecount++] = x;}#defineADD_ATTR2 (x, y) {add_attr (x); Add_attr (y); }unsignedintAttributecount =0; Nsopenglpixelformatattribute attributes[ +];    Add_attr (nsopenglpfaaccelerated);    Add_attr (Nsopenglpfaclosestpolicy); ADD_ATTR2 (Nsopenglpfaauxbuffers,0); ADD_ATTR2 (Nsopenglpfaaccumsize,0); intColorbits = _glcontextattrs.redbits + _glcontextattrs.greenbits +_glcontextattrs.bluebits;    ADD_ATTR2 (Nsopenglpfacolorsize, colorbits);    ADD_ATTR2 (Nsopenglpfaalphasize, _glcontextattrs.alphabits);    ADD_ATTR2 (Nsopenglpfadepthsize, _glcontextattrs.depthbits);    ADD_ATTR2 (Nsopenglpfastencilsize, _glcontextattrs.stencilbits);    Add_attr (Nsopenglpfadoublebuffer); ADD_ATTR2 (Nsopenglpfasamplebuffers,4); Add_attr (0);#undefAdd_attr#undefAdd_attr2_glpixelformat=[[Nsopenglpixelformat alloc] initwithattributes:attributes]; if(!_glpixelformat) {MessageBox ("Failed to create pixel format.","Error"); return false; }        //init content_glcontext = [[Nsopenglcontext alloc] Initwithformat: (nsopenglpixelformat*) _glpixelformat Sharecontext:nil]; [(Nsopenglcontext*) _glcontext Setview: (nsview*) _glview]; [(Nsopenglcontext*) _glcontext Makecurrentcontext]; //Check OpenGL version at first    Constglubyte* glversion =glgetstring (gl_version); if(Utils::atof (Const Char*) glversion) <1.5 )    {        Charstrcomplain[ the] = {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"); return false; }        //Enable point size by default.glenable (gl_vertex_program_point_size); return true;}

Most of the code is moved to and copied over, please call me Nature's porter. There are still some simple rewrite methods, such as Isopenglready, swapbuffers and so on are not posted.

To be continued, there are also sections on framesize, viewport and coordinate transformations.

Cocos2d-x Maker Notes (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.