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
1
Graphics / Re: Storing sprites in a container for better performance
« on: July 05, 2017, 03:41:11 pm »
Thanks for answers guys, I appreciate it.
My project indeed is intented to be a real game, actualy it's Endless Online clone (that doesn't sound serious and even shouldn't sound like that). I have just started with map rendering and didn't really get any good performance (27% CPU usage while drawing a map of 6 layers). I didn't implement any of suggested methods yet and now probably I will. Here's my current code, feel free to comment it - would be great to get some hints on that. Anyway, I've been working on such rendering like 2 years ago and the best performance was while I used vertex arrays. By the way, the map is isometric.

void Map::Draw()
{
    S &s = S::GetInstance();

    int width = s.emf->width;
    int height = s.emf->height;

    int gfx_id[5] = { 3, 4, 5, 6, 6 };
    int g_xoff[5] = { 0, 0, 0, 0, 0 };
    int g_yoff[5] = { 0, 0, 0, 0, 0 };

    int min_x = s.character.x - 26 >= 0? s.character.x - 26 : 0;
    int min_y = s.character.y - 26 >= 0? s.character.y - 26 : 0;
    int max_x = s.character.x + 26 < width? s.character.x + 26 : width;
    int max_y = s.character.y + 26 < height? s.character.y + 26 : height;

    int cursor_closest_dist = 1000;
    int cursor_closest_dist_x = 0;
    int cursor_closest_dist_y = 0;
    int cursor_closest_dist_draw_x = 0;
    int cursor_closest_dist_draw_y = 0;
    shared_ptr<sf::Texture> tex = s.gfx_loader.LoadTexture(gfx_id[0], s.emf->fill_tile);
    sf::Sprite spr_fill(*tex);
    for(int y = min_y; y < max_y; ++y)
    {
        for(int x = min_x; x < max_x; ++x)
        {
            int graphic_id = s.emf->fill_tile;
            if(graphic_id != 0)
            {
                int player_x = s.character.x * 64 - s.character.x * 32 - s.character.y * 32;
                int player_y = s.character.y * 32 + s.character.x * 16 - s.character.y * 16;
                int screen_x = 640 / 2 - 32 - player_x;
                int screen_y = 480 / 2 - 480 / 4 + 16 - player_y;

                int xoff = x * 64;
                int yoff = y * 32;

                xoff -= x * 32;
                yoff += x * 16;
                xoff -= y * 32;
                yoff -= y * 16;

                //shared_ptr<sf::Texture> tex = s.gfx_loader.LoadTexture(gfx_id[0], graphic_id);
                //sf::Sprite spr(*tex);
                spr_fill.setPosition(screen_x + xoff + g_xoff[0], screen_y + yoff + g_yoff[0]);
                s.window.draw(spr_fill);

                if(x == s.character.x && y == s.character.y)
                {
                    tex = s.gfx_loader.LoadTexture(3, 0);
                    //spr.setTexture(*tex);
                    //s.window.draw(spr);
                }

                sf::Vector2i pos = sf::Mouse::getPosition(s.window);
                int distance = path_length(screen_x + xoff + 32, screen_y + yoff + 16, pos.x, pos.y);
                if(distance < cursor_closest_dist)
                {
                    cursor_closest_dist = distance;
                    cursor_closest_dist_x = x;
                    cursor_closest_dist_y = y;
                    cursor_closest_dist_draw_x = screen_x + xoff;
                    cursor_closest_dist_draw_y = screen_y + yoff;
                }
            }
        }
    }

    if(cursor_closest_dist_x != 0)
    {
        shared_ptr<sf::Texture> tex = s.gfx_loader.LoadTexture(2, 24);
        sf::Sprite spr(*tex, sf::IntRect(0, 0, 64, 32));
        spr.setPosition(cursor_closest_dist_draw_x, cursor_closest_dist_draw_y);
        s.window.draw(spr);
    }

    for(int y = min_y; y < max_y; ++y)
    {
        for(int x = min_x; x < max_x; ++x)
        {
            for(int l = 0; l < 5; ++l)
            {
                int graphic_id = s.emf->GetGraphicID(l, x, y);
                if(graphic_id != 0)
                {
                    int player_x = s.character.x * 64 - s.character.x * 32 - s.character.y * 32;
                    int player_y = s.character.y * 32 + s.character.x * 16 - s.character.y * 16;
                    int screen_x = 640 / 2 - 32 - player_x;
                    int screen_y = 480 / 2 - 480 / 4 + 16 - player_y;

                    int xoff = x * 64;
                    int yoff = y * 32;

                    xoff -= x * 32;
                    yoff += x * 16;
                    xoff -= y * 32;
                    yoff -= y * 16;

                    shared_ptr<sf::Texture> tex = s.gfx_loader.LoadTexture(gfx_id[l], graphic_id);

                    if(l == 1) // objects
                    {
                        g_xoff[l] = 32 - tex->getSize().x / 2;
                        g_yoff[l] = 0 - (tex->getSize().y - 32);
                    }
                    else if(l == 3) // wall down
                    {
                        g_xoff[l] = -16 + tex->getSize().x / 2;
                        g_yoff[l] = -tex->getSize().y + 32;
                    }
                    else if(l == 4) // wall right
                    {
                        g_xoff[l] = 16 + tex->getSize().x / 2;
                        g_yoff[l] = -tex->getSize().y + 32;
                    }

                    sf::Sprite spr(*tex);
                    spr.setPosition(screen_x + xoff + g_xoff[l], screen_y + yoff + g_yoff[l]);
                    s.window.draw(spr);

                    if(l == 0 && x == s.character.x && y == s.character.y)
                    {
                        tex = s.gfx_loader.LoadTexture(3, 0);
                        spr.setTexture(*tex);
                        spr.setPosition(screen_x + xoff, screen_y + yoff);
                        s.window.draw(spr);
                    }
                    if(l == 0 && cursor_closest_dist_x != 0 && x == cursor_closest_dist_x && y == cursor_closest_dist_y)
                    {
                        tex = s.gfx_loader.LoadTexture(2, 24);
                        spr = sf::Sprite(*tex, sf::IntRect(0, 0, 64, 32));
                        spr.setPosition(cursor_closest_dist_draw_x, cursor_closest_dist_draw_y);
                        s.window.draw(spr);
                    }
                }
            }
        }
    }
}
 

