1
\$\begingroup\$

So in an attempt to move out of the console environment I have set up an SFML poject in Visual Studio. I am currently trying to draw a stage from a vector within a vector (Matrix?), and the results are not exactly what I was hoping for.

First i tried mapping out everything as '0's representing empty space, changing the top and bottom vector to '1's, representing objects, thus drawing what essentially would be a rectangle covering the bottom and a rectangle covering the top, everything inbetween ignored.

bool stage::load()
{   
 vector<int> tempvec;
 for (int i = 0; i != 30; i++)
 {
    for (int j = 0; j != 40; j++)
    {
        tempvec.push_back(0);
    }
    hitbox.push_back(tempvec);
    tempvec.clear();
 }

 for (int i = 40 - 1; i != -1; i--)
 {
    hitbox[29][i] = 1;
 }
 for (int i = 30 - 1; i != -1; i--)
 {
    hitbox[i][0] = 1;
 }
 return true;    //Will use this more efficently once I get the function to work
}

So this is how I set up the stage variable-wise, hitbox being defined as:

vector <vector<int>> stage::hitbox;

I then try to map out the individual blocks, so every time it finds a '1' in the vector, it should print out a square (quad).

vector<VertexArray> tilesetrender;
VertexArray tmp(Quads, 4);
tmp.resize(4);

level.load();
for (int y = level.hitbox.size() - 1; y >= 0; y--)
{
    for (int x = level.hitbox.at(y).size() - 1; x >= 0; x--)
    {
        switch(level.hitbox[y][x])   //Switch case allows me to expand on this function in the future
            {
            case 1:
                tmp[0].position = Vector2f(y * 20 - 10, x * 20 - 10);
                tmp[1].position = Vector2f(y * 20 - 10, x * 20 + 10);
                tmp[2].position = Vector2f(y * 20 + 10, x * 20 + 10);
                tmp[3].position = Vector2f(y * 20 + 10, x * 20 - 10);
                tilesetrender.push_back(tmp);
            }
    }
}

With all the variables created, I proceed to draw the level.

for (int count = tilesetrender.size() - 1; count >= 0; count--)
{
    renderWindow.draw(tilesetrender.at(count));
}

And what I get is this... The vertices being white, and the background being red. As I have said, I hoped for drawing two rectangles, one at the top and one at the bottom. I have no real idea regarding what part of my code is causing the error, but I've played around with the position commands the most, to no avail.

Thanks in advance.

EDIT: Code partially corrected (Thanks Alayric), thus rendering this image instead: White being the vertices, red the background and blue the player (previously not visible). Although it is a whole lot better, something is still incorrect, as the upper rectangle does not reach all the way, and the lower rectangle does not mirror the upper. I do belive the main problem has been solved, and thus I am able to continue on my own.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Switch statements are supposed to be surrounded by curly braces.

switch(level.hitbox[y][x]) {
    case 1:
        tmp[0].position = Vector2f(y * 20 - 10, x * 20 - 10);
        tmp[1].position = Vector2f(y * 20 - 10, x * 20 + 10);
        tmp[2].position = Vector2f(y * 20 + 10, x * 20 + 10);
        tmp[3].position = Vector2f(y * 20 + 10, x * 20 - 10);
        tilesetrender.push_back(tmp);
}

What you wrote is equivalent to:

switch(level.hitbox[y][x]) {
    case 1:
        tmp[0].position = Vector2f(y * 20 - 10, x * 20 - 10);
}
tmp[1].position = Vector2f(y * 20 - 10, x * 20 + 10);
tmp[2].position = Vector2f(y * 20 + 10, x * 20 + 10);
tmp[3].position = Vector2f(y * 20 + 10, x * 20 - 10);
tilesetrender.push_back(tmp);
\$\endgroup\$
2
\$\begingroup\$

Unfortunately, I don't have enough reputation to add a comment to your original post. Here is my go at a partial answer!

Here is where you populate the top row with 30 white boxes.

for (int i = 30 - 1; i != -1; i--)
 {
    hitbox[i][0] = 1;
 }

It looks like your boxes are 20x20 because

tmp[0].position = Vector2f(y * 20 - 10, x * 20 - 10);

My honest guess is that the screen is wider than you expect. In the first block, change the loop delimiter to something higher than 30. 30x20 = 600. Your window may be larger than 600 pixels, or you may have a misconfigured OpenGL or SFML viewport to be wider than you expect.

As an unrelated style tip, I would suggest you change your loop header to something more readable, ie:

for(int i = 30; i >= 0; i--)

or `for(int i = 0; i < 30; i++)

These two ways of writing the loop header make it more readily apparent that you are counting up/down from 30, rather than saying "I'm gonna start at 30-1, and go to -1." -1 is a weird number unless you're checking for an invalid array index.

\$\endgroup\$
2
  • \$\begingroup\$ I appreciate the answer! I did manage to fully box the player in with vertices by rewriting the load function so that the first vector would represent the x-axis and the secon would represent the y-axis. Thanks for pointing out the format "mistake", I generally try to write as you advised, sadly, that wasn't the case this time. Code is now corrected and fully functioning, will update the first post in time. \$\endgroup\$
    – qwarten
    Jun 9, 2016 at 6:18
  • \$\begingroup\$ Glad I could help. We all make silly mistakes sometimes, and the last person who will ever see it is the originator. Thanks, reinforcement bias and Obama. \$\endgroup\$
    – Daniel
    Jun 9, 2016 at 18:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .