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();`, or you can `mixin GenericGame!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 or for porting something that uses a ScreenPainter. (not implemented)
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. An old school trick is to seed it based on some user input, even just time it took then to go past the title screen.
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`. `designFps` = 0, `requestAnimationFrame`, `requestAnimation(duration)`
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 	void onBecomeActiveScreen() {}
396 	void onExitActiveScreen() {}
397 
398 	private bool loaded;
399 	final void ensureLoaded(GameHelperBase game) {
400 		if(!this.loaded) {
401 			// FIXME: unpause the update thread when it is done
402 			synchronized(game) {
403 				if(!this.loaded) {
404 					this.load();
405 					this.loaded = true;
406 				}
407 			}
408 		}
409 	}
410 }
411 
412 /+
413 	you ask for things to be done - foo();
414 	and other code asks you to do things - foo() { }
415 
416 
417 	Recommended drawing methods:
418 		old opengl
419 		new opengl
420 		nanovega
421 
422 	FIXME:
423 		for nanovega, load might want a withNvg()
424 		both load and drawFrame might want a nvgFrame()
425 
426 		game.nvgFrame((nvg) {
427 
428 		});
429 +/
430 
431 /++
432 	Tip: if your screen is a generic component reused across many games, you might pass `GameHelperBase` as the `Game` parameter.
433 +/
434 class GameScreen(Game) : GameScreenBase {
435 	private Game game_;
436 
437 	// convenience accessors
438 	final AudioOutputThread audio() {
439 		// FIXME: if the audio thread threw, we should forward it at some point
440 		if(this is null || game is null) return AudioOutputThread.init;
441 		return game.audio;
442 	}
443 
444 	final VirtualController snes(int player = 0) {
445 		if(this is null || game is null) return VirtualController.init;
446 		return game.sneses[player];
447 	}
448 
449 	/+
450 		manual draw mode turns off the automatic timer to render and only
451 		draws when you specifically trigger it. might not be worth tho.
452 	+/
453 
454 
455 	// You are not supposed to call this.
456 	final void setGame(Game game) {
457 		// assert(game_ is null);
458 		assert(game !is null);
459 		this.game_ = game;
460 	}
461 
462 	/++
463 		Gives access to your game object for use through the screen.
464 	+/
465 	public override inout(Game) game() inout {
466 		if(game_ is null)
467 			throw new Exception("The game screen isn't showing!");
468 		return game_;
469 	}
470 
471 	/++
472 		`update`'s responsibility is to:
473 
474 		$(LIST
475 			* Process player input
476 			* Update game state - object positions, do collision detection, etc.
477 			* Run any character AI
478 			* Kick off any audio associated with changes in this update
479 			* Transition to other screens if appropriate
480 		)
481 
482 		It is NOT supposed to:
483 
484 		$(LIST
485 			* draw - that's the job of [drawFrame]
486 			* load files, bind textures, or similar - that's the job of [load]
487 			* set uniforms or other OpenGL objects - do one-time things in [load] and per-frame things in [drawFrame]
488 		)
489 	+/
490 	override abstract void update();
491 
492 	/++
493 		`drawFrame`'s responsibility is to draw a single frame. It can use the `interpolate` method to smooth animations between updates.
494 
495 		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.
496 
497 		It should also NOT load any files, create textures, or any other setup task - [load] is supposed to have already done that.
498 	+/
499 	override abstract void drawFrame(float interpolate);
500 
501 	/++
502 		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.
503 	+/
504 	override void load() {}
505 }
506 
507 /// ditto
508 //alias GenericGameScreen = GameScreen!GameHelperBase;
509 
510 ///
511 unittest {
512 	// The TitleScreen has a simple job: show the title until the user presses start. After that, it will progress to the GameplayScreen.
513 
514 	static // exclude from docs
515 	class DemoGame : GameHelperBase {
516 		// I put this inside DemoGame for this demo, but you could define them in separate files if you wanted to
517 		static class TitleScreen : GameScreen!DemoGame {
518 			override void update() {
519 				// you can always access your main Game object through the screen objects
520 				if(game.snes[VirtualController.Button.Start]) {
521 					//game.showScreen(new GameplayScreen());
522 				}
523 			}
524 
525 			override void drawFrame(float interpolate) {
526 
527 			}
528 		}
529 
530 		// and the minimum boilerplate the game itself must provide for the library
531 		// is the window it wants to use and the first screen to load into it.
532 		override TitleScreen firstScreen() {
533 			return new TitleScreen();
534 		}
535 
536 		override SimpleWindow getWindow() {
537 			auto window = create2dWindow("Demo game");
538 			return window;
539 		}
540 	}
541 
542 	void main() {
543 		runGame!DemoGame();
544 	}
545 
546 	main(); // exclude from docs
547 }
548 
549 /+
550 	Networking helper: just send/receive messages and manage some connections
551 
552 	It might offer a controller queue you can put local and network events in to get fair lag and transparent ultiplayer
553 
554 	split screen?!?!
555 
556 +/
557 
558 /+
559 	ADD ME:
560 	Animation helper like audio style. Your game object
561 	has a particular image attached as primary.
562 
563 	You can be like `animate once` or `animate indefinitely`
564 	and it takes care of it, then set new things and it does that too.
565 +/
566 
567 public import arsd.gamehelpers;
568 public import arsd.color;
569 public import arsd.simpledisplay;
570 public import arsd.simpleaudio;
571 
572 import std.math;
573 public import core.time;
574 
575 import arsd.core;
576 
577 import arsd.simpledisplay : Timer;
578 
579 public import arsd.joystick;
580 
581 /++
582 	Creates a simple 2d (old-style) opengl simpledisplay window. It sets the matrix for pixel coordinates and enables alpha blending and textures.
583 +/
584 SimpleWindow create2dWindow(string title, int width = 512, int height = 512) {
585 	auto window = new SimpleWindow(width, height, title, OpenGlOptions.yes, Resizeability.allowResizing);
586 
587 	//window.visibleForTheFirstTime = () {
588 		window.setAsCurrentOpenGlContext();
589 
590 		glEnable(GL_BLEND);
591 		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
592 		glClearColor(0,0,0,0);
593 		glDepthFunc(GL_LEQUAL);
594 
595 		glMatrixMode(GL_PROJECTION);
596 		glLoadIdentity();
597 		glOrtho(0, width, height, 0, 0, 1);
598 
599 		glMatrixMode(GL_MODELVIEW);
600 		glLoadIdentity();
601 		glDisable(GL_DEPTH_TEST);
602 		glEnable(GL_TEXTURE_2D);
603 	//};
604 
605 	// because the render thread holds the opengl context so everything has to happen there, but still can provide the dg here
606 	window.windowResized = (newWidth, newHeight) {
607 		int x, y, w, h;
608 
609 		// FIXME: this works for only square original sizes
610 		if(newWidth < newHeight) {
611 			w = newWidth;
612 			h = newWidth * height / width;
613 			x = 0;
614 			y = (newHeight - h) / 2;
615 		} else {
616 			w = newHeight * width / height;
617 			h = newHeight;
618 			x = (newWidth - w) / 2;
619 			y = 0;
620 		}
621 		window.setAsCurrentOpenGlContext();
622 		glViewport(x, y, w, h);
623 		window.redrawOpenGlSceneSoon();
624 	};
625 
626 	return window;
627 }
628 
629 /++
630 	This is the base class for your game. Create a class based on this, then pass it to [runGame].
631 +/
632 abstract class GameHelperBase : SynchronizableObject {
633 	/++
634 		Implement this to draw.
635 
636 		The `interpolateToNextFrame` argument tells you how close you are to the next frame. You should
637 		take your current state and add the estimated next frame things multiplied by this to get smoother
638 		animation. interpolateToNextFrame will always be >= 0 and < 1.0.
639 
640 		History:
641 			Previous to August 27, 2022, this took no arguments. It could thus not interpolate frames!
642 	+/
643 	deprecated("Move to void drawFrame(float) in a GameScreen instead") void drawFrame(float interpolateToNextFrame) {
644 		drawFrameInternal(interpolateToNextFrame);
645 	}
646 
647 	final void drawFrameInternal(float interpolateToNextFrame) {
648 		if(currentScreen is null)
649 			return;
650 
651 		currentScreen.ensureLoaded(this);
652 		currentScreen.drawFrame(interpolateToNextFrame);
653 	}
654 
655 	// in frames
656 	ushort snesRepeatRate() { return ushort.max; }
657 	ushort snesRepeatDelay() { return snesRepeatRate(); }
658 
659 	/++
660 		Implement this to update your game state by a single fixed timestep. You should
661 		check for user input state here.
662 
663 		Return true if something visibly changed to queue a frame redraw asap.
664 
665 		History:
666 			Previous to August 27, 2022, this took an argument. This was a design flaw.
667 	+/
668 	deprecated("Move to void update in a GameScreen instead") bool update() { return false; }
669 
670 	/+
671 		override this to have more control over synchronization
672 
673 		its main job is to lock on `this` and update what [update] changes
674 		and call `bookkeeping` while inside the lock
675 
676 		but if you have some work that can be done outside the lock - things
677 		that are read-only on the game state - you might split it up here and
678 		batch your update. as long as nothing that the [drawFrame] needs is mutated
679 		outside the lock you'll be ok.
680 
681 		History:
682 			Added November 12, 2022
683 	+/
684 	bool updateWithManualLock(scope void delegate() bookkeeping) shared {
685 		if(currentScreen is null)
686 			return false;
687 		synchronized(this) {
688 			if(currentScreen.loaded)
689 				(cast() this).currentScreen.update();
690 			bookkeeping();
691 			return false;
692 		}
693 	}
694 	//abstract void fillAudioBuffer(short[] buffer);
695 
696 	/++
697 		Returns the main game window. This function will only be
698 		called once if you use runGame. You should return a window
699 		here like one created with `create2dWindow`.
700 	+/
701 	abstract SimpleWindow getWindow();
702 
703 	/++
704 		Override this and return true to initialize the audio system. If you return `true`
705 		here, the [audio] member can be used.
706 	+/
707 	bool wantAudio() { return false; }
708 
709 	/++
710 		Override this and return true if you are compatible with separate render and update threads.
711 	+/
712 	bool multithreadCompatible() { return true; }
713 
714 	/// You must override [wantAudio] and return true for this to be valid;
715 	AudioOutputThread audio;
716 
717 	this() {
718 		audio = AudioOutputThread(wantAudio() && primaryAudioDevice != "null");
719 	}
720 
721 	protected bool redrawForced;
722 
723 	private GameScreenBase currentScreen;
724 
725 	/+
726 	// it will also need a configuration in time and such
727 	enum ScreenTransition {
728 		none,
729 		crossFade
730 	}
731 	+/
732 
733 	/++
734 		Shows the given screen, making it actively responsible for drawing and updating,
735 		optionally through the given transition effect.
736 	+/
737 	void showScreen(this This, Screen)(Screen cs, GameScreenBase transition = null) {
738 		cs.setGame(cast(This) this);
739 		if(currentScreen)
740 			currentScreen.onExitActiveScreen();
741 		currentScreen = cs;
742 		currentScreen.onBecomeActiveScreen();
743 		// FIXME: pause the update thread here, and fast forward the game clock when it is unpaused
744 		// (this actually SHOULD be called from the update thread, except for the initial load... and even that maybe it will then)
745 		// but i have to be careful waiting here because it can deadlock with teh mutex still locked.
746 
747 		// FIXME: do some onDisplay and onOut events in the screen you can override
748 	}
749 
750 	/++
751 		Returns the first screen of your game.
752 	+/
753 	abstract GameScreenBase firstScreen();
754 
755 	/++
756 		Returns the number of game updates per second your game is designed for.
757 
758 		This isn't necessarily the number of frames drawn per second, which may be more
759 		or less due to frame skipping and interpolation, but it is the number of times
760 		your screen's update methods will be called each second.
761 
762 		You actually want to make this as small as possible without breaking your game's
763 		physics and feeling of responsiveness to the controls. Remember, the display FPS
764 		is different - you can interpolate frames for smooth animation. What you want to
765 		ensure here is that the design fps is big enough that you don't have problems like
766 		clipping through walls or sluggishness in player control, but not so big that the
767 		computer is busy doing collision detection, etc., all the time and has no time
768 		left over to actually draw the game.
769 
770 		I personally find 20 actually works pretty well, though the default set here is 60
771 		due to how common that number is. You are encouraged to override this and use what
772 		works for you.
773 	+/
774 	int designFps() { return 60; }
775 
776 	/// Forces a redraw even if update returns false
777 	final public void forceRedraw() {
778 		redrawForced = true;
779 	}
780 
781 	/// These functions help you handle user input. It offers polling functions for
782 	/// keyboard, mouse, joystick, and virtual controller input.
783 	///
784 	/// The virtual digital controllers are best to use if that model fits you because it
785 	/// works with several kinds of controllers as well as keyboards.
786 
787 	JoystickUpdate[4] joysticks;
788 	ref JoystickUpdate joystick1() { return joysticks[0]; }
789 
790 	bool[256] keyboardState;
791 
792 	// FIXME: add a mouse position and delta thing too.
793 
794 	/++
795 
796 	+/
797 	ref VirtualController snes() { return sneses[0]; }
798 
799 	VirtualController[4] sneses;
800 }
801 
802 /++
803 	The virtual controller is based on the SNES. If you need more detail, try using
804 	the joystick or keyboard and mouse members directly.
805 
806 	```
807 	 l          r
808 
809 	 U          X
810 	L R  s  S  Y A
811 	 D          B
812 	```
813 
814 	For Playstation and XBox controllers plugged into the computer,
815 	it picks those buttons based on similar layout on the physical device.
816 
817 	For keyboard control, arrows and WASD are mapped to the d-pad (ULRD in the diagram),
818 	Q and E are mapped to the shoulder buttons (l and r in the diagram).So are U and P.
819 
820 	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.
821 
822 	G is mapped to select (s), and H is mapped to start (S).
823 
824 	The space bar and enter keys are also set to button A, with shift mapped to button B.
825 
826 	Additionally, the mouse is mapped to the virtual joystick, and mouse buttons left and right are mapped to shoulder buttons L and R.
827 
828 
829 	Only player 1 is mapped to the keyboard.
830 +/
831 struct VirtualController {
832 	ushort previousState;
833 	ushort state;
834 
835 	// for key repeat
836 	ushort truePreviousState;
837 	ushort lastStateChange;
838 	bool repeating;
839 
840 	///
841 	enum Button {
842 		Up, Left, Right, Down,
843 		X, A, B, Y,
844 		Select, Start, L, R
845 	}
846 
847 	@nogc pure nothrow @safe:
848 
849 	/++
850 		History: Added April 30, 2020
851 
852 		Parameter `allowAutoRepeat` added December 25, 2025
853 	+/
854 	bool justPressed(Button idx, bool allowAutoRepeat = true) const {
855 		auto before = ((allowAutoRepeat ? previousState : truePreviousState) & (1 << (cast(int) idx))) ? true : false;
856 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
857 		return !before && after;
858 	}
859 	/++
860 		History: Added April 30, 2020
861 
862 		Parameter `allowAutoRepeat` added December 25, 2025
863 	+/
864 	bool justReleased(Button idx, bool allowAutoRepeat = true) const {
865 		auto before = ((allowAutoRepeat ? previousState : truePreviousState) & (1 << (cast(int) idx))) ? true : false;
866 		auto after = (state & (1 << (cast(int) idx))) ? true : false;
867 		return before && !after;
868 	}
869 
870 	/+
871 	+/
872 
873 	VirtualJoystick stick;
874 
875 	///
876 	bool opIndex(Button idx) const {
877 		return (state & (1 << (cast(int) idx))) ? true : false;
878 	}
879 	private void opIndexAssign(bool value, Button idx) {
880 		if(value)
881 			state |= (1 << (cast(int) idx));
882 		else
883 			state &= ~(1 << (cast(int) idx));
884 	}
885 }
886 
887 struct ButtonCheck {
888 	bool wasPressed() {
889 		return false;
890 	}
891 	bool wasReleased() {
892 		return false;
893 	}
894 	bool wasClicked() {
895 		return false;
896 	}
897 	bool isHeld() {
898 		return false;
899 	}
900 
901 	bool opCast(T : bool)() {
902 		return isHeld();
903 	}
904 }
905 
906 /++
907 	Multi-level wrappers for [runGame]. `GenericGame` mixes in a `main` function that forwards its arguments to `runGameMain`, and then `runGameMain` will parse some generic command line arguments before passing the rest of the arguments to `runGame`. These let you focus on game-specific code while keeping things configurable by the user.
908 
909 	The arguments you set are generally maximums, but users can turn it down via the command line.
910 
911 	Params:
912 		GameObjectType = the type of your [GameHelperBase] subclass that implements the game.
913 		targetUpdateRate = the design rate at which your game's `update` methods are called. If 0, it comes from [GameHelperBase.designFps]
914 		maxRedrawRate = the maximum frames per second you will draw. If higher than the update rate, you may be asked to draw interpolated frames. If lower, it will skip some frames as it prioritizes keeping updates on schedule. Users can request a lower redraw rate via command line arguments or gui launcher.
915 
916 	User_Configuration:
917 		NONE OF THIS IS IMPLEMENTED
918 
919 		speed = changing the update rate to make the game run faster or slower in real time
920 
921 		fps = lets the user reduce the max redraw rate
922 
923 		audio = changes the default audio device. `null` means it will not allow audio output at all.
924 
925 		controller = lets you remap the controller
926 
927 		config = load and maybe save changes to a config file
928 
929 		gui = makes a gui launcher to set other args before starting the game
930 
931 	History:
932 		Added January 28, 2026
933 +/
934 mixin template GenericGame(GameObjectType, int targetUpdateRate = 0, int maxRedrawRate = 0) {
935 	int main(string[] args) {
936 		return runGameMain!(GameObjectType, targetUpdateRate, maxRedrawRate)(args);
937 	}
938 }
939 
940 /// ditto
941 int runGameMain(GameObjectType, int targetUpdateRate = 0, int maxRedrawRate = 0)(string[] args) {
942 	import arsd.cli;
943 
944 	static int cliWrapper(int redrawRate, string audioDevice, string[] args) {
945 		//if(redrawRate > maxRedrawRate)
946 			//redrawRate = maxRedrawRate; // FIXME: handle the 0 case of max...
947 
948 		primaryAudioDevice = audioDevice;
949 
950 		return runGame!GameObjectType(targetUpdateRate, redrawRate, [""] ~ args);
951 	}
952 	return runCli!cliWrapper(args);
953 
954 	//return runGame!(GameObjectType)(targetUpdateRate, maxRedrawRate, args);
955 }
956 
957 private __gshared string primaryAudioDevice;
958 
959 /++
960 	Deprecated, use the other overload instead.
961 
962 	History:
963 		Deprecated on May 9, 2020. Instead of calling
964 		`runGame(your_instance);` run `runGame!YourClass();`
965 		instead. If you needed to change something in the game
966 		ctor, make a default constructor in your class to do that
967 		instead.
968 +/
969 deprecated("Use runGame!YourGameType(updateRate, redrawRate); instead now.")
970 void runGame()(GameHelperBase game, int targetUpdateRate = 20, int maxRedrawRate = 0) { assert(0, "this overload is deprecated, use runGame!YourClass instead"); }
971 
972 /++
973 	Runs your game. It will construct the given class and destroy it at end of scope.
974 	Your class must have a default constructor and must implement [GameHelperBase].
975 	Your class should also probably be `final` for a small, but easy performance boost.
976 
977 	$(TIP
978 		If you need to pass parameters to your game class, you can define
979 		it as a nested class in your `main` function and access the local
980 		variables that way instead of passing them explicitly through the
981 		constructor.
982 	)
983 
984 	Params:
985 	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.
986 	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.
987 	args = command line arguments to use to construct the game object
988 +/
989 int runGame(T : GameHelperBase)(int targetUpdateRate = 0, int maxRedrawRate = 0, string[] args = null) {
990 
991 	T game;
992 	if(args.length) {
993 		import arsd.cli;
994 		int ret;
995 		game = constructFromCliArgs!T(args, ret);
996 		if(game is null)
997 			return ret;
998 	} else {
999 		game = new T();
1000 	}
1001 	g_game = game;
1002 	scope(exit) {
1003 		g_game = null;
1004 		.destroy(game);
1005 	}
1006 
1007 	if(targetUpdateRate == 0)
1008 		targetUpdateRate = game.designFps();
1009 
1010 	// this is a template btw because then it can statically dispatch
1011 	// the members instead of going through the virtual interface.
1012 
1013 	auto window = game.getWindow();
1014 	game.showScreen(game.firstScreen());
1015 
1016 	auto lastUpdate = MonoTime.currTime;
1017 	bool isImmediateUpdate;
1018 
1019 	int joystickPlayers;
1020 
1021 	window.redrawOpenGlScene = null;
1022 
1023 	/*
1024 		The game clock should always be one update ahead of the real world clock.
1025 
1026 		If it is behind the real world clock, it needs to run update faster, so it will
1027 		double up on its timer to try to update and skip some render frames to make cpu time available.
1028 		Generally speaking the render should never be more than one full frame ahead of the game clock,
1029 		and since the game clock should always be a bit ahead of the real world clock, if the game clock
1030 		is behind the real world clock, time to skip.
1031 
1032 		If there's a huge jump in the real world clock - more than a couple seconds between
1033 		updates - this probably indicates the computer went to sleep or something. We can't
1034 		catch up, so this will just resync the clock to real world and not try to catch up.
1035 	*/
1036 	MonoTime gameClock;
1037 	// FIXME: render thread should be lower priority than the ui thread
1038 
1039 	int rframeCounter = 0;
1040 	auto drawer = delegate bool() {
1041 		if(gameClock is MonoTime.init)
1042 			return false; // can't draw uninitialized info
1043 		/* // i think this is the same as if delta < 0 below...
1044 		auto time = MonoTime.currTime;
1045 		if(gameClock + (1000.msecs / targetUpdateRate) < time) {
1046 			writeln("frame skip ", gameClock, " vs ", time);
1047 			return false; // we're behind on updates, skip this frame
1048 		}
1049 		*/
1050 
1051 		if(false && isImmediateUpdate) {
1052 			game.drawFrameInternal(0.0);
1053 			isImmediateUpdate = false;
1054 		} else {
1055 			auto now = MonoTime.currTime - lastUpdate;
1056 			Duration nextFrame = msecs(1000 / targetUpdateRate);
1057 			auto delta = cast(float) ((nextFrame - now).total!"usecs") / cast(float) nextFrame.total!"usecs";
1058 
1059 			if(delta < 0) {
1060 				//writeln("behind ", cast(int)(delta * 100));
1061 				return false; // the render is too far ahead of the updater! time to skip frames to let it catch up
1062 			}
1063 
1064 			game.drawFrameInternal(1.0 - delta);
1065 		}
1066 
1067 		rframeCounter++;
1068 		/+
1069 		if(rframeCounter % 60 == 0) {
1070 			writeln("frame");
1071 		}
1072 		+/
1073 
1074 		return true;
1075 	};
1076 
1077 	import core.thread;
1078 	import core..volatile;
1079 	Thread renderThread; // FIXME: low priority
1080 	Thread updateThread; // FIXME: slightly high priority
1081 
1082 	// shared things to communicate with threads
1083 	ubyte exit;
1084 	ulong newWindowSize;
1085 	ubyte loadRequired; // if the screen changed and you need to call load again in the render thread
1086 
1087 	ubyte workersPaused;
1088 	// Event unpauseRender; // maybe a manual reset so you set it then reset after unpausing
1089 	// Event unpauseUpdate;
1090 
1091 	// the input buffers should prolly be double buffered generally speaking
1092 
1093 	// FIXME: i might just want an asset cache thing
1094 	// 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.
1095 
1096 	Timer renderTimer;
1097 	Timer updateTimer;
1098 
1099 	auto updater = delegate() {
1100 		if(gameClock is MonoTime.init) {
1101 			gameClock = MonoTime.currTime;
1102 		}
1103 
1104 		foreach(p; 0 .. joystickPlayers) {
1105 			if(p >= 4)
1106 				continue;
1107 			version(linux)
1108 				readJoystickEvents(joystickFds[p]);
1109 			auto update = getJoystickUpdate(p);
1110 
1111 			static if(__traits(isSame, Button, PS1Buttons)) {
1112 				// PS1 style joystick mapping compiled in
1113 				with(Button) with(VirtualController.Button) {
1114 					// so I did the "wasJustPressed thing because it interplays
1115 					// better with the keyboard as well which works on events...
1116 					if(update.buttonWasJustPressed(square)) game.sneses[p][Y] = true;
1117 					if(update.buttonWasJustPressed(triangle)) game.sneses[p][X] = true;
1118 					if(update.buttonWasJustPressed(cross)) game.sneses[p][B] = true;
1119 					if(update.buttonWasJustPressed(circle)) game.sneses[p][A] = true;
1120 					if(update.buttonWasJustPressed(select)) game.sneses[p][Select] = true;
1121 					if(update.buttonWasJustPressed(start)) game.sneses[p][Start] = true;
1122 					if(update.buttonWasJustPressed(l1)) game.sneses[p][L] = true;
1123 					if(update.buttonWasJustPressed(r1)) game.sneses[p][R] = true;
1124 					// note: no need to check analog stick here cuz joystick.d already does it for us (per old playstation tradition)
1125 					if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < -8) game.sneses[p][Left] = true;
1126 					if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > 8) game.sneses[p][Right] = true;
1127 					if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < -8) game.sneses[p][Up] = true;
1128 					if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > 8) game.sneses[p][Down] = true;
1129 
1130 					if(update.buttonWasJustReleased(square)) game.sneses[p][Y] = false;
1131 					if(update.buttonWasJustReleased(triangle)) game.sneses[p][X] = false;
1132 					if(update.buttonWasJustReleased(cross)) game.sneses[p][B] = false;
1133 					if(update.buttonWasJustReleased(circle)) game.sneses[p][A] = false;
1134 					if(update.buttonWasJustReleased(select)) game.sneses[p][Select] = false;
1135 					if(update.buttonWasJustReleased(start)) game.sneses[p][Start] = false;
1136 					if(update.buttonWasJustReleased(l1)) game.sneses[p][L] = false;
1137 					if(update.buttonWasJustReleased(r1)) game.sneses[p][R] = false;
1138 					if(update.axisChange(Axis.horizontalDpad) > 0 && update.axisPosition(Axis.horizontalDpad) > -8) game.sneses[p][Left] = false;
1139 					if(update.axisChange(Axis.horizontalDpad) < 0 && update.axisPosition(Axis.horizontalDpad) < 8) game.sneses[p][Right] = false;
1140 					if(update.axisChange(Axis.verticalDpad) > 0 && update.axisPosition(Axis.verticalDpad) > -8) game.sneses[p][Up] = false;
1141 					if(update.axisChange(Axis.verticalDpad) < 0 && update.axisPosition(Axis.verticalDpad) < 8) game.sneses[p][Down] = false;
1142 				}
1143 
1144 			} else static if(__traits(isSame, Button, XBox360Buttons)) {
1145 			static assert(0);
1146 				// XBox style mapping
1147 				// the reason this exists is if the programmer wants to use the xbox details, but
1148 				// might also want the basic controller in here. joystick.d already does translations
1149 				// so an xbox controller with the default build actually uses the PS1 branch above.
1150 				/+
1151 				case XBox360Buttons.a: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_A) ? true : false;
1152 				case XBox360Buttons.b: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_B) ? true : false;
1153 				case XBox360Buttons.x: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_X) ? true : false;
1154 				case XBox360Buttons.y: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_Y) ? true : false;
1155 
1156 				case XBox360Buttons.lb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) ? true : false;
1157 				case XBox360Buttons.rb: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) ? true : false;
1158 
1159 				case XBox360Buttons.back: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) ? true : false;
1160 				case XBox360Buttons.start: return (what.Gamepad.wButtons & XINPUT_GAMEPAD_START) ? true : false;
1161 				+/
1162 			}
1163 
1164 			game.joysticks[p] = update;
1165 		}
1166 
1167 		int runs;
1168 
1169 		again:
1170 
1171 		auto now = MonoTime.currTime;
1172 		bool changed;
1173 		changed = (cast(shared)game).updateWithManualLock({ lastUpdate = now; });
1174 
1175 		foreach(ref snes; game.sneses) {
1176 			auto stateChange = snes.truePreviousState ^ game.snes.state;
1177 			snes.previousState = snes.state;
1178 			snes.truePreviousState = snes.state;
1179 
1180 			if(stateChange == 0) {
1181 				snes.lastStateChange++;
1182 				auto r = game.snesRepeatRate();
1183 				if(r != typeof(r).max && !snes.repeating && snes.lastStateChange == game.snesRepeatDelay()) {
1184 					snes.lastStateChange = 0;
1185 					snes.repeating = true;
1186 				} else if(r != typeof(r).max && snes.repeating && snes.lastStateChange == r) {
1187 					snes.lastStateChange = 0;
1188 					snes.previousState = 0;
1189 				}
1190 			} else {
1191 				snes.repeating = false;
1192 			}
1193 		}
1194 
1195 		if(game.redrawForced) {
1196 			changed = true;
1197 			game.redrawForced = false;
1198 		}
1199 
1200 		gameClock += 1.seconds / targetUpdateRate;
1201 
1202 		if(++runs < 3 && gameClock < MonoTime.currTime)
1203 			goto again;
1204 
1205 		// FIXME: rate limiting
1206 		// FIXME: triple buffer it.
1207 		if(changed && renderThread is null) {
1208 			isImmediateUpdate = true;
1209 			window.redrawOpenGlSceneSoon();
1210 		}
1211 	};
1212 
1213 	//window.vsync = false;
1214 
1215 	const maxRedrawTime = maxRedrawRate > 0 ? (1000.msecs / maxRedrawRate) : 4.msecs;
1216 
1217 	if(game.multithreadCompatible()) {
1218 		window.redrawOpenGlScene = null;
1219 		renderThread = new Thread({
1220 			scope(failure) runInGuiThreadAsync(() { window.close(); });
1221 			// FIXME: catch exception and inform the parent
1222 			int frames = 0;
1223 			int skipped = 0;
1224 
1225 			Duration renderTime;
1226 			Duration flipTime;
1227 			Duration renderThrottleTime;
1228 
1229 			MonoTime initial = MonoTime.currTime;
1230 
1231 			auto windowResized = window.windowResized;
1232 			window.windowResized = null;
1233 			int storedWidth;
1234 			int storedHeight;
1235 
1236 			while(!volatileLoad(&exit)) {
1237 				MonoTime start = MonoTime.currTime;
1238 				{
1239 					window.mtLock();
1240 					scope(exit)
1241 						window.mtUnlock();
1242 					window.setAsCurrentOpenGlContext();
1243 				}
1244 
1245 				{
1246 					auto newWidth = window.width;
1247 					auto newHeight = window.height;
1248 					if(newWidth != storedWidth || newHeight != storedHeight)
1249 						windowResized(newWidth, newHeight);
1250 					storedWidth = newWidth;
1251 					storedHeight = newHeight;
1252 				}
1253 
1254 
1255 				bool actuallyDrew;
1256 
1257 				synchronized(game)
1258 					actuallyDrew = drawer();
1259 
1260 				MonoTime end = MonoTime.currTime;
1261 
1262 				if(actuallyDrew) {
1263 					window.mtLock();
1264 					scope(exit)
1265 						window.mtUnlock();
1266 					window.swapOpenGlBuffers();
1267 				}
1268 				// want to ensure the vsync wait occurs here, outside the window and locks
1269 				// some impls will do it on glFinish, some on the next touch of the
1270 				// front buffer, hence the clear being done here.
1271 				if(actuallyDrew) {
1272 					glFinish();
1273 					clearOpenGlScreen(window);
1274 				}
1275 
1276 				// this is just to wake up the UI thread to check X events again
1277 				// (any custom event will force a check of XPending) just cuz apparently
1278 				// the readiness of the file descriptor can be reset by one of the vsync functions
1279 				static if(UsingSimpledisplayX11) {
1280 					__gshared thing = new Object;
1281 					window.postEvent(thing);
1282 				}
1283 
1284 				MonoTime flip = MonoTime.currTime;
1285 
1286 				renderTime += end - start;
1287 				flipTime += flip - end;
1288 
1289 				if(flip - start < maxRedrawTime) {
1290 					renderThrottleTime += maxRedrawTime - (flip - start);
1291 					Thread.sleep(maxRedrawTime - (flip - start));
1292 				}
1293 
1294 				if(actuallyDrew)
1295 					frames++;
1296 				else
1297 					skipped++;
1298 				// if(frames % 60 == 0) writeln("frame");
1299 			}
1300 
1301 			MonoTime finalt = MonoTime.currTime;
1302 
1303 			writeln("Average render time: ", renderTime / frames);
1304 			writeln("Average flip time: ", flipTime / frames);
1305 			writeln("Average throttle time: ", renderThrottleTime / frames);
1306 			writeln("Frames: ", frames, ", skipped: ", skipped, " over ", finalt - initial);
1307 		});
1308 
1309 		updateThread = new Thread({
1310 			scope(failure) runInGuiThreadAsync(() { window.close(); });
1311 			// FIXME: catch exception and inform the parent
1312 			int frames;
1313 
1314 			joystickPlayers = enableJoystickInput();
1315 			scope(exit) closeJoysticks();
1316 
1317 			Duration updateTime;
1318 			Duration waitTime;
1319 
1320 			while(!volatileLoad(&exit)) {
1321 				MonoTime start = MonoTime.currTime;
1322 				updater();
1323 				MonoTime end = MonoTime.currTime;
1324 
1325 				updateTime += end - start;
1326 
1327 				frames++;
1328 				// if(frames % game.designFps == 0) writeln("update");
1329 
1330 				const now = MonoTime.currTime - lastUpdate;
1331 				Duration nextFrame = msecs(1000) / targetUpdateRate;
1332 				const sleepTime = nextFrame - now;
1333 				if(sleepTime.total!"msecs" <= 0) {
1334 					// falling behind on update...
1335 				} else {
1336 					waitTime += sleepTime;
1337 					// writeln(sleepTime);
1338 					Thread.sleep(sleepTime);
1339 				}
1340 			}
1341 
1342 			writeln("Average update time: " , updateTime / frames);
1343 			writeln("Average wait time: " , waitTime / frames);
1344 		});
1345 	} else {
1346 		// single threaded, vsync a bit dangeresque here since it
1347 		// puts the ui thread to sleep!
1348 		window.vsync = false;
1349 	}
1350 
1351 	// FIXME: when single threaded, set the joystick here
1352 	// actually just always do the joystick in the event thread regardless
1353 
1354 	int frameCounter;
1355 
1356 	auto first = window.visibleForTheFirstTime;
1357 	window.visibleForTheFirstTime = () {
1358 		if(first)
1359 			first();
1360 
1361 		if(updateThread) {
1362 			updateThread.start();
1363 		} else {
1364 			updateTimer = new Timer(1000 / targetUpdateRate, {
1365 				frameCounter++;
1366 				updater();
1367 			});
1368 		}
1369 
1370 		if(renderThread) {
1371 			window.suppressAutoOpenglViewport = true; // we don't want the context being pulled back by the other thread now, we'll check it over here.
1372 			// FIXME: set viewport prior to render if width/height changed
1373 			window.releaseCurrentOpenGlContext(); // need to let the render thread take it
1374 			renderThread.start();
1375 			renderThread.priority = Thread.PRIORITY_MIN;
1376 		} else {
1377 			window.redrawOpenGlScene = { synchronized(game) drawer(); };
1378 			renderTimer = new Timer(1000 / 60, { window.redrawOpenGlSceneSoon(); });
1379 		}
1380 	};
1381 
1382 	window.onClosing = () {
1383 		volatileStore(&exit, 1);
1384 
1385 		if(updateTimer) {
1386 			updateTimer.dispose();
1387 			updateTimer = null;
1388 		}
1389 		if(renderTimer) {
1390 			renderTimer.dispose();
1391 			renderTimer = null;
1392 		}
1393 
1394 		if(renderThread) {
1395 			auto ex = renderThread.join(false);
1396 			if(ex) {
1397 				import arsd.core;
1398 				writelnStderr("From Render Thread: ", ex.toString());
1399 			}
1400 			renderThread = null;
1401 		}
1402 		if(updateThread) {
1403 			auto ex = updateThread.join(false);
1404 			if(ex) {
1405 				import arsd.core;
1406 				writelnStderr("From Update Thread: ", ex.toString());
1407 			}
1408 			updateThread = null;
1409 		}
1410 	};
1411 
1412 	Thread.getThis.priority = Thread.PRIORITY_MAX;
1413 
1414 	window.eventLoop(0,
1415 		delegate (KeyEvent ke) {
1416 			game.keyboardState[ke.hardwareCode] = ke.pressed;
1417 
1418 			with(VirtualController.Button)
1419 			switch(ke.key) {
1420 				case Key.Escape: window.close(); break;
1421 
1422 				case Key.Up, Key.W: game.snes[Up] = ke.pressed; break;
1423 				case Key.Down, Key.S: game.snes[Down] = ke.pressed; break;
1424 				case Key.Left, Key.A: game.snes[Left] = ke.pressed; break;
1425 				case Key.Right, Key.D: game.snes[Right] = ke.pressed; break;
1426 				case Key.Q, Key.U: game.snes[L] = ke.pressed; break;
1427 				case Key.E, Key.P: game.snes[R] = ke.pressed; break;
1428 				case Key.Z, Key.K: game.snes[B] = ke.pressed; break;
1429 				case Key.Space, Key.Enter, Key.X, Key.L: game.snes[A] = ke.pressed; break;
1430 				case Key.C, Key.I: game.snes[Y] = ke.pressed; break;
1431 				case Key.V, Key.O: game.snes[X] = ke.pressed; break;
1432 				case Key.G: game.snes[Select] = ke.pressed; break;
1433 				case Key.H: game.snes[Start] = ke.pressed; break;
1434 				case Key.Shift, Key.Shift_r: game.snes[B] = ke.pressed; break;
1435 				default:
1436 			}
1437 		}
1438 	);
1439 
1440 	return 0;
1441 }
1442 
1443 // explicitly undocumented probably will not stick around
1444 __gshared GameHelperBase g_game;
1445 
1446 /++
1447 	Simple class for putting a TrueColorImage in as an OpenGL texture.
1448 +/
1449 // Doesn't do mipmapping btw.
1450 final class OpenGlTexture {
1451 	private uint _tex;
1452 	private int _width;
1453 	private int _height;
1454 	private float _texCoordWidth;
1455 	private float _texCoordHeight;
1456 
1457 	/// Calls glBindTexture
1458 	void bind() {
1459 		doLazyLoad();
1460 		glBindTexture(GL_TEXTURE_2D, _tex);
1461 	}
1462 
1463 	/// For easy 2d drawing of it
1464 	void draw(Point where, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
1465 		draw(where.x, where.y, width, height, rotation, bg);
1466 	}
1467 
1468 	///
1469 	void draw(float x, float y, int width = 0, int height = 0, float rotation = 0.0, Color bg = Color.white) {
1470 		doLazyLoad();
1471 		glPushMatrix();
1472 		glTranslatef(x, y, 0);
1473 
1474 		if(width == 0)
1475 			width = this.originalImageWidth;
1476 		if(height == 0)
1477 			height = this.originalImageHeight;
1478 
1479 		glTranslatef(cast(float) width / 2, cast(float) height / 2, 0);
1480 		glRotatef(rotation, 0, 0, 1);
1481 		glTranslatef(cast(float) -width / 2, cast(float) -height / 2, 0);
1482 
1483 		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);
1484 		glBindTexture(GL_TEXTURE_2D, _tex);
1485 
1486 		glBegin(GL_QUADS);
1487 			glTexCoord2f(0, 0); 				glVertex2i(0, 0);
1488 			glTexCoord2f(texCoordWidth, 0); 		glVertex2i(width, 0);
1489 			glTexCoord2f(texCoordWidth, texCoordHeight); 	glVertex2i(width, height);
1490 			glTexCoord2f(0, texCoordHeight); 		glVertex2i(0, height);
1491 		glEnd();
1492 
1493 		glBindTexture(GL_TEXTURE_2D, 0); // unbind the texture
1494 
1495 		glPopMatrix();
1496 	}
1497 
1498 	/// Use for glTexCoord2f
1499 	float texCoordWidth() { return _texCoordWidth; }
1500 	float texCoordHeight() { return _texCoordHeight; } /// ditto
1501 
1502 	/// Returns the texture ID
1503 	uint tex() { doLazyLoad(); return _tex; }
1504 
1505 	/// Returns the size of the image
1506 	int originalImageWidth() { return _width; }
1507 	int originalImageHeight() { return _height; } /// ditto
1508 
1509 	// explicitly undocumented, i might remove this
1510 	TrueColorImage from;
1511 
1512 	/// Make a texture from an image.
1513 	this(TrueColorImage from) {
1514 		bindFrom(from);
1515 	}
1516 
1517 	/// Generates from text. Requires ttf.d
1518 	/// 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)
1519 	this(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
1520 		bindFrom(font, size, text);
1521 	}
1522 
1523 	/// Creates an empty texture class for you to use with [bindFrom] later
1524 	/// Using it when not bound is undefined behavior.
1525 	this() {}
1526 
1527 	private TrueColorImage pendingImage;
1528 
1529 	private final void doLazyLoad() {
1530 		if(pendingImage !is null) {
1531 			auto tmp = pendingImage;
1532 			pendingImage = null;
1533 			bindFrom(tmp);
1534 		}
1535 	}
1536 
1537 	/++
1538 		After you delete it with dispose, you may rebind it to something else with this.
1539 
1540 		If the current thread doesn't own an opengl context, it will save the image to try to lazy load it later.
1541 	+/
1542 	void bindFrom(TrueColorImage from) {
1543 		assert(from !is null);
1544 		assert(from.width > 0 && from.height > 0);
1545 
1546 		import core.stdc.stdlib;
1547 
1548 		_width = from.width;
1549 		_height = from.height;
1550 
1551 		this.from = from;
1552 
1553 		if(openGLCurrentContext() is null) {
1554 			pendingImage = from;
1555 			return;
1556 		}
1557 
1558 		auto _texWidth = _width;
1559 		auto _texHeight = _height;
1560 
1561 		const(ubyte)* data = from.imageData.bytes.ptr;
1562 		bool freeRequired = false;
1563 
1564 		// gotta round them to the nearest power of two which means padding the image
1565 		if((_texWidth & (_texWidth - 1)) || (_texHeight & (_texHeight - 1))) {
1566 			_texWidth = nextPowerOfTwo(_texWidth);
1567 			_texHeight = nextPowerOfTwo(_texHeight);
1568 
1569 			auto n = cast(ubyte*) malloc(_texWidth * _texHeight * 4);
1570 			if(n is null) assert(0);
1571 			scope(failure) free(n);
1572 
1573 			auto size = from.width * 4;
1574 			auto advance = _texWidth * 4;
1575 			int at = 0;
1576 			int at2 = 0;
1577 			foreach(y; 0 .. from.height) {
1578 				n[at .. at + size] = from.imageData.bytes[at2 .. at2+ size];
1579 				at += advance;
1580 				at2 += size;
1581 			}
1582 
1583 			data = n;
1584 			freeRequired = true;
1585 
1586 			// the rest of data will be initialized to zeros automatically which is fine.
1587 		}
1588 
1589 		glGenTextures(1, &_tex);
1590 		glBindTexture(GL_TEXTURE_2D, tex);
1591 
1592 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1593 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1594 
1595 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1596 		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1597 
1598 		glTexImage2D(
1599 			GL_TEXTURE_2D,
1600 			0,
1601 			GL_RGBA,
1602 			_texWidth, // needs to be power of 2
1603 			_texHeight,
1604 			0,
1605 			GL_RGBA,
1606 			GL_UNSIGNED_BYTE,
1607 			data);
1608 
1609 		assert(!glGetError());
1610 
1611 		_texCoordWidth = cast(float) _width / _texWidth;
1612 		_texCoordHeight = cast(float) _height / _texHeight;
1613 
1614 		if(freeRequired)
1615 			free(cast(void*) data);
1616 		glBindTexture(GL_TEXTURE_2D, 0);
1617 	}
1618 
1619 	/// ditto
1620 	void bindFrom(T, FONT)(FONT* font, int size, in T[] text) if(is(T == char)) {
1621 		assert(font !is null);
1622 		int width, height;
1623 		auto data = font.renderString(text, size, width, height);
1624 		auto image = new TrueColorImage(width, height);
1625 		int pos = 0;
1626 		foreach(y; 0 .. height)
1627 		foreach(x; 0 .. width) {
1628 			image.imageData.bytes[pos++] = 255;
1629 			image.imageData.bytes[pos++] = 255;
1630 			image.imageData.bytes[pos++] = 255;
1631 			image.imageData.bytes[pos++] = data[0];
1632 			data = data[1 .. $];
1633 		}
1634 		assert(data.length == 0);
1635 
1636 		bindFrom(image);
1637 	}
1638 
1639 	/// Deletes the texture. Using it after calling this is undefined behavior
1640 	void dispose() {
1641 		glDeleteTextures(1, &_tex);
1642 		_tex = 0;
1643 	}
1644 
1645 	~this() {
1646 		if(_tex > 0)
1647 			dispose();
1648 	}
1649 }
1650 
1651 /+
1652 	FIXME: i want to do stbtt_GetBakedQuad for ASCII and use that
1653 	for simple cases especially numbers. for other stuff you can
1654 	create the texture for the text above.
1655 +/
1656 
1657 ///
1658 void clearOpenGlScreen(SimpleWindow window) {
1659 	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_ACCUM_BUFFER_BIT);
1660 }
1661 
1662 
1663 /++
1664 	NOT fully compatible with simpledisplay's screenpainter, but emulates some of its api.
1665 
1666 	I want it to be runtime swappable between the fancy opengl and a backup one for my remote X purposes.
1667 +/
1668 class ScreenPainterImpl : BasicDrawing {
1669 	Color outlineColor;
1670 	Color fillColor;
1671 
1672 	import arsd.ttf;
1673 
1674 	SimpleWindow window;
1675 	OpenGlLimitedFontBase!() font;
1676 
1677 	this(SimpleWindow window, OpenGlLimitedFontBase!() font) {
1678 		this.window = window;
1679 		this.font = font;
1680 	}
1681 
1682 	void clear(Color c) {
1683 		fillRectangle(Rectangle(Point(0, 0), Size(window.width, window.height)), c);
1684 	}
1685 
1686 	void drawRectangle(Rectangle r) {
1687 		fillRectangle(r, fillColor);
1688 		Point[4] vertexes = [
1689 			r.upperLeft,
1690 			r.upperRight,
1691 			r.lowerRight,
1692 			r.lowerLeft
1693 		];
1694 		outlinePolygon(vertexes[], outlineColor);
1695 	}
1696 	void drawRectangle(Point ul, Size sz) {
1697 		drawRectangle(Rectangle(ul, sz));
1698 	}
1699 	void drawText(Point upperLeft, scope const char[] text) {
1700 		drawText(Rectangle(upperLeft, Size(4096, 4096)), text, outlineColor);
1701 	}
1702 
1703 
1704 	void fillRectangle(Rectangle r, Color c) {
1705 		glBegin(GL_QUADS);
1706 		glColor4f(c.r / 255.0, c.g / 255.0, c.b / 255.0, c.a / 255.0);
1707 
1708 		with(r) {
1709 			glVertex2i(upperLeft.x, upperLeft.y);
1710 			glVertex2i(upperRight.x, upperRight.y);
1711 			glVertex2i(lowerRight.x, lowerRight.y);
1712 			glVertex2i(lowerLeft.x, lowerLeft.y);
1713 		}
1714 
1715 		glEnd();
1716 	}
1717 	void outlinePolygon(Point[] vertexes, Color c) {
1718 		glBegin(GL_LINE_LOOP);
1719 		glColor4f(c.r / 255.0, c.g / 255.0, c.b / 255.0, c.a / 255.0);
1720 
1721 		foreach(vertex; vertexes) {
1722 			glVertex2i(vertex.x, vertex.y);
1723 		}
1724 
1725 		glEnd();
1726 	}
1727 	void drawText(Rectangle boundingBox, scope const char[] text, Color color) {
1728 		font.drawString(boundingBox.upperLeft.tupleof, text, color);
1729 	}
1730 
1731 	protected int refcount;
1732 
1733 	void flush() {
1734 
1735 	}
1736 }
1737 
1738 struct ScreenPainter {
1739 	ScreenPainterImpl impl;
1740 
1741 	this(ScreenPainterImpl impl) {
1742 		this.impl = impl;
1743 		impl.refcount++;
1744 	}
1745 
1746 	this(this) {
1747 		if(impl)
1748 			impl.refcount++;
1749 	}
1750 
1751 	~this() {
1752 		if(impl)
1753 			if(--impl.refcount == 0)
1754 				impl.flush();
1755 	}
1756 
1757 	alias impl this;
1758 }