Cocos2d-x Study Notes

Source: Internet
Author: User

Part 1 engine Basics

Chapter 1Hello Cocos2d-x

(1) sprite: all visible elements

(2) A Game element is displayed only when it is placed in other game elements that have already been presented.

(3) ccnode: addchild method: combines game elements

Chapter 2Basic Concepts

(1) Game process control-scenario-layer-genie (FPS: Frame Rate per second)

Scenario (ccscene): a set of game elements whose content is relatively unchanged is called a scenario.

Process control: the game switching process between scenarios; game process controller (ccdirectory --> Singleton Mode)

Cclayer: game elements under a scenario (menu, touch, action, and background layer)

Ccsprite: A layer that is a visible image in a scenario.

(2) relationship graph = rendering tree -- attributes of child nodes relative to parent nodes.

(3) Action = ccactioninterval + ccactioninstant. animation can only be applied to Genie.

(4) use the macro using_ns_cc to reference the cocos2d namespace. The class name adopts the hump method.

(5) Cocos2d-x to create an object: all objects are created on the heap and then referenced by pointers.

A. New operator + init Initialization

B. Static factory method (create () method -- autorelease ())

The two are different: the ownership of the object created using the constructor already belongs to the caller, but the ownership of the object created using the factory method does not belong to the caller. Therefore, the caller is required to release the object created using the constructor, but the object created using the factory method is not required.

(6) to ensure that the initialization method can be reloaded by the quilt class, make sure that the initialization method is declared as a virtual function.

(7) selector: A function pointer mechanism similar to the class in C ++.

(8) The Cocos2d-x provides a series of macros to help us easily create attributes that are defined in the "platform/ccplatformmacros. H" in the Engine Directory. Each macro has three parameters:

    1. Vartype: attribute type. If the attribute type is an object, it must be written as a pointer;
    2. Varname: name of the private field of the attribute;
    3. Funname: name of the accessor of the attribute, that is, the part immediately after the get or set prefix.

Cocos2d-xMemory Management:

(9) Two technologies for implementing Intelligent Memory Management: one is reference count-root class ccobjec; the other is (automatic and unpredictable) garbage collection-autorelease () + automatic recycle pool.

(10) The Cocos2d-x releases a recycle pool after each frame ends and creates a new recycle pool before the next frame begins. Cost: Maintain a list of objects to be released.

(11) manually release and create a recycle pool-pop () or push () method of ccpoolmanager. The automatic recycle pool can be nested. Multiple automatic recycle pools are arranged in the stack structure. The autorelease object is only added to the top-end pool.

(12) avoid misuse of autorelease () because 1 occupies memory and CPU; 2. It is difficult to locate the error of the object release operation. autorelease is executed only at the end of each frame. Conclusion: It is used only when the factory method has to be used.

(13) The Cocos2d-x engine provides us with objective-C-style containers such as ccarray and ccdictionary-The Cocos2d-x container ensures that the container's access to objects is always in line with the memory management principles of reference counting.

(14) memory management-related macros in the Cocos2d-x-header file "ccplatformmacro. H"

Cocos2d-xMemory Management Principles:

    1. The program segment must be executed in pairs by retain () and release () or autorelease () to start and end object references;
    2. Before the factory method is returned, the reference to this object should be ended through autorelease;
    3. When passing values to an object, the special circumstances of the same old and new objects should be taken into account;
    4. Use release () instead of autorelease () whenever possible to release object references to ensure optimal performance;
    5. The subclass object that saves ccobject is, should strictly use the container provided by the Cocos2d-x, avoid using STL container, the object must be saved as a pointer.

(15) appdelegate is the entrance to the entire program. It handles four important events in the lifecycle: after the program completes initialization, the program enters the background, the program returns to the foreground, and the program ends and exits.

Chapter 3 basic game elements

(1) ccdirector (Singleton design mode): The game completes rendering settings and process control under the ccdirector management. Cocos2d-x advanced development tutorial (p33)

(2) ccscene: container + process control at the layer. Cctransitionscene: special effect for scenario switching.

(3) cclayer: the sprite container + accepts user input. addchild adds layers to the scenario.