2
Graphics / Re: Storing sprites in a container for better performance
« on: June 06, 2017, 02:04:07 pm »
That's the direction I'm going to take. I won't probably use many sprites so I don't have to store them somewhere. Thanks for quick and good reply  :)

3
Graphics / Storing sprites in a container for better performance
« on: June 06, 2017, 01:52:34 pm »
Will I have better performance if I store created sprites in a container instead of creating them everytime while rendering graphics?

4
Graphics / Re: Keyobard input lowers FPS and almost pauses the game
« on: May 02, 2016, 02:58:26 pm »
OK I see. I HAD no driver installed for my mouse. Uppercase HAD because I was checking if same happens when using other window manager and for some reason my OS doesn't load now. I'm going to reinstall my OS now and see if the bug with events still will occur. I will give you the information about my DPI and mouse driver after installation of the system.
Anyway, I have changed the window manager and the bug still occured ;)

EDIT 19:57 PM:
After reinstall of my OS and compiler it works fine now. it's like 1000 FPS and max 4 events handled without any lag. The only difference which I made was installing g++ instead of gcc :P

5
Graphics / Re: Keyobard input lowers FPS and almost pauses the game
« on: May 02, 2016, 02:36:49 pm »
Even if they are different they cause the same effect - if I hold a key or move the mouse the game will slowdown and will process the events until I release a key or stop moving the mouse.
Anyway, here are details of my mouse:
A4TECH, V-Track
Model: F4

@Mr_Blame: my version if SFML-2.3.2.

