CSFML意外的重复纹理行为

时间:2018-12-15 23:51:56

标签: c graphics sprite sfml

我正在研究CSFML 2d亚军游戏项目,并希望实现一些视差以使玩家印象深刻。为此,我希望通过sfTexture_setRepeated重复一些资产,但可惜。它不会重复它们,而是将其边界扩展到我出于某种未知原因设置的精灵矩形的大小。

以下是有关我要执行的操作的摘要:

runner_objects.h

  

包含在Runner_objects.c中

typedef enum {
    PLAYER      ,
    OBSTACLE    ,
    BACKGOUND1  ,
    BACKGOUND2  ,

    OBJ_COUNT
} objtype_t;

static const char *texture[OBJ_COUNT] = {
        "res/undefined.png"     ,
        "res/undefined.png"     ,
        "res/bg_clouds.png"     ,
        "res/bg_mountain.tga"   ,
};

static const sfIntRect rect[OBJ_COUNT] = {
        {0,         0,          0,          0}          ,
        {0,         0,          0,          0}          ,
        {0,         0,          1920*3,     1080}       ,
        {0,         0,          1920*2,     1080}       ,
};

typedef struct object {
    // Properties
    objtype_t       type;
    sfTexture       *texture;
    sfSprite        *sprite;
    sfIntRect       rect;
    sfVector2f      pos;

    // Linked listing
    struct object   *next;
} obj_t;

runner_objects.c

// Macros like TE_CREATE etc are simply CSFML functions made
// shorter to respect a strict 80 columns limit.
// TE_ = sfTexture, S_ = sfSprite

...
obj_t *object_create (objtype_t type, sfVector2f pos)
{
    obj_t *new_obj = malloc(sizeof(obj_t));

    if (!new_obj)
        return NULL;
    new_obj->texture        = TE_CREATE_WHOLE(texture[type]);
    new_obj->sprite         = S_CREATE;
    if (!new_obj->texture || !new_obj->sprite)
        return object_destroy(&new_obj); // Destroys existing parts and returns NULL
    new_obj->type           = type;
    new_obj->rect           = rect[type];
    new_obj->pos            = pos;
    S_SETTEXTURE(new_obj->sprite, new_obj->texture);
    if (type == BACKGOUND2 || type == BACKGOUND1)
        TE_SETREPREATED(new_obj->texture, true);
    S_SETINTRECT(new_obj->sprite, new_obj->rect);
    S_SETPOS(new_obj->sprite, new_obj->pos);
    new_obj->next           = NULL;
    return new_obj;
}
...

runner_env.c

  

我在哪里设置游戏环境

...
// This function is called by int env_create(env_t **env) which is called
// from my main with the address of my env struct pointer.
int env_create_background(env_t **env)
{
    (*env)->objects = object_create(BACKGOUND2, (sfVector2f) {0 , 0});
    if (!(*env)->objects)
        return mr_puterr(ERR_CREABG); // Puts an error string on STDERR and returns corresponding error code
    (*env)->objects->speed = (sfVector2f) { 0, 0 };
    (*env)->objects->next = object_create(BACKGOUND1, (sfVector2f) {0 , 0});
    if (!(*env)->objects->next)
        return mr_puterr(ERR_CREABG);
    (*env)->objects->next->speed = (sfVector2f) { 0, 0 };
    return NO_ERR;
}
// I know it's very ugly and I'll make an actual linked
// list constructor later this is just for testing for now.
...

如果您好奇的话,我的env_t结构包含这些内容。

typedef struct environment {
    sfRenderWindow      *window;
    sfClock             *clock;
    sfEvent             *evt;
    obj_t               *objects;
} env_t;

现在以直观的方式对此问题进行解释。

这是我得到的屏幕截图。 Not the kind of clouds I expected.

那我做错了什么?我错过了什么 ?有没有办法设置这个我可能搞砸了?

1 个答案:

答案 0 :(得分:0)

好吧,我只是一个白痴,我用TE_SETREPEATED使我的sfTexture_setSmooth宏使用了。我觉得很蠢。