(4) ccsprite: The smallest visible unit that loads a plane texture.

(5) texture: The image displayed by the Genie. More in-depth: The texture is the pattern drawn by OpenGL on the surface of the object in the 3D drawing library. In the texture coordinate system, the original point (0, 0) is located in the upper left corner, the origin point to the right is the positive direction of the X axis, and the origin point downward is the positive direction of the Y axis.

1. Create a genie: Image File, texture, or Sprite frame.

2. texture-related attributes. "P39"

Ccnode

(6) The rendering tree contains many ccnodes. All game elements are inherited from ccnode. The basic functions provided by ccnode are as follows: (for details about ccnode attributes, see "p42)

1. contains other ccnode objects;

2. accept various events and callback functions, such as timer events;

3. Run the action.

(7) Cocos2d-x Coordinate System:

Drawing coordinate system (starting from the lower left corner): the coordinate system used by OpenGL is the same;

Texture coordinate system (starting from the upper left corner): This coordinate system is used only when some matrices are captured from the texture.

(8) anchorpoint: the two parameters X and y of the anchorpoint are usually real numbers between 0 and 1, indicating the position of the anchorpoint relative to the length and width of the current node. "P42"

Position: position refers to the coordinate value of the anchor (coordinate point between the length and width of the current node) in the parent node of the current node.

(9) Timer: a tool that continuously triggers game events at a certain interval. The update method and schedule method are two implementation timing mechanisms. "P44

Update Timer: triggered once before each frame is drawn. Enable (scheduleupdate method) + reload update

Schedule timer: Call a function at a certain interval (the interval must be greater than two frames. (Schedule method enabled)

(10) timer mechanism is the basis of Cocos2d-x scheduling mechanism, Cocos2d-x scheduling is purely serial mechanism, all functions are running in the same thread.

(11) Cocos2d-x built-in common Layer:

Cclayercolor: a pure solid color block

Cclayergradient: A color block, but you can set gradient effects for the two colors

Ccmenu: commonly used game menus (Common statuses, press statuses, and response events)

Cocos2d-xScheduling Principle

(12) event-driven game frameworks, such as timer events, touch events, and sensor events.

(13) main game loop: processing user input, processing scheduled events, drawing

(14) timer Scheduler: an object that manages the timer of all nodes. It is responsible for recording the timer and issuing the scheduled event at the appropriate time. The ccschedule class (timer scheduler class) manages the timer and processes the update timer (updates container) and the general timer (selectors hash) respectively)

(15) cctimer also provides the update method. The difference is that the accumulated received DT time is triggered periodically and repeatedly (or once. "P57"

(16) event-driven general timer call sequence: system time events drive the main cycle of the game. The main cycle of the game calls the update method of ccscheduler, ccscheduler calls the update method of the cctimer object corresponding to the common timer, and the update method of the cctimer class calls the callback function corresponding to the timer. For the update timer, the call sequence is simpler.

(17) Each timer does not interfere with each other and is executed in serial mode.

Chapter 4 Action

(1) ccaction is a base class, And a ccaction can only be used once. Most implementation classes of ccaction are derived from ccfinitetimeaction, which defines the actions that can be completed within a limited period of time. The two main classes derived from ccfinitetimeaction are ccactioninstant and ccactioninterval ).

Transient actions: ccplace, ccflipx, ccflipy, ccshow, cchide, and cccallfunc

Sustained action:

1. Position change actions: ccmoveto, ccjumpto, and ccbezierto;

2. attribute change actions: ccscaleto, ccrotateto, ccfadein/out, ccfadeto, and cctintto;

3. Visual Effects: ccblink (blinking );

4. control actions: ccdelaytime, ccrepeat, and ccrepeatforever;

Compound action: ccrepeat, ccspawn, ccsequence, and ccdelaytime );

Variable Speed: ccspeed, ccactionease

(2) When we create a combination of more actions, the engine splits the actions into two parts. The latter part only contains the last action, the engine sets m_ptwo as the next action and m_pone as the combination of all other actions.