6
Graphics / Re: Keyobard input lowers FPS and almost pauses the game
« on: May 02, 2016, 11:38:17 am »
@Nicox11:
Quote
I think that's it. I have posted functions of the Game class related to keyboard input but I don't think it's because of them since the game slows down even when I hold a key that isn't handled by the game.

...and yes it's a good idea, let's see how many events are generated. BTW I have never used a profiler and IDK what's that xD

EDIT 11:55 AM:
Same happens while using the mouse, I mean... If I keep to move the mouse then the game will slow down and will keep processing mouse events until I stop moving it. While moving the mouse it doesn't even print the FPS counter - it freezes completely.

EDIT 12:07 AM:
// main.cpp

sf::RenderWindow window(sf::VideoMode(640, 480), "Mass Agreement");

    Game game;

    sf::Clock clock;
    int fps = 0;

    while (window.isOpen())
    {
        game.RealTimeInput();

        sf::Event event;
        int event_count = 0;
        while(window.pollEvent(event))
        {
            event_count++;
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            {
                return 0;
            }
            else if(event.type == sf::Event::KeyPressed)
            {
                game.KeyPressed(event.key.code);
            }
        }

        if(clock.getElapsedTime().asSeconds() >= 1)
        {
            clock.restart();
            printf("FPS: %i\nEvents per second: %i\n", fps, event_count);
            fps = 0;
        }

        window.clear(sf::Color::Black);
        game.Draw(&window);
        window.display();
        fps++;
    }

    return 0;
 
Quote
FPS: 7
Events per second: 2164

Then when I stop moving the mouse it goes back to normal:

Quote
FPS: 31
Events per second: 0

7
Graphics / Re: Keyobard input lowers FPS and almost pauses the game
« on: May 02, 2016, 11:08:55 am »
Ubuntu 14.04 LTS is my OS, window manager: Compiz.

8
Graphics / Re: Keyobard input lowers FPS and almost pauses the game
« on: May 02, 2016, 09:41:02 am »
Yeah I guess so.
OK, here's how I handle the input:

// main.cpp

int main()
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "Mass Agreement");

    Game game;

    sf::Clock clock;
    int fps = 0;

    while (window.isOpen())
    {
        game.RealTimeInput();

        sf::Event event;
        while(window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
            {
                return 0;
            }
            else if(event.type == sf::Event::KeyPressed)
            {
                game.KeyPressed(event.key.code);
            }
        }

        if(clock.getElapsedTime().asSeconds() >= 1)
        {
            clock.restart();
            printf("FPS: %i\n", fps);
            fps = 0;
        }

        window.clear(sf::Color::Black);
        game.Draw(&window);
        window.display();
        fps++;
    }

    return 0;
}

// end of main.cpp

// Game::RealTimeInput()

void Game::RealTimeInput()
{
    if(this->editor->active) return;

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
        if(this->player->GetY() > 0)
        player->Walk(0);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        if(this->player->GetX() < map_->Width() - 1)
        this->player->Walk(1);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
        if(this->player->GetY() < map_->Height() - 1)
        this->player->Walk(2);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        if(this->player->GetX() > 0)
        this->player->Walk(3);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
    {
        unsigned int id = this->editor->GetMarkerID();
        shared_ptr<Map::Tile> tile = this->map_->GetTile(this->player->GetX(), this->player->GetY());
        if(id != tile->id)
        {
            tile->id = id;
            shared_ptr<sf::Texture> tex = this->gfxhandler->LoadTexture("gfx/map/1.png");
            tile->sprite = gfxhandler->GetSprite(tex, tile->id - 1);
        }
    }
}

// Game::KeyPressed()

void Game::KeyPressed(sf::Keyboard::Key &key)
{
    if(key == sf::Keyboard::T)
    {
        this->editor->active = !this->editor->active;
    }

    if(this->editor->active)
    {
        if(key == sf::Keyboard::Up)
        {
            this->editor->MoveMarker(0);
        }
        else if(key == sf::Keyboard::Right)
        {
            this->editor->MoveMarker(1);
        }
        else if(key == sf::Keyboard::Down)
        {
            this->editor->MoveMarker(2);
        }
        else if(key == sf::Keyboard::Left)
        {
            this->editor->MoveMarker(3);
        }

        return;
    }
    else
    {
        if(key == sf::Keyboard::S)
        {
            this->map_->Save();
        }
    }
}
 

