Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Vertex Array issue  (Read 5594 times)

0 Members and 2 Guests are viewing this topic.

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
[SOLVED]Vertex Array issue
« on: July 08, 2013, 12:53:37 pm »
Hello,

I am having trouble playing around with tile sets and vertex array.

I followed the tutorial:
http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php

And tried using the tile map example in my code but I can only see 3 tiles being drawn.

I believe that my problem comes from this part of the code:

// define its 4 corners
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);

In my code I only create the map randomly like so:

void           GameWindow::designMap(int width, int height)
{
    int         levelMap[width * height];

    for (int i  = 0; i < width * height ; i++)
        levelMap[i] = rand() % 3 + 1;

    if (!map.load("ressources/tileset4.png", sf::Vector2u(blockWidth, blockHeight), levelMap, width, height))
        throw GlobExcep("Cannot load tile image!");
}

with map being an attribute of type TileMap in my class.

But when I draw the map I can only see 3 different tiles being drawn. Here is the tile set I used ( with tiles being 32*32 like in the example):



and here is the result I get from drawing in map of 20 * 20.



I first thought that it was the format of the tileset I was using that was causing this issue so I changed it to a tileset like so:



but I would still get the same output and never get to see tile 1 drawn even if I create a map of 100 000 * 100 000

So I believe that I'm misunderstanding something about how to split tileset into quads.

So if somebody could help me understanding what I am doing wrong or what do I need to do to adapt this code:
  for (unsigned int i = 0; i < width; ++i)
            for (unsigned int j = 0; j < height; ++j)
            {
                // get the current tile number
                int tileNumber = tiles[i + j * width];

                // find its position in the tileset texture
                int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
                int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);

                // get a pointer to the current tile's quad
                sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

                // define its 4 corners
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
            }

 to any other tileset format that would be great!
« Last Edit: July 11, 2013, 10:57:07 pm by schizo-boy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Vertex Array issue
« Reply #1 on: July 08, 2013, 01:00:25 pm »
levelMap[i ] = rand() % 3 + 1;

rand() % 3 gives 0 1 or 2
rand() % 3 + 1 gives 1 2 or 3
You can't get 4 different tiles with only 3 different values.

Just try levelMap[i ] = rand() % 4; to get 0 1 2 or 3

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #2 on: July 08, 2013, 01:07:52 pm »
levelMap[i ] = rand() % 3 + 1;

rand() % 3 gives 0 1 or 2
rand() % 3 + 1 gives 1 2 or 3
You can't get 4 different tiles with only 3 different values.

Just try levelMap[i ] = rand() % 4; to get 0 1 2 or 3

Thanks a lot, that solved my issue  :)

Any ideas on how to adapt the code to split more than 4 tiles?

Let's say I want to split a tileset of 10 tiles of 32*32 for example.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Vertex Array issue
« Reply #3 on: July 08, 2013, 01:14:03 pm »
It's automatically split by calculating tu and tv with tileNumber.
tileNumber defines what type of tile your quad will use, so if you have 10 different type of tiles in your texture, set tileNumber to up to 10 different values.
levelMap[i ] = rand() % 10; gives 10 different values to tileNumber from 0 to 9.

It seems very obvious to me so I'm not sure what you want or if my "explanation" is understandable.

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #4 on: July 08, 2013, 01:33:07 pm »
Oups my mistake!

I just thought that each quad[i ] was the entire tile with its texture. ::)

I guess I really need some sleep  :)

Thanks for making me realize my obvious mistakes  :)

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #5 on: July 08, 2013, 08:00:39 pm »
I actually have an other question....

How could I do to get a certain tile from the array?

For example if I used that method to split a tileset containing tiles of "players" and I would like to draw them on the map but obviously not all of them...

What I'm looking for would be something like

windaw.draw( map[i ]);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertex Array issue
« Reply #6 on: July 08, 2013, 08:07:20 pm »
window.draw(vertex_ptr, vertex_count, primitive_type);
?
Laurent Gomila - SFML developer

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #7 on: July 08, 2013, 09:34:07 pm »
window.draw(vertex_ptr, vertex_count, primitive_type);
?

Not too sure how that would help me to draw only one vertice (I suppose that is the correct terminology ?)

Unless the vertex_count actually correspond to the vertice that you want to draw from the vertex_ptr?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertex Array issue
« Reply #8 on: July 08, 2013, 10:27:31 pm »
window.draw(&vertex_array[i * 4], 4, sf::Triangles);

This code draw 4 vertices (one tile) starting at tile number i (vertex number i * 4).

But there's absolutely no point using a vertex array if you draw its contents quad by quad.
Laurent Gomila - SFML developer

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #9 on: July 08, 2013, 11:04:58 pm »
Thank I just realized as well that it would be pointless.
It would mean that I would have to call as many time the function draw as I would have done without using the vertex array  :(

Do you have any suggestion on how to handle multiple players on a window that are controlled by an AI and that could all move in any direction all together depending on the AI.

For understanding purpose here is the player tileset I am using



So any of those 8 players could be in any position of the map facing any direction and moving to any direction.

I was thinking on may be creating a map of players like it was explained on the vertex array tutorial and adapt it according to the AI changes and draw that map each time.

Would that be a good solution or is there a simpler, better, easier, or more efficient way of doing such things?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Vertex Array issue
« Reply #10 on: July 09, 2013, 12:10:12 pm »
If it's just 8 entites, use sf::Sprite. Grouping entities into vertex arrays is interesting if you have many of them.
Laurent Gomila - SFML developer

schizo-boy

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Vertex Array issue
« Reply #11 on: July 11, 2013, 10:56:49 pm »
I actually had much more entities so I went with the vertex array method as I thought it would be less resources consuming than drawing many sprites.

Thanks a lot  ;)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED]Vertex Array issue
« Reply #12 on: July 11, 2013, 11:09:14 pm »
Quote
I actually had much more entities
How many?
Laurent Gomila - SFML developer