Sign in to follow this  
DestX

Using all the elements of an array together

Recommended Posts

DestX    122
Another simple question, I.. think, I'm trying to use an array that will contain the names of classes, so they can each have different values and all could show up at once, without having me to write an hundred or so lines of codes at any place I need it in. If the above is hard to understand, I'm trying to do this(Extracted the relevant parts of the codes, parts seperated by tripledots are sperated by other code lines in the program):
//Block class
class Block
{
public:
	double destx;
	double desty;
	int destw;
	int desth;
	int setblock;
	int pos;
	bool move;

//Function call to draw the sprite.
void drawSprite(SDL_Surface *string, SDL_Surface *screen, 
		int srcx, int srcy, int srcw, int srch, 
		double destx, double desty, int destw, int desth)
{
	SDL_Rect source;
		source.x = srcx;
		source.y = srcy;
		source.w = srcw;
		source.h = srch;

	SDL_Rect destination;
		destination.x = destx;
		destination.y = desty;
		destination.w = destw;
		destination.h = desth;

	SDL_BlitSurface(string, &source, screen, &destination);
	
}

};

...

//creating an hundred classes of the above template.
placedBlock[100];

...

//This is outside of the game loop.
//This is meant to determine which class instance would be modified at that time
int bn=0;

...

/*currentBlock is another use of the same class, used for different purposes,
this is meant to copy the data in variables inside of the currentBlock to the current placedBlock(current by the bn var)*/
placedBlock[bn].destx; placedBlock[bn].desty; placedBlock[bn].pos; placedBlock[bn].setblock;
placedBlock[bn].destx = currentBlock.destx; placedBlock[bn].desty = currentBlock.desty; 
placedBlock[bn].pos = currentBlock.pos;	 placedBlock[bn].setblock = currentBlock.setblock; 
newblock=true;
SDL_Delay(463);
/*Increment bn so the next loop of the program would effect the next element of the array*/
bn++;

...

//A place all the above is used in.
if(placedBlock[bn].setblock == 0)
{
placedBlock[bn].drawSprite(sprite, screen, 0, 0, 37, 37, placedBlock[bn].destx, placedBlock[bn].desty, 37, 37);
}

What happens here is that only the instance represented by the element of the number bn represents will be shown on the screen at the last part of the code(Because of being incremented). Now, what I want to do is that the last part of the code above
if(placedBlock[bn].setblock == 0)
{
placedBlock[bn].drawSprite(sprite, screen, 0, 0, 37, 37, placedBlock[bn].destx, placedBlock[bn].desty, 37, 37);
}

Will show ALL of the elements of the array:
if(placedBlock[all100].setblock == 0)
{
placedBlock[all100].drawSprite(sprite, screen, 0, 0, 37, 37, placedBlock[all100].destx, placedBlock[all100].desty, 37, 37);
}

So... how would I write that down in code, as obviously, the above all100 is obviously out of the question =P Sorry if this is a little hard to understand ><, don't hesitate to ask more details on parts you didn't understand. Thanks alot guys.

Share this post


Link to post
Share on other sites
Gage64    1235
If you want to perform some operation on all elements of an array, you can use a for loop:


const int SIZE = 100;
int arr[SIZE];

// Initialize the array to some values
for (int i = 0; i < SIZE; ++i)
arr[i] = 2 * i - 7;

// Add 5 to all elements greater than 3
for (int i = 0; i < SIZE; ++i)
if (arr[i] > 3)
arr[i] += 5;



Is this what you meant?

Share this post


Link to post
Share on other sites
DestX    122
Actually I think that is, I'll try and implent it and write back in a few minutes, thanks!
Edit:Okay, it works! Thanks alot dude!

[Edited by - DestX on April 22, 2008 5:16:19 PM]

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by DestX
Another simple question, I.. think, I'm trying to use an array that will contain the names of classes, so they can each have different values and all could show up at once, without having me to write an hundred or so lines of codes at any place I need it in.


It won't contain "the names of classes"; it will contain objects - also known as instances of the class. There is only one class here: Block. A class defines a type; the Blocks are things which are of that type.


class Block
{
public:
double destx;
double desty;
int destw;
int desth;


These members will be important for more than just drawing, so they should have names that (a) are general, and (b) can easily be read. I suggest "x", "y", "width", "height".

void drawSprite(SDL_Surface *string, SDL_Surface *screen, 
int srcx, int srcy, int srcw, int srch,
double destx, double desty, int destw, int desth)


It looks like the 'src' rectangle will always be the same for Blocks. Therefore, it should be the Block's responsibility to supply that information, not the calling code. Further, the destination x/y/w/h are already part of the object; the calling code should not be expected to provide it. Simplify: get rid of those parameters, and supply the constant values inside the function:


const int BLOCK_WIDTH = 37;
const int BLOCK_HEIGHT = 37;

void Block::drawSprite(SDL_Surface* string, SDL_Surface* screen) {
// An SDL_Rect can be initialized like an array, by providing
// x, y, w, h values in that order:
SDL_Rect source = { 0, 0, BLOCK_WIDTH, BLOCK_HEIGHT };
SDL_Rect destination = { destx, desty, BLOCK_WIDTH, BLOCK_HEIGHT };
// That uses the destx and desty members of whatever Block you call
// the function on.
SDL_BlitSurface(string, &source, screen, &destination);
}




/*currentBlock is another use of the same class, used for different purposes,
this is meant to copy the data in variables inside of the currentBlock to the current placedBlock(current by the bn var)*/
placedBlock[bn].destx; placedBlock[bn].desty; placedBlock[bn].pos; placedBlock[bn].setblock;
placedBlock[bn].destx = currentBlock.destx; placedBlock[bn].desty = currentBlock.desty;
placedBlock[bn].pos = currentBlock.pos; placedBlock[bn].setblock = currentBlock.setblock;


I have no idea what you expect the first four things to do. Right now, they do nothing useful: writing "placedBlock[bn].destx;" means "check what the value is of the destx member of the bn'th placedBlock, and then ignore it".

Anyway, to "copy" a block, you should just assign the structure:

placedBlock[bn] = currentBlock;


Being able to do this is one of the main reasons for using classes in the first place.

This will not always, automatically, do what you want. What it does, if you don't say otherwise, is to copy each data member across. Here, though, that should be fine. (It gets to be a problem typically when you are trying to hold pointers to other things within your object.)


SDL_Delay(463);


Why 463 exactly? And do you really want to pause for nearly half a second each time you copy a block? Didn't you say you're making a grid of 100 of them?

Quote:
What happens here is that only the instance represented by the element of the number bn represents will be shown on the screen at the last part of the code(Because of being incremented).


So, the obvious solution is to cause 'bn' to assume each value in turn, and with each value, perform the statement. The most common way we perform statements multiple times is with a loop. The natural way in C++ to have a variable assume sequential values in a loop, is with a for-loop.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this