4
\$\begingroup\$

So using a class i've created a vector and managed to make one row of SDL_rects(with collision detection and destroy on collision). But when I make a second row(and obviously 2 or more rows):
1) it doesn't make the row
2) it makes the row on top of the first row and overrides the color.

BTW this is a breakout game and i've looked through a bunch of source code but I can't find an answer.

I thought if I pass a variable called(barType) using a switch statement I can do this, but it doesn't work.

Here is part of the Bar class:

Bar(int xPos, int yPos,  int barType){
    x = xPos; y = yPos;
    type = barType; shown = true;
    gBar.x = x;
    gBar.y = y;
    gBar.w = 130;
    gBar.h = 20;
}
int x,y;
int type;

void show() {
    //if(shown) {
        switch(type) {
            case 0:
                SDL_SetRenderDrawColor(gRenderer, 255, 0, 0, 255);
                break;
            case 1:
                SDL_SetRenderDrawColor(gRenderer, 0, 205, 0, 255);
                break;


        }
  //  }
}

I managed to do it by using images but when I use color it doesn't work.I get a "Bad access" error from Xcode.

Here is the for loop that creates the Bars:

for (int j = 0; j < 2; ++j) {
    for (int i =0; i < 5; ++i) {
        //if (bars.size() <=4) {
            bars.push_back(Bar(xOffset, yOffset, j));
        //}

        xOffset = xOffset + 154;
       // SDL_SetRenderDrawColor(Renderer, 204, 204, 0, 255);
        SDL_RenderDrawRect(Renderer, &bars.at(i).barsRectGet());
        SDL_RenderFillRect(Renderer, &bars.at(i).barsRectGet());


    }
    xOffset = 0;
    yOffset += 20;
} 

TLDR: I want to created 4 rows of SDL_Rect. Each row containing 4 or 5 Rects(doesn't really matter right now).And each row being a different color.

I hope I formatted this question so you guys can understand easily.

\$\endgroup\$
1
\$\begingroup\$

I believe your issue is that you're using SDL_RenderDrawRect. If you simply set the color using SDL_SetRenderDrawColor, then call SDL_RenderFillRect, you should get exactly what you want. Here's a quick re-work of your code that should hopefully work:

Bar:

SDL_Rect rect;
int r,g,b;
bool shown;

Bar(int x, int y, int r, int g, int b) {
    this->rect = { x, y, 130, 20 };
    this->shown = true;
    this->r = r;
    this->g = g;
    this->b = b;
}

void show(SDL_Renderer* renderer) {
    if(this->shown) {
        SDL_SetRenderDrawColor(renderer, this->r, this->g, this->b, 255);
        SDL_RenderFillRect(renderer, this->rect);
    }
}

Main Loop:

SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);

for (int i =0; i < 5; ++i) {
    bars.at(i).render(renderer);
}

//Update screen
SDL_RenderPresent(gRenderer);
\$\endgroup\$
  • \$\begingroup\$ Thanks for the reply. The problem with that is I can't easily remove a rect once it has collision. I imagine I can make a for loop and add i to the x to offset it, but I can't think of how to "delete" one of the rects \$\endgroup\$ – Josh Jul 13 '16 at 23:28
  • 1
    \$\begingroup\$ You just don't render it? Since you are clearing the screen every update by using SDL_RenderClear(), all drawn things get removed and then re-drawn. So you just need to have a flag for whether to show it or not. Let me update my main answer with a re-work of your show and render code. \$\endgroup\$ – SaviorXTanren Jul 14 '16 at 3:41
  • \$\begingroup\$ You've helped me out a lot. But if I need to access the rect how do I do this? since it's in the show function \$\endgroup\$ – Josh Jul 14 '16 at 14:47
  • \$\begingroup\$ Updated the example to generate the rectangle as a member variable. \$\endgroup\$ – SaviorXTanren Jul 14 '16 at 17:07
  • 1
    \$\begingroup\$ r = r; and the other two won't do what you expect. both r's name the parameter. \$\endgroup\$ – Quentin Jul 15 '16 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.