1 // register cheat code? or even a fighting game combo..
2 /++
3 	An add-on for simpledisplay.d, joystick.d, and simpleaudio.d
4 	that includes helper functions for writing simple games (and perhaps
5 	other multimedia programs). Whereas simpledisplay works with
6 	an event-driven framework, arsd.game always uses a consistent
7 	timer for updates.
8 
9 	Usage example:
10 
11 	---
12 	final class MyGame : GameHelperBase {
13 		/// Called when it is time to redraw the frame
14 		/// it will try for a particular FPS
15 		override void drawFrame() {
16 			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
17 
18 			glLoadIdentity();
19 
20 			glColor3f(1.0, 1.0, 1.0);
21 			glTranslatef(x, y, 0);
22 			glBegin(GL_QUADS);
23 
24 			glVertex2i(0, 0);
25 			glVertex2i(16, 0);
26 			glVertex2i(16, 16);
27 			glVertex2i(0, 16);
28 
29 			glEnd();
30 		}
31 
32 		int x, y;
33 		override bool update(Duration deltaTime) {
34 			x += 1;
35 			y += 1;
36 			return true;
37 		}
38 
39 		override SimpleWindow getWindow() {
40 			auto window = create2dWindow("My game");
41 			// load textures and such here
42 			return window;
43 		}
44 
45 		final void fillAudioBuffer(short[] buffer) {
46 
47 		}
48 	}
49 
50 	void main() {
51 		auto game = new MyGame();
52 
53 		runGame(game, maxRedrawRate, maxUpdateRate);
54 	}
55 	---
56 
57 	It provides an audio thread, input scaffold, and helper functions.
58 
59 
60 	The MyGame handler is actually a template, so you don't have virtual
61 	function indirection and not all functions are required. The interfaces
62 	are just to help you get the signatures right, they don't force virtual
63 	dispatch at runtime.
64 
65 	See_Also:
66 		[arsd.ttf.OpenGlLimitedFont]
67 +/
68 module arsd.game;
69 
70 public import arsd.gamehelpers;
71 public import arsd.color;
72 public import arsd.simpledisplay;
73 public import arsd.simpleaudio;
74 
75 import std.math;
76 public import core.time;
77 
78 public import arsd.joystick;
79 
80 /++
81 	Creates a simple 2d opengl simpledisplay window. It sets the matrix for pixel coordinates and enables alpha blending and textures.
82 +/
83 SimpleWindow create2dWindow(string title, int width = 512, int height = 512) {
84 	auto window = new SimpleWindow(width, height, title, OpenGlOptions.yes);
85 
86 	window.setAsCurrentOpenGlContext();
87 
88 	glEnable(GL_BLEND);
89 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
90 	glClearColor(0,0,0,0);
91 	glDepthFunc(GL_LEQUAL);
92 
93 	glMatrixMode(GL_PROJECTION);
94 	glLoadIdentity();
95 	glOrtho(0, width, height, 0, 0, 1);
96 
97 	glMatrixMode(GL_MODELVIEW);
98 	glLoadIdentity();
99 	glDisable(GL_DEPTH_TEST);
100 	glEnable(GL_TEXTURE_2D);
101 
102 	window.windowResized = (newWidth, newHeight) {
103 		int x, y, w, h;
104 
105 		// FIXME: this works for only square original sizes
106 		if(newWidth < newHeight) {
107 			w = newWidth;
108 			h = newWidth * height / width;
109 			x = 0;
110 			y = (newHeight - h) / 2;
111 		} else {
112 			w = newHeight * width / height;
113 			h = newHeight;
114 			x = (newWidth - w) / 2;
115 			y = 0;
116 		}
117 
118 		glViewport(x, y, w, h);
119 		window.redrawOpenGlSceneNow();
120 	};
121 
122 	return window;
123 }
124 
125 /++
126 	This is the base class for your game.
127 
128 	You should destroy this explicitly. Easiest
129 	way is to do this in your `main` function:
130 
131 	---
132 		auto game = new MyGameSubclass();
133 		scope(exit) .destroy(game);
134 
135 		runGame(game);
136 	---
137 +/
138 abstract class GameHelperBase {
139 	/// Implement this to draw.
140 	abstract void drawFrame();
141 
142 	ushort snesRepeatRate() { return ushort.max; }
143 	ushort snesRepeatDelay() { return snesRepeatRate(); }
144 
145 	/// Implement this to update. The deltaTime tells how much real time has passed since the last update.
146 	/// Returns true if anything changed, which will queue up a redraw
147 	abstract bool update(Duration deltaTime);
148 	//abstract void fillAudioBuffer(short[] buffer);
149 
150 	/// Returns the main game window. This function will only be
151 	/// called once if you use runGame. You should return a window
152 	/// here like one created with `create2dWindow`.
153 	abstract SimpleWindow getWindow();
154 
155 	/// Override this and return true to initialize the audio system.
156 	/// Note that trying to use the [audio] member without this will segfault!
157 	bool wantAudio() { return false; }
158 
159 	/// You must override [wantAudio] and return true for this to be valid;
160 	AudioOutputThread audio;
161 
162 	this() {
163 		audio = AudioOutputThread(wantAudio());
164 	}
165 
166 	protected bool redrawForced;
167 
168 	/// Forces a redraw even if update returns false
169 	final public void forceRedraw() {
170 		redrawForced = true;
171 	}
172 
173 	/// These functions help you handle user input. It offers polling functions for
174 	/// keyboard, mouse, joystick, and virtual controller input.
175 	///
176 	/// The virtual digital controllers are best to use if that model fits you because it
177 	/// works with several kinds of controllers as well as keyboards.
178 
179 	JoystickUpdate[4] joysticks;
180 	ref JoystickUpdate joystick1() { return joysticks[0]; }
181 
182 	bool[256] keyboardState;
183 
184 	// FIXME: add a mouse position and delta thing too.
185 
186 	/++
187 
188 	+/
189 	VirtualController snes;
190 }
191 
192 /++
193 	The virtual controller is based on the SNES. If you need more detail, try using
194 	the joystick or keyboard and mouse members directly.
195 
196 	```
197 	 l          r
198 
199 	 U          X
200 	L R  s  S  Y A
201 	 D          B
202 	```
203 
204 	For Playstation and XBox controllers plugged into the computer,
205 	it picks those buttons based on similar layout on the physical device.
206 
207 	For keyboard control, arrows and WASD are mapped to the d-pad (ULRD in the diagram),
208 	Q and E are mapped to the shoulder buttons (l and r in the diagram).So are U and P.
209 
210 	Z, X, C, V (for when right hand is on arrows) and K,L,I,O (for left hand on WASD) are mapped to B,A,Y,X buttons.
211 
212 	G is mapped to select (s), and H is mapped to start (S).
213 
214 	The space bar and enter keys are also set to button A, with shift mapped to button B.
215 
216 
217 	Only player 1 is mapped to the keyboard.
218 +/
219 struct VirtualController {
220 	ushort previousState;
221 	ushort state;
222 
223 	// for key repeat
224 	ushort truePreviousState;
225 	ushort lastStateChange;
226 	bool repeating;
227 
228 	///
229 	enum Button {
230 		Up, Left, Right, Down,
231 		X, A, B, Y,
232 		Select, Start, L, R
233 	}
234 
235 	@nogc pure nothrow @safe:
236 
237 	/++
238 		History: Added April 30, 2020
239 	+/
240 	bool justPressed(Button idx) const {
241 		auto before = (previousState & (1 << (cast(int) idx))) ? true : false;
242 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
243 		return !before && after;
244 	}
245 	/++
246 		History: Added April 30, 2020
247 	+/
248 	bool justReleased(Button idx) const {
249 		auto before = (previousState & (1 << (cast(int) idx))) ? true : false;
250 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
251 		return before && !after;
252 	}
253 
254 	///
255 	bool opIndex(Button idx) const {
256 		return (state & (1 << (cast(int) idx))) ? true : false;
257 	}
258 	private void opIndexAssign(bool value, Button idx) {
259 		if(value)
260 			state |= (1 << (cast(int) idx));
261 		else
262 			state &= ~(1 << (cast(int) idx));
263 	}
264 }
265 
266 /++
267 	Deprecated, use the other overload instead.
268 
269 	History:
270 		Deprecated on May 9, 2020. Instead of calling
271 		`runGame(your_instance);` run `runGame!YourClass();`
272 		instead. If you needed to change something in the game
273 		ctor, make a default constructor in your class to do that
274 		instead.
275 +/
276 deprecated("Use runGame!YourGameType(updateRate, redrawRate); instead now.")
277 void runGame()(GameHelperBase game, int maxUpdateRate = 20, int maxRedrawRate = 0) { assert(0, "this overload is deprecated, use runGame!YourClass instead"); }
278 
279 /++
280 	Runs your game. It will construct the given class and destroy it at end of scope.
281 	Your class must have a default constructor and must implement [GameHelperBase].
282 	Your class should also probably be `final` for performance reasons.
283 
284 	$(TIP
285 		If you need to pass parameters to your game class, you can define
286 		it as a nested class in your `main` function and access the local
287 		variables that way instead of passing them explicitly through the
288 		constructor.
289 	)
290 
291 	Params:
292 	maxUpdateRate = The max rates are given in executions per second
293 	maxRedrawRate = Redraw will never be called unless there has been at least one update
294 +/
295 void runGame(T : GameHelperBase)(int maxUpdateRate = 20, int maxRedrawRate = 0) {
296 
297 
298 	auto game = new T();
299 	scope(exit) .destroy(game);
300 
301 	// this is a template btw because then it can statically dispatch
302 	// the members instead of going through the virtual interface.
303 
304 	int joystickPlayers = enableJoystickInput();
305 	scope(exit) closeJoysticks();
306 
307 	auto window = game.getWindow();
308 
309 	window.redrawOpenGlScene = &game.drawFrame;
310 
311 	auto lastUpdate = MonoTime.currTime;
312 
313 	window.eventLoop(1000 / maxUpdateRate,
314 		delegate() {
315 			foreach(p; 0 .. joystickPlayers) {
316 				version(linux)
317 					readJoystickEvents(joystickFds[p]);
318 				auto update = getJoystickUpdate(p);
319 
320 				if(p == 0) {
321 					static if(__traits(isSame, Button, PS1Buttons)) {
322 						// PS1 style joystick mapping compiled in
323 						with(Button) with(VirtualController.Button) {
324 							// so I did the "wasJustPressed thing because it interplays
325 							// better with the keyboard as well which works on events...
326 							if(update.buttonWasJustPressed(square)) game.snes[Y] = true;
327 							if(update.buttonWasJustPressed(triangle)) game.snes[X] = true;
328 							if(update.buttonWasJustPressed(cross)) game.snes[B] = true;
329 							if(update.buttonWasJustPressed(circle)) game.snes[A] = true;
330 							if(update.buttonWasJustPressed(select)) game.snes[Select] = true;
331 							if(update.buttonWasJustPressed(start)) game.snes[Start] = true;
332 							if(update.buttonWasJustPressed(l1)) game.snes[L] = true;
333 							if(update.buttonWasJustPressed(r1)) game.snes[R] = true;
334 							// note: no need to check analog stick here cuz joystick.d already does it for us (per old playstation tradition)
335 							if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < -8) game.snes[Left] = true;
336 							if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > 8) game.snes[Right] = true;
337 							if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < -8) game.snes[Up] = true;
338 							if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > 8) game.snes[Down] = true;
339 
340 							if(update.buttonWasJustReleased(square)) game.snes[Y] = false;
341 							if(update.buttonWasJustReleased(triangle)) game.snes[X] = false;
342 							if(update.buttonWasJustReleased(cross)) game.snes[B] = false;
343 							if(update.buttonWasJustReleased(circle)) game.snes[A] = false;
344 							if(update.buttonWasJustReleased(select)) game.snes[Select] = false;
345 							if(update.buttonWasJustReleased(start)) game.snes[Start] = false;
346 							if(update.buttonWasJustReleased(l1)) game.snes[L] = false;
347 							if(update.buttonWasJustReleased(r1)) game.snes[R] = false;
348 							if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > -8) game.snes[Left] = false;
349 							if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < 8) game.snes[Right] = false;
350 							if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > -8) game.snes[Up] = false;
351 							if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < 8) game.snes[Down] = false;
352 						}
353 
354 					} else static if(__traits(isSame, Button, XBox360Buttons)) {
355 					static assert(0);
356 						// XBox style mapping
357 						// the reason this exists is if the programmer wants to use the xbox details, but
358 						// might also want the basic controller in here. joystick.d already does translations
359 						// so an xbox controller with the default build actually uses the PS1 branch above.
360 						/+
361 						case XBox360Buttons.a: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? true : false;
362 						case XBox360Buttons.b: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? true : false;
363 						case XBox360Buttons.x: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? true : false;
364 						case XBox360Buttons.y: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? true : false;
365 
366 						case XBox360Buttons.lb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? true : false;
367 						case XBox360Buttons.rb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? true : false;
368 
369 						case XBox360Buttons.back: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? true : false;
370 						case XBox360Buttons.start: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? true : false;
371 						+/
372 					}
373 				}
374 
375 				game.joysticks[p] = update;
376 			}
377 
378 			auto now = MonoTime.currTime;
379 			bool changed = game.update(now - lastUpdate);
380 			auto stateChange = game.snes.truePreviousState ^ game.snes.state;
381 			game.snes.previousState = game.snes.state;
382 			game.snes.truePreviousState = game.snes.state;
383 
384 			if(stateChange == 0) {
385 				game.snes.lastStateChange++;
386 				auto r = game.snesRepeatRate();
387 				if(r != typeof(r).max && !game.snes.repeating && game.snes.lastStateChange == game.snesRepeatDelay()) {
388 					game.snes.lastStateChange = 0;
389 					game.snes.repeating = true;
390 				} else if(r != typeof(r).max && game.snes.repeating && game.snes.lastStateChange == r) {
391 					game.snes.lastStateChange = 0;
392 					game.snes.previousState = 0;
393 				}
394 			} else {
395 				game.snes.repeating = false;
396 			}
397 			lastUpdate = now;
398 
399 			if(game.redrawForced) {
400 				changed = true;
401 				game.redrawForced = false;
402 			}
403 
404 			// FIXME: rate limiting
405 			if(changed)
406 				window.redrawOpenGlSceneNow();
407 		},
408 
409 		delegate (KeyEvent ke) {
410 			game.keyboardState[ke.hardwareCode] = ke.pressed;
411 
412 			with(VirtualController.Button)
413 			switch(ke.key) {
414 				case Key.Up, Key.W: game.snes[Up] = ke.pressed; break;
415 				case Key.Down, Key.S: game.snes[Down] = ke.pressed; break;
416 				case Key.Left, Key.A: game.snes[Left] = ke.pressed; break;
417 				case Key.Right, Key.D: game.snes[Right] = ke.pressed; break;
418 				case Key.Q, Key.U: game.snes[L] = ke.pressed; break;
419 				case Key.E, Key.P: game.snes[R] = ke.pressed; break;
420 				case Key.Z, Key.K: game.snes[B] = ke.pressed; break;
421 				case Key.Space, Key.Enter, Key.X, Key.L: game.snes[A] = ke.pressed; break;
422 				case Key.C, Key.I: game.snes[Y] = ke.pressed; break;
423 				case Key.V, Key.O: game.snes[X] = ke.pressed; break;
424 				case Key.G: game.snes[Select] = ke.pressed; break;
425 				case Key.H: game.snes[Start] = ke.pressed; break;
426 				case Key.Shift, Key.Shift_r: game.snes[B] = ke.pressed; break;
427 				default:
428 			}
429 		}
430 	);
431 }
432 
433 /++
434 	Simple class for putting a TrueColorImage in as an OpenGL texture.
435 
436 	Doesn't do mipmapping btw.
437 +/
438 final class OpenGlTexture {
439 	private uint _tex;
440 	private int _width;
441 	private int _height;
442 	private float _texCoordWidth;
443 	private float _texCoordHeight;
444 
445 	/// Calls glBindTexture
446 	void bind() {
447 		glBindTexture(GL_TEXTURE_2D, _tex);
448 	}
449 
450 	/// For easy 2d drawing of it
451 	void draw(Point where, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
452 		draw(where.x, where.y, width, height, rotation, bg);
453 	}
454 
455 	///
456 	void draw(float x, float y, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
457 		glPushMatrix();
458 		glTranslatef(x, y, 0);
459 
460 		if(width == 0)
461 			width = this.originalImageWidth;
462 		if(height == 0)
463 			height = this.originalImageHeight;
464 
465 		glTranslatef(cast(float) width / 2, cast(float) height / 2, 0);
466 		glRotatef(rotation, 0, 0, 1);
467 		glTranslatef(cast(float) -width / 2, cast(float) -height / 2, 0);
468 
469 		glColor4f(cast(float)bg.r/255.0, cast(float)bg.g/255.0, cast(float)bg.b/255.0, cast(float)bg.a / 255.0);
470 		glBindTexture(GL_TEXTURE_2D, _tex);
471 		glBegin(GL_QUADS); 
472 			glTexCoord2f(0, 0); 				glVertex2i(0, 0);
473 			glTexCoord2f(texCoordWidth, 0); 		glVertex2i(width, 0); 
474 			glTexCoord2f(texCoordWidth, texCoordHeight); 	glVertex2i(width, height); 
475 			glTexCoord2f(0, texCoordHeight); 		glVertex2i(0, height); 
476 		glEnd();
477 
478 		glBindTexture(GL_TEXTURE_2D, 0); // unbind the texture
479 
480 		glPopMatrix();
481 	}
482 
483 	/// Use for glTexCoord2f
484 	float texCoordWidth() { return _texCoordWidth; }
485 	float texCoordHeight() { return _texCoordHeight; } /// ditto
486 
487 	/// Returns the texture ID
488 	uint tex() { return _tex; }
489 
490 	/// Returns the size of the image
491 	int originalImageWidth() { return _width; }
492 	int originalImageHeight() { return _height; } /// ditto
493 
494 	// explicitly undocumented, i might remove this
495 	TrueColorImage from;
496 
497 	/// Make a texture from an image.
498 	this(TrueColorImage from) {
499 		bindFrom(from);
500 	}
501 
502 	/// Generates from text. Requires ttf.d
503 	/// pass a pointer to the TtfFont as the first arg (it is template cuz of lazy importing, not because it actually works with different types)
504 	this(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
505 		bindFrom(font, size, text);
506 	}
507 
508 	/// Creates an empty texture class for you to use with [bindFrom] later
509 	/// Using it when not bound is undefined behavior.
510 	this() {}
511 
512 
513 
514 	/// After you delete it with dispose, you may rebind it to something else with this.
515 	void bindFrom(TrueColorImage from) {
516 		assert(from !is null);
517 		assert(from.width > 0 && from.height > 0);
518 
519 		import core.stdc.stdlib;
520 
521 		_width = from.width;
522 		_height = from.height;
523 
524 		this.from = from;
525 
526 		auto _texWidth = _width;
527 		auto _texHeight = _height;
528 
529 		const(ubyte)* data = from.imageData.bytes.ptr;
530 		bool freeRequired = false;
531 
532 		// gotta round them to the nearest power of two which means padding the image
533 		if((_texWidth & (_texWidth - 1)) || (_texHeight & (_texHeight - 1))) {
534 			_texWidth = nextPowerOfTwo(_texWidth);
535 			_texHeight = nextPowerOfTwo(_texHeight);
536 
537 			auto n = cast(ubyte*) malloc(_texWidth * _texHeight * 4);
538 			if(n is null) assert(0);
539 			scope(failure) free(n);
540 
541 			auto size = from.width * 4;
542 			auto advance = _texWidth * 4;
543 			int at = 0;
544 			int at2 = 0;
545 			foreach(y; 0 .. from.height) {
546 				n[at .. at + size] = from.imageData.bytes[at2 .. at2+ size];
547 				at += advance;
548 				at2 += size;
549 			}
550 
551 			data = n;
552 			freeRequired = true;
553 
554 			// the rest of data will be initialized to zeros automatically which is fine.
555 		}
556 
557 		glGenTextures(1, &_tex);
558 		glBindTexture(GL_TEXTURE_2D, tex);
559 
560 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
561 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
562 		
563 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
564 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
565 		
566 		glTexImage2D(
567 			GL_TEXTURE_2D,
568 			0,
569 			GL_RGBA,
570 			_texWidth, // needs to be power of 2
571 			_texHeight,
572 			0,
573 			GL_RGBA,
574 			GL_UNSIGNED_BYTE,
575 			data);
576 
577 		assert(!glGetError());
578 
579 		_texCoordWidth = cast(float) _width / _texWidth;
580 		_texCoordHeight = cast(float) _height / _texHeight;
581 
582 		if(freeRequired)
583 			free(cast(void*) data);
584 		glBindTexture(GL_TEXTURE_2D, 0);
585 	}
586 
587 	/// ditto
588 	void bindFrom(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
589 		assert(font !is null);
590 		int width, height;
591 		auto data = font.renderString(text, size, width, height);
592 		auto image = new TrueColorImage(width, height);
593 		int pos = 0;
594 		foreach(y; 0 .. height)
595 		foreach(x; 0 .. width) {
596 			image.imageData.bytes[pos++] = 255;
597 			image.imageData.bytes[pos++] = 255;
598 			image.imageData.bytes[pos++] = 255;
599 			image.imageData.bytes[pos++] = data[0];
600 			data = data[1 .. $];
601 		}
602 		assert(data.length == 0);
603 
604 		bindFrom(image);
605 	}
606 
607 	/// Deletes the texture. Using it after calling this is undefined behavior
608 	void dispose() {
609 		glDeleteTextures(1, &_tex);
610 		_tex = 0;
611 	}
612 
613 	~this() {
614 		if(_tex > 0)
615 			dispose();
616 	}
617 }
618 
619 /+
620 	FIXME: i want to do stbtt_GetBakedQuad for ASCII and use that
621 	for simple cases especially numbers. for other stuff you can
622 	create the texture for the text above.
623 +/
624 
625 ///
626 void clearOpenGlScreen(SimpleWindow window) {
627 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
628 }
629 
630