Page 1 of 1

question - beginner homework 6

Posted: January 18th, 2018, 3:40 am
by FiceT
hey there
i am kinda new and have a question related to homework 6 (not using x_mobile and y_mobile)
(https://www.youtube.com/watch?v=_mBq68B ... BHBFFRVWsx)

so i get what chili did there but i did it in a complete different way (i guess):

i made this two functions:

Code: Select all

void Game::ScreenCap(int x, int y)
{

	x = x + vx;
	y = y + vy;

	if (x + 5 >= gfx.ScreenWidth)
	{
		x = gfx.ScreenWidth - 6;
		vx = 0;
	}

	if (x - 5 < 0)
	{
		x = 5;
		vx = 0;
	}

	if (y + 5 >= gfx.ScreenHeight)
	{
		y = gfx.ScreenHeight - 6;
		vy = 0;
	}

	if (y - 5 < 0)
	{
		y = 5;
		vy = 0;
	}

}
(still the old version of checking the pixels with velocity reseting and shit)
and:

Code: Select all

int Game::GetCoord(int x)
{
	int y = x;
	return y;
}
and then used

Code: Select all

	const int x = GetCoord(x_mobile);
	const int y = GetCoord(y_mobile);
	ScreenCap(x, y);
in the update model function

tried it and it works fine

but now i am not sure if my version is fucked up or some shit because i use the x_mobile and y_mobile shit with GetCoord(); or am i fine doing so, and i just shouldnt use it in the function itself?

i hope you understand what i mean :D
and that i didnt violate the almighty chili forum rules

have a nice daaaay

Re: question - beginner homework 6

Posted: January 18th, 2018, 6:08 am
by albinopapa
It's a waste of time really to have a function that just makes a copy of a simple variable. The optimizer will probably see what you are doing and delete all that shit anyway, so you wasted your time creating the GetCoord() function. Just pass in the x_mobile and y_mobile vars directly to ScreenCap.

Code: Select all

ScreenCap(x_mobile, y_mobile);
See, much nicer looking and no wasted time or typing.

Re: question - beginner homework 6

Posted: January 18th, 2018, 9:34 am
by FiceT
that makes totaly sense wth is wrong with me :D
but is it fine to use the member variables in the function call?
€: ok chili used the member variable too for the function call, but now i'm wondering why he is using two functions
just so he has seperate functions for x and y? so he can use one if he only needs x or y?

Re: question - beginner homework 6

Posted: January 18th, 2018, 5:43 pm
by albinopapa
Functions should accomplish one task in general; not necessarily one operation, but one task. Clamping the value of X require different bounds checking than for Y ( width != height ). While, in this scenario it seems pointless, the bigger your program becomes, the more generic and generalized you want your functions to be, so you can save yourself some repetitive typing. Check out the example below.

You COULD just make a single more generic function and call it clamp. The parameters would be a MinValue, MaxValue, and TestValue. This way, you could write your ClampXY function as:

Code: Select all

int clamp( int minval, int maxval, int testval )
{
     if( testval < 0 )
     {
          testval = 0;
     }
     else if( testval > maxval )
     {
          testval = maxval;
     }

     return testval;
}
void ClampXY( int& X, int& Y, int Width, int Height )
{
     X = clamp( 0, Graphics::ScreenWidth - Width - 1, X );
     Y = clamp( 0, Graphics::ScreenHeight - Height - 1, Y );
}
Sorry if you haven't gotten to references yet, but in C++ you can't return two separate values. Using references like this is one way you can "return" multiple values. The point is, I didn't have to write the bounds checking code twice and now I have a generic clamp function I can test any value against any min/max values I want.

Re: question - beginner homework 6

Posted: January 19th, 2018, 1:06 am
by FiceT
alright, thank you for the detailed answer :D
i guess i will just use what i've learned and play around with the stuff for a week so
gotta be confident before i dig too deep and release some balrog (does this sentence even make sense? at least i'm learning english too while learning c++)

Re: question - beginner homework 6

Posted: January 19th, 2018, 1:09 am
by chili
Balrog?

Re: question - beginner homework 6

Posted: January 19th, 2018, 1:25 am
by FiceT
this...
i...
wow i...
i feel honored
destroyed by the almighty chili himself