TetroGL – OpenGL/C++ tutorial

I’m currently planning on a career in games programming and was looking for some beginners’ tutorials in OpenGL using C++ and found a goodun over at code project.  It’s a 3 part tutorial, and as I’m writing this the first two parts have been posted, complete with code and Visual Studio project files.  As a relative beginner with C++ and a complete beginner with OpenGL I’ve found this fairly easy to follow.  The code is pretty well commented, and the tutorial itself explains things clearly and doesn’t take too much for granted aside from a basic knowledge of programming/C++.

A trap for young players, there was a slight error in the code from the first part that caused the program to not shut down and stay in memory after the window had closed.  Nobody else seemed to have this problem, and it could have just been me being difficult and using Vista.  Nonetheless, if the code for part 1 hasn’t been updated, the fix is in the comments below part 1.  Basically the main application loop did not clear the message queue with each iteration, only taking the first message.  From what I’ve read on the issue this means that the WM_QUIT message sent from PostQuitMessage() is never received.  This is because PostQuitMessage(0) sets a flag on the message queue which tells it to return a WM_QUIT message if the queue is empty.  Obviously if the queue is never cleared, this message is never returned when the queue is queried for the next message.

If you have any problems with this, feel free to contact me.  Happy coding!

When a Leap Year Isn’t a Leap Year

Have you ever had one of those moments when you realise that something you held as a fundamental law of the universe doesn’t work the way you thought it did?  Well I have, today in fact, while writing a batch script that needed to be aware of leap years.  Multiple choice question (I promise it’s relevant), which of the following is a leap year: a) 1700 b)1900 c)both ‘a’ and ‘b’ d)none of the above.  Correct answer: d).  That’s right, it turns out that being evenly divisible by 4 isn’t enough to qualify as a leap year.

So, now that I’ve dropped that bombshell, who wants to know how to pick a leap year every time without embarassing yourself?  Those of you with your hands raised can lower them and simply keep reading, because I’ll fill you in now.  In plain English, a leap year occurs when the year is evenly divisible by 400, OR if the year is evenly divisible by 4 but NOT 100.  Put another way, a leap year occurs every four years as you’d expect, except for the beginning of a new century, where a leap year only occurs if that year is evenly divisible by 400 (e.g. 1600, 2000 etc.).  A nice way to show this process is with the aid of a spiffy flow chart referenced in the about.com article I found today.

For those of you who might need to code this, the wiki article on leap years has pseudocode and C-style code examples of the algorithm.  Furthermore, for those of you who might need to check leap years in a batch script, here’s a simple batch script to do so.  In its current form, the script is called with the year you’re checking as the first and only argument, printing an answer in plain english before finishing.

@echo off

SET _year=%1
SET _bLeapYear=is not

::Check if the year is evenly divisible by 400
SET /a _modYear=%_year% / 400
SET /a _modYear=%_modYear% * 400
If %_modYear%==%_year% SET _bLeapYear=is
If %_modYear%==%_year% GOTO END

::Check if the year is evenly divisible by 4
SET /a _modYear=%_year% / 4
SET /a _modYear=%_modYear% * 4
If NOT %_modYear%==%_year% GOTO END

::Check if the year is evenly divisible by 100
SET /a _modYear=%_year% / 100
SET /a _modYear=%_modYear% * 100
If NOT %_modYear%==%_year% SET _bLeapYear=is

:END
echo %_year% %_bLeapYear% a leap year

As you might have noticed, I’m substituting the modulus operation found in most algorithms (including the ones on the wiki article) with a division followed by a multiplication.  This is simply because batch scripts don’t have a modulus operator.  For the mathematically curious, this works because we’re using integer arithmetic.  For example, in a batch script, 2003 ÷ 4 = 500.  As you can see, there is no remainder in the result.  Furthermore, there is no rounding.  This means that a ÷ b * b will only equal a when a is evenly divisible by b, or, a mod b = 0.

And there you have it, more than you probably wanted to know about leap years.