Jump to content

  • Log In with Google      Sign In   
  • Create Account


A little help with classes and SDL 2.0


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 bernardlv   Members   -  Reputation: 98

Like
0Likes
Like

Posted 03 January 2014 - 12:07 PM

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.



Sponsor:

#2 bernardlv   Members   -  Reputation: 98

Like
-1Likes
Like

Posted 03 January 2014 - 12:29 PM

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



#3 SeanMiddleditch   Members   -  Reputation: 5100

Like
1Likes
Like

Posted 03 January 2014 - 01:51 PM

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


#4 bernardlv   Members   -  Reputation: 98

Like
0Likes
Like

Posted 03 January 2014 - 01:59 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS