Cocos2d-x painting

Source: Internet
Author: User

Ccrendertexture
Understanding by yourself

Ccrendertexture is similar to a blank"Canvas", The user uses a custom brush
(Ccsprite *), in the touch event, the movement trace of the brush is "recorded" to "Draw" a variety of artistic effects. The record method is simple. First
The ccrendertexture calls its own begin () function, enables the "record" function, then calls the brush-> visit () to "Draw" itself on this canvas, and finally
Ccrendertexture calls end () to end the record, and then OK.

Here I think the ccrendertexture overwrites the texture of the paint brush to its own texture instead of creating new textures, so the consumption is relatively low, even if the painting is frequent, the number of frames can also remain stable, which is a very good class.

As long as the "record" function is enabled in begin (), any subsequent ccnode * object can be "painted" on it as long as visit () is called. ThereforeScreenshotsFunction, can fully use the ccrendertexture to achieve, specific can see in tests example, cocos2d-x has provided the relevant example, look at the source code can be understood.

Advantages

Using ccrendertexture can easily achieve the idealDrawingEffect (you only need to create a very small brush image, then load it with ccsprite, and then call a ccsprite visit () as appropriate), the number of frames is low, it is also very convenient to achieve the gameScreenshotsFunction 《You draw meIt should be the first thing that comes to mind for such a project.

Disadvantages

Because the hardware or OpenGL versions of each Android mobile phone are different, some mobile phones use ccrendertexture.ScreenSuch as HTC. Even the official tests example is hard to escape. This fatal weakness leads to 《You draw meDifferent mobile phones are not allowed...

This bug I think is not a problem with the cocos2d-x engine, some cool say it's because texture is being painted repeatedly (It is okay to draw only once, so the screenshot function should not be affected), Maybe the OpenGL of each Android mobile phone is different, so the problem has not been solved, and you can only wait.Cocos2d-xOr there are otherAfter the oxFixed can be removed.

It is not recommended to use the screen because of the problem. (If you have solved the screen problem, please advise me. Thank you)

(Landscape reference)

Example

Ccrendertexture is probably familiar to everyone. There are also examples in tests. You need to know the usage and check the source code of tests.

Use OpenGL-es for drawing
Understanding by yourself

OpenGL-es is a lite version of OpenGL. Due to its simplicity, many commonly used OpenGL functions are "simplified". As a result, many online image search algorithms cannot be used. Therefore, it is very difficult to draw Images Using OpenGL-es.

In the cocos2d-x, OpenGL-es generally calls its related functions in the draw () function. Of course, cocos2d-x also encapsulates several commonly used drawing functions: ccdrawline, ccdrawcircle and so on, of course, cocos2d-x also provides an example that can be easily found in tests.

Advantages

Not yet, because I am not familiar with OpenGL-es...

Disadvantages

It is difficult to implement and can intercept a large number of people. Because it cannot be drawn in the touch event like the ccrendertexture, The ccpoint to be drawn is usually recorded in the touch event, and then traversed in the draw () function, to achieve the drawing effect.

Shift
The animation points are generally saved. Note that you must save them all. If you only save the current vertex, draw () will draw only one vertex, the effect is like moving the mouse following a "spot ",
Yes. Generally, when you draw a painting, the mouse will drag out N points. If you just save the simple vector <ccpoint>, the vector will be oversized and traverse in the draw.
It also takes a lot of time, resulting in the loss of frames in a short time. Let's take a look at how to save the data structure. Colleagues use line segments as the data structure to record points. This will consume a little, and the number of frames will remain stable.

Another disadvantage is that, if the algorithm is not implemented well, the drawn results will be very poor. Although my colleague's line segment method can draw lines smoothly, the problem of sawtooth is very serious, it does not work even if OpenGL is enabled. If you are familiar with OpenGL-es and have good algorithms, draw () may be the fastest and best way to draw images.

