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