1 module test5.app;
2 
3 import std.stdio;
4 import std.utf;
5 import std.path;
6 
7 import bindbc.sdl;
8 
9 import pixelperfectengine.system.common;
10 
11 import pixelperfectengine.graphics.outputscreen;
12 import pixelperfectengine.graphics.raster;
13 import pixelperfectengine.graphics.layers;
14 
15 import pixelperfectengine.graphics.bitmap;
16 import pixelperfectengine.graphics.text;
17 import pixelperfectengine.graphics.draw;
18 
19 import pixelperfectengine.system.lang.textparser;
20 
21 import pixelperfectengine.system.input;
22 import pixelperfectengine.system.file;
23 
24 
25 ///Test suite for text parsing and drawing.
26 int main(string[] args) {
27 	initialzeSDL();
28 	if (args.length == 1) {
29 		args ~= ["../assets/test5.xml", "../system/unifont-15.0.01.fnt"];
30 	}
31 	try {
32 		Test5 app = new Test5(args);
33 		app.whereTheMagicHappens;
34 	} catch (Throwable t) {
35 		writeln(t);
36 	}
37 	return 0;
38 }
39 
40 public class Test5 : SystemEventListener, InputListener {
41 	bool isRunning;
42 	int textPos;
43 	TextParser txprs;
44 	OutputScreen output;
45 	SpriteLayer s;
46 	Raster r;
47 	InputHandler ih;
48 	BitmapDrawer textOutput;
49 	string[] availTexts;
50 	public this(string[] args) {
51 		//Basic setup
52 		isRunning = true;
53 		output = new OutputScreen("TileLayer test", 424 * 4, 240 * 4);
54 		r = new Raster(424,240,output,0);
55 		output.setMainRaster(r);
56 		s = new SpriteLayer(RenderingMode.AlphaBlend);
57 		r.addLayer(s, 0);
58 		r.addPaletteChunk([Color(0x22,0x22,0x22,0xFF),Color(0xff,0xff,0xff,0xFF),Color(0x80,0x80,0x80,0xFF),
59 				Color(0xff,0x00,0x00,0xFF),Color(0x00,0xff,0x00,0xFF),Color(0x00,0x00,0xff,0xFF)]);
60 		r.setupPalette(256);
61 		ih = new InputHandler();
62 		ih.inputListener = this;
63 		ih.systemEventListener = this;
64 		{
65 			import pixelperfectengine.system.input.scancode;
66 			ih.addBinding(BindingCode(ScanCode.SPACE, 0, Devicetype.Keyboard, 0, KeyModifier.All), InputBinding("up"));
67 		}
68 		//Setup textparsing and parse texts from included ETML file
69 		Fontset!Bitmap8Bit fnt = new Fontset!Bitmap8Bit(File(args[2]), dirName(args[2]) ~ "/");
70 		CharacterFormattingInfo!Bitmap8Bit defFrmt = new CharacterFormattingInfo!Bitmap8Bit(fnt, 0x01, 0, 0x01, 
71 				cast(short)(fnt.size + 1), 0x00);
72 		dstring input;
73 		File xml = File(args[1]);
74 		char[] buffer;
75 		/* buffer.length = 4;
76 		buffer = xml.rawRead(buffer);
77 		xml.seek(0); */
78 		/* switch (buffer) {
79 			case "<?xm": */
80 		buffer.length = cast(size_t)xml.size;
81 		buffer = xml.rawRead(buffer);
82 		input = toUTF32(buffer);
83 		buffer.length = 0;
84 		/* 		break;
85 			//case "\00<\00?":
86 			default:
87 				throw new Exception("Character encoding not supported or cannot be detected.");
88 		} */
89 		txprs = new TextParser(input, defFrmt);
90 		txprs.parse();
91 		foreach (key; txprs.output.byKey) {
92 			availTexts ~= key;
93 		}
94 		writeln(txprs.output);
95 
96 		textOutput = new BitmapDrawer(424, 240);
97 		s.addSprite(textOutput.output, 0, 0, 0);
98 		
99 		drawNextText();
100 	}
101 	public void whereTheMagicHappens() {
102 		while (isRunning) {
103 			r.refresh();
104 			ih.test();
105 		}
106 	}
107 	void drawNextText() {
108 		if (textPos >= availTexts.length) {
109 			isRunning = false;
110 		} else {
111 			textOutput.drawFilledBox(Box.bySize(0, 0, 424, 240), 0);
112 			textOutput.drawMultiLineText(Box.bySize(0, 0, 424, 240), txprs.output[availTexts[textPos]]);
113 			textPos++;
114 		}
115 	}
116 	public void onQuit() {
117 		isRunning = false;
118 	}
119 	public void controllerAdded(uint id) {
120 
121 	}
122 	public void controllerRemoved(uint id) {
123 
124 	}
125 	/**
126 	 * Called when a keybinding event is generated.
127 	 * The `id` should be generated from a string, usually the name of the binding.
128 	 * `code` is a duplicate of the code used for fast lookup of the binding, which also contains other info (deviceID, etc).
129 	 * `timestamp` is the time lapsed since the start of the program, can be used to measure time between keypresses.
130 	 * NOTE: Hat events on joysticks don't generate keyReleased events, instead they generate keyPressed events on release.
131 	 */
132 	public void keyEvent(uint id, BindingCode code, uint timestamp, bool isPressed) {
133 		if (!isPressed)
134 			drawNextText();
135 	}
136 	/**
137 	 * Called when an axis is being operated.
138 	 * The `id` should be generated from a string, usually the name of the binding.
139 	 * `code` is a duplicate of the code used for fast lookup of the binding, which also contains other info (deviceID, etc).
140 	 * `timestamp` is the time lapsed since the start of the program, can be used to measure time between keypresses.
141 	 * `value` is the current position of the axis normalized between -1.0 and +1.0 for joysticks, and 0.0 and +1.0 for analog
142 	 * triggers.
143 	 */
144 	public void axisEvent(uint id, BindingCode code, uint timestamp, float value) {
145 
146 	}
147 }