Smooth keyboard input with GLUT

August 20, 2010 at 4:09 pm (Gaming, Programming/Development)

Recently, I’ve been learning OpenGL in order to ramp up for the still-in-progress Android game mentioned in previous posts.

In my Jedi training I came across a rather annoying behavior of GLUT – slow response to keyboard input. Specifically, response to keys that are held down. The repeat time is like that of a standard Windows app, i.e. in Notepad, press a letter key an hold it, the letter is displayed, there is a short delay, then the letter begins to repeat.
Now, this is all fine and dandy for most every application because immediate responsiveness is usually not necessary. Although, in games, this is awful and just plain unacceptable.

When doing some research, I tended to find three solutions to the problem:
1. Switch to normal Win32 development and use that message loop to handle input
Ick… Nein! I simply want to learn OpenGL, not Win32 programming.
2. That’s just a limitation of GLUT, live with it until you are ready to switch to #1.
Again, I say: nein! 容認できない!
3. Use DirectInput, it’s much better for handling Input on Windows
Ok, granted. BUT, again, I want to learn OpenGL here… for an Android app, I’ll come back to DirectX when the project necessitates it.

Fortunately, there’s actually a very simple solution that allows those learning OpenGL to continue to use GLUT for its inherent benefits all while receiving silky-smooth keyboard input with the added bonus of handling multiple-key input:

Use a flag variable on key-down to signal a press, swap the flag on key-up, update based upon that flag

Here’s an example:

In accessible scope

GLboolean upPressed = false;

In our glutSpecialFunc()

void SpecialKeys(int key, int x, int y)
   // ...
   // Stuff
   // ...
   if (GLUT_KEY_UP == key)
      upPressed = true;

   // More stuff ...

In our glutSpecialUpFunc()

void SpecialKeysUp(int key, int x, int y)
   // ...
   // Stuff
   // ...
   if (GLUT_KEY_UP == key)
      upPressed = false;

   // More stuff ...

In our glutIdleFunc()

void Update()
   // ...
   // Stuff
   // ...
   if (upPressed)
      // Movement kung-fu!

   // More stuff ...

That’s it! Not too bad, right? Not only is the input smoothed out, but now you can handle multiple, simultaneous key presses! This works rather nicely and I’m sure it can be applied to any key (not just “special” keys) or mouse button.

Take that Win32!


Permalink 2 Comments

One more time with Tetris, then I’ll drop it… I promise!

May 18, 2010 at 2:20 pm (Gaming, Programming/Development, Projects)

I can’t seem to stop adding features to my Tetris clone that I initially said I was going to leave out! It’s difficult to look at something you produce knowing that it is “missing” features that you know you could just take a few minutes to add. *sigh* The burdens of game development 😉

I went ahead and added functionality to display the “next piece”. It was just too important to the Tetris gameplay experience that I couldn’t leave it out and still feel ok about it.

Anyway, the game is still available here.

On a quick side-note:
The Android game project is still coming along. We are working on taking the existing game design doc and fleshing out a tech doc. After that step is complete we will begin coding the beast!
Exciting stuff!

Permalink Leave a Comment

Ok, I lied…

April 8, 2010 at 4:22 pm (Gaming, Programming/Development, Projects)

I did add a basic scoring system to my XNA Tetris clone and a system to make the game speed increase as time goes by. It was way too easy to play and play and play and play and never fail at the default speed, so the gamer in me couldn’t accept that.

With those additions, the game is essentially complete. There are other features I might add in the future – next piece preview, set piece “ghost”, quick-drop, speed increase based on score, etc.

Until then, enjoy the version I have and let me know if you have any questions about my code or find any bugs. (Hint: there are at least two minor bugs I know of, but we’ll keep that secret between us, right?)

Click here to download the full source and built executable.

Permalink 1 Comment

XNA Tetris clone & Mobile development update

April 7, 2010 at 1:52 pm (Gaming, Programming/Development, Projects)

