Hey guys!

i have yet another problem and cant find a way around it :O

i have this function in the class Platform

void Platform::set_clips(SDL_Rect platClip[], int i)
{
	for( int j = 0; j < i; j++ )
	{
		platClip[j].x = 0;
		platClip[j].y = 0;
		platClip[j].w = random_number_w();
		platClip[j].h = 5;
	}
}

and i also have another function that checks for collisions, this is not included in the platform class because it uses another class

bool check_collision(Stick C, Platform D, SDL_Rect A[], SDL_Rect B[])
{
	//side of rectangles
	int leftB;
	int rightA;

	//calculate rect A sides
	rightA = C.x + A[C.frame].w;

	//calculate sides rect B
	leftB = //this is where i want it to get platClip[j].x ;

	//if any of sides from A are outside B
	if( rightA <= leftB )
    {
        return false;
    }

	//if none of sides from A are outisde B, collision
	return true;
}

problem is that i want the int leftB to hold the value of the platClip[j].x but there is no way of getting the variables from platClip because it was initialized in the function parameter..

a copy of my whole source code can be found here on google docs : Full Source
The source code is a little old and only has outdated check_collision() function. i have posted the new check_collision function that im using above.

i tried making a SDL_Rect plats[] variable in the class platform so i could access it but i need to put a number in the array to define its size. The reason i made the class is so that i dont have to define the array size..

Thanks for any help :D

Edited 5 Years Ago by evilguyme: n/a

I can't really tell what you are trying to do since the code on google docs is really old (still has errors from last post).

Is this along the lines of what you are trying to accomplish?

struct SDL_Rect //I made this because I didn't want to include all the SDL stuff
{
	int x, y, w, h;
};

class Platform
{
	int numClips;
	SDL_Rect *clips;

	public:
	Platform()
	{
		numClips = 50; //so you can know how many clips you have externally rather than hardcoding all throughout your game
		clips = new SDL_Rect[numClips]; //create 50 clips
	}

	SDL_Rect *getClips()
	{
		return clips; //returns pointer to the first clip
	}

	int getNumClips()
	{
		return numClips;
	}

	~Platform()
	{
		delete clips;
		clips = 0;
	}
};

bool check_collision(Platform B)
{
	int leftB;
	for( int i = 0; i < B.getNumClips(); i++ )
	{
		leftB = B.getClips()[i].x;
	}

	return true;
}


int main()
{
	Platform A;
	check_collision(A);

    return 0;
}

well what im trying to do is get the value of the variable SDL_Rect platClip[j] but it is only declared in a function parameter in the class Platform in the function Platform::set_clips() thing is that i cannot access it using an external function because it is not a class member.. is there any way around this?

im going to try the return pointer method you have mentioned as it does give me hints to try. Thanks :D

i will upload my source again with the new code in like an hour.

thanks though lemme get testing

wow! thanks guys!

but i solved it i wasnt thinking right..

thanks a lot though sfuo your code helped me understand the problem :D

whoops !! this aint solved yet!

i still need help

well what im trying to do is get the value of the variable SDL_Rect platClip[j] but it is only declared in a function parameter in the class Platform in the function Platform::set_clips()
thing is that i cannot access it using an external function because it is not a class member.. is there any way around this?

i have uploaded the new source code that i have so far.. i have made bold and enlarged the code that needs adjusting.

Google Docs : source code

right now, i have made the int w in the Platform constructor to be initialized to the return value of the function random_number_w() but i would like to remove this variable and instead be able to directly use platClip[j].w in the function check_collision()

You cannot use platClip[j].w directly because that is not what it is called.

You have declared levelOnePlatform in the global scope and it is usable by everything.

So if you want to access platClip[j].w you need to put it in a loop so you can iterate through each element and use the name levelOnePlatform because that is what the name of the variable in the global scope is.

If I were you I would place the clips for the player within the stick class and the clips for the platform in the platform class. This way it is more like an object because those attributes belong to that class.

If I were you I would place the clips for the player within the stick class and the clips for the platform in the platform class. This way it is more like an object because those attributes belong to that class.

hmm okay fair enough..

but then does that mean for the level 2 clips i would have to declare another SDL_Rect in the class? and for the rest of the levels the same?

and i will have to make different set_clips() functions for the different level platforms?

is there no way in which i can use one function and SDL_rect to do the above?

but then does that mean for the level 2 clips i would have to declare another SDL_Rect in the class? and for the rest of the levels the same?

I'm not sure. The code you've posted in your Google Doc appears to declare the SDL_Rects as global variables. Which class and specific SDL_Rect are you referring to?

and i will have to make different set_clips() functions for the different level platforms?

Your set_clips() method looks generic enough to be reused. You would just call it again for the platClipsp[] array of RECTs for the next level platform.

is there no way in which i can use one function and SDL_rect to do the above?

There's always a way, but that's not necessarily the best solution. Clean, clear, well-thought-out code isn't required to get a program running, but it helps a great deal when you want to come back and fix something you missed the first time, or add a feature.

I may be wrong, but it sounds like you don't really have a firm sense of which classes should be responsible for various aspects of the functionality. I don't know what all of these various Clips are (maybe bounding-rectangles for collision detection?) or why they're global variables instead of attributes of various class instances, but maybe if you slow down, and think about what the objects are in your game, the answers will become obvious: so far you have a "Stick" (the guy you move around?), a "Timer" (which makes sure that things keep happening?), and a "Platform" (which doesn't seem to do much of anything at all?), but then you talk about having multiple levels in your game. What class represents a level? What does a level contain? How does the Stick interact with the level? Once you have more than one level, how do you get from one level to another? You also have a slew of functions that aren't in any class. Should any of them be grouped together in a new class or classes? Or be made methods of your existing classes?

And even if you're just trying some basic stuff to see if you can get it working (always a good idea), and are going to get back to what the point of the game is later, it's still useful to keep some of these questions in the back of your mind so you can re-use some of the code you're writing to test with.

I hope any of this rambling helps....

there is truth in what you say .. im just trying to practice C++ in a more fun way so i decided to make a game .. i have not mastered classes or functions or anything of that sort

you have definitely helped me so much i cannot express it over a forum post :o

i guess if what im trying right now doesnt work out im going to sit down and first design my game out on paper .. i heard people advising me this but i did not want to waste time :P

now i realise that it can be a great asset to pre-design a program on paper

THANK YOU ONCE AGAIN! :)

This question has already been answered. Start a new discussion instead.