我有一个7 x 7的探针矩阵,可以传递一个信号,表示在调查区域内测量的水平。我可以在SDL中绘制一个7 x 7矩形的矩形,但我无法更新颜色
我上课了
类探针
{
上市:
探测();
〜探针();
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.
答案 0 :(得分: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;
};
你想知道为什么" rect"变量不会保存为私有变量,以及在绘制SDL_Rect时如何更改颜色?
首先,你的" rect"没有得到保存,因为你正在定义一个新的" 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函数,你的颜色将变为该颜色。