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

Author Topic: [Solved] RenderState proper usage?  (Read 17216 times)

0 Members and 1 Guest are viewing this topic.

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
[Solved] RenderState proper usage?
« on: November 21, 2012, 10:26:25 am »
Heyo quick question about sfRenderState in the C bindings. I know in the C++ bindings you can provide a shader for example rather then the entire RenderState when drawing something but in CSFML you have to provide i complete sfRenderState or null.

My question is what should be in that sfRenderState along with the shader, following the example, to make it work. I have been trying to get a shader to work myself in CSFML but no matter what i do it crashes when i call the draw function so i thought that it may actually be the sfRenderState that is causing the crash.

Appreciated, Nekroze.
« Last Edit: November 22, 2012, 03:38:22 pm by Nekroze »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderState proper usage?
« Reply #1 on: November 21, 2012, 10:36:11 am »
I should probably add a sfRenderStates_default function/variable.

The default values are simple: use NULL for transform/shader/texture, and sfBlendAlpha for the blend mode.
Laurent Gomila - SFML developer

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #2 on: November 21, 2012, 11:41:52 am »
Hmmph. Must be something i am doing wrong then. Here is my test case as simple as i can get it and as close to C as possible (i use D so ignore the .ptr it just converts string to char* and the switch/case/default may be a little different but that should be it in the way of differences).

        const sfContextSettings settings;
        sfRenderWindow* win = sfRenderWindow_create(sfVideoMode(800,600,32), "SFML Test", sfDefaultStyle, &settings);

        sfImage* fg = sfImage_createFromColor(400, 300, sfGreen);
        sfTexture* fgt = sfTexture_createFromImage(fg, null);
        sfSprite* fgs = sfSprite_create();
        fgs.sfSprite_setTexture(fgt, 0);

        sfRenderStates state;
       
        string frag = r"
                void main() {
                gl_FragColor = vec4(1,0,0,1);
                }"
;

        sfShader* shader = sfShader_createFromMemory(null, frag.ptr);
        state.shader = shader;
        state.blendMode = sfBlendAlpha;

        sfEvent event;
        while (sfRenderWindow_isOpen(win)) {
                while (sfRenderWindow_pollEvent(win, &event)) {
                        switch (event.type) {
                                case sfEvtClosed:
                                        sfRenderWindow_close(win);
                                        continue;
                                default:
                        }
                }
                sfRenderWindow_clear(win, sfBlack);

                //sfRenderWindow_drawSprite(win, fgs, &state); //Causes crash
                sfRenderWindow_drawSprite(win, fgs, null);

                sfRenderWindow_display(win);
        }

So down by the last few lines, when i render the sprite with the state it instantly crashes however if i draw it without the state then it works and draws a green rect up the top left of the screen. If the state where to work along with the shader then it would draw a red rect instead.

What am i doing wrong here?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderState proper usage?
« Reply #3 on: November 21, 2012, 12:05:29 pm »
Where are state.transform and state.texture set to null?
Laurent Gomila - SFML developer

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #4 on: November 21, 2012, 12:10:35 pm »
ah silly me forgot that setting them to null is not the same as setting them to nothing. It doesnt crash now however it shows only black, why is that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderState proper usage?
« Reply #5 on: November 21, 2012, 12:27:20 pm »
This C++ equivalent works, so there might be a bug in CSFML. I'll investigate when I have more time.

int main()
{
    sf::RenderWindow win(sf::VideoMode(800, 600, 32), "SFML Test");

    sf::Image fg; fg.create(400, 300, sf::Color::Green);
    sf::Texture fgt; fgt.loadFromImage(fg);
    sf::Sprite fgs(fgt);

    const char* frag = "void main() {gl_FragColor = vec4(1,0,0,1);}";
    sf::Shader shader; shader.loadFromMemory(frag, sf::Shader::Fragment);

    sf::RenderStates state;
    state.shader = &shader;
    state.blendMode = sf::BlendAlpha;
    state.transform = sf::Transform::Identity;
    state.texture = NULL;

    while (win.isOpen())
    {
        sf::Event event;
        while (win.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                win.close();
        }

        win.clear();
        win.draw(fgs, state);
        win.display();
    }

    return EXIT_SUCCESS;
}
« Last Edit: November 21, 2012, 01:07:31 pm by Laurent »
Laurent Gomila - SFML developer

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #6 on: November 21, 2012, 12:30:18 pm »
ah, ok then. Thanks a heap Laurent your assistance always swift and useful. Now i can get on with trying my shader lighting and vertex array tilemaps!

