Jump to content
  • Advertisement
Sign in to follow this  

A little help with classes and SDL 2.0

This topic is 2117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post

Link to post
Share on other sites

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)

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());

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!