Jump to content
  • Advertisement
Sign in to follow this  
c4c0d3m0n

convert SDL_Rect' to SDL_Rect*'

This topic is 3885 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

Hey all I am trying to fill a rectangle on a surface with SDL_FillRect(). I get the error: error: cannot convert `SDL_Rect' to `SDL_Rect*' for argument `2' to `int SDL_FillRect(SDL_Surface*, SDL_Rect*, Uint32)' To make the rectangle, I use:
    SDL_Rect playfieldrect;
    playfieldrect.w = 24 * 10;
    playfieldrect.h = 24 * 20;
    playfieldrect.x = (screen->w / 2) - (24 * 5);
    playfieldrect.y = (screen->h / 2) - (24 * 10);

To draw the rectangle, I use:
        SDL_FillRect( screen, playfieldrect, SDL_MapColor( screen, color[whitea] ) );

Can I convert the SDL_Rect to *SDL_Rect? Cos if I define playfieldrect as a pointer, I can't edit its properties anymore... I must admit, I don't know much about pointers/references...

Share this post


Link to post
Share on other sites
Advertisement
That was too easy... I wish I understood what this whole pointer-thing was about. Can't find any places that explained it in layman's terms, even my own books are too complicated.

Share this post


Link to post
Share on other sites
The important thing to note here is that '&' is the reference operator, just as '*' is the dereference operator. &something returns the address to something. *something returns what something is pointing to (assuming that something is a pointer). Because the function is expecting a pointer and you have a plain old SDL_Rect, you get the address to the SDL_Rect by using the reference operator.

Now this all gets especially confusing since * and & are used when declaring pointer and reference variables, respectively.

int integer = 5;
int* pointer = &integer;
int& reference = integer;
int anotherInt = *pointer;

At the end of this, integer is a plain old integer value. Pointer is the address to integer. Reference *is* integer. anotherInt is a separate integer which has the same value as what pointer is pointing to (which is integer). integer, *pointer, reference, and anotherInt all evaluate to 5.

Hopefully that helps. I used to have a link to a website that explained pointers nicely, but I have lost it. :(

Share this post


Link to post
Share on other sites
Alright, this clears up a few things... I've used &reference before already, to make some TicTacToe code more readable. I don't quite see what would be the advantage of sing pointers instead of the real thing though. Why doesn't SDL_FillRect() just eat a normal SDL_Rect instead of a pointer? Thanks for all the help!

Share this post


Link to post
Share on other sites
1) Efficiency. In general, most non-primitive types are expensive enough that it pays to avoid making copies of them.

2) Reference. SDL_FillRect modifies the rectangle passed to it to reflect the area that was actually filled. I'm not entirely sure why, but perhaps this has its uses. SDL_BlitSurface does this too.

void example( SDL_Surface *surface )
{
SDL_Rect rect = { 0, 0, surface->w * 2, surface->h * 2 };
SDL_FillRect(surface,rect,/* some colour */);

// now, rect.w == surface->w and rect.h == surface->h
}

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!