Hello,
I'm kinda stuck with the tutorial example in the C::B installation. I want to reproduce it but with CSFML.
Here's the code:
#include <SFML/Graphics.h>int main
(){ sfContextSettings
* settings
; settings
= malloc(sizeof(sfContextSettings
)); settings
->antialiasingLevel
= 8; sfVideoMode mode
; mode.
height = 200; mode.
width = 200; sfRenderWindow
* renderwindow
; sfCircleShape
* shape
; renderwindow
= sfRenderWindow_create
(mode
, "SFML works!", sfDefaultStyle
,settings
); shape
= sfCircleShape_create
(); sfCircleShape_setFillColor
(shape
, sfGreen
); while (sfRenderWindow_isOpen
(renderwindow
)) { sfEvent
* event
; event
= malloc(sizeof(sfEvent
)); while (sfRenderWindow_pollEvent
(renderwindow
, event
)) { if (event
== sfEvtClosed
) { sfRenderWindow_close
(renderwindow
); } } //sfRenderStates * state; //state = malloc(sizeof(sfRenderStates)); //state->blendMode = sfBlendNone; //state->shader = NULL; //state->texture = NULL. //state->transform = NULL; sfRenderWindow_clear
(renderwindow
, sfWhite
); sfRenderWindow_drawCircleShape
(renderwindow
, shape
, state
); sfRenderWindow_display
(renderwindow
); } return 0;}
The problem is that I do not know how to display a circle shape without having to make sfRenderSettings because I do not need shaders, textures, blendness or transform.
Also can you tell me if something is wrong in my code? it compiles and runs but crashes (make a black window, even if I change the sfBlack to sfWhite/Green/... ).
Thanks in advance.
First off, you should add code tags to make code more readable for people on the forums.
To answer your question, you can actually just pass NULL instead of a "default" sfRenderStates. This tells the SFML inside CSFML to use a default sf::RenderStates. And you can do the same with sfConvexSettings too when you create your window, by the way.
sfRenderWindow_drawCircleShape(renderwindow, shape, NULL);
And just so you know, instead of allocating memory manually for a pointer, I would just create the object normally and then pass the address of said object into the function that wants it. For example,
while (sfRenderWindow_isOpen(renderwindow))
{
sfEvent event;
while (sfRenderWindow_pollEvent(renderwindow, &event))
{
//get events
}
//do stuff
}
Otherwise, you'll need to free that memory manually too. Since your current code doesn't, you've got leaks!
Yes, that's true, I forgot to free() and destroy()...
Here is the last version of the program as you recommend, with passing the addresses of objects
#include <SFML/Graphics.h>
int main()
{
sfVideoMode mode = {200, 200, 32};
sfRenderWindow * renderwindow;
sfCircleShape * shape;
shape = sfCircleShape_create();
sfCircleShape_setRadius(shape, 100.f);
renderwindow = sfRenderWindow_create(mode, "SFML works!", sfDefaultStyle ,NULL);
sfCircleShape_setFillColor(shape, sfGreen);
while (sfRenderWindow_isOpen(renderwindow))
{
sfEvent event;
while (sfRenderWindow_pollEvent(renderwindow, &event))
{
if (event.type == sfEvtClosed)
{
sfRenderWindow_close(renderwindow);
}
}
sfRenderWindow_clear(renderwindow, sfBlack);
sfRenderWindow_drawCircleShape(renderwindow, shape, NULL);
sfRenderWindow_display(renderwindow);
}
sfCircleShape_destroy(shape);
sfRenderWindow_destroy(renderwindow);
return 0;
}
The program compiles with no warnings or errors but crashes. :'(
EDIT: Resolved it, there was some problem with the sfVideoMode. Enjoy. I'm sure it might help some people :)