I think that's it. I have posted functions of the Game class related to keyboard input but I don't think it's because of them since the game slows down even when I hold a key that isn't handled by the game.

EDIT:
Everything works fine when I use "if(window.pollEvent(event))" instead of "while(window.pollEvent(event))" but I guess it won't process all the events if I do that this way...

9
Can anyone tell me why is that? When I hold any key longer than 1 sec FPS of my game goes down from 32-35 to 1-5. I guess the game is flooded by input events and it doesn't process rendering so fast.
So my questions are:
is it common?
do I have to implement multithreading to avoid that?

10
EDIT: solved, look at the bottom of this post and thank you  :)

Hi. The challenge I have is to master the map rendering and I need help with solving this

I mean these tiles that are drawn right to the player. These additional tiles are drawn after every tile as you can see in this example where map size is 1x35. I have no clue what can cause it.

Here's the code I use to draw tiles:

/** gfxhandler.cpp */

shared_ptr<sf::Texture> GFXHandler::LoadTexture(std::string filename)
{
    map<std::string, shared_ptr<sf::Texture> >::iterator it;

    for(auto const &ent1 : textures)
    {
        if(ent1.first == filename)
        {
            return textures[filename];
        }
    }

    sf::Texture texture;
    if(!texture.loadFromFile(filename))
    {
        printf("GFXHandler: can't load texture %s\n", filename.c_str());
        return shared_ptr<sf::Texture>(0);
    }

    printf("GFXHandler: loaded %s\n", filename.c_str());
    textures[filename] = shared_ptr<sf::Texture>(new sf::Texture(texture));

    return textures[filename];
}

void GFXHandler::UnloadTexture(std::string filename)
{
    if(this->textures[filename].get())
    {
        this->textures[filename].reset();
    }
}

sf::Sprite GFXHandler::GetSprite(shared_ptr<sf::Texture> texture, unsigned int id)
{
    sf::Vector2u tsize = texture->getSize();
    sf::Sprite sprite;
    sprite.setTexture(*texture.get());

    int rectx = 0;
    int recty = 0;

    unsigned int yrows = (id * 32) / tsize.x;
    rectx = (id * 32) % tsize.x;
    recty = yrows * 32;

    sprite.setTextureRect(sf::IntRect(rectx, recty, rectx + 32, recty + 32));

    return sprite;
}

/** gfxhandler.hpp */

class GFXHandler
{
private:
map<std::string, shared_ptr<sf::Texture> > textures;

public:
shared_ptr<sf::Texture> LoadTexture(std::string filename);
void UnloadTexture(std::string filename);
sf::Sprite GetSprite(shared_ptr<sf::Texture> texture, unsigned int id);
};

/** map.cpp */
Map::Map(string filename, shared_ptr<GFXHandler> gfxhandler)
: datafile(filename)
{
    this->gfxhandler = gfxhandler;
    this->width = datafile.GetNumber();
    this->height = datafile.GetNumber();

    shared_ptr<sf::Texture> tex = this->gfxhandler->LoadTexture("gfx/map/1.png");

    this->layers[0].xrows.resize(this->width);

    for(int i = 0; i < this->width; ++i)
    {
    this->layers[0].xrows[i].yrow.tiles.resize(this->height);
        for(int ii = 0; ii < this->height; ++ii)
        {
            int id = datafile.GetNumber();
            this->layers[0].xrows[i].yrow.tiles[ii] = shared_ptr<Tile>(new Tile(id));
            shared_ptr<Tile> tile = this->layers[0].xrows[i].yrow.tiles[ii];
            if(tile->id != 0)
            {
                tile->sprite = gfxhandler->GetSprite(tex, tile->id - 1);
                tile->sprite.setPosition(sf::Vector2f(i * 32, ii * 32));
            }
            else
            {
                puts("Map: empty tile detected\n");
            }
        }
    }

    printf("Map created, %ix%i\n", this->width, this->height);
}

