First of all, I think you have come a long way from your first attempt at a card-game!
Can I make some suggestions:
1. You have a tendency to squash braces and code into a single line. That's not good style, in my personal opinion. It gets much easier to read if you format the code over a few more lines (particularly, try to match up the end-brace with the start brace - it makes it much easier to see where each block starts and ends):
Code:
void check_ball_collision()
{
vector2 a_min, a_max, b_min, b_max;
if(ball.velocity.x > 0)
{
a_min = ball.location + ball.d_min;
a_max = ball.location + ball.d_max;
b_min = paddle1.location + paddle1.d_min;
b_max = paddle1.location + paddle1.d_max;
if (collision2d(a_min, a_max, b_min, b_max))
{
reverse_x_velocity(ball.velocity);
}
else if(ball.location.x > 75)
{
reverse_x_velocity(ball.velocity);
}
}
if(ball.velocity.x < 0)
{
a_min = ball.location + ball.d_min;
a_max = ball.location + ball.d_max;
b_min = paddle2.location + paddle2.d_min;
b_max = paddle2.location + paddle2.d_max;
if (collision2d(a_min, a_max, b_min, b_max))
{
reverse_x_velocity(ball.velocity);
}
else if(ball.location.x < -75)
{
reverse_x_velocity(ball.velocity);
}
}
if(ball.velocity.y > 0)
{
if (ball.location.y > 75)
{
reverse_y_velocity(ball.velocity);
}
}
if(ball.velocity.y < 0)
{
if (ball.location.y < -75)
{
reverse_y_velocity(ball.velocity);
}
}
}
2. The constants 75 and -75 is repeated many times. Maybe you should use a (descriptively) named constant instead?
3. Large functions inline within the class declaration makes the header-file quite bulky and it gets hard to follow what member functions and member variables the class actually consists of. And it is unlikely that large functions with several dozen lines calling small functions will get inlined, since the compiler will first inline the small functions into the large function, at which point the large function gets too large to inline.
4. Try to avoid global variables. You have two paddle variables and a ball. It would probably be a good idea to have those as part of the mechanics or some other object, rather than as global variables. Is there any other class that uses paddle1, paddle2 and ball? If not, stick them in mechanics. If they are used by other classes, consider passing them in as parameters and use some overall class to bundle mechanics, the paddles and the ball together.
5. This segment of code is repeated twice:
Code:
a_min = ball.location + ball.d_min;
a_max = ball.location + ball.d_max;
6. This
Code:
void reverse_y_velocity(vector2 & velocity)
{
velocity.y *= -1;
}
could be written as:
Code:
void reverse_y_velocity(vector2 & velocity)
{
velocity.y = -velocity.y;
}
and assuming the compiler does what you tell it to, you'd save a few clock-cycles by not using multiplication [ok, so chances are that the compiler does realize what you are doing and produces a negate instruction either way - but it doesn't seem to be any point to multiplying by -1].
--
Mats