我一直在研究一个简单的基于图块的地形生成系统,用于我正在尝试构建的游戏,并且遇到了一些障碍。我正在尝试开发一种新方法来挑选和存储我需要的各个图块的位图。我有一个非常简单的方法,在我运行世界绘图之前,所有位图都被加载,并且根据它的类型来选择位图。
不幸的是,当我在瓷砖和世界类型上添加变体时,选择过程可能会变得相当复杂。因此,将这些数据存储在tile对象中以及确定它将成为哪个tile所需的所有规则似乎是个好主意。
我遇到的问题是这个方法导致我为每个图块加载位图的副本,而不是只是加载指向位图的指针。反过来,这又耗费了我的记忆力,导致我的GC变得有点乱。
我正在寻找的是一种简单的方法来创建将要使用的不同位图的静态指针,然后引用Tile对象中的那些指针而不是加载位图的新副本。我遇到的问题是我无法在没有上下文引用的情况下加载位图,我无法弄清楚如何在静态方法中引用上下文。
有没有人对如何解决这个问题有任何想法?
这是我的tile类,以及tile类型的一个例子(它们几乎都是一样的)
平铺: 包com.psstudio.hub.gen;
import java.io.Serializable;
import android.graphics.Bitmap;
public class Tile implements Serializable
{
/****
* Create a BitmaHolder class that uses a case for which field type it isn, and then
* loads all applicable bitmaps into objects. The individuak tile types then reference
* those objects and return bitmaps, which are ultimately stored in this super class.
*/
double elevation = 0.0; //tile's elevation
int type = 0; //tile's type
int health = 0; //tile's health - used for harvestable tiles (trees, stone, mountain, etc.)
Bitmap bitmap; //pointer to the bitmap
public void setTile(Bitmap _bitmap, double _elev, int _type){
bitmap = _bitmap;
elevation = _elev;
type = _type;
}
public void setBreakableTile(Bitmap _bitmap, double _elev, int _type, int _health){
bitmap = _bitmap;
elevation = _elev;
type = _type;
health = _health;
}
}
瓷砖类型(草):
package com.psstudio.hub.gen.tiles;
import java.util.Random;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.psstudio.hub.R;
import com.psstudio.hub.gen.Tile;
public class Grass extends Tile {
Bitmap holder;
BitmapFactory.Options bFOptions = new BitmapFactory.Options(); //used to prevent bitmap scaling
Random r = new Random();
public Grass(Context context, int mapType, Double elevation){
super.setTile(pickBitmap(context, mapType), elevation, 4);
pickBitmap(context, mapType);
}
public Bitmap pickBitmap(Context context, int mapType){
bFOptions.inScaled = false;
switch(mapType){
case 1: //Forest Type 1
holder = BitmapFactory.decodeResource(context.getResources(), R.drawable.grass, bFOptions);
break;
}
return holder;
}
}
“holder = BitmapFactory”是我认为导致我的问题的原因,因为它为每个草图块加载一个新的位图,而不是仅指向预先加载的一个。
答案 0 :(得分:0)
好吧,我设法找到了一种方法来做我需要的事情。我不知道这是否是最合适的方式,但它适用于我需要的东西。
我创建了一个BitmapLoader类,它根据世界类型加载图像,然后将它们存储到静态位图中,然后由图块引用。
不是最优雅的解决方案,但它完成了工作。 ^^