void Map::Draw(sf::RenderWindow *window, int x, int y)
{
    for(int i = 0; i < this->width; ++i)
    {
        for(int ii = 0; ii < this->height; ++ii)
        {
            shared_ptr<Tile> tile = this->layers[0].xrows[i].yrow.tiles[ii];
            int tile_x = i * 32 + x;
            int tile_y = ii * 32 + y;
            tile->sprite.setPosition(sf::Vector2f(tile_x, tile_y));
            window->draw(tile->sprite);
        }
    }
}

/** map.hpp */

class Map
{
public:
struct Tile
{
    unsigned int id;

    sf::Sprite sprite;

    Tile()
    : id(0)
    {
    }

    Tile(unsigned int id_)
    : id(id_)
    {

    }
};

struct Layer
{
    struct XRow
    {
        struct YRow
        {
            vector<shared_ptr<Tile>> tiles;
        } yrow;
    };

    vector<XRow> xrows;
};

private:
    DataFile datafile;
    int width;
    int height;
    Layer layers[1];

    shared_ptr<GFXHandler> gfxhandler;

public:
    Map(string filename, shared_ptr<GFXHandler> gfxhandler);
    void Draw(sf::RenderWindow *window, int x, int y);
    int Width() { return width; }
    int Height() { return height; }
};

 

Any help really appreciated...

EDIT!!!

 I've found the answer, this is not really anything serious...

In GFXHandler::GetSprite() function:
sprite.setTextureRect(sf::IntRect(rectx, recty, 32, 32));
// instead of sprite.setTextureRect(sf::IntRect(rectx, recty, rectx + 32, recty + 32));
 

Sorry for bothering anyone and it would be nice if someone delete this thread... ;)

11
Graphics / Re: Too big textures...
« on: August 07, 2013, 10:22:59 pm »
That's good idea of course :) This is what works for me right now:
#include "spritesheet.hpp"

SpriteSheet::BigImage::BigImage(sf::Vector2f img_size)
{
    create(img_size.x, img_size.y, sf::Color(0, 0, 0));
    xRemaining = img_size.x;
    yRemaining = img_size.y;
}

std::shared_ptr<sf::IntRect> SpriteSheet::BigImage::addImage(sf::Image &img)
{
    unsigned int w = img.getSize().x;
    unsigned int h = img.getSize().y;

    unsigned int x = getSize().x - xRemaining;
    unsigned int y = getSize().y - yRemaining;

    if(x < getSize().x)
    {
        xRemaining -= w;
    }
    else if(y < getSize().y)
    {
        xRemaining = getSize().x;
        yRemaining -= h;
    }
    else return std::shared_ptr<sf::IntRect>(0);

    copy(img, x, y, sf::IntRect(0, 0, w, h));

    return std::shared_ptr<sf::IntRect>(new sf::IntRect(x, y, w, h));
}

void SpriteSheet::load(std::shared_ptr<GFXLoader> gfxLoader, int id)
{
    //texture = std::shared_ptr<sf::Texture>(&gfxLoader->dir["map/"].tex[id]);
}

sf::Vector2f SpriteSheet::getImageSize(GFXLoader::Directory &dir)
{
    sf::Vector2f ret(0, 0);
    unsigned int x = 0;
    unsigned int y = 0;
    unsigned int maxh = 0;

    for(auto &img: dir.img)
    {
        unsigned int w = img.second.getSize().x;
        unsigned int h = img.second.getSize().y;
        if(h > maxh) maxh = h;

        if(x + w < sf::Texture::getMaximumSize())
        {
            x += w;
            if(x > ret.x) ret.x = x;
        }
        else
        {
            x = 0;
            y += maxh;
            maxh = 0;
        }
    }

    ret.y = y;

    return ret;
}