Does sf::Transform::Identity really do the same thing as null in the c version? other then that its the same so i have no idea why it would be different. Aleast when you give an example so like mine i know that its not something i have broken this time  :P
« Last Edit: November 21, 2012, 12:36:03 pm by Nekroze »

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #7 on: November 21, 2012, 02:43:10 pm »
Update, i was wrong it does show a black screen and doesn't instantly crash like before however it does crash the moment i move the mouse over the window the same way as before.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderState proper usage?
« Reply #8 on: November 21, 2012, 10:03:37 pm »
The following C code works with the latest sources. Have you tried them, or do you use the RC?

#include <SFML/Audio.h>
#include <SFML/Graphics.h>

int main()
{
    const char* frag;
    sfShader* shader;
    sfRenderWindow* win;
    sfImage* fg;
    sfTexture* fgt;
    sfSprite* fgs;
    sfRenderStates state;
    sfEvent event;
    sfVideoMode mode = {800, 600, 32};

    win = sfRenderWindow_create(mode, "SFML Test", sfDefaultStyle, NULL);

    fg = sfImage_createFromColor(400, 300, sfGreen);
    fgt = sfTexture_createFromImage(fg, NULL);
    fgs = sfSprite_create();
    sfSprite_setTexture(fgs, fgt, 0);
   
    frag = "void main() {gl_FragColor = vec4(1,0,0,1);}";
    shader = sfShader_createFromMemory(NULL, frag);

    state.shader = shader;
    state.blendMode = sfBlendAlpha;
    state.transform = sfTransform_Identity;
    state.texture = NULL;

    while (sfRenderWindow_isOpen(win)) {
        while (sfRenderWindow_pollEvent(win, &event)) {
            switch (event.type) {
                case sfEvtClosed:
                    sfRenderWindow_close(win);
                    continue;
            }
        }
        sfRenderWindow_clear(win, sfBlack);

        sfRenderWindow_drawSprite(win, fgs, &state);
        /*sfRenderWindow_drawSprite(win, fgs, NULL);*/

        sfRenderWindow_display(win);
    }

    return 0;
}
Laurent Gomila - SFML developer

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #9 on: November 22, 2012, 02:18:42 am »
huh, sfTransform_Identity doesnt exist in the D binding which is odd they where updated a week ago. I am using the latest sources that i compiled myself, 2-3 weeks ago however, and the documentation that it built does had sfTransform_Identity. I will file an  issue report with the derelict3 bindings but in the mean time is there any way to replicate sfTransform_Identity myself? I hope thats the only problem.

Sorry that this has now fallen a bit into the D category rather then C but i was originally doing something wrong with the C binding which derelict 3 mirrors.

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #10 on: November 22, 2012, 05:39:43 am »
It turns out that the D bindings aparently cannot support an instance for sfTransform_Identity so i need to define it myself, how would i do that. I havent looked into transforms yet.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: RenderState proper usage?
« Reply #11 on: November 22, 2012, 08:12:03 am »
identity = sfTransform_fromMatrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

It seems that I was wrong, sfRenderStates contain a sfTransform, not a sfTransform*. It was changed recently, but it's definitely older than 3 weeks. If you have the version that takes a sfTransform*, then null is a valid argument for the identity transform.
Laurent Gomila - SFML developer

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #12 on: November 22, 2012, 09:13:11 am »
I have the version that does not take a sfTransform* but just a normal sfTransform so i am up to date.

Changing the states transform to this:
state.transform = sfTransform_fromMatrix(1, 0, 0, 0, 1, 0, 0, 0, 1);

Still doesn't work, one step back actually, now it instantly crashes like it did before hand. As incorrect as setting it to null if its not a pointer is, at least that made it display blackness until the mouse touched the window.

With the chance this is actually D specific i will link my issue report to the Derelict3 bindings to this thread.

aldacron

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
Re: RenderState proper usage?
« Reply #13 on: November 22, 2012, 10:57:44 am »
I have the version that does not take a sfTransform* but just a normal sfTransform so i am up to date.

You were up to date on the C side, but the binding, unfortunately, did not track the changes to sfTransform. It was still declared as an opaque type. I've gone through and made the relevant corrections and added a constant sfTransform_Identity, so you should be able to do this now:

state.shader = shader;
state.blendMode = sfBlendAlpha;
state.transform = sfTransform_Identity;
state.texture = NULL;
 

Hope it works for you now.
« Last Edit: November 22, 2012, 02:39:04 pm by Laurent »

Nekroze

  • Newbie
  • *
  • Posts: 30
    • View Profile
Re: RenderState proper usage?
« Reply #14 on: November 22, 2012, 02:33:05 pm »
awesome, thanks so much for the support. Both of your works and support have been invaluable to me and i really appreciate everything you are both doing.

I will update my bindings and give this a test as soon as i can get back to my work terminal and report back.