1 module PixelPerfectEngine.system.file; 2 /* 3 * Copyright (C) 2015-2017, by Laszlo Szeremi under the Boost license. 4 * 5 * Pixel Perfect Engine, file module 6 */ 7 8 import std.file; 9 import std.stdio; 10 import std.conv; 11 import PixelPerfectEngine.system.etc; 12 import PixelPerfectEngine.system.exc; 13 14 import PixelPerfectEngine.graphics.bitmap; 15 import PixelPerfectEngine.graphics.raster; 16 import PixelPerfectEngine.graphics.fontsets; 17 18 import PixelPerfectEngine.extbmp.extbmp; 19 20 import derelict.sdl2.mixer; 21 22 /** 23 * FILE FORMAT IS DEPRECATED! USE XMP INSTEAD! 24 */ 25 public Bitmap16Bit[] loadBitmapFromFile(string filename){ 26 auto fileData = cast(ushort[])std.file.read(filename); 27 ushort x = fileData[1], y = fileData[2], nOfSprites = fileData[0]; 28 //writeln(fileData.length); 29 Bitmap16Bit[] bar; 30 for(int i; i < nOfSprites; i++){ 31 //writeln(3+(x*y*i)); 32 ushort[] pixeldata = fileData[(3+(x*y*i)) .. (3+(x*y*(i+1)))]; 33 Bitmap16Bit foo = new Bitmap16Bit(pixeldata , x, y); 34 35 bar ~= foo; 36 37 } 38 39 return bar; 40 } 41 /** 42 * Gets a bitmap from the XMP file. 43 */ 44 T loadBitmapFromXMP(T)(ExtendibleBitmap xmp, string ID){ 45 static if(T.stringof == Bitmap4Bit.stringof || T.stringof == Bitmap8Bit.stringof){ 46 T result = new T(cast(ubyte[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID),null); 47 return result; 48 }else static if(T.stringof == Bitmap16Bit.stringof){ 49 T result;// = new T(cast(ushort[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 50 switch(xmp.bitdepth[xmp.searchForID(ID)]){ 51 case "16bit": 52 result = new T(cast(ushort[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 53 break; 54 case "8bit": 55 ushort[] subresult; 56 ubyte[] input = cast(ubyte[])xmp.getBitmap(ID); 57 subresult.length = input.length; 58 for(int i ; i < subresult.length ; i++){ 59 subresult[i] = input[i]; 60 } 61 result = new T(subresult,xmp.getXsize(ID),xmp.getYsize(ID)); 62 break; 63 case "4bit": 64 ushort[] subresult; 65 ubyte[] input = cast(ubyte[])xmp.getBitmap(ID); 66 subresult.length = input.length; 67 for(int i ; i < subresult.length ; i++){ 68 if(i & 1) 69 subresult[i] = input[i>>1]>>4; 70 else 71 subresult[i] = input[i>>1]&0b0000_1111; 72 } 73 result = new T(subresult,xmp.getXsize(ID),xmp.getYsize(ID)); 74 break; 75 /*case "1bit": 76 77 break;*/ 78 default: 79 throw new FileAccessException("Bitdepth error!"); 80 } 81 82 return result; 83 }else static if(T.stringof == Bitmap32Bit.stringof){ 84 T result = new T(cast(Color[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 85 return result; 86 }else static if(T.stringof == ABitmap.stringof){ 87 88 switch(xmp.bitdepth[xmp.searchForID(ID)]){ 89 case "4bit": 90 return new Bitmap4Bit(cast(ubyte[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 91 case "8bit": 92 return new Bitmap8Bit(cast(ubyte[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 93 case "16bit": 94 return new Bitmap16Bit(cast(ushort[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 95 case "32bit": 96 return new Bitmap32Bit(cast(Color[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID)); 97 default: 98 return null; 99 100 } 101 102 }else static assert("Template argument \'" ~ T.stringof ~ "\' not supported in function \'T loadBitmapFromXMP(T)(ExtendibleBitmap xmp, string ID)\'"); 103 } 104 /** 105 * Loads a palette from an XMP file. 106 */ 107 public void loadPaletteFromXMP(ExtendibleBitmap xmp, string ID, Raster target, int offset = 0){ 108 target.palette = cast(Color[])xmp.getPalette(ID); 109 //writeln(target.palette); 110 /*target.setupPalette(0); 111 int max = (palette.length / 3); 112 for(int i ; i < max ; i++){ 113 target.addColor(palette[(i * 3)], palette[(i * 3) + 1], palette[(i * 3) + 2]); 114 //writeln(i); 115 }*/ 116 117 } 118 /** 119 * Loads a fontset from an XMP file. 120 */ 121 Fontset!Bitmap16Bit loadFontsetFromXMP(ExtendibleBitmap xmp, string fontName){ 122 Bitmap16Bit[wchar] characters; 123 foreach(s;xmp.bitmapID){ 124 //writeln(parseHex(s[fontName.length..(s.length-1)])); 125 //if(fontName == s[0..(fontName.length-1)]){ 126 characters[to!wchar(parseHex(s[fontName.length..s.length]))] = loadBitmapFromXMP!Bitmap16Bit(xmp,s); 127 //} 128 } 129 return new Fontset!Bitmap16Bit(fontName, characters['0'].height, characters); 130 } 131 /** 132 * Loads a *.wav file if SDL2 mixer is used 133 */ 134 public Mix_Chunk* loadSoundFromFile(const char* filename){ 135 return Mix_LoadWAV(filename); 136 } 137 138 File loadFileFromDisk(string filename){ 139 return File(filename, "r"); 140 }