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

Author Topic: Is it good way to render a map?  (Read 1590 times)

0 Members and 1 Guest are viewing this topic.

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Is it good way to render a map?
« on: May 10, 2013, 05:53:22 pm »
Hello again. I've got a question about 2D map rendering. I am rendering a map using verticles and single texture with tiles. This is how the code looks like:

MapRender::Tile::Tile(MapRender &map_render_, std::shared_ptr<MapFile::Tile> file_data_)
: map_render(map_render_)
, file_data(file_data_)
{
    verticles.setPrimitiveType(sf::Quads);
    verticles.resize(4);
    update_verticles();
}

void MapRender::Tile::update_verticles()
{
    sf::Vertex *quad = &verticles[0];
    int char_x = map_render.game.me->x * 32;
    int char_y = map_render.game.me->y * 32;
    int x1 = (file_data->x * 32) - char_x;
    int y1 = (file_data->y * 32) - char_y;
    int x2 = ((file_data->x * 32) - char_x) + 32;
    int y2 = ((file_data->y * 32) - char_y) + 32;


    quad[0].position = sf::Vector2f(x1, y1);
    quad[1].position = sf::Vector2f(x2, y1);
    quad[2].position = sf::Vector2f(x2, y2);
    quad[3].position = sf::Vector2f(x1, y2);

    sf::FloatRect rect = file_data->sprite_rect;
    quad[0].texCoords = sf::Vector2f(rect.left, rect.top);
    quad[1].texCoords = sf::Vector2f(rect.left + rect.width, rect.top);
    quad[2].texCoords = sf::Vector2f(rect.left + rect.width, rect.top + rect.height);
    quad[3].texCoords = sf::Vector2f(rect.left, rect.top + rect.height);
}

MapRender::MapRender(MapFile &map_file_, Game &game_)
: map_file(map_file_)
, game(game_)
{
    construct();
}

// it's called in the main loop with sf::RenderWindow
void MapRender::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
    states.texture = &game.gfx_loader->dir["map/"].tex[map_file.texture_id];
    target.draw(tile_verticles, states);
}

void MapRender::process()
{
    update_verticles();
}

void MapRender::update_verticles()
{
    for(int y = 0; y < map_file.height; ++y)
    {
        for(int x = 0; x < map_file.width; ++x)
        {
            tiles[0][x][y]->update_verticles();
            for(int i = 0; i < 4; ++i)
                tile_verticles[(1 * ((((4 * x) + i) + ((y * map_file.width) * 4))))] = tiles[0][x][y]->verticles[i];
        }
    }
}

void MapRender::construct()
{
    tile_verticles.setPrimitiveType(sf::Quads);
    // 1 = amount of layers, 4 = verticles per tile
    tile_verticles.resize(1 * ((map_file.width * map_file.height) * 4));

    for(int y = 0; y < map_file.height; ++y)
    {
        for(int x = 0; x < map_file.width; ++x)
        {
            tiles[0][x][y] = std::shared_ptr<Tile>(new Tile(*this, map_file.tiles[0][x][y]));
            tiles[0][x][y]->update_verticles();
            for(int i = 0; i < 4; ++i)
                tile_verticles[(1 * ((((4 * x) + i) + ((y * map_file.width) * 4))))] = tiles[0][x][y]->verticles[i];
        }
    }
}

There is a simple character too, the map possition is based on it's coordinates. Everytime I press any arrow key, I set the new character possition and call MapRender::process() to update all the verticles to new possitions. Is there a better way to move the map without updating 10000's of verticles? :D I guess it can be done with sf::View but I have no idea how to use it... I have read the tutorial already but there are problems still. Can you give me some hint?
« Last Edit: May 10, 2013, 05:55:28 pm by Jimmyee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is it good way to render a map?
« Reply #1 on: May 10, 2013, 06:15:00 pm »
Have you read the vertex array tutorial, especially the "Transforming vertex arrays" chapter?

PS: vertices or vertexes, not verticles :P
Laurent Gomila - SFML developer

Jimmyee

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Is it good way to render a map?
« Reply #2 on: May 10, 2013, 06:25:02 pm »
I didn't but I will :P Thanks, I'll try to spell it correctly ;)