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

Author Topic: SFML backend for Guichan  (Read 5382 times)

0 Members and 1 Guest are viewing this topic.

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
SFML backend for Guichan
« on: January 11, 2013, 10:09:47 am »
Hi. Today I completed a working backend for Guichan that uses SFML. It's up on GitHub and you can find it here:

https://github.com/zackthehuman/guichan-sfml

Update: Here is the classic Guichan "widgets" example using SFML as the back end:



Update 2: Almost all features are working now!

Update 3: If you don't know what Guichan is, you can find out more here: https://code.google.com/p/guichan/. Guichan is a small GUI library and framework aimed at games. It has various backends (like OpenGL, HGE, SDL, etc.) and a collection of basic widgets (Button, Checkbox, Label, Image, etc.).

Original post:
Right now it is only the gcn::Graphics portion, I'll be working on getting input implemented in the next few days.

Over the years I have seen many people ask for this but never really any solution to getting SFML working with Guichan. Someone on the French forums posted their implementation, but it is now our of date if you're using SFML 2. There were also some problems associated with clip rectangles in that implementation.

I would really appreciate any feedback about this, especially about how it can be improved. I'm hoping that people will find this useful.

[attachment deleted by admin]
« Last Edit: January 24, 2013, 10:42:14 am by ZackTheHuman »
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML backend for Guichan
« Reply #1 on: January 11, 2013, 10:17:55 am »
Quote
        sf::VertexArray point(sf::Points, 1);

        point[0] = sf::Vertex(sf::Vector2f(static_cast<float>(x), static_cast<float>(y)), mSfmlColor);

        mTarget->draw(point);
sf::VertexArray is a std::vector<sf::Vertex>. It's a little overkill for one vertex ;)

Implement it like this:
sf::Vertex point(sf::Vector2f(static_cast<float>(x), static_cast<float>(y)), mSfmlColor);
mTarget->draw(&point, 1, sf::Points);

Same for lines and rectangles.

An efficient implementation for rectangles would look like this:

// outline

sf::Vertex rect[5] =
{
    sf::Vertex(sf::Vector2f(x, y), mSfmlColor),
    sf::Vertex(sf::Vector2f(x + w, y), mSfmlColor),
    sf::Vertex(sf::Vector2f(x + w, y + h), mSfmlColor),
    sf::Vertex(sf::Vector2f(x, y + h), mSfmlColor),
    sf::Vertex(sf::Vector2f(x, y), mSfmlColor)
};

mTarget->draw(rect, 5, sf::Lines);

// filled

sf::Vertex rect[4] =
{
    sf::Vertex(sf::Vector2f(x, y), mSfmlColor),
    sf::Vertex(sf::Vector2f(x + w, y), mSfmlColor),
    sf::Vertex(sf::Vector2f(x + w, y + h), mSfmlColor),
    sf::Vertex(sf::Vector2f(x, y + h), mSfmlColor)
};

mTarget->draw(rect, 4, sf::Quads);

For clipping, you're not supposed to set OpenGL states directly and expect SFML to use them. It might be broken in the future. Until clipping masks are implemented in SFML, you can use a workaround: apply a view (sf::View) whose size and viewports are both set to the clipping rectangle (remember that viewports are defined in normalized coordinates).

As a bonus, if you use a view for clipping, you can avoid applying an offset to everything which is drawn.
« Last Edit: January 11, 2013, 10:23:09 am by Laurent »
Laurent Gomila - SFML developer

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #2 on: January 11, 2013, 10:30:22 am »
Wow, Laurent, that was fast.

Thanks for the suggestions. I'm glad to hear that using sf::Views should solve some of my weird clipping problems. Getting the clipping rectangles (and really glScissor) to work with resized windows and different views was a real pain.

And I agree that the sf::VertexArray stuff is overkill for such simple geometry -- I had never actually used sf::Vertex so it was entertaining to try something new.
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #3 on: January 11, 2013, 10:54:35 pm »
Based on Laurent's feedback I have updated the SGMLGraphics class to use a series of sf::View objects to perform clipping. I'm particularly interested in seeing if my implementation is "good" or not. I simply construct a new sf::View from a ClipRectangle and then scale its viewport by factors of the "original" view. The "original" view is saved before any drawing occurs and the restored after.

