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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - snath

Pages: [1]
1
General / Re: is it a bug, or a feature ?
« on: May 23, 2012, 08:37:31 pm »
I replaced the user defined shape by a circle shape in my previous example, there is no problem with the circles.

So is it a bug for user defined shapes ?

I also tried to define the shape CW or CCW (like in OpenGL), but the problem with the outline is still there.

2
General / Re: is it a bug, or a feature ?
« on: May 23, 2012, 12:01:23 am »
sure!

#include <SFML/Graphics.h>

sfVector2f rectangle[] =
    {{80.0, 80.0}, {80.0, -80.0}, {-80.0, -80.0}, {-80.0, 80.0}};

typedef struct
{
    unsigned int len;
    sfVector2f *points;
} path;

#define path_struct(data) \
  { (sizeof(data) / sizeof(data[0])), data }


sfVector2f position(sfRenderWindow *app)
{
    sfVector2f vec;
    const sfView *view = sfRenderWindow_getView(app);
    sfVector2f size = sfView_getSize(view);
    vec.x = size.x / 2.0f;
    vec.y = size.y / 2.0f;
    return vec;
}

void display(sfRenderWindow *app, sfShape *shape)
{
    sfShape_setPosition(shape, position(app));
    sfRenderWindow_clear(app, sfBlack);
    sfRenderWindow_drawShape(app, shape, 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];
}

int main()
{
    char title[] = "Joystick Position";
    sfRenderWindow *app;
    sfVideoMode mode;
    sfContextSettings settings;

    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, sfResize | sfClose, &settings);
    if (!app) return 1;

    path rect = path_struct(rectangle);

    sfShape *shape = sfShape_create(
        &my_sfShapeGetPointCountCallback,
        &my_sfShapeGetPointCallback,
        (void *)&rect);
    if (!shape) return 1;

#define WITH_OUTLINE 0  /* XXX Without this, the shape is not drawn at all: */

    sfShape_setFillColor(shape, sfWhite);
#if WITH_OUTLINE
    sfShape_setOutlineColor(shape, sfWhite);
    sfShape_setOutlineThickness(shape, 1.0);
#endif

    while (sfRenderWindow_isOpen(app))
    {
        sfEvent event;
        while (sfRenderWindow_pollEvent(app, &event))
        {
            if (event.type == sfEvtClosed)
                sfRenderWindow_close(app);
        }
        display(app, shape);
    }

    sfShape_destroy(shape);
    sfRenderWindow_destroy(app);
    return 0;
}

3
General / Re: is it a bug, or a feature ?
« on: May 22, 2012, 08:32:21 pm »
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 1

If 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;
}

4
General / Re: is it a bug, or a feature ?
« on: May 22, 2012, 02:22:41 am »
The (expected or unexpected) behaviours 1) and 2) are exactly the same in pure C:

#include <SFML/Graphics.h>
#include <string.h>
#include <stdio.h>

float cross[] =
  {   1.0,   1.0,   10.0,   1.0,   10.0, -1.0,     1.0, -1.0,
      1.0, -10.0,   -1.0, -10.0,   -1.0, -1.0,   -10.0, -1.0,
    -10.0,   1.0,   -1.0,   1.0,   -1.0, 10.0,     1.0, 10.0, };

char *buttons_string(unsigned char *buttons)
{
    unsigned int i, j;
    static char str[sfJoystickButtonCount * 2 + 1];
    bzero(str, sfJoystickButtonCount * 2 + 1);
    for (i = 0, j = 0; i < sfJoystickButtonCount; i++) {
        if (buttons[i]) {
            str[j] = '0' + i;
            str[j+1] = ' ';
            j += 2;
        }
    }
    return str;
}

sfVector2f position(sfRenderWindow *app, sfVector2f pos)
{
    sfVector2f vec;
    sfVector2u size = sfRenderWindow_getSize(app);

    float hw = ((float) size.x) / 2.0f;
    float hh = ((float) 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 *shape,
        sfVector2f pos,
        sfText* text,
        unsigned char *buttons)
{
    sfText_setString(text, buttons_string(buttons));
    sfShape_setPosition(shape, position(app, pos));
    sfRenderWindow_clear(app, sfBlack);
    sfRenderWindow_drawText(app, text, NULL);
    sfRenderWindow_drawShape(app, shape, NULL);
    sfRenderWindow_display(app);
}

static unsigned int
my_sfShapeGetPointCountCallback(void *data)
{
    unsigned int len = (sizeof(cross) / sizeof(cross[0])) / 2;
    return (len);
}

static sfVector2f
my_sfShapeGetPointCallback(unsigned int i, void *data)
{
    float *array = (float *)data;
    sfVector2f vec;
    unsigned int j = i * 2;
    vec.x = array[j];
    vec.y = array[j+1];
    return vec;
}

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;

    sfShape *shape = sfShape_create(
        &my_sfShapeGetPointCountCallback,
        &my_sfShapeGetPointCallback,
        (void *)cross);
    if (!shape) return 1;

    sfShape_setFillColor(shape, sfWhite);
#if 1  /* XXX Without this, the shape is not drawn at all: */
    sfShape_setOutlineColor(shape, sfWhite);
    sfShape_setOutlineThickness(shape, 1.0);
#endif

    unsigned char buttons[sfJoystickButtonCount];
    bzero(buttons, sfJoystickButtonCount);

    sfEvent event;
    sfVector2f pos;
    pos.x = 0.0f;
    pos.y = 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, shape, pos, text, buttons);
    }

    sfText_destroy(text);
    sfShape_destroy(shape);
    sfRenderWindow_destroy(app);
    return 0;
}

5
General / is it a bug, or a feature ?
« on: May 21, 2012, 06:34:48 pm »
Hi,
I've written this ocaml-sfml piece of code for rosettacode:

http://rosettacode.org/wiki/Joystick_position#OCaml

and I've noticed 2 problems with it.
(dunno if I should create 2 differents thread for this)

I don't know if these are bugs, or if it is the expected behavior. (Using the RC 2.0)

1) If I omit the calls to SFShape.setOutlineColor and SFShape.setOutlineThickness (because I only want to fill), nothing is drawn at all. The two possible workarounds are define an outlineColor with the same color than the fillColor, OR define the outlineThickness to 0.0. Is it a bug, or is it the expected behavior?

2) As the task requests, the cross may be moved to the entire window with the joystick, but if I reshape the window smaller, the cross then only moves to a subset of the window area. Again is it a bug, or is it the expected behavior?

Pages: [1]
anything