cpp private data member SDL_Rect inactive

83 views Asked by At

I have a 7 x 7 matrix of probes that deliver a signal representing the level measured at that point in the area under investigation. I can draw a matric of 7 x 7 rectangles in SDL but I was unable to update the color.
I have a class class Probe { public: Probe(); ~Probe(); void SetColor(int x); void Set_id(int x, int y, int z);
void Level_id(void);

private: 
    int OldColor; 
    int Xcord; 
    int Ycord; 
    SDL_Rect rect; //This does not keep the rect that I create?
};
//outside all curly brackets {} I have 
Probe Level[7][7];
//I initialize each instance of my class 
Level[x][y].Set_id(x, y, z); and I use x and y to create and position the rectangle
//  Defininlg rectangles
    SDL_Rect rect = {BLOCK_W * Xcord, BLOCK_H * Ycord, BLOCK_W, BLOCK_H};
/*I get what I expected, by the way z sets the color and it is incremented so each rectangle has a      different color. */

//I used function SetColor, it failed untill I regenerated rect
//in the SetColor function.  I have
private:
SDL_Rect rect;
//It is private why do I need to re-create the SDL_Rect rect?
//Why is it not stored like 
private:
int Xcord; // !!?  Regards Ian.
1

There are 1 answers

0
HelpMe000 On

If I understand correctly, your class looks something like this:

class Probe {
public:
   Probe();
   ~Probe();

   void SetColor(int x);
   void Set_id(int x, int y, int z) {
      SDL_Rect rect = {BLOCK_W * Xcord, BLOCK_H * Ycord, BLOCK_W, BLOCK_H};
   }
private:
   int OldColor; 
   int Xcord; 
   int Ycord; 
   SDL_Rect rect;
};

and you're wondering why the "rect" variable won't save as the private one, and how you change the color when drawing an SDL_Rect?

First of all, your "rect" isn't getting saved because you are defining a new "rect" variable in your function. What you should do is either of these:

rect = {BLOCK_W * Xcord, BLOCK_H * Ycord, BLOCK_W, BLOCK_H};

or

rect.x = BLOCK_W * Xcord;
rect.y = BLOCK_H * Ycord;
rect.w = BLOCK_W;
rect.h = BLOCK_H;

And to change the color of the rendering (assuming you are using the SDL_RenderDrawRect function, as that is the only one I know of that you can draw SDL_Rects with), you simply call:

SDL_SetRenderDrawColor(int r, int g, int b, int a);

right before you call the SDL_RenderDrawRect function (every time). If you do not do this, and call the SetRenderDrawColor function somewhere else, your color will change to that color.