https://github.com/zackthehuman/guichan-sfml/blob/master/src/guichan/sfml/sfmlgraphics.cpp#L224

Quote
As a bonus, if you use a view for clipping, you can avoid applying an offset to everything which is drawn.

This is for the most part true, but I still have to add offsets since each Widget's coordinates are relative to their parent.

Thanks for the tips!
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #4 on: January 19, 2013, 01:57:06 am »
Update: I have re-implemented how lines and points are drawn. Using "real" OpenGL primitives didn't yield the desired effect. Since several of the existing backends for Guichan are raster-based and have direct pixel access, scaling them up creates aliasing that may be desired (for my game it is desired). For my backend the graphics were too smooth and didn't look right. This was the most noticeable when drawing points and single lines; so I've updated my backend to draw "faux" pixels using a quad. Points and straight lines are drawn this way; and non-straight lines are simply draw using faux pixels and Bresenham's line algorithm. The effect is much better than before. I believe the performance cost is minimal unless you're drawing lots and lots of random lines and points.

You can see specifically those changes here:

https://github.com/zackthehuman/guichan-sfml/blob/5e7508eb1912e3ef9e68b53f85f92476f01d4f90/src/guichan/sfml/sfmlgraphics.cpp

Quote
    void SFMLGraphics::drawLine(int x1, int y1, int x2, int y2)
    {
        if (y1 == y2)
        {
            drawHorizontalLine(x1, y1, x2);
            return;
        }
        else if (x1 == x2)
        {
            drawVerticalLine(x1, y1, y2);
            return;
        }
        else
        {
            drawBresenham(x1, y1, x2, y2);
            return;
        }
    }

Quote
    void SFMLGraphics::_drawFauxPixel(int x, int y) {
        float px = static_cast<float>(x);
        float py = static_cast<float>(y);

        sf::Vertex rect[4] =
        {
            sf::Vertex(sf::Vector2f(px, py), mSfmlColor),
            sf::Vertex(sf::Vector2f(px + 1, py), mSfmlColor),
            sf::Vertex(sf::Vector2f(px + 1, py + 1), mSfmlColor),
            sf::Vertex(sf::Vector2f(px, py + 1), mSfmlColor)
        };

        mTarget->draw(rect, 4, sf::Quads);
    }

Other changes include the addition of SFMLInput so now SFML events generate Guichan events for keyboard, mouse, and application focus. The mouse events are also normalized so if your window is re-sized the events still line up where Guichan widgets expect them to be. I've also implemented very basic support for SFML Font objects as Guichan fonts.
« Last Edit: January 19, 2013, 01:59:32 am by ZackTheHuman »
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #5 on: January 24, 2013, 12:40:10 am »
As of the latest revision (https://github.com/zackthehuman/guichan-sfml/commit/48e2ee5d639338dbe5cde2fe077ba5aa4c43634c) sf::Fonts are now working correctly and gcn::Alignment is working as well. This pretty much makes the library feature complete, except for getPixel/setPixel in gcn::SFMLGraphics.

Now if only the Guichan developers still worked on the project...
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: SFML backend for Guichan
« Reply #6 on: January 24, 2013, 12:51:55 am »
zack, i can't find much info about guichan online. could u post some screenshots of it running in SFML? or maybe a windows binary? ;)

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #7 on: January 24, 2013, 01:03:33 am »
eigenbom, I sure can! I suppose I should have put some more info about Guichan in my original post... The out-of-the-box components that come with Guichan aren't that amazing -- but the framework is simple and easy to extend. As I implement components of my own I can also post screen shots to show it being SFML-powered!

Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: SFML backend for Guichan
« Reply #8 on: January 24, 2013, 01:58:51 am »
yeh that'd be sweet :)

ZackTheHuman

  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: SFML backend for Guichan
« Reply #9 on: January 24, 2013, 10:43:49 am »
yeh that'd be sweet :)

I updated my first post with the canonical "widget example" from Guichan. The basic example is not visually stunning but it does show a variety of widgets and capabilities of the library. When I implement my own widgets I'll post more images.
Project Hikari Dev Blog: http://hikari.zackthehuman.com/blog/

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: SFML backend for Guichan
« Reply #10 on: January 24, 2013, 11:08:03 pm »
Cheers! Have you looked at SFUI and TGUI btw?

 

anything