所以我正在研究一个项目,目前我正在尝试渲染纹理。目前,RectTextures继承自Textures。 RectTextures有自己的render()函数,因为我希望其他类型的Textures(如文本)以不同的方式呈现。
然后我创建了继承自RectTexture的Quadrant。然而,象限可以保持纹理矢量。当我渲染象限时,我想渲染它和它所拥有的一切。 为此,Quadrant有自己的渲染功能。在这个函数中,它渲染自己(它是一个RectTexture),然后循环遍历其向量中的每个Object并渲染它们。
现在问题就出现了。当我用QuadTexture填充象限时,render()函数将调用Texture而不是RectTexture !! 我被告知这是因为切片,我需要使用指针。我已经实现了这一点,但没有任何改变...... 由于项目非常大,我提供了一个缩短版本,为了清晰起见,移动了某些变量。
MainGame::MainGame(){
_window = NULL;
_renderer = NULL;
_screenWidth = 1920;
_screenHeight = 1080;
_gameState = GameState::PLAY;
}
MainGame::~MainGame(){
}
// Quadrants
vector<Quadrants*> _quadrants;
//Top Left
_quadrants.push_back(new Quadrant(0, 0, _screenWidth * 2 / 3, _screenHeight * 2 / 3, { 0x00 , 0xFF , 0xFF , 0xFF }, QuadDesc::TLEFT));
//Top Right
_quadrants.push_back(new Quadrant(_screenWidth * 2 / 3, 0, _screenWidth * 1 / 3, _screenHeight * 2 / 3, { 0xFF , 0x00 , 0xFF , 0xFF }, QuadDesc::TRIGHT));
//Bot Left
_quadrants.push_back(new Quadrant(0, _screenHeight * 2 / 3, _screenWidth * 2 / 3, _screenHeight * 1 / 3, { 0xFF , 0xFF , 0x00 , 0xFF }, QuadDesc::BLEFT));
//Bot Right
_quadrants.push_back(new Quadrant(_screenWidth * 2 / 3, _screenHeight * 2 / 3, _screenWidth * 1 / 3, _screenHeight * 1 / 3, { 0x00 , 0xFF , 0x00 , 0xFF }, QuadDesc::BLEFT));
SDL_Color color = { 255, 255, 255, 255 };
RectTexture *a = new RectTexture(200, 200, 500, 500, color);
RectTexture *b = new RectTexture(200, 200, 500, 500, color);
(*_quadrants[0]).addTexture(a);
(*_quadrants[0]).addTexture(b);
}
void drawGame() {
//Clear
SDL_SetRenderDrawColor(_renderer, 0x00, 0x00, 0x00, 0xFF);
SDL_RenderClear(_renderer);
//test quadrants
for (unsigned int i = 0; i < _quadrants.size(); i++) {
(*_quadrants[i]).render(_renderer);
}
SDL_RenderPresent(_renderer);
}
Texture.cpp
Texture::Texture(){
x = 0;
y = 0;
color.r = 0x00;
color.g = 0x00;
color.b = 0x00;
color.a = 0x00;
}
Texture::Texture(int nx, int ny, SDL_Color ncolor) {
x = nx;
y = ny;
color = ncolor;
}
Texture::~Texture(){
}
void Texture::render(SDL_Renderer* renderer) {
std::cout << "o" << std::endl;
return;
}
int Texture::getX() {
return x;
}
int Texture::getY() {
return y;
}
int Texture::getR() {
return color.r;
}
int Texture::getG() {
return color.g;
}
int Texture::getB() {
return color.b;
}
int Texture::getA() {
return color.a;
}
SDL_Color Texture::getColor() {
return color;
}
void Texture::setX(int nx) {
x = nx;
}
void Texture::setY(int ny) {
y = ny;
}
void Texture::setR(int na) {
color.a = na;
}
void Texture::setG(int ng) {
color.g = ng;
}
void Texture::setB(int nb) {
color.b = nb;
}
void Texture::setA(int na) {
color.a = na;
}
RectTexture:
void RectTexture::render(SDL_Renderer* renderer) {
SDL_SetRenderDrawColor(renderer, getR(), getG(), getB(), getA());
SDL_Rect rect = { getX(), getY(), getWidth(), getHeight() };
SDL_RenderFillRect(renderer, &rect);
}
象限:
RectTexture::render(renderer);
for (unsigned int j = 0; j < (_textures).size(); j++) {
std::cout << j << "|" << (_textures).size() << "|" << (*_textures[j]).getA() << std::endl;
_textures[j]->render(renderer);
}