SDL_Rect drawing simple boxes to the screen using a class

Hi guys!

First post here, just want to say how much I love this site! On to the meat:

So I've taken up C++, but I've been trying to learn while integrating SDL into my code. I've done literally hours of research on this specific problem, including watching many tutorials on classes, SDL functions and the like while reading many search results and forum posts for even minute hints at my issue. I by no means have read them all, however, so please bear with me...

I'm practicing SDL functions with simple classes, and I'm just not sure how to pass in SDL_Rects into my class...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <SDL.h>
using namespace std;

class Cube
{
public:
    Cube(int x,int y,int w,int h) { xPos = x; yPos = y; width = w; height = h; box.x = xPos; box.y = yPos; box.w = width; box.h = height;}
    SDL_Rect box;

private:
    int xPos;
    int yPos;
    int width;
    int height;
};

int main(int argc, char* argv[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Surface *screen;
    Cube * Box = new Cube(25,25,25,10);
    screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
    bool running = true;
    Uint32 start;
    int unsigned second = 1000;
    int unsigned fps = 30;
    SDL_Event event;
    Uint32 blue = SDL_MapRGB(screen->format,0,0,255);
    while(running)
    {
            start = SDL_GetTicks();
            while(SDL_PollEvent(&event))
            {
                switch(event.type)
                {
                case SDL_QUIT:
                    running = false;
                    break;
                case SDL_KEYDOWN:
                    switch(event.key.keysym.sym)
                    {
                    case SDLK_ESCAPE:
                        running = false;
                        break;
                    }
                break;
                }
            }
            SDL_FillRect(screen,Box->box,blue);
            SDL_Flip(screen);
            if(second/fps>(SDL_GetTicks()-start))
                SDL_Delay(second/fps-(SDL_GetTicks()-start));
    }
    SDL_QUIT;
    return 0;
}


On line 49 is where the problem shows up. I was basically trying to pass the SDL_Rect function into the class so that when the class is called, it is passed the coordinates, builds the rect, applies the coordinates to the rect it creates, then I can draw it to the screen using the object I had created. Maybe SDL_FillRect isn't the correct function?

main.cpp|49|error: cannot convert 'SDL_Rect' to 'SDL_Rect*' for argument '2' to 'int SDL_FillRect(SDL_Surface*, SDL_Rect*, Uint32)'|

I may have missed something, as I might be using the wrong function anyway. Can someone please point me some guidance or an answer if they are willing? This is eluding me! Thank you so much!
SDL_FillRect takes a pointer to a SDL_Rect as argument. Box->box is not a pointer. You can use & to get a pointer to it.
SDL_FillRect(screen, &Box->box, blue);
Last edited on
Wow! *facepalm.

I was placing it as in Box->&box, just &box, &Box, I just didn't get the syntax correct.

Thank you! I joined a forum, posted my first post, and it worked! 15 minutes after I posted. I am very pleased with this forum.

Thanks!
Topic archived. No new replies allowed.