2015-06-11 33 views
-2

我有一个7 x 7的探测矩阵,它提供了一个信号,代表在该地区测量的水平。我可以在SDL中绘制7 x 7矩形的矩阵,但我无法更新颜色。
我有一个类 类探针 { public: Probe(); 〜Probe(); void SetColor(int x); void Set_id(int x,int y,int z);
void Level_id(void);cpp私有数据成员SDL_Rect无效

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. 
+0

//我明白。 SDL_Rect rect = {BLOCK_W * Xcord,BLOCK_H * Ycord,BLOCK_W,BLOCK_H}; //定义一个新的函数本地。 rect = {BLOCK_W * Xcord,BLOCK_H * Ycord,BLOCK_W,BLOCK_H}; //使用在类中定义的矩形作为私有:并且是全局的//类。 //很多谢伊恩。 – stanley82

+0

rect = {BLOCK_W * Xcord,BLOCK_H * Ycord,BLOCK_W,BLOCK_H}; // \t rect.x = BLOCK_W * Xcord; // \t rect.y = BLOCK_H * Ycord; //这些都会导致段错误。 – stanley82

+0

//我把它放在构造函数中,SetColor更新了rect [0] [0] – stanley82

回答

0

如果我理解正确的,你的类看起来是这样的:

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; 
}; 

,你想知道为什么“矩形”变量不会保存为私单,以及如何改变绘制SDL_Rect时的颜色?

首先,您的“矩形”没有得到保存,因为您正在函数中定义一个新的“矩形”变量。你应该做的是任一:

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

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

而改变呈现的颜色(假设你正在使用的SDL_RenderDrawRect功能,因为这是唯一一个我知道的你可以绘制SDL_Rects),您只需拨打:

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

调用SDL_RenderDrawRect功能(每次)权利之前。 如果您不这样做,并在其他地方调用SetRenderDrawColor函数,您的颜色将更改为该颜色。