Coder Social home page Coder Social logo

RenderTexture ContentSize Error about axmol HOT 3 OPEN

asnagni avatar asnagni commented on August 13, 2024
RenderTexture ContentSize Error

from axmol.

Comments (3)

rh101 avatar rh101 commented on August 13, 2024

Try renderTexture->getSprite()->getContentSize().

It is a little strange that RenderTexture content isn't set at creation, but if that were to be implemented, it may impact the positioning of the internal sprite, since it's a child of the RenderTexture. If the content size of the RenderTexture were to be set, then the position and anchor point of the internal sprite would need to be modified in order for it to be correct (and not break existing usage).

from axmol.

asnagni avatar asnagni commented on August 13, 2024

@rh101 Yes, I found that very strange and I step in the code to try to figure out what was the problem but defiantly it is not working. I could find a place where the value was set.

Doing this: renderTexture->getSprite()->getContentSize()
Will work but it is not intuitive and it doesn't fallow the rest the API pattern. I think this issue is not a high priority issue but it would be nice to have a clean interface. It's not intuitive to do this to get the size.

What do you think?

from axmol.

rh101 avatar rh101 commented on August 13, 2024

Will work but it is not intuitive and it doesn't fallow the rest the API pattern. I think this issue is not a high priority issue but it would be nice to have a clean interface. It's not intuitive to do this to get the size.

What do you think?

This is possible, but as I mentioned earlier, the internal sprite would need to be adjusted to ensure it is still drawn correctly.

So, in this method:

bool RenderTexture::initWithWidthAndHeight(int w,
                                           int h,
                                           backend::PixelFormat format,
                                           PixelFormat depthStencilFormat,
                                           bool sharedRenderTarget)
{
    AXASSERT(format == backend::PixelFormat::RGBA8 || format == PixelFormat::RGB8 || format == PixelFormat::RGBA4, "only RGB and RGBA formats are valid for a render texture");

    bool ret = false;
    do
    {
        _fullRect = _rtTextureRect = Rect(0, 0, w, h);
        w                          = (int)(w * AX_CONTENT_SCALE_FACTOR());
        h                          = (int)(h * AX_CONTENT_SCALE_FACTOR());
        _fullviewPort              = Rect(0, 0, w, h);

        setContentSize(Vec2(w, h)); // <<< set content size here

...
        setSprite(Sprite::createWithTexture(_texture2D));

        // !!!! Ensure sprite is displayed at correct coordinates anchoring bottom left.
        _sprite->setAnchorPoint(Vec2::ZERO); // <<< ADDED
        _sprite->setPosition(Vec2::ZERO);  // <<< ADDED
...
}

Also, we can anchor the sprite to middle if that is preferred:

_sprite->setAnchorPoint(Vec2::ANCHOR_MIDDLE); // <<< ADDED
_sprite->setPosition(_contentSize / 2);  // <<< ADDED

RenderTexture::getContentSize() will then return the correct size.

@halx99 Is it reasonable to set the RenderTexture content size and make the above changes to the internal sprite so it is drawn correctly? If so, which is the preferred anchor point of the sprite, 0,0 or middle of render texture?

from axmol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.