void SpriteBatch::addSprite(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, SpriteBatch::SpriteVertex* vertices) { GP_ASSERT(vertices); const float x2 = x + width; const float y2 = y + height; SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w); }
void SpriteBatch::addSprite(float x, float y, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Rectangle& clip, SpriteBatch::SpriteVertex* vertices) { GP_ASSERT(vertices); // Only add a sprite if at least part of the sprite is within the clip region. if (clipSprite(clip, x, y, width, height, u1, v1, u2, v2)) { const float x2 = x + width; const float y2 = y + height; SPRITE_ADD_VERTEX(vertices[0], x, y, 0, u1, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[1], x, y2, 0, u1, v2, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[2], x2, y, 0, u2, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(vertices[3], x2, y2, 0, u2, v2, color.x, color.y, color.z, color.w); } }
void SpriteBatch::draw(float x, float y, float z, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Vector2& rotationPoint, float rotationAngle, bool positionIsCenter) { // Treat the given position as the center if the user specified it as such. if (positionIsCenter) { x -= 0.5f * width; y -= 0.5f * height; } // Expand the destination position by scale into 4 points. float x2 = x + width; float y2 = y + height; Vector2 upLeft(x, y); Vector2 upRight(x2, y); Vector2 downLeft(x, y2); Vector2 downRight(x2, y2); // Rotate points around rotationAxis by rotationAngle. if (rotationAngle != 0) { Vector2 pivotPoint(rotationPoint); pivotPoint.x *= width; pivotPoint.y *= height; pivotPoint.x += x; pivotPoint.y += y; upLeft.rotate(pivotPoint, rotationAngle); upRight.rotate(pivotPoint, rotationAngle); downLeft.rotate(pivotPoint, rotationAngle); downRight.rotate(pivotPoint, rotationAngle); } // Write sprite vertex data. static SpriteVertex v[4]; SPRITE_ADD_VERTEX(v[0], downLeft.x, downLeft.y, z, u1, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[1], upLeft.x, upLeft.y, z, u1, v2, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[2], downRight.x, downRight.y, z, u2, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[3], upRight.x, upRight.y, z, u2, v2, color.x, color.y, color.z, color.w); static unsigned short indices[4] = { 0, 1, 2, 3 }; _batch->add(v, 4, indices, 4); }
void SpriteBatch::draw(float x, float y, float z, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, bool positionIsCenter) { // Treat the given position as the center if the user specified it as such. if (positionIsCenter) { x -= 0.5f * width; y -= 0.5f * height; } // Write sprite vertex data. const float x2 = x + width; const float y2 = y + height; static SpriteVertex v[4]; SPRITE_ADD_VERTEX(v[0], x, y, z, u1, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[1], x, y2, z, u1, v2, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[2], x2, y, z, u2, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[3], x2, y2, z, u2, v2, color.x, color.y, color.z, color.w); static unsigned short indices[4] = { 0, 1, 2, 3 }; _batch->add(v, 4, indices, 4); }
void SpriteBatch::draw(const Vector3& position, const Vector3& right, const Vector3& forward, float width, float height, float u1, float v1, float u2, float v2, const Vector4& color, const Vector2& rotationPoint, float rotationAngle) { // Calculate the vertex positions. Vector3 tRight(right); tRight *= width * 0.5f; Vector3 tForward(forward); tForward *= height * 0.5f; Vector3 p0 = position; p0 -= tRight; p0 -= tForward; Vector3 p1 = position; p1 += tRight; p1 -= tForward; tForward = forward; tForward *= height; Vector3 p2 = p0; p2 += tForward; Vector3 p3 = p1; p3 += tForward; // Calculate the rotation point. Vector3 rp = p0; tRight = right; tRight *= width * rotationPoint.x; tForward *= rotationPoint.y; rp += tRight; rp += tForward; // Rotate all points the specified amount about the given point (about the up vector). static Vector3 u; Vector3::cross(right, forward, &u); static Matrix rotation; Matrix::createRotation(u, rotationAngle, &rotation); p0 -= rp; p0 *= rotation; p0 += rp; p1 -= rp; p1 *= rotation; p1 += rp; p2 -= rp; p2 *= rotation; p2 += rp; p3 -= rp; p3 *= rotation; p3 += rp; // Add the sprite vertex data to the batch. static SpriteVertex v[4]; SPRITE_ADD_VERTEX(v[0], p0.x, p0.y, p0.z, u1, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[1], p1.x, p1.y, p1.z, u2, v1, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[2], p2.x, p2.y, p2.z, u1, v2, color.x, color.y, color.z, color.w); SPRITE_ADD_VERTEX(v[3], p3.x, p3.y, p3.z, u2, v2, color.x, color.y, color.z, color.w); static const unsigned short indices[4] = { 0, 1, 2, 3 }; _batch->add(v, 4, const_cast<unsigned short*>(indices), 4); }