예제 #1
0
void SpriteBatch::draw (const TextureRegion& region,float x,float y,float width,float height) {
    if (!drawing)
        throw std::runtime_error("SpriteBatch.begin must be called before draw.");

    Texture::ptr texture = region.getTexture();
    if (texture.get() != lastTexture) {
        renderMesh();
        lastTexture = texture.get();
        invTexWidth = 1.0f / texture->getWidth();
        invTexHeight = 1.0f / texture->getHeight();
    } else if (idx == verticesSize) //
        renderMesh();

    float fx2 = x + width;
    float fy2 = y + height;
    float u = region.u;
    float v = region.v2;
    float u2 = region.u2;
    float v2 = region.v;

    utils::float_buffer& buffer = mesh->getVerticesBuffer();
    buffer.limit(buffer.position() + 20);

    vertices[idx++] = x;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v;

    vertices[idx++] = x;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = fy2;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;

    vertices[idx++] = fx2;
    vertices[idx++] = y;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v;
}
예제 #2
0
void SpriteBatch::draw (const TextureRegion& region,float x,float y,float originX,float originY,float width,float height,float scaleX,float scaleY,float rotation,bool clockwise) {
    if (!drawing)
        throw std::runtime_error("SpriteBatch.begin must be called before draw.");

    Texture::ptr texture = region.getTexture();
    if (texture.get() != lastTexture) {
        renderMesh();
        lastTexture = texture.get();
        invTexWidth = 1.0f / texture->getWidth();
        invTexHeight = 1.0f / texture->getHeight();
    } else if (idx == verticesSize) {
        renderMesh();
    }

    // bottom left and top right corner points relative to origin
    float worldOriginX = x + originX;
    float worldOriginY = y + originY;
    float fx = -originX;
    float fy = -originY;
    float fx2 = width - originX;
    float fy2 = height - originY;

    // scale
    if (scaleX != 1 || scaleY != 1) {
        fx *= scaleX;
        fy *= scaleY;
        fx2 *= scaleX;
        fy2 *= scaleY;
    }

    // construct corner points, start from top left and go counter clockwise
    float p1x = fx;
    float p1y = fy;
    float p2x = fx;
    float p2y = fy2;
    float p3x = fx2;
    float p3y = fy2;
    float p4x = fx2;
    float p4y = fy;

    float x1;
    float y1;
    float x2;
    float y2;
    float x3;
    float y3;
    float x4;
    float y4;

    // rotate
    if (rotation != 0) {
        float cos = math::utils::cosDeg(rotation);
        float sin = math::utils::sinDeg(rotation);

        x1 = cos * p1x - sin * p1y;
        y1 = sin * p1x + cos * p1y;

        x2 = cos * p2x - sin * p2y;
        y2 = sin * p2x + cos * p2y;

        x3 = cos * p3x - sin * p3y;
        y3 = sin * p3x + cos * p3y;

        x4 = x1 + (x3 - x2);
        y4 = y3 - (y2 - y1);
    } else {
        x1 = p1x;
        y1 = p1y;

        x2 = p2x;
        y2 = p2y;

        x3 = p3x;
        y3 = p3y;

        x4 = p4x;
        y4 = p4y;
    }

    x1 += worldOriginX;
    y1 += worldOriginY;
    x2 += worldOriginX;
    y2 += worldOriginY;
    x3 += worldOriginX;
    y3 += worldOriginY;
    x4 += worldOriginX;
    y4 += worldOriginY;

    float u1, v1, u2, v2, u3, v3, u4, v4;
    if (clockwise) {
        u1 = region.u2;
        v1 = region.v2;
        u2 = region.u;
        v2 = region.v2;
        u3 = region.u;
        v3 = region.v;
        u4 = region.u2;
        v4 = region.v;
    } else {
        u1 = region.u;
        v1 = region.v;
        u2 = region.u2;
        v2 = region.v;
        u3 = region.u2;
        v3 = region.v2;
        u4 = region.u;
        v4 = region.v2;
    }

    vertices[idx++] = x1;
    vertices[idx++] = y1;
    vertices[idx++] = color;
    vertices[idx++] = u1;
    vertices[idx++] = v1;

    vertices[idx++] = x2;
    vertices[idx++] = y2;
    vertices[idx++] = color;
    vertices[idx++] = u2;
    vertices[idx++] = v2;

    vertices[idx++] = x3;
    vertices[idx++] = y3;
    vertices[idx++] = color;
    vertices[idx++] = u3;
    vertices[idx++] = v3;

    vertices[idx++] = x4;
    vertices[idx++] = y4;
    vertices[idx++] = color;
    vertices[idx++] = u4;
    vertices[idx++] = v4;
}