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 - DevilEdge

Pages: [1] 2
1
Ok, so I'm getting all of the coordinates correctly now. I'm able to keep the view within the map, but I'm having trouble allowing the camera to move one direction with the player but not the other. An example would be if the player is on the very left side of the map (so the x-direction of the camera stays within the map), but the y-axis of the viewport would move up and down. Here's some of the code I was trying to use, but it's not working as expected:

    static bool regularLeft = true;
        static bool regularUp = true;
        static bool regularRight = true;
        static bool regularDown = true;

        static auto xPos = 0.f;
        static auto yPos = 0.f;

        if (topLeft.x <= 0.f)
        {
                if (xPos == 0.f)
                        xPos = m_spriteToFollow->getPosition().x;

                regularLeft = false;

                if (m_spriteToFollow->getPosition().x > xPos)
                        regularLeft = true;

                else
                {
                        if ((regularUp && !regularDown) || (!regularUp && regularDown))
                                m_view.setCenter(xPos, m_spriteToFollow->getPosition().y);

                        else
                                m_view.setCenter(xPos, yPos);
                }
        }

        if (topLeft.y <= 0.f)
        {
                if (yPos == 0.f)
                        yPos = m_spriteToFollow->getPosition().y;

                regularUp = false;

                if (m_spriteToFollow->getPosition().y > yPos)
                        regularUp = true;

                else
                {
                        if ((regularLeft && !regularRight) || (!regularLeft && regularRight))
                                m_view.setCenter(m_spriteToFollow->getPosition().x, yPos);

                        else
                                m_view.setCenter(xPos, yPos);
                }
        }

        if (regularLeft && regularUp && regularRight && regularDown)
                m_view.setCenter(m_spriteToFollow->getPosition());

This is within my Camera::update() method.

2
You only need top-left and bottom-right (they store all four edges). If you want the other corners, you can create from those two.
Bottom-right is simply top-left + size, thus:
bottomRight = topLeft + view.getSize();

You could also get top-left a bit simpler by mapping (0, 0) to coords instead of the view's centre, thus:
topLeft = window.mapPixelToCoords(sf::Vector2i(0, 0));

The other option for bottom-right is to map it directly, rather than an offset from top-left, thus:
bottomRight = window.mapPixelToCoords(sf::Vector2i(window.getSize()));
Notice that it's the size of the window, not the size of the view.

Thanks a lot, I have it all solved now.

3
Hi there,

For my project, I'm trying to restrict the player view area so they can't see the empty space of outside of the map. I am getting the top left coordinate correct, but for the other 3 I'm a bit confused on how to get them. Here's the code I'm trying to use:

auto viewCenterWorldCoords = window.mapPixelToCoords(sf::Vector2i(m_view.getCenter().x, m_view.getCenter().y));

m_topLeft.x = viewCenterWorldCoords.x - (m_view.getSize().x / 2);
        m_topLeft.y = viewCenterWorldCoords.y - (m_view.getSize().y / 2);
m_bottomLeft = sf::Vector2f(m_topLeft.x, viewCenterWorldCoords.y(viewCenterWorldCoords.y - (m_view.getSize().y / 2)));
        m_topRight = sf::Vector2f((viewCenterWorldCoords.x + (m_view.getSize().x / 2)), m_topLeft.y);
        m_bottomRight = sf::Vector2f(m_topRight.x, (viewCenterWorldCoords.y + (m_view.getSize().y / 2)));

Thanks for any help!

4
Window / Re: No events are being caught with SFML 2.1 + Win32?
« on: October 17, 2014, 12:54:58 pm »
Thanks guys, I resolved the issue.

5
Window / No events are being caught with SFML 2.1 + Win32?
« on: October 17, 2014, 03:32:32 am »
Hi there,

I have the following code:

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName  = NULL;
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "Test",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    Msg.message = static_cast<UINT>(~WM_QUIT);

    SE::Engine engine(hwnd);

        //For finding delta time
        sf::Clock deltaClock;

        sf::Time lastUpdate = sf::Time::Zero;

        //60fps max
        sf::Time timePerFrame = sf::seconds(1.f / 60.f);

    while (Msg.message != WM_QUIT)
    {
        if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
        {
            // If a message was waiting in the message queue, process it
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }

        else
        {
            while (engine.running())
            {
                lastUpdate += deltaClock.restart();

                while (lastUpdate > timePerFrame)
                {
                    lastUpdate -= timePerFrame;

                    engine.update(timePerFrame);
                    engine.render();
                }
            }
        }

ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    return Msg.wParam;
    }

And engine.update(timePerFrame) is basically along the lines of this:

sf::Event evt;

while (SceneManager::getWindow().pollEvent(evt))
            m_input_manager.update(dt, evt);
}

I tried referencing several posts and the integrating SFML 1.6 into Win32, but they're dated and I can't seem to get it to work. If anyone has any ideas I would be extremely grateful.

