Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualkidman171

Posted 18 September 2012 - 09:51 PM

Maybe you should consider using vectors instead of arrays. Consider this example:

class Board
{
public:
	void set_board(vector<SDL_Rect> boxes)
	{
		for(int i = 0; i < boxes.size(); ++i)
		{
			cout << boxes[i].x << endl;
			cout << boxes[i].y << endl;
			cout << boxes[i].w << endl;
			cout << boxes[i].h << endl;
		}
	}
};
int main()
{
	Board game_field;
	SDL_Rect sr1;
	sr1.x = 10;
	sr1.y = 20;
	sr1.w = 30;
	sr1.h = 40;
	SDL_Rect sr2;
	sr2.x = 50;
	sr2.y = 60;
	sr2.w = 70;
	sr2.h = 80;
	vector<SDL_Rect> blockBoxes;
	blockBoxes.push_back(sr1);
	blockBoxes.push_back(sr2);
	game_field.set_board(blockBoxes);
	return 0;
}

This prints out:
10
20
30
40
50
60
70
80
By using a vector, you have the advantage of a dynamic-length container and it also remembers its size, making it easy to pass data around.

#1kidman171

Posted 18 September 2012 - 09:50 PM

Maybe you should consider using vectors instead of arrays. Consider this example:

class Board
{
public:
    void set_board(vector<SDL_Rect> boxes)
    {
	    for(int i = 0; i < boxes.size(); ++i)
	    {
		    cout << boxes[i].x << endl;
		    cout << boxes[i].y << endl;
		    cout << boxes[i].w << endl;
		    cout << boxes[i].h << endl;
	    }
    }
};
int main()
{
    Board game_field;
    SDL_Rect sr1;
    sr1.x = 10;
    sr1.y = 20;
    sr1.w = 30;
    sr1.h = 40;
    SDL_Rect sr2;
    sr2.x = 50;
    sr2.y = 60;
    sr2.w = 70;
    sr2.h = 80;
    vector<SDL_Rect> blockBoxes;
    blockBoxes.push_back(sr1);
    blockBoxes.push_back(sr2);
    game_field.set_board(blockBoxes);
    return 0;
}

By using a vector, you have the advantage of a dynamic-length container and it also remembers its size, making it easy to pass data around.

PARTNERS