    001package cpw.mods.fml.client;
    002
    003import java.nio.ByteBuffer;
    004import java.util.List;
    005
    006import net.minecraft.client.renderer.texture.Texture;
    007import net.minecraft.client.renderer.texture.TextureStitched;
    008
    009public abstract class TextureHelper {
    010
    011    /**
    012     * Copy the texture from the source to the atlas at the specified position
    013     *
    014     * This will use the devised GL helper to do either GL-side copy or a subimage upload
    015     *
    016     * @param atlas The atlas texture we're copying into
    017     * @param source The source texture we're copying from (complete)
    018     * @param atlasX The X position on the atlas
    019     * @param atlasY The Y position on the atlas
    020     */
    021    public abstract void doTextureCopy(Texture atlas, Texture source, int atlasX, int atlasY);
    022
    023    /**
    024     * Upload the texture to the GPU for GL side copying operations
    025     * This may be a no-op depending on the active implementation.
    026     *
    027     * @param source The texture to upload
    028     */
    029    public abstract void doTextureUpload(TextureStitched source);
    030
    031    /**
    032     * Rotate the texture so that it doesn't need a rotational transform applied each tick
    033     *
    034     * @param texture The texture to rotate
    035     * @param buffer The buffer for the texture
    036     */
    037    public void rotateTexture(Texture texture, ByteBuffer buffer)
    038    {
    039        ByteBuffer bytebuffer = buffer;
    040        buffer.position(0);
    041        ByteBuffer other = ByteBuffer.allocateDirect(buffer.capacity());
    042        other.position(0);
    043
    044        int texHeight = texture.getHeight();
    045        int texWidth = texture.getWidth();
    046
    047        for (int row = 0; row < texHeight; ++row)
    048        {
    049            int targCol = texHeight - row - 1;
    050            int srcRowOffset = row * texWidth;
    051
    052            for (int col = 0; col < texWidth; ++col)
    053            {
    054                int targIndex = col * texHeight + targCol;
    055                int srcIndex = srcRowOffset + col;
    056
    057                srcIndex <<=2;
    058                targIndex <<=2;
    059
    060                other.put(targIndex + 0, bytebuffer.get(srcIndex + 0));
    061                other.put(targIndex + 1, bytebuffer.get(srcIndex + 1));
    062                other.put(targIndex + 2, bytebuffer.get(srcIndex + 2));
    063                other.put(targIndex + 3, bytebuffer.get(srcIndex + 3));
    064            }
    065        }
    066        buffer.position(0);
    067        buffer.put(other);
    068    }
    069
    070}