void SpriteSheet::mergeImages(GFXLoader::Directory &dir)
{
    sf::Vector2f texSize = getImageSize(dir);

    printf("total texture size: %fx%f\n", texSize.x, texSize.y);

    unsigned int img_w = texSize.x > sf::Texture::getMaximumSize()?
                sf::Texture::getMaximumSize() : texSize.x;
    unsigned int img_h = texSize.y > sf::Texture::getMaximumSize()?
                sf::Texture::getMaximumSize() : texSize.y;

    BigImage bigImg(sf::Vector2f(img_w, img_h));
    unsigned int textureId = 0;
    unsigned int spriteId = 0;
    unsigned int totalYSize = 0;
    for(auto &img: dir.img)
    {
        std::shared_ptr<sf::IntRect> rect = bigImg.addImage(img.second);
        if(rect.get())
        {
            SpriteInfo sprInfo(*rect.get());
            sprInfo.textureId = textureId;
            sprites[spriteId] = std::shared_ptr<SpriteInfo>(new SpriteInfo(sprInfo));
            spriteId++;
            //sprites.insert(std::make_pair(spriteId++, sprInfo));
        }
        else
        {
            images.push_back(bigImg);
            totalYSize += bigImg.getSize().y;
            textureId++;

            sf::Texture tex;
            tex.loadFromImage(images[images.size() - 1]);
            textures.push_back(tex);

            unsigned int img_w = texSize.x > sf::Texture::getMaximumSize()?
                                 sf::Texture::getMaximumSize() : texSize.x;
            unsigned int img_h = texSize.y - totalYSize > sf::Texture::getMaximumSize()?
                                 sf::Texture::getMaximumSize() : texSize.y - totalYSize;

            bigImg = BigImage(sf::Vector2f(img_w, img_h));

            rect = bigImg.addImage(img.second);
            if(rect.get())
            {
                SpriteInfo sprInfo(*rect.get());
                sprInfo.textureId = textureId;
                sprites[spriteId] = std::shared_ptr<SpriteInfo>(new SpriteInfo(sprInfo));
                spriteId++;
                //sprites.insert(std::make_pair(spriteId++, sprInfo));
            }
            else puts("fatal error: could not create sprite sheet");
        }
    }

    images.push_back(bigImg);
    sf::Texture tex;
    tex.loadFromImage(images[images.size() - 1]);
    textures.push_back(tex);
}

 

I didn't rewrite the Map::draw() function but seems like it will be more complicated now.
Also,
Quote
If I were you I`d implement a streaming system on a separate thread, and implement the offscreen big texture essentially like a queue, so what`s needed would replace the one that`s not, if this is possible in your context then it would be a nice addition in terms of memory, and perhaps speed. :)

can you explain me little more what do you mean?

12
Graphics / Re: Too big textures...
« on: August 06, 2013, 08:31:57 pm »
What about chopping that big texture that`s becoming a problem into smaller ones, like 1024*1024 or whatever works for you? And have some sort of container besides those containing what is where, then referencing that through it? Just my 2 cents tho.

Yeah, this is the best solution I found so far. I was just not sure if it's a good way :P

13
Graphics / Re: Too big textures...
« on: August 06, 2013, 08:16:33 pm »
How big is the map? Is it much larger then what your camera can see?

If this is the case try to only retrieve what your camera can see and just draw that, as there's no point in drawing images that are 2000 pixels off screen.

Of course it is larger ;) So, I will have to draw each object (not a tile, object is my second map layer) separately? Won't it slow my game down much? I think there can be like 400 of them at once.

Also, I forgot to tell you that "objects" is just another map layer for me :P They are static objects.

14
Graphics / Too big textures...
« on: August 06, 2013, 08:03:24 pm »
I've got a problem while rendering a 2D map which is drawn using vertex arrays; first let me explain my idea:
I want to draw objects on the map. They have irregular size and are stored in separate graphic files. I need to load them by ID (ID is loaded from a map file).
I also want to have them all in one texture, so I will call the draw function only once.
Until now, my game was creating a virtual image and putting them all in it. It was also storing an information about each object size and possition on the "big texture".
The problem is that when I added more objects there were too many of them and the texture was too big (it gives me SFML error).
My question is: is there any other way to manage graphics like that? I don't want to call draw() too often :S