(3) create a custom action: ccaction contains two important methods: Step and update. The step method is triggered when each frame action is updated. This method accepts a parameter DT indicating the call interval. The accumulation of DT is the total time of the action. The engine uses accumulation time to calculate the progress (a real number ranging from 0 to 1) of an action and calls the update method to update the action. The update method is the core of ccaction. It is called by the step method and receives a parameter indicating the action progress. Each action must use the progress value to change the attributes of the target node or execute other commands. You only need to start with these two methods for custom actions. Generally, you only need to modify the update method to implement simple actions.

(4) Cocos2d-x Action Principle: P80

(5) Working Principle of ccactionmanager: both action scheduling and timer scheduling are subject to the control of ccscheduler. Specifically, we can conveniently pause or resume the operation of the timer and action at the same time, they do not have to be synchronized.

Chapter 5 animation and special scene Effects

(1) Animation: consists of frames (Frame Animation), and uses the timer to constantly change the display content of the genie.

(2) frame class (ccspriteframe): texture + area -- texture is the texture to be displayed, and area is the part to be displayed.

(3) animation frame class (ccanimationframe): two attributes: the reference to a frame + The Frame delay.

(4) animation class (ccanimation): The description of an animation, which contains the animation frames required to display the animation.

(5) animation action (ccanimate): We use ccanimation to describe an animation, while the sprite shows the animation action as a ccanimate object. The animation action ccanimate is an animation action displayed by the genie. It is created by an animation object and executed by the genie. The relationship between an animation and an animation is the same as that between a CD player and a CD player. The former records the animation content, and the latter plays an animation.

(6) scenario switching special effects (cctransitionscene): the implementation of special scene effects is similar to that of compound actions. You can use the original scenario as a parameter to create a special effect scenario. Source code: layers_scenes_transitions_nodes/cctransition. H (CPP)

Chapter 6 music and sound effects

(1) sound engine library cocosdenshion of Cocos2d-x:

1. cdsoundengine (encapsulated openal, only supported by OS X)

2. cdaudiomanager (openal encapsulated, only supported by OS X platform)

3. simpleaudioengine (Singleton): pre-load, play and stop, pause and Resume playback "p89

Chapter 7 user input

(1) ccstandardtouchdelegate interface: Four touch Event Callback functions (START, move, end, and cancel); touchenable attribute: enable or disable the receiving touch input.

(2) Respond to touch events: the touch layer -- notifies other parts in the game to respond to touch events.

(3) Two Kinds of Cocos2d-x touch event: "p94. We have seen the cclayer method for processing touch events. When the touchenable attribute of cclayer is enabled, the four callback functions in the layer are triggered when an event is received. standard touch events feature that all touch events are received equally at any layer.

In addition, the Cocos2d-x provides another touch event mechanism called 2. Targeted touch with a target. In the touch mechanism with the target, the receiver is not equal. The receiver who handles the event earlier has the right to stop distributing the event so that it will not be transmitted to other recipients. In other words, touch events with targets are not necessarily broadcast to all recipients. In general, the game's menu buttons, joystick buttons and other components often use the target touch event to ensure that the touch event does not have adverse effects on other layers.

(4) Steps for standard touch events:

1. This object is required to implement the ccstandardtouchdelegate interface.

2. Use addstandarddelegate to register yourself with the touch event distributor.

3. Reload the Event Callback Function to process touch events (began/moved/ended/cancelled );

? 4. When you no longer need to receive touch events, use the removedelegate method to cancel the receipt of touch events.

(5) Steps for touch events with targets:

1. Implement the cctargetedtouchdelegate interface.

? 2. Use the addtargeteddelegate method to register with the touch event distributor.

? 3. Reload the Event Callback Function. Note: We must return true for the event to be accepted in the touch start event to capture the event.

    1. When you no longer need to accept touch events, use the removedelegate method to cancel the receipt of touch events. Compared with the standard touch event, the main difference is that the start touch event needs to return a value indicating whether to capture the event.

(6) How the cctouchdispatcher class works: P97

(7) use touch events:

Touch layer (Receiving System touch events) -- proxy (Protocol Class) -- genie layer (actually processing touch events, inheriting and processing touch events in the proxy class)

 

Part 2 advanced engine