Thanks

6
General / Is it possible to use Windows File Dialog Boxes/Menus with SFML?
« on: September 21, 2014, 04:37:45 am »
Hi there,

For the program that I've been working on, I need the user to be able to traverse through the system's filesystem to find image files to be used by the program. In Windows API there's functions for this, and is it possible to use it with SFML windows? If not, what other options could I look at?

Thanks

7
Graphics / Re: Having trouble black lines with tilemaps - again
« on: August 22, 2014, 12:08:56 am »
Ok I just tested the sample version again, and I don't get the lines. So that leaves me to conclude that something about how I'm using my views is causing the problem. Here's the relevant code in my player's view:

Player.cpp:

Player::Player(const std::string& id, bool active) :
                m_viewArea(sf::Vector2f(100, 100))
        {
                m_texture.loadFromFile("Resources/player.png");
                m_sprite.setTexture(m_texture);
                m_sprite.setPosition(350, 300);

                registerAttribute("renderable", new bool(true));
                registerAttribute("health", new int(20));

                SceneManager::getView().setCenter(m_sprite.getPosition());
                SceneManager::getView().setSize(m_viewArea);
        }

bool Player::nextPosValid(const sf::Vector2f& offset)
        {
                sf::Vector2f offsettedPos(m_sprite.getPosition().x + offset.x, m_sprite.getPosition().y + offset.y);

                //The bounding rectangle of the future player
                sf::FloatRect rect(offsettedPos, sf::Vector2f(m_sprite.getGlobalBounds().width, m_sprite.getGlobalBounds().height));

                if (SceneManager::getWorld().getCurrentCell()->getTileMap().collidesWithType(2, rect))
            return false;

                return true;
        }

void Player::move(const sf::Vector2f& offset)
        {
                m_sprite.move(offset);
                SceneManager::getView().setCenter(m_sprite.getPosition());
        }

        void Player::update(sf::Time dt)
        {
                if (InputManager::isActive(User_Input::UPHOLD))
                {
                        if (nextPosValid(sf::Vector2f(0, -100 * dt.asSeconds())))
                                move(sf::Vector2f(0, -100 * dt.asSeconds()));
                }

                if (InputManager::isActive(User_Input::LEFTHOLD))
                {
                        if (nextPosValid(sf::Vector2f(-100 * dt.asSeconds(), 0)))
                                move(sf::Vector2f(-100 * dt.asSeconds(), 0));
                }

                if (InputManager::isActive(User_Input::DOWNHOLD))
                {
                        if (nextPosValid(sf::Vector2f(0, 100 * dt.asSeconds())))
                                move(sf::Vector2f(0, 100 * dt.asSeconds()));
                }

                if (InputManager::isActive(User_Input::RIGHTHOLD))
                {
                        if (nextPosValid(sf::Vector2f(100 * dt.asSeconds(), 0)))
                                move(sf::Vector2f(100 * dt.asSeconds(), 0));
                }

                if (InputManager::isActive(User_Input::PPRESS))
                {
                        getAttribute<int>("health") -= 5;

                        std::cout << "Player health is now: " << getAttribute<int>("health") << std::endl;
                }

                if (InputManager::isActive(User_Input::VHOLD))
                {
                        if (SceneManager::getView().getSize().x < 300 ||
                                SceneManager::getView().getSize().y < 300)
                                SceneManager::getView().setSize(sf::Vector2f(SceneManager::getView().getSize().x + 0.5, SceneManager::getView().getSize().y + 0.5));
                }

                else if (InputManager::isActive(User_Input::IHOLD))
                {
                        if (SceneManager::getView().getSize().x >= 100 ||
                                SceneManager::getView().getSize().y >= 100)
                                SceneManager::getView().setSize(sf::Vector2f(SceneManager::getView().getSize().x - 0.5, SceneManager::getView().getSize().y - 0.5));
                }

                if (getAttribute<int>("health") <= 0)
                        setAttribute("renderable", new bool(false));

                setPosition(m_sprite.getPosition());
                setBounds(m_sprite.getGlobalBounds());
        }

8
Graphics / Re: Having trouble black lines with tilemaps - again
« on: August 20, 2014, 07:52:46 pm »
class TileMap : public sf::Drawable, public sf::Transformable
{
public:

    bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
    {
        // load the tileset texture
        if (!m_tileset.loadFromFile(tileset))
            return false;

        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // populate the vertex array, with one quad per tile
        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);
            }

        return true;
    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the transform
        states.transform *= getTransform();

        // apply the tileset texture
        states.texture = &m_tileset;

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
};

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tilemap");

    // define the level with an array of tile indices
    const int level[] =
    {
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 1, 1,
        0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 1, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 1, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };

    // create the tilemap from the level definition
    TileMap map;
    if (!map.load("spritesheet.png", sf::Vector2u(32, 32), level, 16, 8))
        return -1;

    // run the main loop
    while (window.isOpen())
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        // draw the map
        window.clear();
        window.draw(map);
        window.display();
    }

    return 0;
}

