blades-engine load bmp directly
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@@ -0,0 +1,103 @@
|
||||
package;
|
||||
|
||||
import sys.io.File;
|
||||
import haxe.io.Bytes;
|
||||
import flash.display.BitmapData;
|
||||
import flixel.util.FlxColor;
|
||||
|
||||
typedef ByteStream = {
|
||||
bytes:Bytes,
|
||||
position:Int
|
||||
};
|
||||
|
||||
class Bmp {
|
||||
static function getByteStream(file:String):ByteStream {
|
||||
return {
|
||||
bytes: File.getBytes(file),
|
||||
position:0
|
||||
};
|
||||
}
|
||||
|
||||
static function readByte(bytes:ByteStream):Int {
|
||||
return bytes.bytes.get(bytes.position++);
|
||||
}
|
||||
|
||||
static function readInt32(bytes:ByteStream):Int {
|
||||
var int = bytes.bytes.getInt32(bytes.position);
|
||||
bytes.position += 4;
|
||||
return int;
|
||||
}
|
||||
|
||||
static function readUInt32(bytes:ByteStream):Int {
|
||||
var int = readInt32(bytes);
|
||||
return if (int < 0) cast (4294967296 + cast int) else int;
|
||||
}
|
||||
|
||||
static function readUInt16(bytes:ByteStream):Int {
|
||||
var int = bytes.bytes.getUInt16(bytes.position);
|
||||
bytes.position += 2;
|
||||
return int;
|
||||
}
|
||||
|
||||
static function checkHeader(bytes:ByteStream) {
|
||||
// 42 4D
|
||||
if (readByte(bytes) != 66 || readByte(bytes) != 77)
|
||||
throw 'Bad bmp header!';
|
||||
}
|
||||
|
||||
public static function loadBitmapData(bmpFile:String) {
|
||||
var stream = getByteStream(bmpFile);
|
||||
checkHeader(stream);
|
||||
|
||||
var fileSize = readUInt32(stream);
|
||||
var fileReserved = readUInt32(stream);
|
||||
var fileOffBits = readUInt32(stream);
|
||||
|
||||
var imgSize = readUInt32(stream);
|
||||
|
||||
var imgWidth = readInt32(stream);
|
||||
var imgHeight = readInt32(stream);
|
||||
|
||||
var planes = readUInt16(stream);
|
||||
var bitsPerPixel = readUInt16(stream);
|
||||
|
||||
var compression = readUInt32(stream);
|
||||
var sizeImage = readUInt32(stream);
|
||||
|
||||
var xPixelsPerMeter = readInt32(stream);
|
||||
var yPixelsPerMeter = readInt32(stream);
|
||||
|
||||
var colorsUsed = readUInt32(stream);
|
||||
var colorsImportant = readUInt32(stream);
|
||||
|
||||
var colors:Array<FlxColor> = [];
|
||||
|
||||
for (c in 0...colorsUsed) {
|
||||
var blue = readByte(stream);
|
||||
var green = readByte(stream);
|
||||
var red = readByte(stream);
|
||||
var _ = readByte(stream);
|
||||
colors.push(FlxColor.fromRGB(red, green, blue));
|
||||
}
|
||||
|
||||
// BMPs can be encoded upside-down when the height is negative
|
||||
var realHeight:Int = cast Math.abs(imgHeight);
|
||||
var data = new BitmapData(imgWidth, realHeight);
|
||||
var y = if (imgHeight > 0) 0 else realHeight - 1;
|
||||
var dy = if (imgHeight > 0) 1 else -1;
|
||||
var rowPadding:Int = cast (sizeImage - (realHeight * imgWidth)) / realHeight;
|
||||
for (_ in 0... realHeight) {
|
||||
for (x in 0... imgWidth) {
|
||||
var colorIdx = readByte(stream);
|
||||
data.setPixel(x, realHeight-1-y, colors[colorIdx]);
|
||||
}
|
||||
for (_ in 0... rowPadding) {
|
||||
readByte(stream);
|
||||
}
|
||||
|
||||
y += dy;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
@@ -9,7 +9,7 @@
|
||||
(method &override :Void create []
|
||||
(super.create)
|
||||
// TODO load from a more accessible path
|
||||
(let [bitmapData (BitmapData.fromFile "Data/Terrain Graphics/G796.png")
|
||||
(let [bitmapData (Bmp.loadBitmapData "Data/Terrain Graphics/G796.bmp")
|
||||
spriteSheet (.loadGraphic (new FlxSprite) (FlxGraphic.fromBitmapData bitmapData))]
|
||||
(spriteSheet.replaceColor FlxColor.WHITE FlxColor.TRANSPARENT)
|
||||
(let [sheetWidth (/ (- spriteSheet.frameWidth SPRITE_PADDING) (+ SPRITE_PADDING FLOOR_WIDTH))
|
||||
|
Reference in New Issue
Block a user