• C++ game development : Function to center image

    MarkGrillo Member

    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..
    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..

    theirs already functions for getting the center x and center 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;}
    

    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?

  • Amit Member

    I don’t understand your final question. Do you mean how would you get center of image (width and height)? Simple divide is by far the most usable option. You have to divide height and width by 2.

    int x_c, y_c; // x_center and y_center
    x_c = (spriteData.Width / 2);
    y_c = (spriteData.Height / 2);
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish