Cocos2d-x 3.0 texture

Source: Internet
Author: User

1. Texture Control.

Sprite *pSprite = Sprite::create("background.png");TexParams params = {GL_NEAREST,GL_NEAREST,GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE};pSprite->getTexture()->setTexParameters(params);

The most important function above is settexparameters (), which controls how the texture of a texture image maps to the pixels on the Screen Based on cctexparameters. Based on what to achieve the effect of scaling the texture and repeating the texture (repeating the texture.

  /**     Extension to set the Min / Mag filter     */    typedef struct _TexParams {        GLuint    minFilter;        GLuint    magFilter;        GLuint    wrapS;        GLuint    wrapT;    }TexParams;

Let's take a look at the structure of texparams. The first two member variables of texparams are called Basic filters. Used to specify the rules used to control the scaling effect of the texture.

Two basic texture filtering rules:

(1) gl_linear: "When the texture is displayed, the neighboring pixels are used for interpolation to fill points when the size of the texture is greater than or less than the size of the original texture"

Features: After the image is stretched or reduced, it looks distorted, but the effect is better than gl_nearest. It does not seem to have any trace after manual operation.

(2) gl_nearest: (nearest filter) the simplest and quickest filter method.

Features: When the texture is stretched to a very large scale, a large volume of mottled pixels will appear.

The two member variables after texparameters are called the texture surround mode. They are used to control how to process pixels outside the texture boundary. The coordinate range of the OpenGL texture is (0.0f, 1.0f). This means how to process the texture when the texture coordinate is greater than 1.0f or the texture coordinate is less than 0.0f.

Two common texture surround modes:

(1) gl_repeat: Repeat the texture when the texture exceeds 1.0f. If you want to display pixel points beyond the texture boundary, tile the texture pixels next to it.

    cocos2d::Texture2D::TexParams tp ;    tp.minFilter = GL_LINEAR;    tp.magFilter = GL_LINEAR;    tp.wrapS = GL_REPEAT;    tp.wrapT = GL_REPEAT;        matteLayer=Sprite::create("middle_background.png");    matteLayer->retain();    matteLayer->setPosition(s.width/2,s.height/2);    matteLayer->setScale(4.0f, 4.0f);    matteLayer->getTexture()->setTexParameters(tp);            this->addChild(matteLayer);        this->schedule(schedule_selector(BackgroundLayer::changColor), 1.0f);        this->schedule(schedule_selector(BackgroundLayer::scrollBackground), 0.01f);

In scrollbackground:

void BackgroundLayer::update(float dt){      int nLen = 100;    static int nOffset = 0;    nOffset += nLen*dt;    matteLayer->setTextureRect(Rect(0,-nOffset, matteLayer->getTexture()->getPixelsWide(),matteLayer->getTexture()->getPixelsHigh()));}

The above Code adds noffset to draw areas outside the texture boundary of the genie. The surround mode is previously set to gl_repeat. The texture pixels next to the sprite are tiled when the areas outside the sprite are drawn. Creates a repeated texture.

In the program, the texture of this genie will continuously scroll from right to left. Note: the location of the genie will not change, so gl_repeat is used to achieve the effect of repeated textures.

The width and height of repeatsprite.png must be 2 to the Npower. Otherwise, when you use gl_repeat as the surround mode, a warning will be prompted: gl_repeat mode must be pot. the image size in gl_repeat mode must be an exponential power of 2 (POT ). after warning, the system crashes.

(2) gl_clamp: If the texture coordinate is greater than 1.0, it is set to 1.0. If the coordinate is less than 0.0, it is set to 0.0;

If the X coordinate is greater than the texture width, a pixel on the rightmost side of the texture is used to draw the subsequent area.

2. Texture buffer. (texturecache)

The sprite: Create () function contains the following code: auto texurecache = Director: getinstance ()-> gettexturecache ();

The genie uses this code to generate the texture object of the genie Based on the provided images. Therefore, it is not difficult to see that when an image is used to create a Sprite object, the image data is loaded into the memory. This memory is called the Texture buffer (texturecache ), as long as you do not manually delete image data from the Texture buffer, the image data will always occupy the memory and will not be released. Therefore, this raises some issues concerning the Texture buffer. the buffer (memory) for storing the loaded image data in cocos2d is divided into two types: the area where the large image is spliced by multiple small images and the area where the single image is stored. The two classes correspond to spriteframecache and texturecache respectively.

Why do these two classes exist? We can see from the name that they are buffers. The only reason for this is that the next time we need the same image data, we can directly retrieve it from the buffer without loading the image file into the memory. This saves memory and improves rendering speed.

 

Assume that an image example.png is loaded into the buffer. If this image is first loaded into the memory, it is first loaded into the buffer and then read from the buffer, creates a texture object.

If the data of this image is not deleted in the memory, then when the programmer wants to use image example.png to create a texture object, cocos2d first checks the Texture buffer to check whether the image data exists. If yes, it will be read directly from the buffer. It does not need to be loaded from the hard disk to the memory again. How convenient! Isn't it? This saves you memory and improves efficiency.

Release:

If the game has many scenarios, you can release all the memory in the previous scenario when switching the scenario to prevent the total memory from being too high.

Director: getinstance ()-> gettexturecache ()-> removealltextures (); release all loaded images so far

Director: getinstance ()-> gettexturecache ()-> removeunusedtextures (); release the image with the reference count of 1.

Director: getinstance ()-> gettexturecache ()-> removetexture (); release an image separately

Spriteframecache is similar to the texturecache release method.

It is worth noting that the release time is usually used to release resources during scenario switching. If you switch from scenario A to scenario B, the call sequence is B: Init () ---->:: exit () ----> B: onenter () if the switching effect is used, such as ctransitionjumpzoom: transitionwithduration, the call sequence of the function is changed to B: Init () ----> B: onenter () ----> A: exit () and the second method overwrites the resources of the two scenarios, it is very likely that the memory will crash due to a tight diet.

Sometimes, when all resources are forcibly released, an exception occurs when an animation being executed is out of reference. You can call ctor: getinstance ()-> getactionmanager ()-> removeallactions ();.

Related Article

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.