You can go ahead and use the tilemap I supplied in the first post.

Thanks!

9
Graphics / Re: Having trouble black lines with tilemaps - again
« on: August 19, 2014, 11:57:28 pm »
black lines? they seem more like blue lines to me.
try filling the whole map with the lava tile.

edit: there's nothing wrong in your tileset. are you smoothing the texture?

You're right, they are more blue, my bad. I just tried filling the whole map with the lava tile, and the same blue line error occurs. Regards to smoothing, I'm not smoothing it, but I tested it to see if it did anything, and boy it did. Instead of making random lines like in the video, and creates a clear space in between each tile with the blue line.

10
Graphics / Re: Having trouble black lines with tilemaps - again
« on: August 19, 2014, 07:56:17 pm »
I'm not seeing the tilemap attachment for some reason ... :-(

Oops, forgot to attach it haha. It should be attached now.

I can answer a couple of the questions and give some info:

1. My graphics drivers are 100% up-to-date (I have a Nvidia GTX 760)
2. I tried the program on my brother's Intel HD 3000 machine, and the same error occurs.
3. I haven't tried it on any other OS, and I don't have the resources to do so unfortunately.
4. I'm using PNG right now, I can try others as well later.
5. The same things occurs on both VS 13 and CodeBlocks 13.12.
6. This occurs on both release and debug
7. I haven't done either, I will do that soon.
8. Nope, not using any multithreading.

11
Graphics / Having trouble black lines with tilemaps - again
« on: August 19, 2014, 07:27:25 pm »
Hi there, some of you may remember my earlier post about the same thing, and at the end I thought I had solved it. Unfortunately, it didn't solve my problem, and it's really starting to frustrate me. I've made a video describing my problem here: https://www.youtube.com/watch?v=km5j22YYr-Q&feature=youtu.be

As you can see, there's black lines that are inconsistently spread out across the tiles, and make the whole map look extremely ugly. My tilemap code is exactly the same as the SFML example, so what I'm wondering is if my tileset being created incorrectly is causing the issue. I've attached the tilemap below, and I'd be so grateful if someone where to test it on a machine of theirs using the tilemap and seeing if they get the same results. If this isn't the problem, any help getting started in the right direction would be great as well.

Thanks!

12
Hi there,

If I were to use the SFML tilemap example, how does the spritesheet containing all of the tiles need to be like? For example, what's the padding between the tiles, etc. Also, if anyone can recommend any good tools for creating tiles/spritesheets that'd be awesome as well.

Thanks!

13
Graphics / Re: Line artifacts when drawing tilemap
« on: August 18, 2014, 05:33:00 pm »
I finally (somewhat) resolved the issue. VS 13 keeps giving me the bug even with the simple example, but CodeBlocks 13.12 doesn't, so I'm sticking with codeblocks. Thanks to anyone who tried to help!

14
Graphics / Re: Line artifacts when drawing tilemap
« on: August 17, 2014, 05:58:50 pm »
The limit has now been changed and you should be able again to attache things.

Thank you!

And for the people that were kind enough to try to help me, I've attached the spritesheet that I'm using in my engine.

15
Graphics / Re: Line artifacts when drawing tilemap
« on: August 17, 2014, 04:44:53 pm »
class TileMap : public sf::Drawable, public sf::Transformable
{
public:

    bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
    {
        // load the tileset texture
        if (!m_tileset.loadFromFile(tileset))
            return false;

        // resize the vertex array to fit the level size
        m_vertices.setPrimitiveType(sf::Quads);
        m_vertices.resize(width * height * 4);

        // populate the vertex array, with one quad per tile
        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);
            }

        return true;
    }

private:

    virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
    {
        // apply the transform
        states.transform *= getTransform();

        // apply the tileset texture
        states.texture = &m_tileset;

        // draw the vertex array
        target.draw(m_vertices, states);
    }

    sf::VertexArray m_vertices;
    sf::Texture m_tileset;
};

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(512, 256), "tilemap");

    // define the level with an array of tile indices
    const int level[] =
    {
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 0, 0, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 1, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 1, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 1, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };

    // create the tilemap from the level definition
    TileMap map;
    if (!map.load("spritesheet.png", sf::Vector2u(32, 32), level, 16, 8))
        return -1;

    // run the main loop
    while (window.isOpen())
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                window.close();
        }

        // draw the map
        window.clear();
        window.draw(map);
        window.display();
    }

    return 0;
}

I wanted to attach the spritesheet that I'm using, but unfortunately it doesn't let me attach it to the post. For some reason it says that the upload folder is "full" and I need to contact an administrator, even though the file is only 136 bytes.

Thanks!

Pages: [1] 2