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

Pages: [1]
1
Graphics / Re: Stop drawing pixels out of region
« on: November 22, 2014, 02:30:32 pm »
Thanks, it works.
Here's my code. Maybe it will help someone.
View newV;
newV.setSize(size(vp));
newV.setCenter(globalCenter(vp));
newV.setRotation(getRotation());
Vector2f vpPos(getPosition().x / target.getSize().x, getPosition().y / target.getSize().y);
Vector2f vpSz((size(vp).x * getScale().x) / target.getSize().x, (size(vp).y * getScale().y) / target.getSize().y);
newV.setViewport(FloatRect(vpPos, vpSz));
target.setView(newV);
 

2
Graphics / Re: Stop drawing pixels out of region
« on: November 21, 2014, 07:23:47 pm »
It doesn't work or I do something wrong.
The whole problem is because I'm rendering a physics simulation that have its own coordinates.
And I want to render specified rect of physics simulation to specified rect on window (not on whole window)

// some useful inlines that are missing in SFML
inline Vector2f size(const FloatRect& rect)
{
    return Vector2f(rect.width, rect.height);
}

inline Vector2f pos(const FloatRect& rect)
{
    return Vector2f(rect.left, rect.top);
}

inline Vector2f localCenter(const FloatRect& rect)
{
    return size(rect) * 0.5f;
}

inline Vector2f globalCenter(const FloatRect& rect)
{
    return pos(rect) + localCenter(rect);
}

void WorldRender::draw(RenderTarget &target, RenderStates states) const
{
    //states.transform *= getTransform(); // i don't need Transformable transform because i use the same transform in view

// vp is source region of simulation

    // view set
    View oldV = target.getView();
    View newV;
newV.setRotation(getRotation());
    newV.setSize(size(vp));
    newV.setCenter(globalCenter(vp));
    newV.setViewport(FloatRect(getPosition(), Vector2f(size(vp).x * getScale().x, size(vp).y * getScale().y)));
    target.setView(newV);

// only for example    
CircleShape cs(50);
    cs.setPosition(0, 0);
    cs.setFillColor(Color::Black);
    target.draw(cs, states);

//...

target.setView(oldV); // restore old view
}
 
And in this case NOTHING renders. Even simple circle  (cs in my code).
But out of this function everything works properly.

