Jump to content
  • Advertisement
Sign in to follow this  
dynamiteandy

C++ Function to center image?

This topic is 1877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey,
So i'm working with a game engine that was wrote by charles kelly for his book programming 2D games.

His engine is rather good, however positioning images is rather bad as it does it as x = left corner and y = top of image
I don't want to do the maths to center an image in the position x,y i'd much rather write a function for it..

there's already functions for getting the center x and center y.

 

however how would i go about writing a function so that it takes int x, int y and moves the image so that its center is x,y?

// Return center X.
    virtual float getCenterX()      {return spriteData.x + spriteData.width/2*getScale();}

    // Return center Y.
    virtual float getCenterY()      {return spriteData.y + spriteData.height/2*getScale();}

// getScale() incase needed
// Return scale factor.
    virtual float getScale()    {return spriteData.scale;}

14o6l9e.png

The image to above shows what im after..

red = current setting.

blue = what im after

Edited by Dynamite Andy

Share this post


Link to post
Share on other sites
Advertisement

I think this should work.

virtual void setCenteredPosition(float x, float y) {
    spriteData.x = x - spriteData.width/2*getScale();
    spriteData.y = y - sprideData.height/2*getScale();
}
Edited by SimonForsman

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!