Chapter 8 particle effect

(1) Particle System in Cocos2d-x (ccparticlesystem): the code for Particle effect is placed in the "ccparticlesystem. cpp" file in the "particle _nodes" directory under the Cocos2d-x Engine Directory:

1. generate particles;

2. Update the particle state;

3. Reclaim invalid particles;

Creating a new particle system is usually cumbersome, and in most cases we prefer to save the parameters of the Particle System in the file, while Cocos2d-x is usingPlist FileTo save these parameters.

 

Chapter 9 large-scale map

(1) Large maps-tile maps-creating tiles with many smaller textures

(2) tilemap map (tmx file) supports three different views: Orthogonal view (horizontal and vertical arrangement of tiles), hexagonal view (hexagonal view, hexagonal tile closely connected) and the same axis view (isometric view, 45-degree strabismus arrangement)

(3) Tiled map editor: map editor, unlike the Cocos2d-x, tilemap coordinate system origin is located in the upper left corner; tilemap each tile has a unique number GID.

(4) Cocos2d-x provides us with cctmxtilemap and cctmxlayer two classes to deal with tile map.

(5) Tiled map editor supports the concept of layers, each layer in the map exactly corresponds to the cclayer in the Cocos2d-x, each layer in the cocos2d world Z sequence values are different. We set the Z coordinate of the water grain to 10, and it is determined that the Z coordinate of the fish should be below 10, while the Z coordinate of the gun and the bullet should be above 10. (The larger the value of Z, the higher the upper layer)

Chapter 10 principle and optimization of Cocos2d-x drawing

OpenGLBasics:

(1) OpenGL is a C-language-based 3D graphics API. OpenGL is a state-based drawing model.State Machine, Advantage 1. OpenGL saves all parameters as States. 2. We can artificially divide the drawing device into two parts: "server side", responsible for drawing and rendering, and "client", responsible for sending drawing instructions to the server side. The CPU in the device is responsible for running the game logic, and sends drawing commands to the GPU (hardware graphics card or software simulated graphics card.

(3) The glenum type is used to indicate the number of OpenGL States. The list of all States is defined in the "gl2.h" header file.

(4) OpenGL is a 3D graphic interface that uses the right-hand 3D Coordinate System in a program. Specifically, during initialization, the screen orientation to the right is X, the screen orientation is Y, and the screen orientation is Z. OpenGL is responsible for converting objects in 3D space into 2D images through projection and grating, and then rendering them to the screen.

(5) When OpenGL is not set, the initial coordinate system is called the world coordinate system. OpenGL also maintains a drawing coordinate system.

(6) OpenGL introduced the programmable shadow (shader) from version 2.0 ). The programmable coloring tool mainly includes Vertex coloring devices and fragment coloring devices. The former is responsible for geometric transformation and illumination Calculation of vertices, and the latter is responsible for processing the pixels and textures obtained by raster.

 

(7) OpenGL's processing of vertices can be summarized into three steps: accepting vertex data, performing projection, and obtaining transformed vertex data.

(8) transformation is represented by a matrix. Common transformations include translation transformation, Rotation Transformation, and scaling transformation. They correspond to the translation matrix, rotation matrix, and scaling matrix. OpenGL provides us with a series of functions for creating transformation matrices. Therefore, in actual development, we do not need to manually construct transformation matrices. (OpenGL ES 2.0)

OpenGLDrawing principle:

(9) The code for drawing the genie is located in "sprite_nodes \ ccsprite. cpp" of the engine source code. Open the ccsprite code file. The draw method is used to draw the sprite. Different policies can be taken to reduce the number of OpenGL calls for different situations, thus greatly improving game performance.

(10) Rendering tree: no matter how complicated a game scenario is, it is composed of different layers and positions. Therefore, as long as the genie can be set to the front and back layers, the game scenario is drawn from different locations. The rendering tree is a tree structure composed of various game elements according to hierarchies. It shows the drawing layers of the Cocos2d-x game. Therefore, the rendering order of the game is determined by the rendering tree. Game elements are represented as nodes in the rendering tree, and the ownership relationship of game elements is converted to the ownership relationship between nodes. The visit method of ccnode is used to draw a rendering tree. To draw a node in the tree, you need to draw your own child node until no child node can be drawn. When a parent node is drawn, the child node is drawn. The method of drawing the child node is also related to the attributes of the parent node.

