Beginner Tutorial 6 HW Code
Beginner Tutorial 6 HW Code
I must be missing something here , but what's the difference between void Game::ContainBox() and int Game::ClampScreenX(int x) and y from Beginner Tutorial 6 HW.
-
- Posts: 4373
- Joined: February 28th, 2013, 3:23 am
- Location: Oklahoma, United States
Re: Beginner Tutorial 6 HW Code
Look at the function signatures:
As chili stated, the ContainBox() function doesn't return any values and doesn't take any arguments. This means the ContainBox() function only works for mobile_x and mobile_y.
The ClampScreen?(int) functions take in any integer, clamps it between 0 and Graphics::ScreenWidth or Graphics::ScreenHeight thus removing the limit on what variables you can clamp inside the screen borders.
Think of it this way.
If you have 20 different moving objects that shouldn't leave the screen, would you want to write 20 different functions for clamping them to the screen? or just two functions?
Things get better once you learn how to make your own classes. This way you can have one function that returns both x and y as a single object.
The point of the tutorial is to get you thinking about how to write your own functions and why you should write them, however, just as a side note there is a function called 'clamp' in the standard library. It takes in the value to be checked, the lowest values and the highest value 'value' can be and returns the value clamped between the two.
Code: Select all
void ContainBox();
Code: Select all
int ClampScreenX( int x );
int ClampScreenY( int y );
The ClampScreen?(int) functions take in any integer, clamps it between 0 and Graphics::ScreenWidth or Graphics::ScreenHeight thus removing the limit on what variables you can clamp inside the screen borders.
Think of it this way.
If you have 20 different moving objects that shouldn't leave the screen, would you want to write 20 different functions for clamping them to the screen? or just two functions?
Things get better once you learn how to make your own classes. This way you can have one function that returns both x and y as a single object.
The point of the tutorial is to get you thinking about how to write your own functions and why you should write them, however, just as a side note there is a function called 'clamp' in the standard library. It takes in the value to be checked, the lowest values and the highest value 'value' can be and returns the value clamped between the two.
Code: Select all
#include <algorithm>
mobile_x = std::clamp( mobile_x, 5, Graphics::ScreenWidth - 5 - 1 );
mobile_y = std::clamp( mobile_y, 5, Graphics::ScreenHeight - 5 - 1 );
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
Re: Beginner Tutorial 6 HW Code
So what's the point in keeping Game::ContainBox() in the code. It seems redundant.