1 module test2.app;
2 
3 import bindbc.sdl;
4 
5 import std.stdio;
6 import std.typecons : BitFlags;
7 import std.format;
8 
9 import pixelperfectengine.graphics.outputscreen;
10 import pixelperfectengine.graphics.raster;
11 import pixelperfectengine.graphics.layers;
12 
13 import pixelperfectengine.graphics.bitmap;
14 
15 import pixelperfectengine.collision.common;
16 import pixelperfectengine.collision.objectcollision;
17 
18 import pixelperfectengine.system.input;
19 import pixelperfectengine.system.file;
20 import pixelperfectengine.system.etc;
21 import pixelperfectengine.system.config;
22 
23 import pixelperfectengine.system.common;
24 
25 import pixelperfectengine.map.mapformat;
26 
27 int main(string[] args) {
28 	initialzeSDL();
29 	string path = "../assets/test2.xmf";
30 	if (args.length > 1)
31 		path = args[1];
32 	try {
33 		MapFormatTester app = new MapFormatTester(path);
34 		app.whereTheMagicHappens();
35 	} catch (Throwable e) {
36 		writeln(e);
37 	}
38 	return 0;
39 }
40 
41 public class MapFormatTester : SystemEventListener, InputListener {
42 	enum StateFlags {
43 		isRunning	=	1<<0,
44 	}
45 	enum ControlFlags {
46 		up			=   1<<0,
47 		down		=   1<<1,
48 		left		=   1<<2,
49 		right		=   1<<3,
50 	}
51 	MapFormat		mapSource;
52 	OutputScreen	output;
53 	Raster			r;
54 	InputHandler	ih;
55 	ObjectCollisionDetector	ocd;
56 	BitFlags!StateFlags	stateFlags;
57 	BitFlags!ControlFlags controlFlags;
58 	TileLayer		textLayer;
59 	SpriteLayer		gameField;
60 	this(string path) {
61 		stateFlags.isRunning = true;
62 		output = new OutputScreen("TileLayer test", 424 * 4, 240 * 4);
63 		r = new Raster(424,240,output,0);
64 		output.setMainRaster(r);
65 		Image fontSource = loadImage(File("../system/codepage_8_8.png"));
66 		ih = new InputHandler();
67 		ih.systemEventListener = this;
68 		ih.inputListener = this;
69 		{
70 			import pixelperfectengine.system.input.scancode;
71 			ih.addBinding(BindingCode(ScanCode.UP, 0, Devicetype.Keyboard, 0, KeyModifier.All), InputBinding("up"));
72 			ih.addBinding(BindingCode(ScanCode.DOWN, 0, Devicetype.Keyboard, 0, KeyModifier.All), InputBinding("down"));
73 			ih.addBinding(BindingCode(ScanCode.LEFT, 0, Devicetype.Keyboard, 0, KeyModifier.All), InputBinding("left"));
74 			ih.addBinding(BindingCode(ScanCode.RIGHT, 0, Devicetype.Keyboard, 0, KeyModifier.All), InputBinding("right"));
75 		}
76 
77 
78 		textLayer = new TileLayer(8,8, RenderingMode.AlphaBlend);
79 		textLayer.paletteOffset = 512;
80 		textLayer.masterVal = 127;
81 		textLayer.loadMapping(53, 30, new MappingElement[](53 * 30));
82 		{
83 			Bitmap8Bit[] fontSet = loadBitmapSheetFromImage!Bitmap8Bit(fontSource, 8, 8);
84 			for (ushort i; i < fontSet.length; i++) {
85 				textLayer.addTile(fontSet[i], i, 1);
86 			}
87 		}
88 		textLayer.writeTextToMap(0, 1, 0, "Collision:", BitmapAttrib(true, false));
89 		textLayer.writeTextToMap(0, 2, 0, "Col. type:", BitmapAttrib(true, false));
90 
91 		ocd = new ObjectCollisionDetector(&onCollision, 0);
92 
93 		stateFlags.isRunning = true;
94 		loadMap(path);
95 	}
96 	void loadMap(string m) {
97 		mapSource = new MapFormat(File(m));
98 		mapSource.loadTiles(r);
99 		mapSource.loadAllSpritesAndObjects(r, ocd);
100 		mapSource.loadMappingData();
101 		r.loadLayers(mapSource.layeroutput);
102 		gameField = cast(SpriteLayer)(mapSource.layeroutput[16]);
103 		r.addLayer(textLayer, 32);
104 		r.loadPaletteChunk([Color(0x00,0x00,0x00,0xFF),Color(0xff,0xff,0xff,0xFF),Color(0x00,0x00,0x00,0xFF),
105 				Color(0xff,0x00,0x00,0xFF),Color(0x00,0x00,0x00,0xFF),Color(0x00,0xff,0x00,0xFF),Color(0x00,0x00,0x00,0xFF),
106 				Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),
107 				Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),Color(0x00,0x00,0xff,0xFF),
108 				Color(0x00,0x00,0xff,0xFF)], 512);
109 	}
110 	void whereTheMagicHappens() {
111 		while (stateFlags.isRunning) {
112 			r.refresh();
113 			ih.test();
114 			ocd.objects.ptrOf(65_536).position = gameField.getSpriteCoordinate(65_536);
115 			if(controlFlags.up) {
116 				gameField.relMoveSprite(65_536,0,-1);
117 				textLayer.writeTextToMap(10,2,0,"        None",BitmapAttrib(true, false));
118 			}
119 			if(controlFlags.down) {
120 				gameField.relMoveSprite(65_536,0,1);
121 				textLayer.writeTextToMap(10,2,0,"        None",BitmapAttrib(true, false));
122 			}
123 			if(controlFlags.left) {
124 				gameField.relMoveSprite(65_536,-1,0);
125 				textLayer.writeTextToMap(10,2,0,"        None",BitmapAttrib(true, false));
126 			}
127 			if(controlFlags.right) {
128 				gameField.relMoveSprite(65_536,1,0);
129 				textLayer.writeTextToMap(10,2,0,"        None",BitmapAttrib(true, false));
130 			}
131 			ocd.testSingle(65_536);
132 		}
133 	}
134 	public void onCollision(ObjectCollisionEvent event) {
135 		textLayer.writeTextToMap(10,1,0,format("%8X"w,event.idB),BitmapAttrib(true, false));
136 		final switch (event.type) with (ObjectCollisionEvent.Type) {
137 			case None:
138 				textLayer.writeTextToMap(10,2,0,"        None",BitmapAttrib(true, false));
139 				break;
140 			case BoxEdge:
141 				textLayer.writeTextToMap(10,2,0,"     BoxEdge",BitmapAttrib(true, false));
142 				break;
143 			case BoxOverlap:
144 				textLayer.writeTextToMap(10,2,0,"  BoxOverlap",BitmapAttrib(true, false));
145 				break;
146 			case ShapeOverlap:
147 				textLayer.writeTextToMap(10,2,0,"ShapeOverlap",BitmapAttrib(true, false));
148 				break;
149 		}
150 	}
151 
152 	public void onQuit() {
153 		stateFlags.isRunning = false;
154 	}
155 	
156 	public void controllerAdded(uint id) {
157 		
158 	}
159 	
160 	public void controllerRemoved(uint id) {
161 		
162 	}
163 	
164 	public void keyEvent(uint id, BindingCode code, uint timestamp, bool isPressed) {
165 		switch (id) {
166 			case hashCalc("up"):	//up
167 				controlFlags.up = isPressed;
168 				break;
169 			case hashCalc("down"):	//down
170 				controlFlags.down = isPressed;
171 				break;
172 			case hashCalc("left"):	//left
173 				controlFlags.left = isPressed;
174 				break;
175 			case hashCalc("right"):	//right
176 				controlFlags.right = isPressed;
177 				break;
178 			default:
179 				break;
180 		}
181 	}
182 	
183 	public void axisEvent(uint id, BindingCode code, uint timestamp, float value) {
184 		
185 	}
186 	
187 }