The last disadvantage is that draw () is always shown on the top of the Samsung mobile phone of Google, and then the lines will cover the game UI, no matter how Z-order is set, it is estimated that the OpenGL-es of Google's Samsung mobile phone has done some special processing.Potholes..."

 

(The teeth of all evil ...)

Example

Related examples provided by the Cocos2d-x (drawprimitivestest), the common draw line, circle, draw betiller line, etc., there are functions provided, specific or open their own tests to see the source code, here is not detailed.

 

(Drawprimitivestest)


Ccspritebatchnode
Understanding by yourself

The ccsprite generated by ccspritebatchnode shares a single texture. The benefit of this is that many identical
Ccsprite is very efficient, and the number of frames can be very high and stable. Because of this feature, it is a good choice to achieve the particle effect, but I have not seen the implementation of the cocos2d-x particle system, this
So as not to mislead everyone. The usage of ccspritebatchnode is still in tests. If you are interested, you can check it yourself.

Advantages

Because the implementation of draw () is too difficult, and the ccrendertexture has the problem of blurred screen, we try to use
Ccspritebatchnode is used for drawing. The drawing effect is very good, because ccsprite is used for custom brushes. The number of frames remains between 58 and 60, which is very stable.
Create ccsprite *, and then addchild to the layer. Dropping the frame will be very serious)

Disadvantages

Although ccspritebatchnode works well, there is also a fatal drawback:
The ccspritebatchnode * object cannot be addchild too many ccsprite * objects. My colleagues have done some experiments, about addchild to 16000 + objects.
After ccsprite, ccspritebatchnode cannot be addchild. That is to say, after drawing more than 16000 points, the paint brush will "no ink", causing the mouse
It's no effect to drag it.

After checking the source code, the ccspritebatchnode will re-allocate the memory when addchild is used. When the memory to be allocated is large, the memory allocation will fail, and ccspritebatchnode will invalidate this addchild.

Solution
The idea is to generate a count. When ccspritebatchnode reaches 10000, it uses the ccrendertexture to capture a graph and save it
First, clear the ccspritebatchnode and set the count to zero. This operation is delayed, so it is not implemented.


Ccribbon
Understanding by yourself

Ccribbon is a line segment set. Unlike the above method, ccribbon can only be a single color. That is to say, if you set it to red, all the lines you have drawn will be red. If it is blue, all the lines will be blue.

To generate a ccribbon * object, you must specify parameters such as the width of the brush, the image of the brush, the length of the line segment, and the color of the brush. (FADE has not understood this parameter yet ...).

Ccribbon provides addpointat (ccpoint location, float width) to add vertices to ccribbon, where the cocos2d-x calculates the size of this vertex so that the line reaches"Narrow head and tail, wide body. This is both an advantage and a disadvantage.Narrow head and tail, wide bodyIt looks like a paint brush, but it won't work if you want to make a uniform paint brush.

Advantages

The number of frames is stable, and the paint effect is good. Because ccribbon uses draw () for drawing, you don't have to worry about the screen rendering problem like ccrendertexture. Currently, ccribbon is an ideal drawing method.

Disadvantages

If you set a vertex at (100,100) and run to (400,400) and then click another vertex, ccribbon automatically connects the two vertices,
Ccribbon won't work if you want to make the line layout method of "click farming. The solution is to generate a ccribbon for each touchbegan operation.
This problem may occur, but too many ccribbon instances are generated. I don't know if other issues will occur, such as ccspritebatchnode...

Because ccribbon is a single color, it is impossible to draw a color chart. The solution is to generate multiple ccribbon * objects, as shown in the preceding figure. The color of each ccribbon * object is different.

The last thing we don't feel very human-friendly is that the paint brush can only "narrow head and tail, wide body". If you want to draw "Uniform head and tail" lines, you may not be able to do it with ccribbon.

Example
The Cocos2d-x does not directly give the ccribbon example, so this can not be seen on the tests use method, but a slight attempt on the line, the usage is quite simple.

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.