Converted ShatteredPD to Build from Gradle
Major Changes: - Shattered now builds effortlessly either from gradle CLI or android studio - Much better dependency management through gradle (although it's not really used atm) - Separate PD-classes repo is now SPD-classes module within main repo
This commit is contained in:
99
SPD-classes/src/main/java/com/watabou/gltextures/Atlas.java
Normal file
99
SPD-classes/src/main/java/com/watabou/gltextures/Atlas.java
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2016 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.gltextures;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.graphics.RectF;
|
||||
|
||||
public class Atlas {
|
||||
|
||||
public SmartTexture tx;
|
||||
|
||||
protected HashMap<Object,RectF> namedFrames;
|
||||
|
||||
protected float uvLeft;
|
||||
protected float uvTop;
|
||||
protected float uvWidth;
|
||||
protected float uvHeight;
|
||||
protected int cols;
|
||||
|
||||
public Atlas( SmartTexture tx ) {
|
||||
|
||||
this.tx = tx;
|
||||
tx.atlas = this;
|
||||
|
||||
namedFrames = new HashMap<Object, RectF>();
|
||||
}
|
||||
|
||||
public void add( Object key, int left, int top, int right, int bottom ) {
|
||||
add( key, uvRect( tx, left, top, right, bottom ) );
|
||||
}
|
||||
|
||||
public void add( Object key, RectF rect ) {
|
||||
namedFrames.put( key, rect );
|
||||
}
|
||||
|
||||
public void grid( int width ) {
|
||||
grid( width, tx.height );
|
||||
}
|
||||
|
||||
public void grid( int width, int height ) {
|
||||
grid( 0, 0, width, height, tx.width / width );
|
||||
}
|
||||
|
||||
public void grid( int left, int top, int width, int height, int cols ) {
|
||||
uvLeft = (float)left / tx.width;
|
||||
uvTop = (float)top / tx.height;
|
||||
uvWidth = (float)width / tx.width;
|
||||
uvHeight= (float)height / tx.height;
|
||||
this.cols = cols;
|
||||
}
|
||||
|
||||
public RectF get( int index ) {
|
||||
float x = index % cols;
|
||||
float y = index / cols;
|
||||
float l = uvLeft + x * uvWidth;
|
||||
float t = uvTop + y * uvHeight;
|
||||
return new RectF( l, t, l + uvWidth, t + uvHeight );
|
||||
}
|
||||
|
||||
public RectF get( Object key ) {
|
||||
return namedFrames.get( key );
|
||||
}
|
||||
|
||||
public float width( RectF rect ) {
|
||||
return rect.width() * tx.width;
|
||||
}
|
||||
|
||||
public float height( RectF rect ) {
|
||||
return rect.height() * tx.height;
|
||||
}
|
||||
|
||||
public static RectF uvRect( SmartTexture tx, int left, int top, int right, int bottom ) {
|
||||
return new RectF(
|
||||
(float)left / tx.width,
|
||||
(float)top / tx.height,
|
||||
(float)right / tx.width,
|
||||
(float)bottom / tx.height );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2016 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.gltextures;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
|
||||
public class Gradient extends SmartTexture {
|
||||
|
||||
public Gradient( int colors[] ) {
|
||||
|
||||
super( Bitmap.createBitmap( colors.length, 1, Bitmap.Config.ARGB_8888 ) );
|
||||
|
||||
for (int i=0; i < colors.length; i++) {
|
||||
bitmap.setPixel( i, 0, colors[i] );
|
||||
}
|
||||
bitmap( bitmap );
|
||||
|
||||
filter( LINEAR, LINEAR );
|
||||
wrap( CLAMP, CLAMP );
|
||||
|
||||
TextureCache.add( Gradient.class, this );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2016 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.gltextures;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.RectF;
|
||||
|
||||
import com.watabou.glwrap.Texture;
|
||||
|
||||
public class SmartTexture extends Texture {
|
||||
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
public int fModeMin;
|
||||
public int fModeMax;
|
||||
|
||||
public int wModeH;
|
||||
public int wModeV;
|
||||
|
||||
public Bitmap bitmap;
|
||||
|
||||
public Atlas atlas;
|
||||
|
||||
public SmartTexture( Bitmap bitmap ) {
|
||||
this( bitmap, NEAREST, CLAMP, false );
|
||||
}
|
||||
|
||||
public SmartTexture( Bitmap bitmap, int filtering, int wrapping, boolean premultiplied ) {
|
||||
|
||||
super();
|
||||
|
||||
bitmap( bitmap, premultiplied );
|
||||
filter( filtering, filtering );
|
||||
wrap( wrapping, wrapping );
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void filter(int minMode, int maxMode) {
|
||||
super.filter( fModeMin = minMode, fModeMax = maxMode);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void wrap( int s, int t ) {
|
||||
super.wrap( wModeH = s, wModeV = t );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void bitmap( Bitmap bitmap ) {
|
||||
bitmap( bitmap, false );
|
||||
}
|
||||
|
||||
public void bitmap( Bitmap bitmap, boolean premultiplied ) {
|
||||
if (premultiplied) {
|
||||
super.bitmap( bitmap );
|
||||
} else {
|
||||
handMade( bitmap, true );
|
||||
}
|
||||
|
||||
this.bitmap = bitmap;
|
||||
width = bitmap.getWidth();
|
||||
height = bitmap.getHeight();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
id = new SmartTexture( bitmap, NEAREST, CLAMP, premultiplied ).id;
|
||||
filter( fModeMin, fModeMax );
|
||||
wrap( wModeH, wModeV );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete() {
|
||||
|
||||
super.delete();
|
||||
|
||||
bitmap.recycle();
|
||||
bitmap = null;
|
||||
}
|
||||
|
||||
public RectF uvRect( int left, int top, int right, int bottom ) {
|
||||
return new RectF(
|
||||
(float)left / width,
|
||||
(float)top / height,
|
||||
(float)right / width,
|
||||
(float)bottom / height );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* Pixel Dungeon
|
||||
* Copyright (C) 2012-2015 Oleg Dolya
|
||||
*
|
||||
* Shattered Pixel Dungeon
|
||||
* Copyright (C) 2014-2016 Evan Debenham
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
|
||||
package com.watabou.gltextures;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.LinearGradient;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.Shader.TileMode;
|
||||
|
||||
import com.watabou.glwrap.Texture;
|
||||
|
||||
public class TextureCache {
|
||||
|
||||
public static Context context;
|
||||
|
||||
private static HashMap<Object,SmartTexture> all = new HashMap<>();
|
||||
|
||||
// No dithering, no scaling, 32 bits per pixel
|
||||
private static BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
|
||||
static {
|
||||
bitmapOptions.inScaled = false;
|
||||
bitmapOptions.inDither = false;
|
||||
bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
||||
}
|
||||
|
||||
public static SmartTexture createSolid( int color ) {
|
||||
final String key = "1x1:" + color;
|
||||
|
||||
if (all.containsKey( key )) {
|
||||
|
||||
return all.get( key );
|
||||
|
||||
} else {
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap( 1, 1, Bitmap.Config.ARGB_8888 );
|
||||
bmp.eraseColor( color );
|
||||
|
||||
SmartTexture tx = new SmartTexture( bmp );
|
||||
all.put( key, tx );
|
||||
|
||||
return tx;
|
||||
}
|
||||
}
|
||||
|
||||
public static SmartTexture createGradient( int width, int height, int... colors ) {
|
||||
|
||||
final String key = "" + width + "x" + height + ":" + colors;
|
||||
|
||||
if (all.containsKey( key )) {
|
||||
|
||||
return all.get( key );
|
||||
|
||||
} else {
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
|
||||
Canvas canvas = new Canvas( bmp );
|
||||
Paint paint = new Paint();
|
||||
paint.setShader( new LinearGradient( 0, 0, 0, height, colors, null, TileMode.CLAMP ) );
|
||||
canvas.drawPaint( paint );
|
||||
|
||||
SmartTexture tx = new SmartTexture( bmp );
|
||||
all.put( key, tx );
|
||||
return tx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void add( Object key, SmartTexture tx ) {
|
||||
all.put( key, tx );
|
||||
}
|
||||
|
||||
public static SmartTexture get( Object src ) {
|
||||
|
||||
if (all.containsKey( src )) {
|
||||
|
||||
return all.get( src );
|
||||
|
||||
} else if (src instanceof SmartTexture) {
|
||||
|
||||
return (SmartTexture)src;
|
||||
|
||||
} else {
|
||||
|
||||
SmartTexture tx = new SmartTexture( getBitmap( src ) );
|
||||
all.put( src, tx );
|
||||
return tx;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void clear() {
|
||||
|
||||
for (Texture txt:all.values()) {
|
||||
txt.delete();
|
||||
}
|
||||
all.clear();
|
||||
|
||||
}
|
||||
|
||||
public static void reload() {
|
||||
for (SmartTexture tx:all.values()) {
|
||||
tx.reload();
|
||||
}
|
||||
}
|
||||
|
||||
public static Bitmap getBitmap( Object src ) {
|
||||
|
||||
try {
|
||||
if (src instanceof Integer){
|
||||
|
||||
return BitmapFactory.decodeResource(
|
||||
context.getResources(), (Integer)src, bitmapOptions );
|
||||
|
||||
} else if (src instanceof String) {
|
||||
|
||||
return BitmapFactory.decodeStream(
|
||||
context.getAssets().open( (String)src ), null, bitmapOptions );
|
||||
|
||||
} else if (src instanceof Bitmap) {
|
||||
|
||||
return (Bitmap)src;
|
||||
|
||||
} else {
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
} catch (Exception e) {
|
||||
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean contains( Object key ) {
|
||||
return all.containsKey( key );
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user