-
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.
-
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.
-
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?
-
Where are state.transform and state.texture set to null?
-
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?
-
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;
}
-
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
-
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.
-
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;
}
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
Confirmed now that it all works thank you very much, Laurent feel free to move this over to the D catagory if you see fit. Thanks again to you both!