mirror of
https://github.com/RandomityGuy/MBHaxe.git
synced 2026-04-26 04:31:40 +00:00
bmp support
This commit is contained in:
parent
c7da2c2de6
commit
70daee42f7
3 changed files with 153 additions and 48 deletions
|
|
@ -329,6 +329,31 @@ class ResourceLoader {
|
||||||
|
|
||||||
public static function getTextureRealpath(path:String) {
|
public static function getTextureRealpath(path:String) {
|
||||||
if (zipFilesystem.exists(path.toLowerCase())) {
|
if (zipFilesystem.exists(path.toLowerCase())) {
|
||||||
|
if (StringTools.endsWith(path.toLowerCase(), ".bmp")) { // Handle bmp specially
|
||||||
|
var bmpContents = zipFilesystem.get(path.toLowerCase());
|
||||||
|
var bmpreader = new format.bmp.Reader(new haxe.io.BytesInput(bmpContents.getBytes()));
|
||||||
|
var bmpdata = bmpreader.read();
|
||||||
|
|
||||||
|
var bbuf = new haxe.io.BytesBuffer();
|
||||||
|
var i = 0;
|
||||||
|
while (i < bmpdata.pixels.length) {
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i));
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i + 1));
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i + 2));
|
||||||
|
bbuf.addByte(1);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
var pixs = new hxd.Pixels(bmpdata.header.width, bmpdata.header.height, bbuf.getBytes(), hxd.PixelFormat.BGRA);
|
||||||
|
var tex = h3d.mat.Texture.fromPixels(pixs);
|
||||||
|
tex.mipMap = Nearest;
|
||||||
|
tex.flags.set(MipMapped);
|
||||||
|
|
||||||
|
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
|
||||||
|
textureCache.set(path, textureresource);
|
||||||
|
|
||||||
|
return textureresource;
|
||||||
|
} else {
|
||||||
var img = new hxd.res.Image(zipFilesystem.get(path.toLowerCase()));
|
var img = new hxd.res.Image(zipFilesystem.get(path.toLowerCase()));
|
||||||
Image.setupTextureFlags = (texObj) -> {
|
Image.setupTextureFlags = (texObj) -> {
|
||||||
texObj.flags.set(MipMapped);
|
texObj.flags.set(MipMapped);
|
||||||
|
|
@ -340,9 +365,35 @@ class ResourceLoader {
|
||||||
|
|
||||||
return textureresource;
|
return textureresource;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (textureCache.exists(path))
|
if (textureCache.exists(path))
|
||||||
return textureCache.get(path);
|
return textureCache.get(path);
|
||||||
if (fileSystem.exists(path)) {
|
if (fileSystem.exists(path)) {
|
||||||
|
if (StringTools.endsWith(path.toLowerCase(), ".bmp")) { // Handle bmp specially
|
||||||
|
var bmpContents = zipFilesystem.get(path.toLowerCase());
|
||||||
|
var bmpreader = new format.bmp.Reader(new haxe.io.BytesInput(bmpContents.getBytes()));
|
||||||
|
var bmpdata = bmpreader.read();
|
||||||
|
|
||||||
|
var bbuf = new haxe.io.BytesBuffer();
|
||||||
|
var i = 0;
|
||||||
|
while (i < bmpdata.pixels.length) {
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i));
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i + 1));
|
||||||
|
bbuf.addByte(bmpdata.pixels.get(i + 2));
|
||||||
|
bbuf.addByte(1);
|
||||||
|
i += 3;
|
||||||
|
}
|
||||||
|
var pixs = new hxd.Pixels(bmpdata.header.width, bmpdata.header.height, bbuf.getBytes(), hxd.PixelFormat.BGRA);
|
||||||
|
pixs.setFlip(true);
|
||||||
|
var tex = h3d.mat.Texture.fromPixels(pixs);
|
||||||
|
tex.mipMap = Nearest;
|
||||||
|
tex.flags.set(MipMapped);
|
||||||
|
|
||||||
|
var textureresource = new Resource(tex, path, textureCache, tex -> tex.dispose());
|
||||||
|
textureCache.set(path, textureresource);
|
||||||
|
|
||||||
|
return textureresource;
|
||||||
|
} else {
|
||||||
var img = loader.load(path).toImage();
|
var img = loader.load(path).toImage();
|
||||||
Image.setupTextureFlags = (texObj) -> {
|
Image.setupTextureFlags = (texObj) -> {
|
||||||
texObj.flags.set(MipMapped);
|
texObj.flags.set(MipMapped);
|
||||||
|
|
@ -355,6 +406,7 @@ class ResourceLoader {
|
||||||
|
|
||||||
return textureresource;
|
return textureresource;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
29
src/Sky.hx
29
src/Sky.hx
|
|
@ -94,6 +94,7 @@ class Sky extends Object {
|
||||||
}
|
}
|
||||||
|
|
||||||
var worker = new ResourceLoaderWorker(() -> {
|
var worker = new ResourceLoaderWorker(() -> {
|
||||||
|
var fnames = [];
|
||||||
for (i in 0...6) {
|
for (i in 0...6) {
|
||||||
var line = StringTools.trim(lines[i]);
|
var line = StringTools.trim(lines[i]);
|
||||||
var filenames = ResourceLoader.getFullNamesOf(dmlDirectory + '/' + line);
|
var filenames = ResourceLoader.getFullNamesOf(dmlDirectory + '/' + line);
|
||||||
|
|
@ -102,10 +103,13 @@ class Sky extends Object {
|
||||||
skyboxImages.push(pixels);
|
skyboxImages.push(pixels);
|
||||||
// var tex = new h3d.mat.Texture();
|
// var tex = new h3d.mat.Texture();
|
||||||
// skyboxImages.push(new BitmapData(128, 128));
|
// skyboxImages.push(new BitmapData(128, 128));
|
||||||
|
fnames.push("");
|
||||||
Console.error("Skybox image " + filenames[0] + " does not exist");
|
Console.error("Skybox image " + filenames[0] + " does not exist");
|
||||||
} else {
|
} else {
|
||||||
var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap();
|
var pixels = ResourceLoader.getTexture(filenames[0]).resource.capturePixels(0, 0);
|
||||||
var pixels = image.getPixels();
|
fnames.push(filenames[0]);
|
||||||
|
// var image = ResourceLoader.getResource(filenames[0], ResourceLoader.getImage, this.imageResources).toBitmap();
|
||||||
|
// var pixels = image.getPixels();
|
||||||
skyboxImages.push(pixels);
|
skyboxImages.push(pixels);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -118,15 +122,36 @@ class Sky extends Object {
|
||||||
maxwidth = texture.width;
|
maxwidth = texture.width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle the bmp files specially to flip y
|
||||||
|
if (StringTools.endsWith(fnames[0].toLowerCase(), ".bmp")) {
|
||||||
|
Util.flipImage(skyboxImages[0], true, true);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[0], true, false);
|
Util.flipImage(skyboxImages[0], true, false);
|
||||||
|
if (StringTools.endsWith(fnames[4].toLowerCase(), ".bmp")) {
|
||||||
|
Util.flipImage(skyboxImages[4], true, true);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[4], true, false);
|
Util.flipImage(skyboxImages[4], true, false);
|
||||||
Util.rotateImage(skyboxImages[5], Math.PI);
|
Util.rotateImage(skyboxImages[5], Math.PI);
|
||||||
|
if (StringTools.endsWith(fnames[5].toLowerCase(), ".bmp")) {
|
||||||
|
Util.flipImage(skyboxImages[5], true, true);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[5], true, false);
|
Util.flipImage(skyboxImages[5], true, false);
|
||||||
Util.rotateImage(skyboxImages[1], -Math.PI / 2);
|
Util.rotateImage(skyboxImages[1], -Math.PI / 2);
|
||||||
|
if (StringTools.endsWith(fnames[1].toLowerCase(), ".bmp")) {
|
||||||
|
// Util.flipImage(skyboxImages[1], true, true);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[1], true, false);
|
Util.flipImage(skyboxImages[1], true, false);
|
||||||
Util.rotateImage(skyboxImages[2], Math.PI);
|
Util.rotateImage(skyboxImages[2], Math.PI);
|
||||||
|
if (StringTools.endsWith(fnames[2].toLowerCase(), ".bmp")) {
|
||||||
|
Util.flipImage(skyboxImages[2], true, true);
|
||||||
|
// Util.flipImage(skyboxImages[2], true, false);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[2], true, false);
|
Util.flipImage(skyboxImages[2], true, false);
|
||||||
Util.rotateImage(skyboxImages[3], Math.PI / 2);
|
Util.rotateImage(skyboxImages[3], Math.PI / 2);
|
||||||
|
if (StringTools.endsWith(fnames[3].toLowerCase(), ".bmp")) {
|
||||||
|
// Util.flipImage(skyboxImages[3], true, false);
|
||||||
|
// Util.flipImage(skyboxImages[3], false, true);
|
||||||
|
} else
|
||||||
Util.flipImage(skyboxImages[3], true, false);
|
Util.flipImage(skyboxImages[3], true, false);
|
||||||
|
|
||||||
var cubemaptexture = new Texture(maxheight, maxwidth, [Cube]);
|
var cubemaptexture = new Texture(maxheight, maxwidth, [Cube]);
|
||||||
|
|
|
||||||
28
src/Util.hx
28
src/Util.hx
|
|
@ -124,6 +124,33 @@ class Util {
|
||||||
|
|
||||||
public static function flipImage(bitmap:hxd.Pixels, hflip:Bool, vflip:Bool) {
|
public static function flipImage(bitmap:hxd.Pixels, hflip:Bool, vflip:Bool) {
|
||||||
var curpixels = bitmap.clone();
|
var curpixels = bitmap.clone();
|
||||||
|
|
||||||
|
if (hflip && vflip) {
|
||||||
|
for (x in 0...curpixels.width) {
|
||||||
|
for (y in 0...curpixels.height) {
|
||||||
|
var psrc = ((curpixels.width - x - 1)
|
||||||
|
+ (curpixels.width - y - 1) * curpixels.width) * @:privateAccess curpixels.bytesPerPixel
|
||||||
|
+ curpixels.offset;
|
||||||
|
|
||||||
|
var pdest = ((x + y * curpixels.width) * @:privateAccess curpixels.bytesPerPixel) + curpixels.offset;
|
||||||
|
|
||||||
|
switch (curpixels.format) {
|
||||||
|
case R8:
|
||||||
|
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
|
||||||
|
case BGRA | RGBA | ARGB:
|
||||||
|
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
|
||||||
|
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
|
||||||
|
bitmap.bytes.set(pdest + 2, curpixels.bytes.get(psrc + 2));
|
||||||
|
bitmap.bytes.set(pdest + 3, curpixels.bytes.get(psrc + 3));
|
||||||
|
case RG8:
|
||||||
|
bitmap.bytes.set(pdest, curpixels.bytes.get(psrc));
|
||||||
|
bitmap.bytes.set(pdest + 1, curpixels.bytes.get(psrc + 1));
|
||||||
|
default:
|
||||||
|
null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
if (hflip)
|
if (hflip)
|
||||||
for (x in 0...curpixels.width) {
|
for (x in 0...curpixels.width) {
|
||||||
for (y in 0...curpixels.height) {
|
for (y in 0...curpixels.height) {
|
||||||
|
|
@ -171,6 +198,7 @@ class Util {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static function splitIgnoreStringLiterals(str:String, splitter:String, strLiteralToken = '"') {
|
public static function splitIgnoreStringLiterals(str:String, splitter:String, strLiteralToken = '"') {
|
||||||
var indices = [];
|
var indices = [];
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue