question - beginner homework 6

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
FiceT
Posts: 4
Joined: January 18th, 2018, 3:26 am

question - beginner homework 6

Post by FiceT » January 18th, 2018, 3:40 am

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

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: question - beginner homework 6

Post by albinopapa » January 18th, 2018, 6:08 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

FiceT
Posts: 4
Joined: January 18th, 2018, 3:26 am

Re: question - beginner homework 6

Post by FiceT » January 18th, 2018, 9:34 am

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?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: question - beginner homework 6

Post by albinopapa » January 18th, 2018, 5:43 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

FiceT
Posts: 4
Joined: January 18th, 2018, 3:26 am

Re: question - beginner homework 6

Post by FiceT » January 19th, 2018, 1:06 am

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++)

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: question - beginner homework 6

Post by chili » January 19th, 2018, 1:09 am

Balrog?
Attachments
afd2d180e2576df95ca678c2a351f8c5d550849d6a00be2f51f4da40bc9fa713_large.jpg
afd2d180e2576df95ca678c2a351f8c5d550849d6a00be2f51f4da40bc9fa713_large.jpg (29.01 KiB) Viewed 2073 times
Chili

FiceT
Posts: 4
Joined: January 18th, 2018, 3:26 am

Re: question - beginner homework 6

Post by FiceT » January 19th, 2018, 1:25 am

this...
i...
wow i...
i feel honored
destroyed by the almighty chili himself

Post Reply