Thread: I present to cprogramming.com: Prongject X

  1. #1
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968

    I present to cprogramming.com: Prongject X

    http://i301.photobucket.com/albums/n.../Prongject.jpg

    And the source code that makes it all work:
    Code:
    #include "Paddle.h"
    #include "Ball.h"
    #include "Textures.h"
    #include "Boundaries.h"
    #include "vector2.h"
    
    Paddle paddle1;
    Paddle paddle2;
    Ball ball;
    Boundaries border;
    
    class Mechanics
    {
    public:
    	Mechanics(){paddle1.location = Player_1_Start; paddle2.location = Player_2_Start;}
    	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);}}}
    	void check_paddle_collision(){
    		if (paddle1.location.y == 75)
    			{reverse_y_velocity(paddle1.location);}
    		else if(paddle1.location.y == -75)
    			{reverse_y_velocity(paddle1.location);}
    		if (paddle2.location.y == 75)
    			{reverse_y_velocity(paddle2.location);}
    		else if(paddle2.location.y == -75)
    			{reverse_y_velocity(paddle2.location);}}
    private:	
    	bool collision2d(vector2 & a_min, vector2 & a_max, vector2 & b_min, vector2 & b_max) {
    	return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);}
    	void reverse_y_velocity(vector2 & velocity)
    	{velocity.y *= -1;}
    	void reverse_x_velocity(vector2 & velocity)
    	{velocity.x *= -1;}
    };
    
    Mechanics game;
    Code:
    int DrawGLScene(GLvoid)
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    	game.check_ball_collision();
    	game.check_paddle_collision();
    	border.draw();
    	ball.draw(ball.location);
    	paddle1.draw();
    	paddle2.draw();
    	glLoadIdentity();
    	return TRUE;
    }
    Last edited by Shamino; 04-01-2009 at 06:18 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  2. #2
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Congratulations

    It's a beauty.

  3. #3
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    ty ty, now I'm working on getting freetype lib working so I can get some scoring and winning and stuff in.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  4. #4
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    The screenshot takes me back to the pre-Atari console we had in the very late 70's.

    How would I compile it to see it in all it's glory, or do you have a lazy man's .exe you could attach. (I've not advanced to multiple file programming yet).

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    968
    Matsp thanks so much for your suggestion it's exactly what I needed to make the paddle loop go away!
    Actually I did something a little different now that I look again, I did this:
    Code:
    #include "Player.h"
    #include "Ball.h"
    #include "Boundaries.h"
    #include "vector2.h"
    
    
    Player player1;
    Player player2;
    Ball ball;
    Boundaries border;
    
    class Logic
    {
    public:
    	Logic(){player1.paddle->location = Player_1_Start; player2.paddle->location = Player_2_Start;}
    	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 = player1.paddle->location + player1.paddle->d_min;
    			b_max = player1.paddle->location + player1.paddle->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);
    				player2.goal();
    			}
    		}
    		if(ball.velocity.x < 0)
    		{
    			a_min = ball.location + ball.d_min;
    			a_max = ball.location + ball.d_max;
    			b_min = player2.paddle->location + player2.paddle->d_min;
    			b_max = player2.paddle->location + player2.paddle->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);
    				player1.goal();
    			}
    		}
    		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);
    			}
    		}
    	}
    	void check_paddle_collision()
    	{
    		if (player1.paddle->location.y >= 70)
    		{
    			player1.paddle->location.y = 70;
    		}
    		else if(player1.paddle->location.y <= -70)
    		{
    			player1.paddle->location.y = -70;
    		}
    		if (player2.paddle->location.y >= 70)
    		{
    			player2.paddle->location.y = 70;
    		}
    		else if(player2.paddle->location.y <= -70)
    		{
    			player2.paddle->location.y = -70;
    		}
    	}
    private:	
    	bool collision2d(vector2 & a_min, vector2 & a_max, vector2 & b_min, vector2 & b_max) 
    	{
    		return (a_min.x <= b_max.x) && (a_min.y <= b_max.y) && (a_max.x >= b_min.x) && (a_max.y >= b_min.y);
    	}
    	void reverse_y_velocity(vector2 & velocity){velocity.y = -velocity.y;}
    	void reverse_x_velocity(vector2 & velocity){velocity.x = -velocity.x;}
    };
    
    Logic logic;
    Last edited by Shamino; 04-01-2009 at 11:51 AM.
    Sometimes I forget what I am doing when I enter a room, actually, quite often.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    great game :] you should post it in the 'post your games thread', i've got one that looks like this in there

    it uses glfont2 http://students.cs.byu.edu/~bfish/glfont2.php -- it works alright but it is many years old there might be better solutions now
    Last edited by Tonto; 04-02-2009 at 08:10 PM.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The new code is better.

    But you are still repeating the a_min and a_max calculation twice. The only time it's not going to be done is if ball.velocity.x == 0 [not sure if that's valid at all! - if it isn't, perhaps you should add a debug assert to ensure it doesn't happen].

    Also, you still have the constants 75 and -75 in the code. It should be "field_size" or some such, I think.

    Is the 70 in "paddle_collision" also related to the 75? If so, you probably should have that as a calculation from the named constant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cprogramming.com IRC channel
    By codec in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-06-2004, 10:31 AM
  2. #cprogramming.com
    By Eibro in forum Contests Board
    Replies: 5
    Last Post: 03-25-2003, 08:06 PM
  3. My birthday present to myself
    By Liger86 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 01-25-2003, 09:49 PM
  4. Changes at CProgramming.com
    By kermi3 in forum C++ Programming
    Replies: 0
    Last Post: 01-04-2002, 10:58 AM
  5. Talk about cprogramming.com
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-02-2002, 11:07 AM