0

So I am working on a project and I am currently trying to render textures. Currently RectTextures inherits from Textures. RectTextures has its own render() function because I want other types of Textures (like text) to render differently.

I then Created Quadrant which inherits from RectTexture. Quadrant, however can hold a vector of textures. When I render quadrant, I want to render it and everything it holds. For that, Quadrant has its own render function. Inside this function, it renders itself (which is a RectTexture) and then loops through every Object in its vector and renders them.

Now here is the issue. When I fill a Quadrant with a RectTexture, the render() function will call that of Texture and not RectTexture!! I was told this was because of Slicing and that I needed to use pointers. I have implemented this but nothing has changed... Since the project is quite big, I'm including a shortened version with certain variables moved around for clarity.

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

Quadrant:

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);
}
Bryan Chen
  • 45,816
  • 18
  • 112
  • 143
Meuktwo
  • 1
  • 2
  • 2
    Are you using virtual functions? – AndyG Jan 23 '17 at 20:43
  • 1
    There really isn't enough information here to answer your question appropriately. Please show your class inheritance. Also, you have a lot of code in your `MainGame` destructor which is either a typo or inappropriate. – tegtmeye Jan 23 '17 at 20:44
  • @tegtmeye The destructor closes almost immediately. However, it looks like they forgot the method signature for that method, as it seems to exist on its own in the class without a method signature. – Xirema Jan 23 '17 at 20:45
  • I didn't know about virutal!! Thank you very much! It now works. Also I apologize for the code. I cut some pieces out because they weren't too relevant, and I clearly made a mistake. – Meuktwo Jan 23 '17 at 20:50
  • Make sure you make your destructor virtual as well otherwise you will not destroy the textures properly when done. – NathanOliver Jan 23 '17 at 20:56

0 Answers0