LDC beta, DConf blog link, Adam introduces gamehelpers.d
Posted 2019-03-25
Blog
Articles- terminal.d gets clipboard functions, ldc 1.20 out.
- DConf keynote speaker announced: Lua architect Roberto Ierusalimschy, Named args DIP discussed
- February 3, 2020
- Adam's terminal suite explained
- Understanding mixin templates, terminal.d improvements
- My attribute-by-default proposal. Also dmd 2.090 came out.
- DConf 2020 announced: June 17-20 in London. @safe by default debated. Adam did: Android, JNI, WebSocket in arsd libs
- tar.xz, --DRT tip, dom bug fixes, more Android and JNI, link to old phobos docs
- LDC 1.19 - Android, AVR. My rant on tests, update on JNI and COM.
- Walter's string interpolation proposal is OK but not great. My Android thing nearing beta release. dub downtime explained.
- Android project update, introduction to arsd.jni
- New pattern about interface contracts
- Adam shares Windows console secrets - DO NOT USE chcp!!
- Adam's rant on benchmarks
- Socket tutorial
- November 4, 2019
- October 28, 2019
- arsd package updates, forum nonsense
- Update on Android
- Adam does iOS "goodbye world"
- September 30, 2019
- D turns 20, Adam rants on software freedom
- Named arg DIPs and my thoughts on code organization
- September 9, 2019
- I wrote about mixin templates vs string mixins on Stack Overflow
- August 26, 2019
- Bug bounty in D again - my hot take, on reusing code, a fun picture, my tentative plan for the next month
- Time invested is worth a lot
- cgi.d's new scheduler, static this tricks
- July 29, 2019
- July 22, 2019
- Solving vs managing problems
- A big week in the arsd repo
- July 1, 2019
- June 24, 2019
- June 17, 2019
- CRTP thoughts, named arguments DIP review, DConf videos now on youtube
- musings on hybrid CT/RT tests, some more progress on new web framework
- a little more webassembly
- May 20, 2019
- Adam's string interpolation proposal
- DMD 2.086 live, GCC 9 with D support formally released, DConf coming soon, links to posts on builder pattern and disallowing implicit conversions with templates, and 2d array op overloads
- template constraint error improvements coming?
- dmd 2.086 beta, dstep 1.0 released, Adam works on memory usage
- obj-c and webassembly report, tips on is expressions linked.
- new ldc, new dmd, dpp on the blog
- D's future discussed in forums
- LDC beta, DConf blog link, Adam introduces gamehelpers.d
- March 18, 2019
- LDC 1.15.0-beta1, responsive design rant
- dmd 2.085.0 released
- Obj-C interop and D without druntime code to copy/paste
- dmd beta, more info coming next time, demo of new web framework initial prototype
- automatic web interface discussion, reflection tips and tricks
- Adam busy with weather and a move, lots of community announcements
- January 28, 2019
- Working on official blog 2018 retro, C++ new wrapped, dmd reading zips?
- dmd obj-c growing, Adam static foreaches an interface to RPC
- dmd 2.084, hope for future, but busy non-D week for me
- IDE tools released, my cgi.d gets new features
- DConf announced, tip, Adam rants: mouse trap
- This Week in D is back!
I forgot to post last week, so I slapped that post up without commentary, and now for this week, I write a little about a module that has been sitting in my repo for a long time, but hasn't been very useful. Now, it is minimally useful.
Core D Development Statistics
In the community
Community announcements
See more at the announce forum.
What Adam is working on
I had some time to kill offline this week, and I decided to spend it hacking on my gamehelpers.d file. This is one of my framework modules - one that takes pieces from several other (usually unopinionated) independent modules and makes an opinionated whole out of them - which is aimed toward making simple games, in the style of older consoles. This means 2d graphics output, sound output, and gamepad input (and keyboard+mouse is available too, just not the focus here).
However, until this last weekend, I never actually did very much work on this. It imported a few modules and offered a few simple helper functions, but nothing else. That has changed. Let's look at the pieces together.
Overall model
gamehelpers imports arsd.simpledisplay, arsd.simpleaudio, and arsd.joystick to act as the foundation of the framework, and provides an abstract class for you to inherit from to get started. Instead of writing event handlers, you write two primary functions: drawFrame and update, and a few initialization/configuration functions. Your main function calls runGame, passing the instance of your class and the max frame rate you want to shoot for. (BTW I'm of the opinion that the best games ever made were on hardware much less than 1/100 the power of modern computers, so there is no reason for my games to need even 1% of my CPU or memory. I personally like to use 20 fps, and my libraries are liable to cap you out at 60, even if you ask for more.)
The framework passes a Duration argument to update, telling you how much time has elapsed since the last call. You may use this to maintain constant real-time game speed, even if the function calls aren't as frequent as you like. Or, you can simply ignore it and assume your frame rate is close enough. update returns a bool value - true if anything actually changed, false if it didn't. It uses this to optimize redrawing during dead periods, like if your game is paused. You are free to just always return true if you don't want to think about it.
update should, well, update the game's state, but it is also responsible for checking the state of the user's controller and acting accordingly.
After calling update, the framework will (eventually, it might limit the framerate more than it limits calls to update) call drawFrame. drawFrame is responsible for drawing the current state of the game and shouldn't change anything.
Meanwhile, in the background, an audio output thread is created, if you request it by implementing override bool wantAudio() { return true; } in your class. You can send messages to it via the audio object.
Input
gamehelpers builds off arsd.joystick and arsd.simpledisplay. joystick provides a way to check state of gamepads and joysticks (mostly focused on playstation and xbox 360 controllers), and simpledisplay, of course, gives keyboard and mouse input events. gamehelpers saves the events in state tables, with one abstraction in particular: a SNES controller.
I usually use an old PSone controller on my Linux box, and an XBox One controller on Windows. I also have some XBox 360 controllers I sometimes use for player two. As such, my joystick.d library is heavily biased towards these pieces of hardware. It, via a compile-time version, will convert input from one of these devices to the other, so you just pick the api you like. It defaults to PS1, since it is comparably less functional; when in doubt, I like to default to the lowest common denominator.
Which brings me to the SNES choice for gamehelpers' abstraction: it is a couple steps backwards in console game controller evolution to the point where it was easy for me to fully support from a keyboard.
So, the virtual controller in gamehelpers can be used from any of these devices - keyboard or game controller - with no loss. If a game needs analog input, or other keys on the keyboard, it can just access the underlying objects directly, bypassing the snes layer.
Graphics
gamehelpers includes a function create2dWindow which initializes a SimpleWindow with an old-style OpenGL context ready for simple 2d drawing.
It also has a OpenGlTexture class which helps make a drawable texture out of an image. When combined with arsd.png, this makes loading images easy. I am working on .apng file support, coming soon.
Sound
arsd.simpleaudio is pretty bare-bones right now, but it does include an ogg player and some functions to make simple sounds, like beeps, noise, and boops (the function names reflect this literally). It builds on the Windows multimedia API and Linux' ALSA. It also has MIDI support, but that's not usable outside special circumstances right now.
You simply call, for example audio.beep(); and it beeps, mixing in up to 16 simultaneous sounds automatically in a background thread.
So far, it is Atari 2600 style sounds :) But I will add more later.
Example
That just makes a white box bouncing around the window, that you can influence with the controller. Pressing buttons will make some sound - beware of volume, it can be loud!
Future directions
Well, I have a lot to do to make this fun, but my goal is pretty simply to make 8 or 16 bit style games. I am writing animated png file support (and I have an associated homemade editor using arsd.minigui, I will talk about that eventually) and probably some state transition helpers.
But after this weekend's slapping together, gamehelpers is basically usable now. I just need to find the time to finish all my crazy codes!