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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Jimmyee

Pages: 1 [2]
16
Graphics / Re: How to merge textures quickly?
« on: May 10, 2013, 06:03:43 pm »
Thank you.
I just wanted to use multiple sprite sets saved in several files. I'd build a custom virtual texture with the tiles the map is gonna use. Anyways, I realised it's complicated and just moved all the sprites to one texture :P

17
Graphics / 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?

18
Graphics / How to merge textures quickly?
« on: May 09, 2013, 09:30:57 pm »
How to do that quickly? D I have to use RenderTexture? I have to create a big texture so I can use only one while drawing 2D map with vertex arrays. I thought it's possible through sf::Image somehow but I can't get it working.

19
Oh OK. I found found needed functions.
Thank you for your opinions!
By the way, I'm new so I should tell you who I am.
I am Charlie and I'm from Poland, 20 years old. I'm making a 2D RPG using the SFML 2.0 engine. It gonna be MMO in the future but first I have to master all the functions of SFML I need. I'm a C++ programmer for like 5 years now but I don't feel like experienced with making games :P

20
Yes, this is what I was doing last time - each tile had it's individual verticles initialized in the class constructor and the texture was chosen using the tile ID number, then they all were drawn in a MapRender class in a big loop. It's still slow so I'm just gonna use one texture for now... Until I don't find better solution :)

21
Hello. I've been trying to render a map without lose of CPU. Some day I found this: http://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php It's a good tutorial but it didn't help me at all and there is my question: how can I render a map using vertex arrays with use of multiple textures loaded from files? My map tiles are saved in several files and I don't want to call draw() so often but only once. Should I join all the textures to a big one (if it's possible) and use it?

Pages: 1 [2]