Register    Login    Forum    Search    FAQ

Board index » Everything




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post Posted: January 18th, 2018, 3:40 am 
 

Joined: January 18th, 2018, 3:26 am
Posts: 4
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=_mBq68B7_08&index=13&list=PLqCJpWy5FohcehaXlCIt8sVBHBFFRVWsx)

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

i made this two functions:

Code:
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:
int Game::GetCoord(int x)
{
   int y = x;
   return y;
}

and then used

Code:
   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


Top 
 Profile  
Reply with quote  
 Post Posted: January 18th, 2018, 6:08 am 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2962
Location: Oklahoma, United States
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:
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


Top 
 Profile  
Reply with quote  
 Post Posted: January 18th, 2018, 9:34 am 
 

Joined: January 18th, 2018, 3:26 am
Posts: 4
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?


Top 
 Profile  
Reply with quote  
 Post Posted: January 18th, 2018, 5:43 pm 
 

Joined: February 28th, 2013, 3:23 am
Posts: 2962
Location: Oklahoma, United States
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:
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


Top 
 Profile  
Reply with quote  
 Post Posted: January 19th, 2018, 1:06 am 
 

Joined: January 18th, 2018, 3:26 am
Posts: 4
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++)


Top 
 Profile  
Reply with quote  
 Post Posted: January 19th, 2018, 1:09 am 
Site Admin
User avatar

Joined: December 31st, 2011, 4:53 pm
Posts: 3537
Location: Japan
Balrog?


Attachments:
afd2d180e2576df95ca678c2a351f8c5d550849d6a00be2f51f4da40bc9fa713_large.jpg
afd2d180e2576df95ca678c2a351f8c5d550849d6a00be2f51f4da40bc9fa713_large.jpg [ 29.01 KiB | Viewed 69 times ]

_________________
Chili
Top 
 Profile  
Reply with quote  
 Post Posted: January 19th, 2018, 1:25 am 
 

Joined: January 18th, 2018, 3:26 am
Posts: 4
this...
i...
wow i...
i feel honored
destroyed by the almighty chili himself


Top 
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 7 posts ] 

Board index » Everything


 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron