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 
13 import PixelPerfectEngine.graphics.bitmap;
14 import PixelPerfectEngine.graphics.raster;
15 import PixelPerfectEngine.graphics.fontsets;
16 
17 import PixelPerfectEngine.extbmp.extbmp;
18 
19 import derelict.sdl2.mixer;
20 
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 /**
43  * FILE FORMAT IS DEPRECATED! USE XMP INSTEAD!
44  */
45 public void loadPaletteFromFile(string filename, Raster target){
46 	auto palette = cast(ubyte[])std.file.read(filename);
47 	//writeln(palette.length);
48 	target.setupPalette(0);
49 	int max = (palette.length / 3);
50 	for(int i ; i < max ; i++){
51 		target.addColor(palette[(i * 3)], palette[(i * 3) + 1], palette[(i * 3) + 2]);
52 		//writeln(i);
53 	}
54 }
55 
56 /**
57  * FILE FORMAT IS DEPRECATED! USE XMP INSTEAD!
58  */
59 public void load24bitPaletteFromFile(string filename, Raster target){
60 	auto palette = cast(ubyte[])std.file.read(filename);
61 	//writeln(palette.length);
62 	target.setupPalette(0);
63 	int max = (palette.length / 3);
64 	target.addColor(palette[0], palette[1], palette[2], 0);
65 	for(int i = 1; i < max ; i++){
66 		target.addColor(palette[(i * 3)], palette[(i * 3) + 1], palette[(i * 3) + 2]);
67 		//writeln(i);
68 	}
69 }
70 
71 /**
72  * Gets the bitmap from the XMP file.
73  */
74 Bitmap16Bit loadBitmapFromXMP(ExtendibleBitmap xmp, string ID){
75 
76 	Bitmap16Bit result = new Bitmap16Bit(xmp.get16bitBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID));
77 	return result;
78 }
79 
80 Bitmap32Bit load32BitBitmapFromXMP(ExtendibleBitmap xmp, string ID){
81 	Bitmap32Bit result = new Bitmap32Bit(cast(ubyte[])xmp.getBitmap(ID),xmp.getXsize(ID),xmp.getYsize(ID));
82 	return result;
83 }
84 
85 public void loadPaletteFromXMP(ExtendibleBitmap xmp, string ID, Raster target){
86 	target.palette = cast(ubyte[])xmp.getPalette(ID);
87 	//writeln(target.palette);
88 	/*target.setupPalette(0);
89 	int max = (palette.length / 3);
90 	for(int i ; i < max ; i++){
91 		target.addColor(palette[(i * 3)], palette[(i * 3) + 1], palette[(i * 3) + 2]);
92 		//writeln(i);
93 	}*/
94 
95 }
96 
97 Fontset loadFontsetFromXMP(ExtendibleBitmap xmp, string fontName){
98 	Bitmap16Bit[wchar] characters;
99 	foreach(s;xmp.bitmapID){
100 		//writeln(parseHex(s[fontName.length..(s.length-1)]));
101 		//if(fontName == s[0..(fontName.length-1)]){
102 			characters[to!wchar(parseHex(s[fontName.length..s.length]))] = loadBitmapFromXMP(xmp,s);
103 		//}
104 	}
105 	return new Fontset(fontName, characters['0'].getY, characters);
106 }
107 
108 public deprecated void saveBitmapToFile(Bitmap16Bit[] bitmap, string filename){
109 	ushort[] rawData;
110 	rawData ~= to!ushort(bitmap.length);
111 	rawData ~= to!ushort(bitmap[0].getX());
112 	rawData ~= to!ushort(bitmap[0].getY());
113 	for(int i; i < bitmap.length; i++){
114 		for(int j; j < bitmap[0].getY(); j++)
115 			rawData ~= bitmap[i].readRow(j);
116 	}
117 	std.file.write(filename, rawData);
118 }
119 
120 public Mix_Chunk* loadSoundFromFile(const char* filename){
121 	return Mix_LoadWAV(filename);
122 }
123 
124 File loadFileFromDisk(string filename){
125 	return File(filename, "r");
126 }