First thing’s first:
I’m about finished with my XNA Tetris clone. The only feature left to implement is the game-over state.
I don’t plan on making this a fully featured Tetris knock-off with special effects, scoring and multiple game modes. The objective with this project was really a proof-of-concept for me; verification that I could develop the core gameplay mechanics and logic… the rest is fluff 🙂
Once I get that last part completed, I’ll upload the game and source for anyone interested in developing a Tetris-style game. I tried to keep it pretty thoroughly commented and clear.
Here’s a preview of the game screen (note my incredible artistic talents):
XNA Tetris

In other news:
The iPhone game that I was slated to work on is going to be completed without me. Well, technically, it’s going to be completed without my friend (mentioned in the previous post) or myself. It turns out the developer who is currently working on it is only about 1 month from code-complete and my friend and I don’t feel that it’d be in our best interest to try to cram and learn Objective-C for this project if it’d be finished a week after we are ready to go!
Instead, we are going to focus on Android as a development platform. The group that we are working with on this game plans on shifting to Android after this initial iPhone release anyway, so we are going to come in once they are ready for porting and future Android development.
There are added benefits to developing for Android as opposed to iPhone that also influenced our decision. The primary ones are that a Mac is not required for development and there are Android phones across multiple providers. Now, I know that these hurdles can be overcome with some h@x0ring and unlocked phones, respectively – but we already run Windows/Linux and we want to target the mass market, not just tech-savvy mobile device users.
Oh, and I already have an Android phone… so it’ll be easier to test our stuff on an actual device 🙂

Anyway, that’s all for today!
Next post will have the source for my XNA Tetris clone!

Permalink Leave a Comment

Tetris-clone update and a bit more

March 16, 2010 at 4:22 pm (Gaming, Programming/Development, Projects)

First of all, my Tetris-clone is still under development.
It’s coming along… slooooowly but surely. Free time to spend working on an personal game development project is hard to come by these days.

…and that is partly because I’ve become involved with a small, independent group of developers working on their first iPhone game. A college friend/developer and I were looking to hop into Android development, then he talked to another friend of his who was part of this iPhone game development effort. It turns out that they were looking for some more programming support, so he and I decided this would be a good place to start our mobile application development journey.
Yes, this is not an Android application, but plans to move to Android development – as well as iPhone development – are in place for the future.

After gathering some more information from this new group, I may or may not provide some details about the game. We haven’t discussed publicity yet, so I don’t want to reveal anything without consent.

Either way, it looks like I have a busy and exciting spring/summer ahead!

Until next time…

Permalink Leave a Comment


February 12, 2010 at 6:47 pm (Gaming, Programming/Development, Projects)

I’m starting a new “Projects” category for my blog. It’ll contain posts related to personal projects that I am working on.

First project to mention:

That’s right, I’ve decided to tackle a personal programming “unicorn” of mine, and attempt to put together a clone of the classic block-based puzzle game. When first getting into programming games, there were a couple times that I began throwing a Tetris clone together, but I could never quite finish it. I would get caught up with something like matrix rotation or handling the rows in the play grid.
I think I’ve finally gotten to a point where I can handle the problems I previously stumbled upon.
I’ll be using XNA for this project as it’s a solid framework with an easy to understand graphics system and many helpful libraries… plus, I haven’t gotten enough of a grip on DirectX yet! (That will be for version 2… ^_^ )

I’ll post the project on the “Projects/XNA” section of my site when it’s complete, and I’ll post status updates here when I can.

Stay tuned…

Permalink Leave a Comment

Oh, what’s in a name?

February 3, 2010 at 1:16 pm (Programming/Development)

As time goes by, I realize how many holes there were in my college education. Indeed, part of me is very proud of my alma mater and satisfied with the outcome of my undergraduate efforts – that is, I was able to use that degree and education to land a solid job at a great company. Unfortunately, another part of me is absolutely shocked that the school can label the degree, “Software Engineering”.

This brings to light the gross disservice that various colleges and universities are doing my using degree names interchangeably. Commonly, “Information Technology”, “Computer Programming”, “Computer Science”, “Software Engineering”, are all used to describe collegiate degrees involving the development of computer software. Sometimes a degree from one school called “Software Engineering” would be equivalent to another school’s “Computer Science” degree. Sometimes they are very different. Unfortunately, I don’t believe that the schools even know what to name the degrees.

