1 /*
2 Copyright (C) 2015, by Laszlo Szeremi under the Boost license.
3 
4 VDP Engine
5 */
6 
7 
8 module app;
9 
10 import std.stdio;
11 import std..string;
12 import std.conv;
13 import std.random;
14 
15 import bindbc.sdl;
16 //import derelict.freeimage.freeimage;
17 
18 //import system.config;
19 
20 import PixelPerfectEngine.graphics.outputScreen;
21 import PixelPerfectEngine.graphics.raster;
22 import PixelPerfectEngine.graphics.layers;
23 
24 import PixelPerfectEngine.graphics.bitmap;
25 //import PixelPerfectEngine.collision;
26 import PixelPerfectEngine.system.inputHandler;
27 import PixelPerfectEngine.system.file;
28 import PixelPerfectEngine.system.etc;
29 import PixelPerfectEngine.system.config;
30 import PixelPerfectEngine.system.binarySearchTree;
31 import PixelPerfectEngine.system.common;
32 
33 import editor;
34 import PixelPerfectEngine.extbmp.extbmp;
35 
36 int main(string[] args){
37 	initialzeSDL();
38 
39 	if(args.length > 1){
40 		if(args[1] == "--test"){
41 			TileLayerTest prg = new TileLayerTest();
42 			prg.whereTheMagicHappens;
43 			writeln(prg.isRunning);
44 			return 0;
45 		}
46 	}
47 
48 	Editor e = new Editor(args);
49 	e.whereTheMagicHappens;
50 
51 	//testBinarySearchTrees(11, 1);
52 	return 0;
53 }
54 
55 void testBinarySearchTrees(int nOfElements, int nOfTimes){
56 	for(int i; i < nOfTimes; i++){
57 		writeln("start test no.",i);
58 		BinarySearchTree!(int,int) sequ, rand;
59 		//sequential element test
60 		for(int j; j < nOfElements; j++){
61 			sequ[j] = j;
62 		}
63 		writeln(sequ);
64 		//randomized element test
65 		for(int j; j < nOfElements; j++){
66 			int k = uniform(short.min,short.max);
67 			rand[k] = k;
68 		}
69 		writeln(rand);
70 		//rand.optimize();
71 		//writeln(rand);
72 	}
73 	readln();
74 }
75 
76 class TileLayerTest : SystemEventListener, InputListener{
77 	bool isRunning, up, down, left, right, scrup, scrdown, scrleft, scrright;
78 	OutputScreen output;
79 	Raster r;
80 	TileLayer t;
81 	TransformableTileLayer!(Bitmap16Bit,32,32) tt;
82 	ABitmap[] tiles;
83 	Bitmap16Bit dlangMan;
84 	SpriteLayer s;
85 	//Bitmap16Bit[wchar] tiles;
86 	InputHandler ih;
87 	float theta;
88 	//CollisionDetector c;
89 	this(){
90 		theta = 0;
91 		isRunning = true;
92 		ExtendibleBitmap tileSource = new ExtendibleBitmap("../assets/tiletest.xmp");
93 		ExtendibleBitmap spriteSource = new ExtendibleBitmap("../assets/collisionTest.xmp");
94 		//t = new TileLayer(32,32, LayerRenderingMode.COPY);
95 		tt = new TransformableTileLayer!(Bitmap16Bit,32,32)(LayerRenderingMode.COPY);
96 		s = new SpriteLayer(LayerRenderingMode.ALPHA_BLENDING);
97 		//c = new CollisionDetector();
98 		dlangMan = loadBitmapFromXMP!Bitmap16Bit(spriteSource,"DLangMan");
99 		//CollisionModel cm = new CollisionModel(dlangMan.width, dlangMan.height, dlangMan.generateStandardCollisionModel());
100 		dlangMan.offsetIndexes(256,false);
101 		s.addSprite(dlangMan,0,0,0);
102 		//s.scaleSpriteHoriz(0,-1024);
103 		//s.scaleSpriteVert(0,-1024);
104 		for(int i = 1 ; i < 2 ; i++){
105 			s.addSprite(dlangMan,i,uniform(-31,320),uniform(-31,240));
106 		}
107 		//s.collisionDetector[1] = c;
108 		//c.source = s;
109 		//c.addCollisionModel(cm,0);
110 		//c.addCollisionModel(cm,1);
111 		//c.addCollisionListener(this);
112 		tiles.length = tileSource.bitmapID.length;
113 		for(int i; i < tileSource.bitmapID.length; i++){
114 			string hex = tileSource.bitmapID[i];
115 			//writeln(hex[hex.length-4..hex.length]);
116 			ABitmap ab = loadBitmapFromXMP!Bitmap16Bit(tileSource, hex);
117 			tiles[i] = ab;
118 			tt.addTile(ab, to!wchar(parseHex(hex[hex.length-4..hex.length])));
119 		}
120 		//wchar[] mapping;
121 		MappingElement[] mapping;
122 		mapping.length = 16*16;
123 		//attrMapping.length = 256*256;
124 		for(int i; i < mapping.length; i++){
125 			//mapping[i] = to!wchar(uniform(0x0000,0x00AA));
126 			int rnd = uniform(0,1024);
127 			//attrMapping[i] = BitmapAttrib(rnd & 1 ? true : false, rnd & 2 ? true : false);
128 			mapping[i] = MappingElement(to!wchar(uniform(0x0000,0x00AA)), BitmapAttrib(rnd & 1 ? true : false, rnd & 2 ? true : false));
129 		}
130 		ih = new InputHandler();
131 		ih.sel ~= this;
132 		ih.il ~= this;
133 		ih.kb ~= KeyBinding(0, SDL_SCANCODE_UP,0, "up", Devicetype.KEYBOARD, KeyModifier.ANY);
134 		ih.kb ~= KeyBinding(0, SDL_SCANCODE_DOWN,0, "down", Devicetype.KEYBOARD, KeyModifier.ANY);
135 		ih.kb ~= KeyBinding(0, SDL_SCANCODE_LEFT,0, "left", Devicetype.KEYBOARD, KeyModifier.ANY);
136 		ih.kb ~= KeyBinding(0, SDL_SCANCODE_RIGHT,0, "right", Devicetype.KEYBOARD, KeyModifier.ANY);
137 		ih.kb ~= KeyBinding(0, ScanCode.np8,0, "scrup", Devicetype.KEYBOARD, KeyModifier.ANY);
138 		ih.kb ~= KeyBinding(0, ScanCode.np2,0, "scrdown", Devicetype.KEYBOARD, KeyModifier.ANY);
139 		ih.kb ~= KeyBinding(0, ScanCode.np4,0, "scrleft", Devicetype.KEYBOARD, KeyModifier.ANY);
140 		ih.kb ~= KeyBinding(0, ScanCode.np6,0, "scrright", Devicetype.KEYBOARD, KeyModifier.ANY);
141 		ih.kb ~= KeyBinding(0, ScanCode.F1,0, "A+", Devicetype.KEYBOARD, KeyModifier.ANY);
142 		ih.kb ~= KeyBinding(0, ScanCode.F2,0, "A-", Devicetype.KEYBOARD, KeyModifier.ANY);
143 		ih.kb ~= KeyBinding(0, ScanCode.F3,0, "B+", Devicetype.KEYBOARD, KeyModifier.ANY);
144 		ih.kb ~= KeyBinding(0, ScanCode.F4,0, "B-", Devicetype.KEYBOARD, KeyModifier.ANY);
145 		ih.kb ~= KeyBinding(0, ScanCode.F5,0, "C+", Devicetype.KEYBOARD, KeyModifier.ANY);
146 		ih.kb ~= KeyBinding(0, ScanCode.F6,0, "C-", Devicetype.KEYBOARD, KeyModifier.ANY);
147 		ih.kb ~= KeyBinding(0, ScanCode.F7,0, "D+", Devicetype.KEYBOARD, KeyModifier.ANY);
148 		ih.kb ~= KeyBinding(0, ScanCode.F8,0, "D-", Devicetype.KEYBOARD, KeyModifier.ANY);
149 		ih.kb ~= KeyBinding(0, ScanCode.F9,0, "x0+", Devicetype.KEYBOARD, KeyModifier.ANY);
150 		ih.kb ~= KeyBinding(0, ScanCode.F10,0, "x0-", Devicetype.KEYBOARD, KeyModifier.ANY);
151 		ih.kb ~= KeyBinding(0, ScanCode.PAGEUP,0, "y0+", Devicetype.KEYBOARD, KeyModifier.ANY);
152 		ih.kb ~= KeyBinding(0, ScanCode.PAGEDOWN,0, "y0-", Devicetype.KEYBOARD, KeyModifier.ANY);
153 		ih.kb ~= KeyBinding(0, ScanCode.NP_PLUS,0, "theta+", Devicetype.KEYBOARD, KeyModifier.ANY);
154 		ih.kb ~= KeyBinding(0, ScanCode.NP_MINUS,0, "theta-", Devicetype.KEYBOARD, KeyModifier.ANY);
155 		ih.kb ~= KeyBinding(0, ScanCode.n1,0, "sV+", Devicetype.KEYBOARD, KeyModifier.ANY);
156 		ih.kb ~= KeyBinding(0, ScanCode.n2,0, "sV-", Devicetype.KEYBOARD, KeyModifier.ANY);
157 		ih.kb ~= KeyBinding(0, ScanCode.n3,0, "sH+", Devicetype.KEYBOARD, KeyModifier.ANY);
158 		ih.kb ~= KeyBinding(0, ScanCode.n4,0, "sH-", Devicetype.KEYBOARD, KeyModifier.ANY);
159 		ih.kb ~= KeyBinding(0, ScanCode.Q,0, "HM", Devicetype.KEYBOARD, KeyModifier.ANY);
160 		ih.kb ~= KeyBinding(0, ScanCode.W,0, "VM", Devicetype.KEYBOARD, KeyModifier.ANY);
161 
162 		tt.loadMapping(16,16,mapping);
163 		tt.setWarpMode(true);
164 		//tt.hBlankInterrupt = &ttlHBlankInterrupt;
165 		//t.setWrapMode(true);
166 		//tt.D = -256;
167 		output = new OutputScreen("TileLayer test", 1280,960);
168 		r = new Raster(320,240,output);
169 		output.setMainRaster(r);
170 		loadPaletteFromXMP(tileSource, "default", r);
171 		/*for(int y ; y < 240 ; y++){
172 			for(int x ; x < 240 ; x++){
173 				writeln('[',x,',',y,"] : ", t.transformFunc([x,y]));
174 			}
175 		}*/
176 		r.addLayer(tt, 0);
177 		r.addLayer(s, 1);
178 		r.palette ~= cast(Color[])spriteSource.getPalette("default");
179 		r.palette[0].alpha = 255;
180 		r.palette[256].raw = 0;
181 		writeln(tt);
182 		//r.palette[0] = 255;
183 		//r.addRefreshListener(output, 0);
184 
185 	}
186 	private @nogc void ttlHBlankInterrupt(ref short[4] localABCD, ref short[2] localsXsY, ref short[2] localx0y0, short y){
187 		localABCD[0]++;
188 	}
189 	public void whereTheMagicHappens(){
190 		while(isRunning){
191 			r.refresh();
192 			ih.test();
193 			if(up) s.relMoveSprite(0,0,-1);
194 			if(down) s.relMoveSprite(0,0,1);
195 			if(left) s.relMoveSprite(0,-1,0);
196 			if(right) s.relMoveSprite(0,1,0);
197 			if(scrup) tt.relScroll(0,-1);
198 			if(scrdown) tt.relScroll(0,1);
199 			if(scrleft) tt.relScroll(-1,0);
200 			if(scrright) tt.relScroll(1,0);
201 			//t.relScroll(1,0);
202 		}
203 	}
204 	override public void onQuit() {
205 		isRunning = false;
206 	}
207 	override public void controllerAdded(uint ID) {
208 
209 	}
210 	override public void controllerRemoved(uint ID) {
211 
212 	}
213 	override public void keyPressed(string ID,uint timestamp,uint devicenumber,uint devicetype) {
214 		//writeln(ID);
215 		import PixelPerfectEngine.graphics.transformFunctions;
216 		switch(ID){
217 			case "up": up = true; break;
218 			case "down": down = true; break;
219 			case "left": left = true; break;
220 			case "right": right = true; break;
221 			case "scrup": scrup = true; break;
222 			case "scrdown": scrdown = true; break;
223 			case "scrleft": scrleft = true; break;
224 			case "scrright": scrright = true; break;
225 			case "A+": tt.A = cast(short)(tt.A + 16);
226 				break;
227 			case "A-": tt.A = cast(short)(tt.A - 16);
228 				break;
229 			case "B+": tt.B = cast(short)(tt.B + 16); break;
230 			case "B-": tt.B = cast(short)(tt.B - 16); break;
231 			case "C+": tt.C = cast(short)(tt.C + 16); break;
232 			case "C-": tt.C = cast(short)(tt.C - 16); break;
233 			case "D+": tt.D = cast(short)(tt.D + 16); break;
234 			case "D-": tt.D = cast(short)(tt.D - 16); break;
235 			case "x0+": tt.x_0 = cast(short)(tt.x_0 + 1); break;
236 			case "x0-": tt.x_0 = cast(short)(tt.x_0 - 1); break;
237 			case "y0+": tt.y_0 = cast(short)(tt.y_0 + 1); break;
238 			case "y0-": tt.y_0 = cast(short)(tt.y_0 - 1); break;
239 			case "sH-":
240 				if(s.readSpriteAttribute!("scaleHoriz", int)(0) == 16){
241 					s.scaleSpriteHoriz(0,-16);
242 					return;
243 				}
244 				s.scaleSpriteHoriz(0,s.readSpriteAttribute!("scaleHoriz", int)(0) - 16);
245 				writeln(s.readSpriteAttribute!("scaleHoriz", int)(0));
246 				break;
247 			case "sH+":
248 				if(s.readSpriteAttribute!("scaleHoriz", int)(0) == -16){
249 					s.scaleSpriteHoriz(0,16);
250 					return;
251 				}
252 				s.scaleSpriteHoriz(0,s.readSpriteAttribute!("scaleHoriz", int)(0) + 16);
253 				writeln(s.readSpriteAttribute!("scaleHoriz", int)(0));
254 				break;
255 			case "sV-":
256 				if(s.readSpriteAttribute!("scaleVert", int)(0) == 16){
257 					s.scaleSpriteVert(0,-16);
258 					return;
259 				}
260 				s.scaleSpriteVert(0,s.readSpriteAttribute!("scaleVert", int)(0) - 16);
261 				break;
262 			case "sV+":
263 				if(s.readSpriteAttribute!("scaleVert", int)(0) == -16){
264 					s.scaleSpriteVert(0,16);
265 					return;
266 				}
267 				s.scaleSpriteVert(0,s.readSpriteAttribute!("scaleVert", int)(0) + 16);
268 				break;
269 			case "HM":
270 				s.scaleSpriteHoriz(0,s.readSpriteAttribute!("scaleHoriz", int)(0) * -1);
271 				break;
272 			case "VM":
273 				s.scaleSpriteVert(0,s.readSpriteAttribute!("scaleVert", int)(0) * -1);
274 				break;
275 			case "theta+":
276 				theta += 1;
277 				short[4] newTP = rotateFunction(theta);
278 				tt.A = newTP[0];
279 				tt.B = newTP[1];
280 				tt.C = newTP[2];
281 				tt.D = newTP[3];
282 				break;
283 			case "theta-":
284 				theta -= 1;
285 				short[4] newTP = rotateFunction(theta);
286 				tt.A = newTP[0];
287 				tt.B = newTP[1];
288 				tt.C = newTP[2];
289 				tt.D = newTP[3];
290 				break;
291 			default: break;
292 		}
293 	}
294 	override public void keyReleased(string ID,uint timestamp,uint devicenumber,uint devicetype) {
295 		switch(ID){
296 			case "up": up = false; break;
297 			case "down": down = false; break;
298 			case "left": left = false; break;
299 			case "right": right = false; break;
300 			case "scrup": scrup = false; break;
301 			case "scrdown": scrdown = false; break;
302 			case "scrleft": scrleft = false; break;
303 			case "scrright": scrright = false; break;
304 			default: break;
305 		}
306 	}
307 /*public void spriteCollision(CollisionEvent ce){
308 		writeln("COLLISION!!!!11!1111!!!ONEONEONE!!!");
309 	}
310 
311 	public void backgroundCollision(CollisionEvent ce){}*/
312 }