convert SDL_Rect' to SDL_Rect*'

Started by
5 comments, last by speciesUnknown 16 years, 2 months ago
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...
Advertisement
&playfieldrect
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.
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. :(
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!
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}
Quote:Original post by c4c0d3m0n
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.


Binky to the rescue!
Don't thank me, thank the moon's gravitation pull! Post in My Journal and help me to not procrastinate!

This topic is closed to new replies.

Advertisement