SimpleJavaEngine/src/main/java/speiger/src/coreengine/rendering/models/DataType.java

96 lines
1.8 KiB
Java

package speiger.src.coreengine.rendering.models;
import java.nio.ByteBuffer;
import java.util.Map;
import com.google.gson.JsonElement;
import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap;
import speiger.src.coreengine.rendering.models.loader.VertexLoader.JsonList;
public enum DataType
{
BYTE("byte", 1),
SHORT("short", 2),
INT("int", 4),
LONG("long", 8),
FLOAT("float", 4),
DOUBLE("double", 8);
static final Map<String, DataType> BY_ID = new Object2ObjectOpenHashMap<>();
final String type;
final int byteSize;
private DataType(String type, int byteSize)
{
this.type = type;
this.byteSize = byteSize;
}
public String getType()
{
return type;
}
public int getByteSize()
{
return byteSize;
}
public boolean isFloatingPoint()
{
return this == FLOAT || this == DOUBLE;
}
public void putIntoBuffer(ByteBuffer buffer, JsonList list, int offset, int stride, int size)
{
int index = offset;
for(int i = 0,m=list.size();i<m;)
{
putIntoBuffer(buffer, index, list.get(i));
index += byteSize;
if(++i % size == 0) {
index+=stride;
}
}
}
public void putIntoBuffer(ByteBuffer buffer, int index, JsonElement element)
{
switch(this)
{
case BYTE:
buffer.put(index, element.getAsByte());
break;
case SHORT:
buffer.putShort(index, element.getAsShort());
break;
case INT:
buffer.putInt(index, element.getAsInt());
break;
case LONG:
buffer.putLong(index, element.getAsLong());
break;
case FLOAT:
buffer.putFloat(index, element.getAsFloat());
break;
case DOUBLE:
buffer.putDouble(index, element.getAsDouble());
break;
}
}
public static DataType byID(String id)
{
return BY_ID.get(id);
}
static
{
for(DataType type : values())
{
BY_ID.put(type.getType(), type);
}
}
}