3
Graphics / Re: Stop drawing pixels out of region
« on: November 21, 2014, 05:26:58 pm »
So i have another question: does view transformations applies on end of frame or for each draw()?
I'm asking it because i have idea:
virtual void draw(RenderTarget& target, RenderStates rs)
{
rs.transform *= getTransform();

View oldV = target.getView();

View newV;
// setting parameters of new view
target.setView(newV);

// draw my stuff

target.setView(oldV);
 
Does my stuff draw with newV wiew or oldV view?
Can I apply view on only a few of objects?

4
Graphics / Re: Stop drawing pixels out of region
« on: November 20, 2014, 07:29:34 pm »
Shaders isn't my best side :)

5
Graphics / Stop drawing pixels out of region
« on: November 20, 2014, 07:23:14 pm »
I'm drawing lots of rotated sprites.
And i have to draw some of this sprites partly - that ones, that are partly out of given region.
Briefly, I want do draw only that pixels of sprites that are in specified region.
Effect have to be like using sf::View but on single drawable object

PS I do it in my own Drawable component so i can't use sf::View::setViewport().

Any ideas?

6
Graphics / Re: Textured shape - texture not rotate (SFML + Box2d)
« on: February 16, 2014, 12:04:21 pm »
Must I use setOrigin() and setRotation()? Maybe can I use glRotatef() before creating shape?

/edit: It doesn't work.  :'(
I've got a screen like these:


My code:
// comments for this code are in first post
void Body::displayBody(RenderWindow &win, b2Body *b, Texture** fillTexture)
{
    bool autoInterpolate = true;

    int i = 0;
    for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext(), i++)
    {
        b2Shape* sh = f->GetShape();
        b2AABB aabb = f->GetAABB(0);
        Transform st = Transform::Identity;
        //st.rotate(RDtoDG(b->GetAngle()), B2toSF(aabb.GetCenter()));

        if(fillTexture[i] && autoInterpolate)
            fillTexture[i]->setSmooth(true);

        switch(sh->GetType())
        {
        case b2Shape::e_polygon:
        {          
            b2PolygonShape* s = dynamic_cast<b2PolygonShape*>(sh);
            ConvexShape cs(s->GetVertexCount());
            if(fillTexture[i])
                cs.setTexture(fillTexture[i]);
            else
                cs.setFillColor(Color::Red);

            cs.setOrigin(B2toSF(aabb.GetCenter() - aabb.lowerBound)); // GetCenter() is in body coordinates
            cs.setPosition(Vector2f() - cs.getOrigin()); // set left-top position to zero
            cs.rotate(RDtoDG(b->GetAngle()));

            for(int i = 0; i < s->GetVertexCount(); i++)
                cs.setPoint(i, B2toSF(b->GetWorldPoint(s->GetVertex(i))));
            win.draw(cs);
            break;
        }
        case b2Shape::e_circle:
        {
            static const float pi = 3.141592654f;
            b2CircleShape* s = dynamic_cast<b2CircleShape*>(sh);
            int pts = 30;
            ConvexShape cs(pts);
            if(fillTexture[i])
                cs.setTexture(fillTexture[i]);
            else
                cs.setFillColor(Color::Blue);

            cs.setOrigin(B2toSF(aabb.GetCenter() - aabb.lowerBound));
            cs.setPosition(B2toSF(b->GetWorldPoint(aabb.GetCenter())));
            cs.rotate(RDtoDG(b->GetAngle()));

            // calculating circle points manually
            float rad = B2toSF(s->m_radius);
            Vector2f center = B2toSF(b->GetWorldPoint(aabb.GetCenter()));
            for(int i = 0; i < pts; i++)
            {
                float angle = i * 2 * pi / pts - pi / 2;
                cs.setPoint(i, Vector2f(center.x + cos(angle) * rad, center.y + sin(angle) * rad));
            }
            win.draw(cs);
            break;
        }
        case b2Shape::e_edge:
        {
            b2EdgeShape* s = dynamic_cast<b2EdgeShape*>(sh);
            VertexArray va(sf::Lines, 2);
            va[0].position = B2toSF(b->GetWorldPoint(s->m_vertex1));
            va[0].color = Color::White;
            va[1].position = B2toSF(b->GetWorldPoint(s->m_vertex2));
            va[1].color = Color::White;

            win.draw(va);
            break;
        }
        case b2Shape::e_chain:
        {
            b2ChainShape* s = dynamic_cast<b2ChainShape*>(sh);

            for (int32 i = 0; i < s->GetChildCount(); ++i)
            {
                b2EdgeShape edge;
                s->GetChildEdge(&edge, i);
                VertexArray va(sf::Lines, 2);
                va[0].position = B2toSF(b->GetWorldPoint(edge.m_vertex1));
                va[0].color = Color::White;
                va[1].position = B2toSF(b->GetWorldPoint(edge.m_vertex2));
                va[1].color = Color::White;

                win.draw(va);
            }
            break;
        }
        }
    }
}
 

7
Graphics / Textured shape - texture not rotate (SFML + Box2d)
« on: February 15, 2014, 02:33:18 pm »
Hi!
I'm writing an offroad 2d game using Box2d and SFML. And i have got a problem with texturing objects - texture of the shape don't rotate with it ->
// B2toSF() and SFtoB2() convert units beetween pixels and Box2d
// RDtoDG() convert radians to degreeses
void Body::displayBody(RenderWindow &win, b2Body *b, const Texture** fillTexture)
// draws all fixtures in *b on win
// fillTexture is ptr to list of ptrs to textures to all fixtures, NULL is NO TEXTURING
{
    int i = 0;
    for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext(), i++)
    // for each fixtures in body
    {
         // shape and aabb of body - don't interest with it, it's box2d utility
        b2AABB aabb = f->GetAABB(0);
        b2Shape* sh = f->GetShape();
        RenderStates st; // transform for all shapes
       
        // rotate shape by center of its aabb
        st.transform.rotate(RDtoDG(b->GetAngle()), B2toSF(aabb.GetCenter()));

        switch(sh->GetType())
        {
        case b2Shape::e_polygon: // polygon shape
        {
            b2PolygonShape* s = dynamic_cast<b2PolygonShape*>(sh);
            ConvexShape cs(s->GetVertexCount());
            // shapes are NOT ROTATED, RenderStates::transform contains their rotation
            if(fillTexture[i])
                cs.setTexture(fillTexture[i]);
            else
                cs.setFillColor(Color::Red);
            for(int i = 0; i < s->GetVertexCount(); i++)
                cs.setPoint(i, B2toSF(b->GetWorldPoint(s->GetVertex(i))));
                // GetWorldPoint() translates local position to global coordinates
            win.draw(cs, st);
            break;
        }
        case b2Shape::e_circle: // circle
        {
            b2CircleShape* s = dynamic_cast<b2CircleShape*>(sh);
            CircleShape cs(B2toSF(s->m_radius));
            cs.setPosition(B2toSF(aabb.lowerBound)); // left-top of aabb
            if(fillTexture[i])
                    cs.setTexture(fillTexture[i]);
            else
                cs.setFillColor(Color::Blue);
            win.draw(cs, st);
            break;
        }
        case b2Shape::e_edge:
        // edge and chain cannot be textured
        {
            b2EdgeShape* s = dynamic_cast<b2EdgeShape*>(sh);
            VertexArray va(sf::Lines, 2);
            va[0].position = B2toSF(b->GetWorldPoint(s->m_vertex1));
            va[0].color = Color::White;
            va[1].position = B2toSF(b->GetWorldPoint(s->m_vertex2));
            va[1].color = Color::White;

            win.draw(va, st);
            break;
        }
        case b2Shape::e_chain: // chain is list of edges
        {
            b2ChainShape* s = dynamic_cast<b2ChainShape*>(sh);

            for (int32 i = 0; i < s->GetChildCount(); ++i) // for each edges in chain
            {
                // draw child edge
                b2EdgeShape edge;                
                s->GetChildEdge(&edge, i);
                VertexArray va(sf::Lines, 2);
                va[0].position = B2toSF(b->GetWorldPoint(edge.m_vertex1));
                va[0].color = Color::White;
                va[1].position = B2toSF(b->GetWorldPoint(edge.m_vertex2));
                va[1].color = Color::White;

                win.draw(va, st);
            }
            break;
        }
        }
    }
}
 
Is it a bug in SFML? Or is my code wrong?

8
Feature requests / Re: Drawable and Transformable class
« on: January 16, 2014, 04:23:30 pm »
And how i can draw it?
win.draw(*((Drawable*)transformablePtr));
???

This is a very bad code, because i don't know whether transformablePtr is Drawable.

9
Feature requests / Drawable and Transformable class
« on: January 16, 2014, 04:07:20 pm »
I think all drawable and transformable things could have the same parent class.
For example:
namespace sf
{
class Object
: sf::Drawable, sf::Transformable
{
};

// and all drawable and transformable classes derives it
// for exmaple:
class Sprite
: sf::Object
{
//...
};
 

I came up with this idea when i written SFML and Box2d framework, when I needed to write method returning drawable object which i can set it position.

Pages: [1]
anything