A little help with classes and SDL 2.0

Started by
2 comments, last by bernardlv 10 years, 3 months ago

Hello, everyone. This is my first post, so I'm sorry if I've missed something.

I've made 2 classes - a Paddle and a Ball class. Both within them have a SDL_Rect object which I need to get out for the SDL_HasIntersection() function, but, no matter what I try, I'm getting this error:

\dir\ - timer.cpp|140|error: cannot convert 'SDL_Rect' to 'const SDL_Rect*' for argument '1' to 'SDL_bool SDL_HasIntersection(const SDL_Rect*, const SDL_Rect*)'|

\dir\ - timer.cpp|141|error: cannot convert 'Ball' to 'const SDL_Rect*' for argument '1' to 'SDL_bool SDL_HasIntersection(const SDL_Rect*, const SDL_Rect*)'|

Both of the classes have a Draw(SDL_Renderer* gRenderer) method which I think acts just like SDL_FillRect(), so I tried making it available like this:

void Paddle::Draw(SDL_Renderer* gRenderer){
SDL_Rect Paddle;
Paddle.x = posX;
Paddle.y = posY;
Paddle.w = PADDLE_WIDTH;
Paddle.h = PADDLE_HEIGTH;

SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
SDL_RenderDrawRect(gRenderer, &Paddle);
thisPaddle = Paddle;
}

AND in my game loop - SDL_HasIntersection(ball.thisBall, paddle2.thisPaddle), but it didn't work. Using a method like this

SDL_Rect Ball::getRect()
{
return thisBall;
}

also didn't work. I'm guessing getting rid of the classes isn't a great idea?

Any help, tips, code is appreciated.

Advertisement

I'd be grateful if anyone could help, this is urgent (I don't mean to be rude).

You seem to be having basic C/C++ issues; nothing about this is really specific to SDL.

You can't convert SDL_Rect to a const SDL_Rect* becaue the former is a _value_ and the later is a _pointer_. Use & to get the address of a value (get a pointer pointing to it).


// this is a value (created locally)
SDL_Rect rect;

// this is a pointer that points to rect
SDL_Rect* pRect = ▭

// this is illegal and will not compile since a value doesn't not automtically convert to a pointer
SDL_Rect* pRect = rect;

// this is legal and compiles because a value does automatically convert to a C++ reference
SDL_Rect& rRect = rect;

You can't convert a Ball to an SDL_Rect because a Ball is not a rectangle. You need to access the member variable that is the rect (thisBall, I guess; your choice of names is very unconventional and not the easiest to follow). You can't take the address of a temporary, so you can't return an SDL_Rect from a method and then take the address of it, e.g. this is illegal:


// illegal if getRect returns a value, since the result is an rvalue (temporary)
&ball.getRect()

You should just directly return a pointer to the member variable from your getRect method or you should return a C++ reference (which you can take the address of):


// do this for ball and paddle
const SDL_Rect& Ball::getRect() const { return thisBall; }

// this is how you call the code
SDL_HasIntersection(&paddle.getRect(), &ball.getRect());

Sean Middleditch – Game Systems Engineer – Join my team!

Sean, you can't imagine how grateful I am. Thank you very much! I was waiting for someone to reply.

This topic is closed to new replies.

Advertisement