Example #1
0
void RenderQueue_addDrawable(RenderQueue* self, Drawable* drawable, float positionX, float positionY)
{
    smug_assert(_invariant(self));
    Sprite* sprite = Drawable_getSprite(drawable);
    smug_assert(sprite != NULL);
    int id = Sprite_getTextureId(sprite);
    RenderBatch* renderBatch = (RenderBatch*)Map_get(self->renderBatches, &id);
    if (renderBatch == NULL)
    {
        renderBatch = RenderBatch_new(RENDERBATCH_INITIAL_SIZE, TRUE);
        Map_set(self->renderBatches, _allocInt(id), renderBatch);
    }
    Drawable_addRenderData(drawable, renderBatch, positionX, positionY);
}
Example #2
0
static void writeBatchData(Drawable* drawable, BatchData* batchdata, unsigned int start)
{
    smug_assert(_invariant(drawable));
    smug_assert(GameObject_isType((GameObject*)drawable, SMUG_TYPE_DRAWABLE));
    static unsigned int vertexstart, colorstart, texturestart;
    static float x1, x2, y1, y2;
    static float r, g, b, a;
    static float tx1, ty1, tx2, ty2;
    static Sprite* sprite = NULL;
    static Point dpos;
    static Shape* shape;
    static Rectangle box;
    static Color color;
    static BOOL useColor;

    vertexstart = start*2;
    colorstart = start*4;
    texturestart = start*2;

    Drawable_getShape(drawable, &shape);
    box = Shape_getAsRectangle(shape);

    // write vertices in anti-clockwise order
    PositionedObject_getPosForDrawing((GameObject*)drawable, &dpos);
    x1 = Point_getX(dpos) + Rectangle_getX(&box);
    y1 = Point_getY(dpos) + Rectangle_getY(&box);
    x2 = Point_getX(dpos) + Rectangle_getW(&box);
    y2 = Point_getY(dpos) + Rectangle_getH(&box);

    batchdata->vertexData[vertexstart + 0 * 2 + 0] = x1;
    batchdata->vertexData[vertexstart + 0 * 2 + 1] = y1;

    batchdata->vertexData[vertexstart + 1 * 2 + 0] = x1;
    batchdata->vertexData[vertexstart + 1 * 2 + 1] = y2;

    batchdata->vertexData[vertexstart + 2 * 2 + 0] = x2;
    batchdata->vertexData[vertexstart + 2 * 2 + 1] = y2;

    batchdata->vertexData[vertexstart + 3 * 2 + 0] = x2;
    batchdata->vertexData[vertexstart + 3 * 2 + 1] = y1;
#ifdef SMUG_GLES
    batchdata->vertexData[vertexstart + 4 * 2 + 0] = x1;
    batchdata->vertexData[vertexstart + 4 * 2 + 1] = y1;

    batchdata->vertexData[vertexstart + 5 * 2 + 0] = x2;
    batchdata->vertexData[vertexstart + 5 * 2 + 1] = y2;
#endif /* SMUG_GLES */


    Drawable_getUseColor(drawable, &useColor);
    Drawable_getColor(drawable, &color);
    a = Color_Af(color);
    if (useColor)
    {
        // write colordata
        r = Color_Rf(color);
        g = Color_Gf(color);
        b = Color_Bf(color);
    }
    else
    {
        r = 1.0f;
        g = 1.0f;
        b = 1.0f;
    }

    batchdata->colorData[colorstart + 0 * 4 + 0] = r;
    batchdata->colorData[colorstart + 0 * 4 + 1] = g;
    batchdata->colorData[colorstart + 0 * 4 + 2] = b;
    batchdata->colorData[colorstart + 0 * 4 + 3] = a;

    batchdata->colorData[colorstart + 1 * 4 + 0] = r;
    batchdata->colorData[colorstart + 1 * 4 + 1] = g;
    batchdata->colorData[colorstart + 1 * 4 + 2] = b;
    batchdata->colorData[colorstart + 1 * 4 + 3] = a;

    batchdata->colorData[colorstart + 2 * 4 + 0] = r;
    batchdata->colorData[colorstart + 2 * 4 + 1] = g;
    batchdata->colorData[colorstart + 2 * 4 + 2] = b;
    batchdata->colorData[colorstart + 2 * 4 + 3] = a;

    batchdata->colorData[colorstart + 3 * 4 + 0] = r;
    batchdata->colorData[colorstart + 3 * 4 + 1] = g;
    batchdata->colorData[colorstart + 3 * 4 + 2] = b;
    batchdata->colorData[colorstart + 3 * 4 + 3] = a;
#ifdef SMUG_GLES
    batchdata->colorData[colorstart + 4 * 4 + 0] = r;
    batchdata->colorData[colorstart + 4 * 4 + 1] = g;
    batchdata->colorData[colorstart + 4 * 4 + 2] = b;
    batchdata->colorData[colorstart + 4 * 4 + 3] = a;

    batchdata->colorData[colorstart + 5 * 4 + 0] = r;
    batchdata->colorData[colorstart + 5 * 4 + 1] = g;
    batchdata->colorData[colorstart + 5 * 4 + 2] = b;
    batchdata->colorData[colorstart + 5 * 4 + 3] = a;
#endif /* SMUG_GLES */

    // write texture data only if sprite exists
    if ((sprite = Drawable_getSprite(drawable)) == NULL)
    {
        return;
    }

    tx1 = Rectangle_getX(&sprite->rect) * sprite->texture->px;
    ty1 = Rectangle_getY(&sprite->rect) * sprite->texture->py;
    tx2 = tx1 + Rectangle_getW(&sprite->rect) * sprite->texture->px;
    ty2 = ty1 + Rectangle_getH(&sprite->rect) * sprite->texture->py;

    batchdata->textureData[texturestart + 0 * 2 + 0] = tx1;
    batchdata->textureData[texturestart + 0 * 2 + 1] = ty1;
    batchdata->textureData[texturestart + 1 * 2 + 0] = tx1;
    batchdata->textureData[texturestart + 1 * 2 + 1] = ty2;
    batchdata->textureData[texturestart + 2 * 2 + 0] = tx2;
    batchdata->textureData[texturestart + 2 * 2 + 1] = ty2;
    batchdata->textureData[texturestart + 3 * 2 + 0] = tx2;
    batchdata->textureData[texturestart + 3 * 2 + 1] = ty1;
#ifdef SMUG_GLES
    batchdata->textureData[texturestart + 4 * 2 + 0] = tx1;
    batchdata->textureData[texturestart + 4 * 2 + 1] = ty1;
    batchdata->textureData[texturestart + 5 * 2 + 0] = tx2;
    batchdata->textureData[texturestart + 5 * 2 + 1] = ty2;
#endif /* SMUG_GLES */
}