For me, there is a big difference between computer programming and computer science. The former is more of a stereotypical “code-monkey” type of thing. A higher-level knowledge of programming, familiar with only the most common data structures, unconcerned with what is going on in memory and such. The latter is a lower-level study of programming and computer theory. Understanding how a language is built, how a compiler and linker work (and knowing that they exist in the first place!), how memory is managed, how and why certain data structures are implemented, etc.
There are certainly benefits to producing “programmers” and “computer scientists”, and I am not going to discredit anyone who would prefer learning one or the other discipline. But again, I think there is a difference and a very important one.

As I mentioned earlier, it can be a hindrance to students and employers alike when schools use curriculum names interchangeably. It is very hard to know what you are getting without detailed descriptions of the courses involved. Even then, it is hard as a new student with little or no knowledge of terms or theory. And employers are left wasting time and money sifting through various candidates for a position to find out who has the skills needed for their positions, despite having a vaguely named degree.

I feel slightly gypped after completing my degree. In hindsight, I believe there should have been more classes on lower level studies (memory management, data structures and algorithms in depth, compiler/linker design, etc). Again, I definitely learned a lot and that knowledge was invaluable as far as getting my job, BUT it could have been better.

Fortunately, all it not lost to those of us yearning for more understanding and seeking true mastery of particular topics. Our industry is one that is overflowing with great learning resources. From books, to web sites, to experienced coworkers – there is never a shortage of resources to learn from.
This is something that I embrace wholeheartedly. I am constantly taking in new and valuable information. Learning and growing from books and friends. I have determined not to limit myself to what was deemed sufficient by one school, but to constantly challenge myself with new material and projects.

For anyone else in my situation, here is a recommendation: set up for yourself a Personal Development Timeline and stick to it. Buy a few books, find some coursework, whatever will work for you, and write down what you will accomplish and by what time. I’ve done this myself and have found it incredibly useful. I have the next several months of my life planned out as far as personal development and education goes. It is encouraging to know that, just because I might have missed out on something, doesn’t mean that I have to accept it and move on.

As Mark Twain once said, “I have never let my schooling interfere with my education.”

I have the power and hunger to grow – and I will – and so does anyone else who wants to.

Permalink Leave a Comment

Continuing Engineering in a Nutshell

January 24, 2010 at 8:35 pm (Programming/Development)

If this doesn’t describe bug finding/fixing to a T, I don’t know what else can.

As we know,
There are known knowns.
There are things we know we know.
We also know
There are known unknowns.
That is to say
We know there are some things
We do not know.
But there are also unknown unknowns,
The ones we don’t know
We don’t know.

Former Secretary of Confusing Poetry – er…- Defense, Donald Rumsfeld
—Feb. 12, 2002, Department of Defense news briefing

Permalink Leave a Comment

Ref: Using C# For a Commercial Game

November 17, 2009 at 11:25 am (Gaming, Programming/Development)

Alistair Doulin has written a great article supporting something I’ve been behind for a while now – the viability of C# as a commercial game language.

Check it out here.

C# is becoming more and more powerful with each new version. The tools and libraries available to it are fantastic and would save tons of development time. And the fact that it includes – brace yourself, I’m about to go there! – garbage collection – gasp! – saves you from those pesky memory leaks that can plague C & C++.
I know that garbage collection is a sensitive subject with a lot of developers when performance is on the line, but like Alistair mentions, like any other language “bad code will run slow”.

Permalink Leave a Comment

Dirty Coding Tricks

October 9, 2009 at 12:05 pm (Gaming, Programming/Development)

I wanted to make a quick post referring to a great little article I just read on the superlative game design & development site, Gamasutra.
Really, if you haven’t visited Gamasutra before, you are missing out.

The article is called Dirty Coding Tricks. It has several first-hand accounts of the not-so-pretty things that desperate developers do when crunch-time comes knocking. We’ve all seen things like this before and, in a strange way, it makes you feel better that you’re not the only one who’s had to stand behind a product that has some nasty bits of code in it.

Be sure to check it out! It’s a great read…

Permalink Leave a Comment

Next page »