(11) The visit method calls the transform method to perform a series of transformations, so that you and the subnodes can be drawn to the correct position. To understand the transform method, we first explain the meaning of the draw method. The draw method is used to draw the image. However, from the learning in the previous section, we can see that the draw method does not care about the position of texture painting. In fact, it only draws the texture to the origin in the current coordinate system. In order to draw the texture to the correct position, we need to adjust the current coordinate system before drawing. This operation is completed by the transform method, after the transformed coordinate system, the texture can be drawn to the correct position.

(12) transform method: In the rendering tree, one of the most critical steps is to transform the coordinate system. If there is no coordinate system transformation, the texture cannot be drawn at the correct position. At the same time, the transformation of the coordinate system also plays an important role in other occasions (such as collision detection. Vividly speaking, the task of the transform method is to calculate how to convert the drawing coordinate system to the matrix of the new Coordinate System Based on the attributes of the current node.

(13) if we need to determine whether a point is within the same rectangle in another coordinate system, we can convert this point to the world coordinate system and then from the world coordinate system to the target coordinate system, after that, you only need to determine through the contentsize attribute.

(14) Cocos2d-x drawing process: in the main loop of the game calls the visit method of the scene, in which the transform method is used for coordinate system transformation, and then recursively call the visit method of the subnode, draws the entire rendering tree. The Cocos2d-x uses an Open Source geometric computing library kazmath.

Drawing Optimization

(1) The Frame Rate of the game is reduced to a certain value, and the obvious delay will lead to a very poor gaming experience.

Drawing bottleneck:

    1. The texture is too small.
    2. Too many texture switching times.
    3. The texture is too large.

Optimization:

    1. Fragment graph compression (the image size is exactly in line with OpenGL's texture specifications, broken graph merge tool: texturepacker) and sprite frame.
    2. Batch rendering: if you do not need to switch and bind textures, several OpenGL rendering requests can be submitted in batches (ccspritebatchnode). That is to say, the rendering under the same texture can be completed at one time. The usage of ccspritebatchnode is also very simple. It is a special node. We only need to add the sprite to be drawn as its subnode, and then add ccspritebatchnode to the layer or scenario.
    3. Deep color optimization: suitable for low-end devices with insufficient CPU and memory.

Summary:

In Cocos2d-x 2.0, many revolutionary new features were introduced, especially a lot of improvements in the drawing mechanism, the use of the new OpenGL ES 2.0 graphics, support for programmable pipeline shader and so on. The bottom-layer graph matrix optimization is transparent and explicit to developers. We can use more special effects to achieve richer graph effects .?

OpenGL ES is a subset of OpenGL 3D graphics APIs. It is designed for mobile devices. Its version 1.0 is for fixed-line hardware, and its version 2.0 has been extended to support programmable pipeline hardware. Cocos2d-x 2.0 is upgrading the underlying plot from OpenGL ES 1.0 to OpenGL ES 2.0.

Chapter 2 OpenGL plotting skills

(1) The engine reserved a custom drawing interface for us in ccnode: Draw. You can reload this method. It is best to only draw custom content in this method.

(2) The draw function is a function called every frame. It is clearly pointed out in the comment that we should write the custom rendering effect in this function normally, do not plot anything other than this function. This is because the engine prepares the drawing environment and sets the necessary status between the context of this function call. Adding a drawing code elsewhere may cause unexpected errors.

(3) shortcut drawing interfaces: These interfaces are provided by "ccdrawingprimitives. H" and the corresponding CPP file.

(4) In fact, The plotting function encapsulated in "ccdrawingprimitives. H", such as ccdrawline, triggers OpenGL rendering every time it is called, but only a small amount of painting is completed. Calling a large number of drawing functions is not a small burden on the GPU, so this type of drawing function is only suitable for a small amount of use under specific circumstances.

(5) The mask effect, also known as the scissors effect, allows all rendering results to be displayed only in a specified area of the screen: After the mask effect is enabled, all rendering submissions are normally rendered, however, only the specified area on the screen will be drawn.

Data Exchange

(1) Two Types of underlying data communication must be introduced: ccimage and cctexture2d. This is the class provided by the engine to describe texture images and the data structure mainly involved in data exchange with the video card.

(2) ccimage is defined in "ccimage. H" to indicate a texture image loaded into memory. In its internal implementation, the texture is stored in the memory with the color value of each pixel. Ccimage is usually used as a tool for data exchange between files and video cards. Therefore, ccimage provides two functions: file loading and storage, and memory buffer reading and writing. We can use ccimage to easily read and write image files. Ccimage also provides an interface for reading and writing memory. Note: currently, only images in the rgba8888 format can be loaded from the memory.

(3) cctexture2d: The texture size contained in this class must be a power of 2, so the texture size is not necessarily equal to the image size. In addition, unlike ccimage, this is a texture that exists in the video memory and does not necessarily exist in the memory. We use an underlying function glreadpixels of OpenGL for implementation. OpenGL draws pixels from top to bottom.

(4) Texture rendering class ccrendertexture: it is used to transfer the drawing device from the screen to a texture, so that a continuous drawing is saved to the texture. We can use this rendering texture class together with the drawing that is actively called to achieve the effect. The reason for this is "a short paragraph" is that the execution cycle of graphic rendering is very short and programs that are too bloated are not allowed. Therefore, they are usually relatively short.

Programmable shader:

(1) vertex shader ). Call each vertex once to complete vertex Transformation (Projection Transformation and view model transformation), normal transformation and normalization, texture coordinate generation, texture coordinate transformation, illumination, color material application, and other operations, and finally determine the rendering area. In the world of Cocos2d-x, Genie and layers are all rectangles, and one rendering of them calls four vertex pasters.

(2) fragment shader (also known as fragment shader ). This shader is called when each pixel is rendered. That is to say, if we display a 320x480 image on the screen, the pixel shader will be called 153 times. Fortunately, there are usually more than one graphic processing unit in the video card. The rendering process is parallel, and the rendering efficiency is much higher than that with the Serial CPU.

(3) The two paintors cannot be used independently and must appear in pairs. This is because the vertex paintors first determine the attributes of each vertex displayed on the screen, then the areas composed of these vertices are divided into a series of pixels. Each of these pixels calls a segment shader, and the processed pixels are displayed on the screen. The two work collaboratively.

(4) The engine provides the ccglprogram class to handle coloring machine operations. Import custom shader effects in a Cocos2d-x game.

(5) A style of OpenGL interface: Objects (textures, coloring programs, or other non-standard types) are represented by integer identifiers.

(6) variable transfer: "p158. loading only is certainly not enough. We also need to pass the necessary input data for the coloring tool. There are two types of input data in the shader, which are marked as attribute and uniform respectively. Attribute variables are the variables that the application directly transmits to the vertex shader and cannot be accessed in the segment shader. The uniform variable is global and can be accessed in both the vertex coloring tool and segment coloring tool. These variables are read-only and cannot be changed during rendering.

(7)Void gluniform1i (glint location, glint X );

The above function sets the value of the target uniform variable to an integer value. We can see that the end of OpenGL functions is always followed by suffixes like "1i" and "3f", which is another characteristic of OpenGL functions. A value-passing function may accept a variety of different parameters. Numbers in the suffix correspond to scalar vectors and 2, 3, and 4-dimensional vectors from 1 to 4. Another character in the suffix represents the parameter type. Common types include "F" (float), "I" (integer), and "B" (byte. This method can avoid type conversion and improve program efficiency.

(8) the direct use of attribute variables is relatively complicated. We need to deal with details such as variable binding and number management.

(9) Two call timing problems:

The first is the time to bind the attribute variable. After ccglprogram is loaded, you need to call the link function to connect the program to the video card. Before binding, ensure that the binding can be correctly transmitted to the video card.

Second, set the time to enable array binding. Because this is a State shared between different rendering programs, it will be turned off repeatedly by different plotting, so it must be set automatically each time it is drawn.

(10) In the Cocos2d-x, due to the use of programmable pipelines, each ccnode will be accompanied by a coloring program. After loading the custom shader, we should call setshaderprogram to set it to ccnode. Finally, we stress again that the attribute variable should be bound before the link to the coloring er program.

(11) attribute vec4 a_color; // watermark color

Attribute vec4 a_position; // vertex Coordinate

Uniform mat4 u_mvpmatrix; // Coordinate Transformation Matrix

? Uniform float time; // time

? Uniform vec2 resolution; // resolution

? Uniform sampler2d tex0; // background texture

(12) after initializing the colorant program, you should prepare the values of each variable to be passed. In the two attribute variables, the color is directly set by the setting function, and the vertex position is directly transmitted in the draw function. Therefore, we will first introduce how to prepare three uniform variables: time, resolution, and background texture.

1. Time only needs to be refreshed within the update function

2. Resolution refers to the resolution of the current coverage area.

3. The background texture can be considered as the effect area and used as the texture.

Note: The target texture here is not a specific texture, but a special texture slot activated, the captured drawing data is stored in the texture bound to the slot. In the Cocos2d-x, this texture slot is generally gl_texture_2d, that is, the 2D Texture slot. 163.

(13) ccglbindtexture2d interface of the Cocos2d-x: This is the unified binding interface of the Cocos2d-x, its internal Cache Optimization, can improve the efficiency of texture binding.

(14) Misuse of OpenGL makes the code messy and difficult to maintain. Therefore, OpenGL should not be considered first unless necessary. The engine encapsulates a special sequence class ccactiongrid3d, which can simulate some simple 3D effects and replace OpenGL in some cases.

(15) Efficiency problem: the encapsulation effect provided by the engine will definitely suffer a loss in efficiency. The programmable Pipeline provided by OpenGL ES 2.0 must be less efficient than the hardware rendering of OpenGL ES 1.0, even if it achieves the same effect.

Summary:

1. Draw method: it is a method provided by each ccnode. It is used to draw its own content when drawing a node. Generally, all custom drawings should be completed in this method. When drawing custom content, you need to reload the draw method and add the drawing code to the draw method.

2. glscissor: used for cropping. Before using the glscissor function, you need to call glable (gl_scissor_test) to enable cropping. After setting the cropping area, only the part in the area is actually drawn, and the rest is transparent.

3. Texture Image: ccimage and cctexture represent an image and a texture that can be loaded to the display. Cctexture can be created by ccimage, while ccimage can load or save files in PNG, Tiff, JPG, and other formats.

4. Shadow: A shadow is a program used to replace the rendering pipeline. It can be used to perform operations on top points and pixels. In this chapter, we use a colorant to achieve the water effect.

5. ccgrid3d: Cocos2d-x provides a set of grid transform function, through ccactiongrid3d transform class can realize some simple 3D transform screen, such as water grain effect.

Chapter 2 Physical Engine

(1) The physical engine is born for such a scenario. The physical engine fully considers the physical nature of Game objects and computes the interaction between objects as accurately as possible. Generally, the physical engine binds the objects in the game to a physical model. The model contains attributes such as the shape, density, material, and speed of an object. The engine changes the model attributes in real time based on the interaction between models, the game engine is used to render objects.

(2) box2d is a set of physical engines based on rigid body simulation. Its core concepts include the world, objects, shapes, constraints, and joints. These concepts are implemented as the components of box2d.

(3) In the game, what we want to do is usually to assign the Genie the physical nature. We will create a box2d physical model for the Genie. However, after the physical engine operates, to display the motion of the physical model on the screen, we must manually synchronize the data of the physical model to the genie. Each genie connected to box2d has an associated physical model "object" in the physical world, that is, b2body.

(4) the header file related to box2d is "box2d/box2d. H ". Like "cocos2d. H", this header file contains all the header files required by box2d. Box2d does not have its own namespace, so it does not need relevant namespace references.

(5) The optimal object size in box2d: According to the box2d reference manual, box2d is 0.1 ~ The processing efficiency of 10 objects per unit length is relatively high.

 

Cocos2d-x Study Notes

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.