1 // FIXME: the audio thread needs to trigger an event in the event of its death too
2 
3 // i could add a "time" uniform for the shaders automatically. unity does a float4 i think with ticks in it
4 // register cheat code? or even a fighting game combo..
5 /++
6 	An add-on for simpledisplay.d, joystick.d, and simpleaudio.d
7 	that includes helper functions for writing simple games (and perhaps
8 	other multimedia programs). Whereas simpledisplay works with
9 	an event-driven framework, arsd.game always uses a consistent
10 	timer for updates.
11 
12 	$(PITFALL
13 		I AM NO LONGER HAPPY WITH THIS INTERFACE AND IT WILL CHANGE.
14 
15 		While arsd 11 included an overhaul (so you might want to fork
16 		an older version if you relied on it, but the transition is worth
17 		it and wasn't too hard for my game), there's still more stuff changing.
18 
19 		This is considered unstable as of arsd 11.0 and will not re-stabilize
20 		until some 11.x release to be determined in the future (and then it might
21 		break again in 12.0, but i'll commit to long term stabilization after that
22 		at the latest).
23 	)
24 
25 
26 	The general idea is you provide a game class which implements a minimum of
27 	three functions: `update`, `drawFrame`, and `getWindow`. Your main function
28 	calls `runGame!YourClass();`.
29 
30 	`getWindow` is called first. It is responsible for creating the window and
31 	initializing your setup. Then the game loop is started, which will call `update`,
32 	to update your game state, and `drawFrame`, which draws the current state.
33 
34 	`update` is called on a consistent timer. It should always do exactly one delta-time
35 	step of your game work and the library will ensure it is called often enough to keep
36 	game time where it should be with real time. `drawFrame` will be called when an opportunity
37 	arises, possibly more or less often than `update` is called. `drawFrame` gets an argument
38 	telling you how close it is to the next `update` that you can use for interpolation.
39 
40 	How, exactly, you decide to draw and update is up to you, but I strongly recommend that you
41 	keep your game state inside the game class, or at least accessible from it. In other words,
42 	avoid using global and static variables.
43 
44 	It might be easier to understand by example. Behold:
45 
46 	---
47 	import arsd.game;
48 
49 	final class MyGame : GameHelperBase {
50 		/// Called when it is time to redraw the frame. The interpolate member
51 		/// tells you the fraction of an update has passed since the last update
52 		/// call; you can use this to make smoother animations if you like.
53 		override void drawFrame(float interpolate) {
54 			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
55 
56 			glLoadIdentity();
57 
58 			glColor3f(1.0, 1.0, 1.0);
59 			glTranslatef(x, y, 0);
60 			glBegin(GL_QUADS);
61 
62 			glVertex2i(0, 0);
63 			glVertex2i(16, 0);
64 			glVertex2i(16, 16);
65 			glVertex2i(0, 16);
66 
67 			glEnd();
68 		}
69 
70 		int x, y;
71 		override bool update() {
72 			x += 1;
73 			y += 1;
74 			return true;
75 		}
76 
77 		override SimpleWindow getWindow() {
78 			// if you want to use OpenGL 3 or nanovega or whatever, you can set it up in here too.
79 			auto window = create2dWindow("My game");
80 			// load textures and such here
81 			return window;
82 		}
83 	}
84 
85 	void main() {
86 		runGame!MyGame(20 /*targetUpdateRate - shoot for 20 updates per second of game state*/);
87 		// please note that it can draw faster than this; updates should be less than drawn frames per second.
88 	}
89 	---
90 
91 	Of course, this isn't much of a game, since there's no input. The [GameHelperBase] provides a few ways for your
92 	`update` function to check for user input: you can check the current state of and transition since last update
93 	of a SNES-style [VirtualController] through [GameHelperBase.snes], or the computer keyboard and mouse through
94 	[GameHelperBase.keyboardState] and (FIXME: expose mouse). Touch events are not implemented at this time and I have
95 	no timetable for when they will be, but I do want to add them at some point.
96 
97 	The SNES controller is great if your game can work with it because it will automatically map to various gamepads
98 	as well as to the standard computer keyboard. This gives the user a lot of flexibility in how they control the game.
99 	If it doesn't though, you can try the other models. However, I don't recommend you try to mix them in the same game mode,
100 	since you wouldn't want a user to accidentally trigger the controller while trying to type their name, for example.
101 
102 	If you just do the basics here, you'll have a working basic game. You can also get additional
103 	features by implementing more functions, like `override bool wantAudio() { return true; } ` will
104 	enable audio, for example. You can then trigger sounds and music to play in your `update` function.
105 
106 	Let's expand the example to show this:
107 
108 	// FIXME: paste in game2.d contents here
109 
110 	A game usually isn't just one thing, and it might help to separate these out. I call these [GameScreen]s.
111 	The name might not be perfect, but the idea is that even a basic game might still have, for example, a
112 	title screen and a gameplay screen. These are likely to have different controls, different drawing, and some
113 	different state.
114 
115 
116 	The MyGame handler is actually a template, so you don't have virtual
117 	function indirection and not all functions are required. The interfaces
118 	are just to help you get the signatures right, they don't force virtual
119 	dispatch at runtime.
120 
121 	$(H2 Input)
122 
123 	In the overview, I mentioned that there's input available through a few means. Among the functions are:
124 
125 	Checking capabilities:
126 		keyboardIsPresent, mouseIsPresent, gamepadIsPresent, joystickIsPresent, touchIsPresent - return true if there's a physical device for this (tho all can be emulated from just keyboard/mouse)
127 
128 	Gamepads, mouse buttons, and keyboards:
129 		wasPressed - returns true if the button was not pressed but became pressed over the update period.
130 		wasReleased - returns true if the button was pressed, but was released over the update period
131 		wasClicked - returns true if the button was released but became pressed and released again since you last asked without much other movement in between
132 		isHeld - returns true if the button is currently held down
133 	Gamepad specific (remember the keyboard emulates a basic gamepad):
134 		startRecordingButtons - starts recording buttons
135 		getRecordedButtons - gets the sequence of button presses with associated times
136 		stopRecordingButtons - stops recording buttons
137 
138 		You might use this to check for things like cheat codes and fighting game style special moves.
139 	Keyboard-specific:
140 		startRecordingCharacters - starts recording keyboard character input
141 		getRecordedCharacters - returns the characters typed since you started recording characters
142 		stopRecordingCharacters - stops recording characters and clears the recording
143 
144 		You might use this for taking input for chat or character name selection.
145 
146 		FIXME: add an on-screen keyboard thing you can use with gamepads too
147 	Mouse and joystick:
148 		startRecordingPath - starts recording paths, each point coming off the operating system is noted with a timestamp relative to when the recording started
149 		getRecordedPath - gets the current recorded path
150 		stopRecordingPath - stops recording the path and clears the recording.
151 
152 		You might use this for things like finding circles in Mario Party.
153 	Mouse-specific:
154 		// actually instead of capture/release i might make it a property of the screen. we'll see.
155 		captureCursor - captures the cursor inside the window
156 		releaseCursor - releases any existing capture
157 		currentPosition - returns the current position over the window, in pixels, with (0,0) being the upper left.
158 		changeInPosition - returns the change in position since last time you asked
159 		wheelMotion - change in wheel ticks since last time you asked
160 	Joystick-specific (be aware that the mouse will act as an emulated joystick):
161 		currentPosition - returns the current position of the stick, 0,0 being centered and -1, 1 being the upper left corner and 1,-1 being the lower right position. Note that there is a dead zone in the middle of joysticks that does not count so minute wiggles are filtered out.
162 		changeInPosition - returns the change in position since last time you asked
163 
164 		There may also be raw input data available, since this uses arsd.joystick.
165 	Touch-specific:
166 
167 	$(H2 Window control)
168 
169 	FIXME: no public functions for this yet.
170 
171 	You can check for resizes and if the user wants to close to give you a chance to save the game before closing. You can also call `window.close();`. The library normally takes care of this for you.
172 
173 	Minimized windows will put the game on hold automatically. Maximize and full screen is handled automatically. You can request full screen when creating the window, or use the simpledisplay functions in runInGuiThreadAsync (but don't if you don't need to).
174 
175 	Showing and hiding cursor can be done in sdpy too.
176 
177 	Text drawing prolly shouldn't bitmap scale when the window is blown up, e.g. hidpi. Other things can just auto scale tho. The library should take care of this automatically.
178 
179 	You can set window title and icon when creating it too.
180 
181 	$(H2 Drawing)
182 
183 	I try not to force any one drawing model upon you. I offer four options out of the box and any opengl library has a good chance of working with appropriate setup.
184 
185 	The out-of-the-box choices are:
186 
187 	$(LIST
188 		* Old-style OpenGL, 2d or 3d, with glBegin, glEnd, glRotate, etc. For text, you can use [arsd.ttf.OpenGlLimitedFont]
189 
190 		* New-style OpenGL, 2d or 3d, with shaders and your own math libraries. For text, you can use [arsd.ttf.OpenGlLimitedFont] with new style flag enabled.
191 
192 		* [Nanovega|arsd.nanovega] 2d vector graphics. Nanovega supports its own text drawing functions.
193 
194 		* The `BasicDrawing` functions provided by `arsd.game`. To some extent, you'll be able to mix and match these with other drawing models. It is just bare minimum functionality you might find useful made in a more concise form than even old-style opengl.
195 	)
196 
197 	Please note that the simpledisplay ScreenPainter will NOT work in a game `drawFrame` function.
198 
199 	You can switch between 2d and 3d modes when drawing either with opengl functions or with my helper functions like go2d (FIXME: not in the right module yet).
200 
201 	$(H3 Images)
202 
203 	use arsd.image and the OpenGlTexture object.
204 
205 	$(H3 Text)
206 
207 	use [OpenGlLimitedFont] and maybe [OperatingSystemFont]
208 
209 	$(H3 3d models)
210 
211 	FIXME add something
212 
213 	$(H2 Audio)
214 
215 	done through arsd.simpleaudio
216 
217 	$(H2 Collision detection)
218 
219 	Nanovega actually offers this but generally you're on your own. arsd's Rectangle functions offer some too.
220 
221 	$(H2 Labeling variables)
222 
223 	You can label and categorize variables in your game to help get and set them automatically. For example, marking them as `@Saved` and `@ResetOnNewDungeon` which you use to do batch updates. FIXME: implement this.
224 
225 	$(H2 Random numbers)
226 
227 	std.random works but might want another thing so the seed is saved with the game.
228 
229 	$(H2 Screenshots)
230 
231 	simpledisplay has a function for it. FIXME give a one-stop function here.
232 
233 	$(H2 Stuff missing from raylib that might be useful)
234 
235 	the screen space functions. the 3d model stuff.
236 
237 	$(H2 Online play)
238 
239 	FIXME: not implemented
240 
241 	If you make your games input strictly use the virtual controller functions, it supports multiple players. Locally, they can be multiple gamepads plugged in to the computer. Over the network, you can have multiple players connect to someone acting as a server and it sends input from each player's computers to everyone else which is exposed to the game as other virtual controllers.
242 
243 	The way this works is before your game actually starts running, if the game was run with the network flag (which can come from command line or through the `runGame` parameter), one player will act as the server and others will connect to them
244 
245 	There is also a chat function built in.
246 
247 		getUserChat(recipients, prompt) - tells the input system that you want to accept a user chat message.
248 		drawUserChat(Point, Color, Font) - returns null if not getting user chat, otherwise returns the current string (what about the carat?)
249 		cancelGetChat - cancels a getUserChat.
250 
251 		sendBotChat(recipients, sender, message) - sends a chat from your program to the other users (will be marked as a bot message)
252 
253 		getChatHistory
254 		getLatestChat - returns the latest chat not yet returned, or null if none have come in recently
255 
256 		Chat messages take an argument defining the recipients, which you might want to limit if there are teams.
257 
258 	In your Game object, there is a `filterUserChat` method you can optionally implement. This is given the message they typed. If you return the message, it will send it to other players. Or you can return null to cancel sending it on the network. You might then use the chat function to implement cheat codes like the old Warcraft and Starcraft games. If the player is not connected on the network, nothing happens even if you do return a message, since there is nobody to send it to.
259 
260 	You can also implement a `chatHistoryLength` which tells how many messages to keep in memory.
261 
262 	Finally, you can send custom network messages with `sendNetworkUpdate` and `getNetworkUpdate`, which work with your own arbitrary structs that represent data packets. Each one can be sent to recipients like chat messages but this is strictly for the program to read  These take an argument to decide if it should be the tcp or udp connections.
263 
264 	$(H2 Split screen)
265 
266 	When playing locally, you might want to split your window for multiple players to see. The library might offer functions to help you in future versions. Your code should realize when it is split screen and adjust the ui accordingly regardless.
267 
268 	$(H2 Library internals)
269 
270 	To better understand why things work the way they do, here's an overview of the internal architecture of the library. Much of the information here may be changed in future versions of the library, so try to think more about the concepts than the specifics as you read.
271 
272 	$(H3 The game clock)
273 
274 	$(H3 Thread layout)
275 
276 	It runs four threads: a UI thread, a graphics thread, an audio thread, and a game thread.
277 
278 	The UI thread runs your `getWindow` function, but otherwise is managed by the library. It handles input messages, window resizes, and other things. Being built on [arsd.simpledisplay], it is possible for you to do work in it with the `runInGuiThread` and `runInGuiThreadAsync` functions, which might be useful if, for example, you wanted to open other windows. But you should generally avoid it.
279 
280 	The graphics thread runs your `load` and `drawFrame` functions. It gets the OpenGL context bound to it after the window is created, and expects to always have it. Since OpenGL contexts cannot be simultaneously shared across two threads, this means your other functions shouldn't try to access any of these objects. (It is possible to release the context from one thread, then attach it in another - indeed, the library does this between `getWindow` and `load` - but doing this in your user code is not supported and you'd try it at your own risk.)
281 
282 	The audio thread is created if `wantAudio` is true and is communicated to via the `audio` object in your game class. The library manages it for you and the methods in the `audio` object tell it what to do. You are permitted to call these from your `update` function, or to load sound assets from your `load` function.
283 
284 	Finally, the game thread is responsible for running your `update` function at a regular interval. The library coordinates sharing your game state between it and the graphics thread with a mutex. You can get more fine-grained control over this by overriding `updateWithManualLock`. The default is for `drawFrame` and `update` to never run simultaneously to keep data sharing to a minimum, but if you know what you're doing, you can make the lock time very very small by limiting the amount of writable data is actually shared. The default is what it is to keep things simple for you and should work most the time, though.
285 
286 	Most computer programs are written either as batch processors or as event-driven applications. Batch processors do their work when requested, then exit. Event-driven applications, including many video games, wait for something to happen, like the user pressing a key or clicking the mouse, respond to it, then go back to waiting. These might do some animations, but this is the exception to its run time, not the rule. You are assumed to be waiting for events, but can `requestAnimationFrame` for the special occasions.
287 
288 	But this is the rule for the third category of programs: time-driven programs, and many video games fall into this category. This is what `arsd.game` tries to make easy. It assumes you want a timed `update` and a steady stream of animation frames, and if you want to make an exception, you can pause updates until an event comes in. FIXME: `pauseUntilNextInput`.
289 
290 	$(H3 Webassembly implementation)
291 
292 	See_Also:
293 		[arsd.ttf.OpenGlLimitedFont]
294 
295 	History:
296 		The [GameHelperBase], indeed most the module, was completely redesigned in November 2022. If you
297 		have code that depended on the old way, you're probably better off keeping a copy of the old module
298 		and not updating it again.
299 
300 		However, if you want to update it, you can approximate the old behavior by making a single `GameScreen`
301 		and moving most your code into it, especially the `drawFrame` and `update` methods, and returning that
302 		as the `firstScreen`.
303 +/
304 module arsd.game;
305 
306 /+
307 	Platformer demo:
308 		dance of sugar plum fairy as you are the fairy jumping around
309 	Board game demo:
310 		good old chess
311 	3d first person demo:
312 		orbit simulator. your instruments show the spacecraft orientation relative to direction of motion (0 = prograde, 180 = retrograde yaw then the pitch angle relative to the orbit plane with up just being a thing) and your orbit params (apogee, perigee, phase, etc. also show velocity and potential energy relative to planet). and your angular velocity in three dimensions
313 
314 		you just kinda fly around. goal is to try to actually transfer to another station successfully.
315 
316 		play blue danube song lol
317 
318 +/
319 
320 
321 // i will want to keep a copy of these that the events update, then the pre-frame update call just copies it in
322 // just gotta remember potential cross-thread issues; the write should prolly be protected by a mutex so it all happens
323 // together when the frame begins
324 struct VirtualJoystick {
325 	// the mouse sets one thing and the right stick sets another
326 	// both will update it, so hopefully people won't move mouse and joystick at the same time.
327 	private float[2] currentPosition_ = 0.0;
328 	private float[2] positionLastAsked_ = 0.0;
329 
330 	float[2] currentPosition() {
331 		return currentPosition_;
332 	}
333 
334 	float[2] changeInPosition() {
335 		auto tmp = positionLastAsked_;
336 		positionLastAsked_ = currentPosition_;
337 		return [currentPosition_[0] - tmp[0], currentPosition_[1] - tmp[1]];
338 	}
339 
340 }
341 
342 struct MouseAccess {
343 	// the mouse buttons can be L and R on the virtual gamepad
344 	int[2] currentPosition_;
345 }
346 
347 struct KeyboardAccess {
348 	// state based access
349 
350 	int lastChange; // in terms of the game clock's frame counter
351 
352 	void startRecordingCharacters() {
353 
354 	}
355 
356 	string getRecordedCharacters() {
357 		return "";
358 	}
359 
360 	void stopRecordingCharacters() {
361 
362 	}
363 }
364 
365 struct MousePath {
366 	static struct Waypoint {
367 		// Duration timestamp
368 		// x, y
369 		// button flags
370 	}
371 
372 	Waypoint[] path;
373 
374 }
375 
376 struct JoystickPath {
377 	static struct Waypoint {
378 		// Duration timestamp
379 		// x, y
380 		// button flags
381 	}
382 
383 	Waypoint[] path;
384 }
385 
386 /++
387 	See [GameScreen] for the thing you are supposed to use. This is just for internal use by the arsd.game library.
388 +/
389 class GameScreenBase {
390 	abstract inout(GameHelperBase) game() inout;
391 	abstract void update();
392 	abstract void drawFrame(float interpolate);
393 	abstract void load();
394 
395 	private bool loaded;
396 	final void ensureLoaded(GameHelperBase game) {
397 		if(!this.loaded) {
398 			// FIXME: unpause the update thread when it is done
399 			synchronized(game) {
400 				if(!this.loaded) {
401 					this.load();
402 					this.loaded = true;
403 				}
404 			}
405 		}
406 	}
407 }
408 
409 /+
410 	you ask for things to be done - foo();
411 	and other code asks you to do things - foo() { }
412 
413 
414 	Recommended drawing methods:
415 		old opengl
416 		new opengl
417 		nanovega
418 
419 	FIXME:
420 		for nanovega, load might want a withNvg()
421 		both load and drawFrame might want a nvgFrame()
422 
423 		game.nvgFrame((nvg) {
424 
425 		});
426 +/
427 
428 /++
429 	Tip: if your screen is a generic component reused across many games, you might pass `GameHelperBase` as the `Game` parameter.
430 +/
431 class GameScreen(Game) : GameScreenBase {
432 	private Game game_;
433 
434 	// convenience accessors
435 	final AudioOutputThread audio() {
436 		if(this is null || game is null) return AudioOutputThread.init;
437 		return game.audio;
438 	}
439 
440 	final VirtualController snes() {
441 		if(this is null || game is null) return VirtualController.init;
442 		return game.snes;
443 	}
444 
445 	/+
446 		manual draw mode turns off the automatic timer to render and only
447 		draws when you specifically trigger it. might not be worth tho.
448 	+/
449 
450 
451 	// You are not supposed to call this.
452 	final void setGame(Game game) {
453 		// assert(game_ is null);
454 		assert(game !is null);
455 		this.game_ = game;
456 	}
457 
458 	/++
459 		Gives access to your game object for use through the screen.
460 	+/
461 	public override inout(Game) game() inout {
462 		if(game_ is null)
463 			throw new Exception("The game screen isn't showing!");
464 		return game_;
465 	}
466 
467 	/++
468 		`update`'s responsibility is to:
469 
470 		$(LIST
471 			* Process player input
472 			* Update game state - object positions, do collision detection, etc.
473 			* Run any character AI
474 			* Kick off any audio associated with changes in this update
475 			* Transition to other screens if appropriate
476 		)
477 
478 		It is NOT supposed to:
479 
480 		$(LIST
481 			* draw - that's the job of [drawFrame]
482 			* load files, bind textures, or similar - that's the job of [load]
483 			* set uniforms or other OpenGL objects - do one-time things in [load] and per-frame things in [drawFrame]
484 		)
485 	+/
486 	override abstract void update();
487 
488 	/++
489 		`drawFrame`'s responsibility is to draw a single frame. It can use the `interpolate` method to smooth animations between updates.
490 
491 		It should NOT change any variables in the game state or attempt to do things like collision detection - that's [update]'s job. When interpolating, just assume the objects are going to keep doing what they're doing.
492 
493 		It should also NOT load any files, create textures, or any other setup task - [load] is supposed to have already done that.
494 	+/
495 	override abstract void drawFrame(float interpolate);
496 
497 	/++
498 		Load your graphics and other assets in this function. You are allowed to draw to the screen while loading, but note you'll have to manage things like buffer swapping yourself if you do. [drawFrame] and [update] will be paused until loading is complete. This function will be called exactly once per screen object, right as it is first shown.
499 	+/
500 	override void load() {}
501 }
502 
503 /// ditto
504 //alias GenericGameScreen = GameScreen!GameHelperBase;
505 
506 ///
507 unittest {
508 	// The TitleScreen has a simple job: show the title until the user presses start. After that, it will progress to the GameplayScreen.
509 
510 	static // exclude from docs
511 	class DemoGame : GameHelperBase {
512 		// I put this inside DemoGame for this demo, but you could define them in separate files if you wanted to
513 		static class TitleScreen : GameScreen!DemoGame {
514 			override void update() {
515 				// you can always access your main Game object through the screen objects
516 				if(game.snes[VirtualController.Button.Start]) {
517 					//game.showScreen(new GameplayScreen());
518 				}
519 			}
520 
521 			override void drawFrame(float interpolate) {
522 
523 			}
524 		}
525 
526 		// and the minimum boilerplate the game itself must provide for the library
527 		// is the window it wants to use and the first screen to load into it.
528 		override TitleScreen firstScreen() {
529 			return new TitleScreen();
530 		}
531 
532 		override SimpleWindow getWindow() {
533 			auto window = create2dWindow("Demo game");
534 			return window;
535 		}
536 	}
537 
538 	void main() {
539 		runGame!DemoGame();
540 	}
541 
542 	main(); // exclude from docs
543 }
544 
545 /+
546 	Networking helper: just send/receive messages and manage some connections
547 
548 	It might offer a controller queue you can put local and network events in to get fair lag and transparent ultiplayer
549 
550 	split screen?!?!
551 
552 +/
553 
554 /+
555 	ADD ME:
556 	Animation helper like audio style. Your game object
557 	has a particular image attached as primary.
558 
559 	You can be like `animate once` or `animate indefinitely`
560 	and it takes care of it, then set new things and it does that too.
561 +/
562 
563 public import arsd.gamehelpers;
564 public import arsd.color;
565 public import arsd.simpledisplay;
566 public import arsd.simpleaudio;
567 
568 import std.math;
569 public import core.time;
570 
571 import arsd.core;
572 
573 public import arsd.joystick;
574 
575 /++
576 	Creates a simple 2d opengl simpledisplay window. It sets the matrix for pixel coordinates and enables alpha blending and textures.
577 +/
578 SimpleWindow create2dWindow(string title, int width = 512, int height = 512) {
579 	auto window = new SimpleWindow(width, height, title, OpenGlOptions.yes);
580 
581 	window.setAsCurrentOpenGlContext();
582 
583 	glEnable(GL_BLEND);
584 	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
585 	glClearColor(0,0,0,0);
586 	glDepthFunc(GL_LEQUAL);
587 
588 	glMatrixMode(GL_PROJECTION);
589 	glLoadIdentity();
590 	glOrtho(0, width, height, 0, 0, 1);
591 
592 	glMatrixMode(GL_MODELVIEW);
593 	glLoadIdentity();
594 	glDisable(GL_DEPTH_TEST);
595 	glEnable(GL_TEXTURE_2D);
596 
597 	window.windowResized = (newWidth, newHeight) {
598 		int x, y, w, h;
599 
600 		// FIXME: this works for only square original sizes
601 		if(newWidth < newHeight) {
602 			w = newWidth;
603 			h = newWidth * height / width;
604 			x = 0;
605 			y = (newHeight - h) / 2;
606 		} else {
607 			w = newHeight * width / height;
608 			h = newHeight;
609 			x = (newWidth - w) / 2;
610 			y = 0;
611 		}
612 
613 		glViewport(x, y, w, h);
614 		window.redrawOpenGlSceneSoon();
615 	};
616 
617 	return window;
618 }
619 
620 /++
621 	This is the base class for your game. Create a class based on this, then pass it to [runGame].
622 +/
623 abstract class GameHelperBase {
624 	/++
625 		Implement this to draw.
626 
627 		The `interpolateToNextFrame` argument tells you how close you are to the next frame. You should
628 		take your current state and add the estimated next frame things multiplied by this to get smoother
629 		animation. interpolateToNextFrame will always be >= 0 and < 1.0.
630 
631 		History:
632 			Previous to August 27, 2022, this took no arguments. It could thus not interpolate frames!
633 	+/
634 	deprecated("Move to void drawFrame(float) in a GameScreen instead") void drawFrame(float interpolateToNextFrame) {
635 		drawFrameInternal(interpolateToNextFrame);
636 	}
637 
638 	final void drawFrameInternal(float interpolateToNextFrame) {
639 		if(currentScreen is null)
640 			return;
641 
642 		currentScreen.ensureLoaded(this);
643 		currentScreen.drawFrame(interpolateToNextFrame);
644 	}
645 
646 	ushort snesRepeatRate() { return ushort.max; }
647 	ushort snesRepeatDelay() { return snesRepeatRate(); }
648 
649 	/++
650 		Implement this to update your game state by a single fixed timestep. You should
651 		check for user input state here.
652 
653 		Return true if something visibly changed to queue a frame redraw asap.
654 
655 		History:
656 			Previous to August 27, 2022, this took an argument. This was a design flaw.
657 	+/
658 	deprecated("Move to void update in a GameScreen instead") bool update() { return false; }
659 
660 	/+
661 		override this to have more control over synchronization
662 
663 		its main job is to lock on `this` and update what [update] changes
664 		and call `bookkeeping` while inside the lock
665 
666 		but if you have some work that can be done outside the lock - things
667 		that are read-only on the game state - you might split it up here and
668 		batch your update. as long as nothing that the [drawFrame] needs is mutated
669 		outside the lock you'll be ok.
670 
671 		History:
672 			Added November 12, 2022
673 	+/
674 	bool updateWithManualLock(scope void delegate() bookkeeping) shared {
675 		if(currentScreen is null)
676 			return false;
677 		synchronized(this) {
678 			if(currentScreen.loaded)
679 				(cast() this).currentScreen.update();
680 			bookkeeping();
681 			return false;
682 		}
683 	}
684 	//abstract void fillAudioBuffer(short[] buffer);
685 
686 	/++
687 		Returns the main game window. This function will only be
688 		called once if you use runGame. You should return a window
689 		here like one created with `create2dWindow`.
690 	+/
691 	abstract SimpleWindow getWindow();
692 
693 	/++
694 		Override this and return true to initialize the audio system. If you return `true`
695 		here, the [audio] member can be used.
696 	+/
697 	bool wantAudio() { return false; }
698 
699 	/++
700 		Override this and return true if you are compatible with separate render and update threads.
701 	+/
702 	bool multithreadCompatible() { return true; }
703 
704 	/// You must override [wantAudio] and return true for this to be valid;
705 	AudioOutputThread audio;
706 
707 	this() {
708 		audio = AudioOutputThread(wantAudio());
709 	}
710 
711 	protected bool redrawForced;
712 
713 	private GameScreenBase currentScreen;
714 
715 	/+
716 	// it will also need a configuration in time and such
717 	enum ScreenTransition {
718 		none,
719 		crossFade
720 	}
721 	+/
722 
723 	/++
724 		Shows the given screen, making it actively responsible for drawing and updating,
725 		optionally through the given transition effect.
726 	+/
727 	void showScreen(this This, Screen)(Screen cs, GameScreenBase transition = null) {
728 		cs.setGame(cast(This) this);
729 		currentScreen = cs;
730 		// FIXME: pause the update thread here, and fast forward the game clock when it is unpaused
731 		// (this actually SHOULD be called from the update thread, except for the initial load... and even that maybe it will then)
732 		// but i have to be careful waiting here because it can deadlock with teh mutex still locked.
733 	}
734 
735 	/++
736 		Returns the first screen of your game.
737 	+/
738 	abstract GameScreenBase firstScreen();
739 
740 	/++
741 		Returns the number of game updates per second your game is designed for.
742 
743 		This isn't necessarily the number of frames drawn per second, which may be more
744 		or less due to frame skipping and interpolation, but it is the number of times
745 		your screen's update methods will be called each second.
746 
747 		You actually want to make this as small as possible without breaking your game's
748 		physics and feeling of responsiveness to the controls. Remember, the display FPS
749 		is different - you can interpolate frames for smooth animation. What you want to
750 		ensure here is that the design fps is big enough that you don't have problems like
751 		clipping through walls or sluggishness in player control, but not so big that the
752 		computer is busy doing collision detection, etc., all the time and has no time
753 		left over to actually draw the game.
754 
755 		I personally find 20 actually works pretty well, though the default set here is 60
756 		due to how common that number is. You are encouraged to override this and use what
757 		works for you.
758 	+/
759 	int designFps() { return 60; }
760 
761 	/// Forces a redraw even if update returns false
762 	final public void forceRedraw() {
763 		redrawForced = true;
764 	}
765 
766 	/// These functions help you handle user input. It offers polling functions for
767 	/// keyboard, mouse, joystick, and virtual controller input.
768 	///
769 	/// The virtual digital controllers are best to use if that model fits you because it
770 	/// works with several kinds of controllers as well as keyboards.
771 
772 	JoystickUpdate[4] joysticks;
773 	ref JoystickUpdate joystick1() { return joysticks[0]; }
774 
775 	bool[256] keyboardState;
776 
777 	// FIXME: add a mouse position and delta thing too.
778 
779 	/++
780 
781 	+/
782 	VirtualController snes;
783 }
784 
785 /++
786 	The virtual controller is based on the SNES. If you need more detail, try using
787 	the joystick or keyboard and mouse members directly.
788 
789 	```
790 	 l          r
791 
792 	 U          X
793 	L R  s  S  Y A
794 	 D          B
795 	```
796 
797 	For Playstation and XBox controllers plugged into the computer,
798 	it picks those buttons based on similar layout on the physical device.
799 
800 	For keyboard control, arrows and WASD are mapped to the d-pad (ULRD in the diagram),
801 	Q and E are mapped to the shoulder buttons (l and r in the diagram).So are U and P.
802 
803 	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.
804 
805 	G is mapped to select (s), and H is mapped to start (S).
806 
807 	The space bar and enter keys are also set to button A, with shift mapped to button B.
808 
809 	Additionally, the mouse is mapped to the virtual joystick, and mouse buttons left and right are mapped to shoulder buttons L and R.
810 
811 
812 	Only player 1 is mapped to the keyboard.
813 +/
814 struct VirtualController {
815 	ushort previousState;
816 	ushort state;
817 
818 	// for key repeat
819 	ushort truePreviousState;
820 	ushort lastStateChange;
821 	bool repeating;
822 
823 	///
824 	enum Button {
825 		Up, Left, Right, Down,
826 		X, A, B, Y,
827 		Select, Start, L, R
828 	}
829 
830 	@nogc pure nothrow @safe:
831 
832 	/++
833 		History: Added April 30, 2020
834 	+/
835 	bool justPressed(Button idx) const {
836 		auto before = (previousState & (1 << (cast(int) idx))) ? true : false;
837 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
838 		return !before && after;
839 	}
840 	/++
841 		History: Added April 30, 2020
842 	+/
843 	bool justReleased(Button idx) const {
844 		auto before = (previousState & (1 << (cast(int) idx))) ? true : false;
845 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
846 		return before && !after;
847 	}
848 
849 	/+
850 	+/
851 
852 	VirtualJoystick stick;
853 
854 	///
855 	bool opIndex(Button idx) const {
856 		return (state & (1 << (cast(int) idx))) ? true : false;
857 	}
858 	private void opIndexAssign(bool value, Button idx) {
859 		if(value)
860 			state |= (1 << (cast(int) idx));
861 		else
862 			state &= ~(1 << (cast(int) idx));
863 	}
864 }
865 
866 struct ButtonCheck {
867 	bool wasPressed() {
868 		return false;
869 	}
870 	bool wasReleased() {
871 		return false;
872 	}
873 	bool wasClicked() {
874 		return false;
875 	}
876 	bool isHeld() {
877 		return false;
878 	}
879 
880 	bool opCast(T : bool)() {
881 		return isHeld();
882 	}
883 }
884 
885 /++
886 	Deprecated, use the other overload instead.
887 
888 	History:
889 		Deprecated on May 9, 2020. Instead of calling
890 		`runGame(your_instance);` run `runGame!YourClass();`
891 		instead. If you needed to change something in the game
892 		ctor, make a default constructor in your class to do that
893 		instead.
894 +/
895 deprecated("Use runGame!YourGameType(updateRate, redrawRate); instead now.")
896 void runGame()(GameHelperBase game, int targetUpdateRate = 20, int maxRedrawRate = 0) { assert(0, "this overload is deprecated, use runGame!YourClass instead"); }
897 
898 /++
899 	Runs your game. It will construct the given class and destroy it at end of scope.
900 	Your class must have a default constructor and must implement [GameHelperBase].
901 	Your class should also probably be `final` for a small, but easy performance boost.
902 
903 	$(TIP
904 		If you need to pass parameters to your game class, you can define
905 		it as a nested class in your `main` function and access the local
906 		variables that way instead of passing them explicitly through the
907 		constructor.
908 	)
909 
910 	Params:
911 	targetUpdateRate = The number of game state updates you get per second. You want this to be quick enough that players don't feel input lag, but conservative enough that any supported computer can keep up with it easily.
912 	maxRedrawRate = The maximum draw frame rate. 0 means it will only redraw after a state update changes things. It will be automatically capped at the user's monitor refresh rate. Frames in between updates can be interpolated or skipped.
913 +/
914 void runGame(T : GameHelperBase)(int targetUpdateRate = 0, int maxRedrawRate = 0) {
915 
916 	auto game = new T();
917 	scope(exit) .destroy(game);
918 
919 	if(targetUpdateRate == 0)
920 		targetUpdateRate = game.designFps();
921 
922 	// this is a template btw because then it can statically dispatch
923 	// the members instead of going through the virtual interface.
924 
925 	auto window = game.getWindow();
926 	game.showScreen(game.firstScreen());
927 
928 	auto lastUpdate = MonoTime.currTime;
929 	bool isImmediateUpdate;
930 
931 	int joystickPlayers;
932 
933 	window.redrawOpenGlScene = null;
934 
935 	/*
936 		The game clock should always be one update ahead of the real world clock.
937 
938 		If it is behind the real world clock, it needs to run update faster, so it will
939 		double up on its timer to try to update and skip some render frames to make cpu time available.
940 		Generally speaking the render should never be more than one full frame ahead of the game clock,
941 		and since the game clock should always be a bit ahead of the real world clock, if the game clock
942 		is behind the real world clock, time to skip.
943 
944 		If there's a huge jump in the real world clock - more than a couple seconds between
945 		updates - this probably indicates the computer went to sleep or something. We can't
946 		catch up, so this will just resync the clock to real world and not try to catch up.
947 	*/
948 	MonoTime gameClock;
949 	// FIXME: render thread should be lower priority than the ui thread
950 
951 	int rframeCounter = 0;
952 	auto drawer = delegate bool() {
953 		if(gameClock is MonoTime.init)
954 			return false; // can't draw uninitialized info
955 		/* // i think this is the same as if delta < 0 below...
956 		auto time = MonoTime.currTime;
957 		if(gameClock + (1000.msecs / targetUpdateRate) < time) {
958 			writeln("frame skip ", gameClock, " vs ", time);
959 			return false; // we're behind on updates, skip this frame
960 		}
961 		*/
962 
963 		if(false && isImmediateUpdate) {
964 			game.drawFrameInternal(0.0);
965 			isImmediateUpdate = false;
966 		} else {
967 			auto now = MonoTime.currTime - lastUpdate;
968 			Duration nextFrame = msecs(1000 / targetUpdateRate);
969 			auto delta = cast(float) ((nextFrame - now).total!"usecs") / cast(float) nextFrame.total!"usecs";
970 
971 			if(delta < 0) {
972 				//writeln("behind ", cast(int)(delta * 100));
973 				return false; // the render is too far ahead of the updater! time to skip frames to let it catch up
974 			}
975 
976 			game.drawFrameInternal(1.0 - delta);
977 		}
978 
979 		rframeCounter++;
980 		/+
981 		if(rframeCounter % 60 == 0) {
982 			writeln("frame");
983 		}
984 		+/
985 
986 		return true;
987 	};
988 
989 	import core.thread;
990 	import core..volatile;
991 	Thread renderThread; // FIXME: low priority
992 	Thread updateThread; // FIXME: slightly high priority
993 
994 	// shared things to communicate with threads
995 	ubyte exit;
996 	ulong newWindowSize;
997 	ubyte loadRequired; // if the screen changed and you need to call load again in the render thread
998 
999 	ubyte workersPaused;
1000 	// Event unpauseRender; // maybe a manual reset so you set it then reset after unpausing
1001 	// Event unpauseUpdate;
1002 
1003 	// the input buffers should prolly be double buffered generally speaking
1004 
1005 	// FIXME: i might just want an asset cache thing
1006 	// FIXME: ffor audio, i want to be able to play a sound to completion without necessarily letting it play twice simultaneously and then replay it later. this would be a sound effect thing. but you might also play it twice anyway if there's like two shots so meh. and then i'll need BGM controlling in the game and/or screen.
1007 
1008 	Timer renderTimer;
1009 	Timer updateTimer;
1010 
1011 	auto updater = delegate() {
1012 		if(gameClock is MonoTime.init) {
1013 			gameClock = MonoTime.currTime;
1014 		}
1015 
1016 		foreach(p; 0 .. joystickPlayers) {
1017 			version(linux)
1018 				readJoystickEvents(joystickFds[p]);
1019 			auto update = getJoystickUpdate(p);
1020 
1021 			if(p == 0) {
1022 				static if(__traits(isSame, Button, PS1Buttons)) {
1023 					// PS1 style joystick mapping compiled in
1024 					with(Button) with(VirtualController.Button) {
1025 						// so I did the "wasJustPressed thing because it interplays
1026 						// better with the keyboard as well which works on events...
1027 						if(update.buttonWasJustPressed(square)) game.snes[Y] = true;
1028 						if(update.buttonWasJustPressed(triangle)) game.snes[X] = true;
1029 						if(update.buttonWasJustPressed(cross)) game.snes[B] = true;
1030 						if(update.buttonWasJustPressed(circle)) game.snes[A] = true;
1031 						if(update.buttonWasJustPressed(select)) game.snes[Select] = true;
1032 						if(update.buttonWasJustPressed(start)) game.snes[Start] = true;
1033 						if(update.buttonWasJustPressed(l1)) game.snes[L] = true;
1034 						if(update.buttonWasJustPressed(r1)) game.snes[R] = true;
1035 						// note: no need to check analog stick here cuz joystick.d already does it for us (per old playstation tradition)
1036 						if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < -8) game.snes[Left] = true;
1037 						if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > 8) game.snes[Right] = true;
1038 						if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < -8) game.snes[Up] = true;
1039 						if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > 8) game.snes[Down] = true;
1040 
1041 						if(update.buttonWasJustReleased(square)) game.snes[Y] = false;
1042 						if(update.buttonWasJustReleased(triangle)) game.snes[X] = false;
1043 						if(update.buttonWasJustReleased(cross)) game.snes[B] = false;
1044 						if(update.buttonWasJustReleased(circle)) game.snes[A] = false;
1045 						if(update.buttonWasJustReleased(select)) game.snes[Select] = false;
1046 						if(update.buttonWasJustReleased(start)) game.snes[Start] = false;
1047 						if(update.buttonWasJustReleased(l1)) game.snes[L] = false;
1048 						if(update.buttonWasJustReleased(r1)) game.snes[R] = false;
1049 						if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > -8) game.snes[Left] = false;
1050 						if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < 8) game.snes[Right] = false;
1051 						if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > -8) game.snes[Up] = false;
1052 						if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < 8) game.snes[Down] = false;
1053 					}
1054 
1055 				} else static if(__traits(isSame, Button, XBox360Buttons)) {
1056 				static assert(0);
1057 					// XBox style mapping
1058 					// the reason this exists is if the programmer wants to use the xbox details, but
1059 					// might also want the basic controller in here. joystick.d already does translations
1060 					// so an xbox controller with the default build actually uses the PS1 branch above.
1061 					/+
1062 					case XBox360Buttons.a: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? true : false;
1063 					case XBox360Buttons.b: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? true : false;
1064 					case XBox360Buttons.x: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? true : false;
1065 					case XBox360Buttons.y: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? true : false;
1066 
1067 					case XBox360Buttons.lb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? true : false;
1068 					case XBox360Buttons.rb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? true : false;
1069 
1070 					case XBox360Buttons.back: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? true : false;
1071 					case XBox360Buttons.start: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? true : false;
1072 					+/
1073 				}
1074 			}
1075 
1076 			game.joysticks[p] = update;
1077 		}
1078 
1079 		int runs;
1080 
1081 		again:
1082 
1083 		auto now = MonoTime.currTime;
1084 		bool changed;
1085 		changed = (cast(shared)game).updateWithManualLock({ lastUpdate = now; });
1086 		auto stateChange = game.snes.truePreviousState ^ game.snes.state;
1087 		game.snes.previousState = game.snes.state;
1088 		game.snes.truePreviousState = game.snes.state;
1089 
1090 		if(stateChange == 0) {
1091 			game.snes.lastStateChange++;
1092 			auto r = game.snesRepeatRate();
1093 			if(r != typeof(r).max && !game.snes.repeating && game.snes.lastStateChange == game.snesRepeatDelay()) {
1094 				game.snes.lastStateChange = 0;
1095 				game.snes.repeating = true;
1096 			} else if(r != typeof(r).max && game.snes.repeating && game.snes.lastStateChange == r) {
1097 				game.snes.lastStateChange = 0;
1098 				game.snes.previousState = 0;
1099 			}
1100 		} else {
1101 			game.snes.repeating = false;
1102 		}
1103 
1104 		if(game.redrawForced) {
1105 			changed = true;
1106 			game.redrawForced = false;
1107 		}
1108 
1109 		gameClock += 1.seconds / targetUpdateRate;
1110 
1111 		if(++runs < 3 && gameClock < MonoTime.currTime)
1112 			goto again;
1113 
1114 		// FIXME: rate limiting
1115 		// FIXME: triple buffer it.
1116 		if(changed && renderThread is null) {
1117 			isImmediateUpdate = true;
1118 			window.redrawOpenGlSceneSoon();
1119 		}
1120 	};
1121 
1122 	//window.vsync = false;
1123 
1124 	const maxRedrawTime = maxRedrawRate > 0 ? (1000.msecs / maxRedrawRate) : 4.msecs;
1125 
1126 	if(game.multithreadCompatible()) {
1127 		window.redrawOpenGlScene = null;
1128 		renderThread = new Thread({
1129 			// FIXME: catch exception and inform the parent
1130 			int frames = 0;
1131 			int skipped = 0;
1132 
1133 			Duration renderTime;
1134 			Duration flipTime;
1135 			Duration renderThrottleTime;
1136 
1137 			MonoTime initial = MonoTime.currTime;
1138 
1139 			while(!volatileLoad(&exit)) {
1140 				MonoTime start = MonoTime.currTime;
1141 				{
1142 					window.mtLock();
1143 					scope(exit)
1144 						window.mtUnlock();
1145 					window.setAsCurrentOpenGlContext();
1146 				}
1147 
1148 				bool actuallyDrew;
1149 
1150 				synchronized(game)
1151 					actuallyDrew = drawer();
1152 
1153 				MonoTime end = MonoTime.currTime;
1154 
1155 				if(actuallyDrew) {
1156 					window.mtLock();
1157 					scope(exit)
1158 						window.mtUnlock();
1159 					window.swapOpenGlBuffers();
1160 				}
1161 				// want to ensure the vsync wait occurs here, outside the window and locks
1162 				// some impls will do it on glFinish, some on the next touch of the
1163 				// front buffer, hence the clear being done here.
1164 				if(actuallyDrew) {
1165 					glFinish();
1166 					clearOpenGlScreen(window);
1167 				}
1168 
1169 				// this is just to wake up the UI thread to check X events again
1170 				// (any custom event will force a check of XPending) just cuz apparently
1171 				// the readiness of the file descriptor can be reset by one of the vsync functions
1172 				static if(UsingSimpledisplayX11) {
1173 					__gshared thing = new Object;
1174 					window.postEvent(thing);
1175 				}
1176 
1177 				MonoTime flip = MonoTime.currTime;
1178 
1179 				renderTime += end - start;
1180 				flipTime += flip - end;
1181 
1182 				if(flip - start < maxRedrawTime) {
1183 					renderThrottleTime += maxRedrawTime - (flip - start);
1184 					Thread.sleep(maxRedrawTime - (flip - start));
1185 				}
1186 
1187 				if(actuallyDrew)
1188 					frames++;
1189 				else
1190 					skipped++;
1191 				// if(frames % 60 == 0) writeln("frame");
1192 			}
1193 
1194 			MonoTime finalt = MonoTime.currTime;
1195 
1196 			writeln("Average render time: ", renderTime / frames);
1197 			writeln("Average flip time: ", flipTime / frames);
1198 			writeln("Average throttle time: ", renderThrottleTime / frames);
1199 			writeln("Frames: ", frames, ", skipped: ", skipped, " over ", finalt - initial);
1200 		});
1201 
1202 		updateThread = new Thread({
1203 			// FIXME: catch exception and inform the parent
1204 			int frames;
1205 
1206 			joystickPlayers = enableJoystickInput();
1207 			scope(exit) closeJoysticks();
1208 
1209 			Duration updateTime;
1210 			Duration waitTime;
1211 
1212 			while(!volatileLoad(&exit)) {
1213 				MonoTime start = MonoTime.currTime;
1214 				updater();
1215 				MonoTime end = MonoTime.currTime;
1216 
1217 				updateTime += end - start;
1218 
1219 				frames++;
1220 				// if(frames % game.designFps == 0) writeln("update");
1221 
1222 				const now = MonoTime.currTime - lastUpdate;
1223 				Duration nextFrame = msecs(1000) / targetUpdateRate;
1224 				const sleepTime = nextFrame - now;
1225 				if(sleepTime.total!"msecs" <= 0) {
1226 					// falling behind on update...
1227 				} else {
1228 					waitTime += sleepTime;
1229 					// writeln(sleepTime);
1230 					Thread.sleep(sleepTime);
1231 				}
1232 			}
1233 
1234 			writeln("Average update time: " , updateTime / frames);
1235 			writeln("Average wait time: " , waitTime / frames);
1236 		});
1237 	} else {
1238 		// single threaded, vsync a bit dangeresque here since it
1239 		// puts the ui thread to sleep!
1240 		window.vsync = false;
1241 	}
1242 
1243 	// FIXME: when single threaded, set the joystick here
1244 	// actually just always do the joystick in the event thread regardless
1245 
1246 	int frameCounter;
1247 
1248 	auto first = window.visibleForTheFirstTime;
1249 	window.visibleForTheFirstTime = () {
1250 		if(first)
1251 			first();
1252 
1253 		if(updateThread) {
1254 			updateThread.start();
1255 		} else {
1256 			updateTimer = new Timer(1000 / targetUpdateRate, {
1257 				frameCounter++;
1258 				updater();
1259 			});
1260 		}
1261 
1262 		if(renderThread) {
1263 			window.suppressAutoOpenglViewport = true; // we don't want the context being pulled back by the other thread now, we'll check it over here.
1264 			// FIXME: set viewport prior to render if width/height changed
1265 			window.releaseCurrentOpenGlContext(); // need to let the render thread take it
1266 			renderThread.start();
1267 			renderThread.priority = Thread.PRIORITY_MIN;
1268 		} else {
1269 			window.redrawOpenGlScene = { synchronized(game) drawer(); };
1270 			renderTimer = new Timer(1000 / 60, { window.redrawOpenGlSceneSoon(); });
1271 		}
1272 	};
1273 
1274 	window.onClosing = () {
1275 		volatileStore(&exit, 1);
1276 
1277 		if(updateTimer) {
1278 			updateTimer.dispose();
1279 			updateTimer = null;
1280 		}
1281 		if(renderTimer) {
1282 			renderTimer.dispose();
1283 			renderTimer = null;
1284 		}
1285 
1286 		if(renderThread) {
1287 			renderThread.join();
1288 			renderThread = null;
1289 		}
1290 		if(updateThread) {
1291 			updateThread.join();
1292 			updateThread = null;
1293 		}
1294 	};
1295 
1296 	Thread.getThis.priority = Thread.PRIORITY_MAX;
1297 
1298 	window.eventLoop(0,
1299 		delegate (KeyEvent ke) {
1300 			game.keyboardState[ke.hardwareCode] = ke.pressed;
1301 
1302 			with(VirtualController.Button)
1303 			switch(ke.key) {
1304 				case Key.Up, Key.W: game.snes[Up] = ke.pressed; break;
1305 				case Key.Down, Key.S: game.snes[Down] = ke.pressed; break;
1306 				case Key.Left, Key.A: game.snes[Left] = ke.pressed; break;
1307 				case Key.Right, Key.D: game.snes[Right] = ke.pressed; break;
1308 				case Key.Q, Key.U: game.snes[L] = ke.pressed; break;
1309 				case Key.E, Key.P: game.snes[R] = ke.pressed; break;
1310 				case Key.Z, Key.K: game.snes[B] = ke.pressed; break;
1311 				case Key.Space, Key.Enter, Key.X, Key.L: game.snes[A] = ke.pressed; break;
1312 				case Key.C, Key.I: game.snes[Y] = ke.pressed; break;
1313 				case Key.V, Key.O: game.snes[X] = ke.pressed; break;
1314 				case Key.G: game.snes[Select] = ke.pressed; break;
1315 				case Key.H: game.snes[Start] = ke.pressed; break;
1316 				case Key.Shift, Key.Shift_r: game.snes[B] = ke.pressed; break;
1317 				default:
1318 			}
1319 		}
1320 	);
1321 }
1322 
1323 /++
1324 	Simple class for putting a TrueColorImage in as an OpenGL texture.
1325 +/
1326 // Doesn't do mipmapping btw.
1327 final class OpenGlTexture {
1328 	private uint _tex;
1329 	private int _width;
1330 	private int _height;
1331 	private float _texCoordWidth;
1332 	private float _texCoordHeight;
1333 
1334 	/// Calls glBindTexture
1335 	void bind() {
1336 		doLazyLoad();
1337 		glBindTexture(GL_TEXTURE_2D, _tex);
1338 	}
1339 
1340 	/// For easy 2d drawing of it
1341 	void draw(Point where, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
1342 		draw(where.x, where.y, width, height, rotation, bg);
1343 	}
1344 
1345 	///
1346 	void draw(float x, float y, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
1347 		doLazyLoad();
1348 		glPushMatrix();
1349 		glTranslatef(x, y, 0);
1350 
1351 		if(width == 0)
1352 			width = this.originalImageWidth;
1353 		if(height == 0)
1354 			height = this.originalImageHeight;
1355 
1356 		glTranslatef(cast(float) width / 2, cast(float) height / 2, 0);
1357 		glRotatef(rotation, 0, 0, 1);
1358 		glTranslatef(cast(float) -width / 2, cast(float) -height / 2, 0);
1359 
1360 		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);
1361 		glBindTexture(GL_TEXTURE_2D, _tex);
1362 		glBegin(GL_QUADS);
1363 			glTexCoord2f(0, 0); 				glVertex2i(0, 0);
1364 			glTexCoord2f(texCoordWidth, 0); 		glVertex2i(width, 0);
1365 			glTexCoord2f(texCoordWidth, texCoordHeight); 	glVertex2i(width, height);
1366 			glTexCoord2f(0, texCoordHeight); 		glVertex2i(0, height);
1367 		glEnd();
1368 
1369 		glBindTexture(GL_TEXTURE_2D, 0); // unbind the texture
1370 
1371 		glPopMatrix();
1372 	}
1373 
1374 	/// Use for glTexCoord2f
1375 	float texCoordWidth() { return _texCoordWidth; }
1376 	float texCoordHeight() { return _texCoordHeight; } /// ditto
1377 
1378 	/// Returns the texture ID
1379 	uint tex() { doLazyLoad(); return _tex; }
1380 
1381 	/// Returns the size of the image
1382 	int originalImageWidth() { return _width; }
1383 	int originalImageHeight() { return _height; } /// ditto
1384 
1385 	// explicitly undocumented, i might remove this
1386 	TrueColorImage from;
1387 
1388 	/// Make a texture from an image.
1389 	this(TrueColorImage from) {
1390 		bindFrom(from);
1391 	}
1392 
1393 	/// Generates from text. Requires ttf.d
1394 	/// 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)
1395 	this(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
1396 		bindFrom(font, size, text);
1397 	}
1398 
1399 	/// Creates an empty texture class for you to use with [bindFrom] later
1400 	/// Using it when not bound is undefined behavior.
1401 	this() {}
1402 
1403 	private TrueColorImage pendingImage;
1404 
1405 	private final void doLazyLoad() {
1406 		if(pendingImage !is null) {
1407 			auto tmp = pendingImage;
1408 			pendingImage = null;
1409 			bindFrom(tmp);
1410 		}
1411 	}
1412 
1413 	/++
1414 		After you delete it with dispose, you may rebind it to something else with this.
1415 
1416 		If the current thread doesn't own an opengl context, it will save the image to try to lazy load it later.
1417 	+/
1418 	void bindFrom(TrueColorImage from) {
1419 		assert(from !is null);
1420 		assert(from.width > 0 && from.height > 0);
1421 
1422 		import core.stdc.stdlib;
1423 
1424 		_width = from.width;
1425 		_height = from.height;
1426 
1427 		this.from = from;
1428 
1429 		if(openGLCurrentContext() is null) {
1430 			pendingImage = from;
1431 			return;
1432 		}
1433 
1434 		auto _texWidth = _width;
1435 		auto _texHeight = _height;
1436 
1437 		const(ubyte)* data = from.imageData.bytes.ptr;
1438 		bool freeRequired = false;
1439 
1440 		// gotta round them to the nearest power of two which means padding the image
1441 		if((_texWidth & (_texWidth - 1)) || (_texHeight & (_texHeight - 1))) {
1442 			_texWidth = nextPowerOfTwo(_texWidth);
1443 			_texHeight = nextPowerOfTwo(_texHeight);
1444 
1445 			auto n = cast(ubyte*) malloc(_texWidth * _texHeight * 4);
1446 			if(n is null) assert(0);
1447 			scope(failure) free(n);
1448 
1449 			auto size = from.width * 4;
1450 			auto advance = _texWidth * 4;
1451 			int at = 0;
1452 			int at2 = 0;
1453 			foreach(y; 0 .. from.height) {
1454 				n[at .. at + size] = from.imageData.bytes[at2 .. at2+ size];
1455 				at += advance;
1456 				at2 += size;
1457 			}
1458 
1459 			data = n;
1460 			freeRequired = true;
1461 
1462 			// the rest of data will be initialized to zeros automatically which is fine.
1463 		}
1464 
1465 		glGenTextures(1, &_tex);
1466 		glBindTexture(GL_TEXTURE_2D, tex);
1467 
1468 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1469 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1470 
1471 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1472 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1473 
1474 		glTexImage2D(
1475 			GL_TEXTURE_2D,
1476 			0,
1477 			GL_RGBA,
1478 			_texWidth, // needs to be power of 2
1479 			_texHeight,
1480 			0,
1481 			GL_RGBA,
1482 			GL_UNSIGNED_BYTE,
1483 			data);
1484 
1485 		assert(!glGetError());
1486 
1487 		_texCoordWidth = cast(float) _width / _texWidth;
1488 		_texCoordHeight = cast(float) _height / _texHeight;
1489 
1490 		if(freeRequired)
1491 			free(cast(void*) data);
1492 		glBindTexture(GL_TEXTURE_2D, 0);
1493 	}
1494 
1495 	/// ditto
1496 	void bindFrom(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
1497 		assert(font !is null);
1498 		int width, height;
1499 		auto data = font.renderString(text, size, width, height);
1500 		auto image = new TrueColorImage(width, height);
1501 		int pos = 0;
1502 		foreach(y; 0 .. height)
1503 		foreach(x; 0 .. width) {
1504 			image.imageData.bytes[pos++] = 255;
1505 			image.imageData.bytes[pos++] = 255;
1506 			image.imageData.bytes[pos++] = 255;
1507 			image.imageData.bytes[pos++] = data[0];
1508 			data = data[1 .. $];
1509 		}
1510 		assert(data.length == 0);
1511 
1512 		bindFrom(image);
1513 	}
1514 
1515 	/// Deletes the texture. Using it after calling this is undefined behavior
1516 	void dispose() {
1517 		glDeleteTextures(1, &_tex);
1518 		_tex = 0;
1519 	}
1520 
1521 	~this() {
1522 		if(_tex > 0)
1523 			dispose();
1524 	}
1525 }
1526 
1527 /+
1528 	FIXME: i want to do stbtt_GetBakedQuad for ASCII and use that
1529 	for simple cases especially numbers. for other stuff you can
1530 	create the texture for the text above.
1531 +/
1532 
1533 ///
1534 void clearOpenGlScreen(SimpleWindow window) {
1535 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
1536 }
1537 
1538