1

I'm having trouble creating a scrolling background. I'm literally trying to translate the C# I did two years ago, in to C++ and as a 'newb' (as it were), I'm having trouble.

Below are the variables and objects I am using.

//ScrollingBackground Inits from the Contructor/Main Method
_screenHeight = Graphics::GetViewportHeight();
_screenWidth = Graphics::GetViewportWidth();

//ScrollingBackground Content from the Load Content Method
_backgroundPosition = new Vector2(_screenWidth / 2, _screenHeight / 2);
_origin = new Vector2(_backgroundTexture->GetHeight() / 2, 0);
_textureSize = new Vector2(0, _backgroundTexture->GetHeight());
_backgroundTexture->Load("background.dds", false);

This is the Method that I'm trying to make where the scrolling occurs.

void Player::Scrolling(float deltaX)
{
    //This is where the scrolling happens
    _backgroundPosition->X += deltaX;
    _backgroundPosition->X = _backgroundPosition->X % _textureSize->Y;
}

Still relatively new to this so please forgive me if I'm vague or sound like I don't have a clue what I'm talking about.

Many thanks,

Ryan.

  • I keep making a habit of not explaining what is going wrong LOL , my apologies... _backgroundPosition->X = _backgroundPosition->X % _textureSize->Y; I keep getting '% : illegal, left operand has the type 'float' and same with the right operand – Ryan 'Maverick' Buxton Nov 18 '14 at 18:25

1 Answers1

0

You cannot use % operator on floats. The following fixes your problem, but will not give a true remainder. If precision is not an issue, you can use the following code and not see a huge issue with the scrolling background.

void Player::Scrolling(float deltaX)
{
    //This is where the scrolling happens
    _backgroundPosition->X += deltaX;
    _backgroundPosition->X = static_cast<int>(_backgroundPosition->X) % static_cast<int>(_textureSize->Y);
}
user2970916
  • 1,146
  • 3
  • 15
  • 37
  • Thank you for input. This returned 4 errors, 'syntax error: identifier _backgroundPosition' 'Syntax Error: ;', intellisense expected a ')'and '>' at columns 1, 1, 45 and 86 respectively. – Ryan 'Maverick' Buxton Nov 18 '14 at 18:38
  • It does not make sense for it to work on decimal types. The modulus returns the remainder of a division. What is the remainder when dividing two decimals. The only way to get a true remainder for the decimal would be to find a way to get _backgroundPosition->X and _textureSize->Y to an integer. For example if _backgroundPosition->X is 3.5 and _textureSize->Y is 4.2, you would need to multiply both by 10 to get it to an integer. – user2970916 Nov 18 '14 at 18:44
  • http://stackoverflow.com/questions/6102948/why-does-modulus-division-only-work-with-integers --> Here they explain more about why % does not support decimal types. – user2970916 Nov 18 '14 at 18:48
  • That makes sense to be honest :-)) – Ryan 'Maverick' Buxton Nov 18 '14 at 18:49
  • That was an alright little link you know. Thank you kindly :-)) – Ryan 'Maverick' Buxton Nov 18 '14 at 19:12