Android Open Source - android_game_engine Sprite Sheet






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.es.engine.graphics.sprite;
/*ww  w.j a v  a 2  s . co m*/
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;

/**s
 * @author Cyril Leroux
 *         Created on 09/02/14.
 */
public class SpriteSheet {

    private final Bitmap mSpriteSheet;
    private final Rect[][] mFrameGrid;

    public SpriteSheet(Bitmap spriteSheet, int columnCount, int rowCount) {
        mSpriteSheet = spriteSheet;
        mFrameGrid = getRectGrid(spriteSheet, columnCount, rowCount);
    }

    /**
     * Constructor that will load a bitmap from the resources.
     *
     * @param resources Context resources used to load sprite sheet bitmap.
     * @param resourceId The id of the sprite sheet resource.
     */
    public SpriteSheet(Resources resources, int resourceId, int columnCount, int rowCount) {
        this(BitmapFactory.decodeResource(resources, resourceId), columnCount, rowCount);
    }

    private static Rect[][] getRectGrid(final Bitmap spriteSheet, int columnCount, int rowCount) {

        Rect[][] grid = new Rect[rowCount][columnCount];
        final int frameWidth  = spriteSheet.getWidth()  / columnCount;
        final int frameHeight = spriteSheet.getHeight() / rowCount;

        int currentX = 0;
        int currentY = 0;

        // For each line
        for (int y = 0; y < rowCount; y++) {
            // For each element in the line
            for (int x = 0; x < columnCount; x++) {
                grid[y][x] = new Rect(currentX, currentY, currentX + frameWidth, currentY + frameHeight);
                currentX += frameWidth;
            }
            // End of the line : reset x, update y
            currentX = 0;
            currentY += frameHeight;
        }
        return grid;
    }

    public Bitmap getBitmap() { return mSpriteSheet; }

    public Rect getRect(int y, int x) { return mFrameGrid[y][x]; }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java