November 1-3 this year, there was an event called the Colorado Springs Startup Weekend. It’s not annual, because there were two this year, but this was the second one. I perhaps was not the kind of person they usually expect to have there, but I attended with plans to learn about starting a company and maybe show off what I could do, and I achieved everything I had initially set out to do. OK, mostly.
Most notably, I ended up working on a program that had some rudimentary GUI functionality, which meant I had to refine and learn some new portions of how the user interfaces with the computer. Particularly, a mouse, and panes. Hey wait, I can do mouse inputs now! That means no more of this:
It’s time for this!
First thing I need to do is update the existing engine with all the new tricks and treats I learned at the startup weekend. Until now, keyboard inputs have been handled inside the main program loop, which is a bad way to do things. I’ll be gathering up all the relevant parts into Keyboard, Joystick, and Mouse structures, which can then update themselves according to SDL events.
There are three SDL events that deal with the mouse: movement, button pressed, and button released. Buttons include the wheel, which may answer a few questions. Apparently if you have one of those fancy gamer mice that has a wheel you can set to spin freely, it just spams the computer with “Mouse up, mouse up, mouse up!”
Obviously, I want to include information about the mouse location. This is reported by SDL as an integer value, measured in pixels, so that’s how I’ll store it.
We also want three sets of booleans for each mouse button: Pressed, State, and Released. State is the state of the button: true if the button is down, false if it’s up. Pressed means that the button has changed from an up state to a down state, and Released is the opposite. To make resetting easier, I made a Union structure which combines a 5-value array with the 5 individual button values(left, middle, and right button, and wheel up and down).
It’s silly to create a Struct if you aren’t going to let it do anything on its own, so I’ll add a few utility functions. reset(), which clears all the button states and resets the mouse location to 0,0, and frameStart(), which only clears the Pressed and Released states, so that a new wave of events can be processed. And, of course, I add functions to process the incoming movement, pressed, and released events.
To get the mouse position, we ask for mouse.location
To get if, say, the user has pressed the left mouse button this frame, we ask for mouse.buttonPressed.lmb
The keyboard is a bit simpler than the mouse. There are way more keys, but I only care about the three states(Pressed, State, and Released), and SDL has a handy set of constant declarations, so I just move all the old logic from the program loop into the structure.
To ask if the user is holding down the Spacebar, we simply ask for keyboard.keyState[SDLK_SPACE]
Not doing a joystick just yet. Joysticks are a little trickier than mice and keyboards, because you’re expected to have 1 mouse and 1 keyboard, and even if you’ve got more connected, the signals all go to the same place. The number of joysticks attached to a computer may vary between zero and a hundred and fifty thousand, and they may or may not all be plugged in when you start the program up, so while a single Mouse or Keyboard object is all you need, Joysticks need to do several of their own things.
I will need joysticks, but I’ll save that for later.