The only idea that comes to my mind is to create just another texture and put the rest of the objects there. Here's a code I wrote a while ago. It doesn't work but I hope it will show you what I mean...
void SpriteSheet::mergeImages(GFXLoader::Directory &dir)
{
    sf::Vector2f totalTextureSize;
    sf::Vector2f currentPossition;

    for(auto &img: dir.img)
    {
        unsigned int w = img.second.getSize().x;
        unsigned int h = img.second.getSize().y;

        if(currentPossition.x + w < sf::Texture::getMaximumSize())
        {
            currentPossition.x += w;
            if(totalTextureSize.x < currentPossition.x) totalTextureSize.x = currentPossition.x;
        }
        else
        {
            currentPossition.x = 0;
            currentPossition.y += h;
            if(totalTextureSize.y < currentPossition.y) totalTextureSize.y = currentPossition.y;
        }

        if(totalTextureSize.y < h) totalTextureSize.y = h;
    }

    unsigned int img_w = totalTextureSize.x > sf::Texture::getMaximumSize()?
                sf::Texture::getMaximumSize() : totalTextureSize.x;
    unsigned int img_h = totalTextureSize.y > sf::Texture::getMaximumSize()?
                sf::Texture::getMaximumSize() : totalTextureSize.y;

    sf::Image bigImage;
    bigImage.create(img_w, img_h, sf::Color(0, 0, 0));

    unsigned int textureCount = 0;
    unsigned int maxHeight = 0;
    unsigned int sprId = 0;
    sf::Vector2f remaining;
    remaining.x = totalTextureSize.x;
    remaining.y = totalTextureSize.y;
    currentPossition.x = 0;
    currentPossition.y = 0;
    for(auto &img: dir.img)
    {
        unsigned int w = img.second.getSize().x;
        unsigned int h = img.second.getSize().y;
        SpriteInfo sprInfo;
        sprInfo.textureId = textureCount;

        if(h > maxHeight) maxHeight = h;

        if(currentPossition.x + w < sf::Texture::getMaximumSize())
        {
            bigImage.copy(img.second, currentPossition.x, currentPossition.x, sf::IntRect(0, 0, w, h));
            sprInfo.rect = sf::IntRect(currentPossition.x, currentPossition.x, w, h);
            sprites[sprId] = sprInfo;
            currentPossition.x += w;
            sprId++;
        }
        else if(currentPossition.y + h < sf::Texture::getMaximumSize())
        {
            currentPossition.x = 0;
            currentPossition.y += maxHeight;
            remaining.y -= maxHeight;
            bigImage.copy(img.second, currentPossition.x, currentPossition.y, sf::IntRect(0, 0, w, h));
            sprInfo.rect = sf::IntRect(currentPossition.x, currentPossition.y, w, h);
            sprites[sprId] = sprInfo;
            sprId++;
        }
        else
        {
            images.push_back(bigImage);
            sf::Texture tex;
            tex.loadFromImage(images[images.size() - 1]);
            textures.push_back(tex);
            currentPossition.x = 0;
            currentPossition.y = 0;
            if(remaining.y > 0)
            {
                img_w = totalTextureSize.x > sf::Texture::getMaximumSize()?
                        sf::Texture::getMaximumSize() : totalTextureSize.x;
                img_h = remaining.y > sf::Texture::getMaximumSize()?
                        sf::Texture::getMaximumSize() : remaining.y;
                bigImage.create(img_w, img_h, sf::Color(0, 0, 0));
                textureCount++;
            }
        }
    }
}
 

15
Graphics / Re: Is it good way to render a map?
« on: May 10, 2013, 06:25:02 pm »
I didn't but I will :P Thanks, I'll try to spell it correctly ;)

Pages: [1] 2