Hi,
1) positionning in the window area using sfView resolve the first problem, thanks for the solution!
2) using convex shapes instead of concave ones still produces the same error when the outline is omitted.
If you compile with this macro, the shape is drawn right:
#define WITH_OUTLINE 1If you compile with this macro (omitting outline), then the convex shapes are not drawn:
#define WITH_OUTLINE 0#include <SFML/Graphics.h>
#include <string.h>
#include <stdio.h>
sfVector2f cross1[] = {{10.0, 1.0}, { 10.0, -1.0}, {-10.0, -1.0}, {-10.0, 1.0}};
sfVector2f cross2[] = {{1.0, -10.0}, {-1.0, -10.0}, {-1.0, 10.0}, { 1.0, 10.0}};
typedef struct
{
unsigned int len;
sfVector2f *points;
} path;
#define path_struct(data) \
{ (sizeof(data) / sizeof(data[0])), data }
char *buttons_string(unsigned char *buttons)
{
unsigned int i, j;
static char str[sfJoystickButtonCount * 2 + 1];
for (i = 0, j = 0; i < sfJoystickButtonCount; i++) {
if (buttons[i]) {
str[j] = '0' + i;
str[j+1] = ' ';
j += 2;
}
}
str[j] = 0;
return str;
}
sfVector2f position(sfRenderWindow *app, sfVector2f pos)
{
sfVector2f vec;
const sfView *view = sfRenderWindow_getView(app);
sfVector2f size = sfView_getSize(view);
float hw = size.x / 2.0f;
float hh = size.y / 2.0f;
vec.x = hw + ((pos.x / 100.0f) * hw);
vec.y = hh + ((pos.y / 100.0f) * hh);
return vec;
}
void display(
sfRenderWindow *app,
sfShape *shape1,
sfShape *shape2,
sfVector2f pos,
sfText* text,
unsigned char *buttons)
{
sfText_setString(text, buttons_string(buttons));
sfShape_setPosition(shape1, position(app, pos));
sfShape_setPosition(shape2, position(app, pos));
sfRenderWindow_clear(app, sfBlack);
sfRenderWindow_drawText(app, text, NULL);
sfRenderWindow_drawShape(app, shape1, NULL);
sfRenderWindow_drawShape(app, shape2, NULL);
sfRenderWindow_display(app);
}
static unsigned int
my_sfShapeGetPointCountCallback(void *data)
{
return (((path *)data)->len);
}
static sfVector2f
my_sfShapeGetPointCallback(unsigned int i, void *data)
{
return (((path *)data)->points)[i];
}
void get_joystick(sfEvent *event, sfVector2f *pos, unsigned char *buttons)
{
switch (event->type) {
case sfEvtJoystickButtonPressed:
if (event->joystickButton.joystickId == 0) {
buttons[event->joystickButton.button] = 1;
}
break;
case sfEvtJoystickButtonReleased:
if (event->joystickButton.joystickId == 0) {
buttons[event->joystickButton.button] = 0;
}
break;
case sfEvtJoystickMoved:
if (event->joystickMove.joystickId == 0) {
if (event->joystickMove.axis == sfJoystickX) {
pos->x = event->joystickMove.position;
}
if (event->joystickMove.axis == sfJoystickY) {
pos->y = event->joystickMove.position;
}
}
break;
}
}
int main()
{
char title[] = "Joystick Position";
sfRenderWindow *app;
sfVideoMode mode;
sfContextSettings settings;
sfUint32 style = sfResize | sfClose;
mode.width = 800;
mode.height = 600;
mode.bitsPerPixel = 32;
settings.depthBits = 24;
settings.stencilBits = 8;
settings.antialiasingLevel = 0;
settings.majorVersion = 0;
settings.minorVersion = 0;
app = sfRenderWindow_create(mode, title, style, &settings);
if (!app) return 1;
sfText* text = sfText_create();
if (!text) return 1;
path path1 = path_struct(cross1);
path path2 = path_struct(cross2);
sfShape *shape1 = sfShape_create(
&my_sfShapeGetPointCountCallback,
&my_sfShapeGetPointCallback,
(void *)&path1);
if (!shape1) return 1;
sfShape *shape2 = sfShape_create(
&my_sfShapeGetPointCountCallback,
&my_sfShapeGetPointCallback,
(void *)&path2);
if (!shape2) return 1;
#define WITH_OUTLINE 0 /* XXX Without this, the shape is not drawn at all: */
sfShape_setFillColor(shape1, sfWhite);
#if WITH_OUTLINE
sfShape_setOutlineColor(shape1, sfWhite);
sfShape_setOutlineThickness(shape1, 1.0);
#endif
sfShape_setFillColor(shape2, sfWhite);
#if WITH_OUTLINE
sfShape_setOutlineColor(shape2, sfWhite);
sfShape_setOutlineThickness(shape2, 1.0);
#endif
unsigned char buttons[sfJoystickButtonCount];
bzero(buttons, sfJoystickButtonCount);
sfEvent event;
sfVector2f pos = { 0.0f, 0.0f };
while (sfRenderWindow_isOpen(app))
{
while (sfRenderWindow_pollEvent(app, &event))
{
if (event.type == sfEvtClosed)
sfRenderWindow_close(app);
if (event.type == sfEvtKeyPressed && event.key.code == sfKeyEscape)
sfRenderWindow_close(app);
get_joystick(&event, &pos, buttons);
}
display(app, shape1, shape2, pos, text, buttons);
}
sfText_destroy(text);
sfShape_destroy(shape1);
sfShape_destroy(shape2);
sfRenderWindow_destroy(app);
return 0;
}