Archive

Archive for October, 2012

Tetris Aside: Coding for T-Spins

October 13, 2012 3 comments

(the source code and EXE file for this update of SimpleTetris can be found at the end of the article)

One of my Tetris-playing colleagues pointed out to me recently that T-spins do not work in my implementation of SimpleTetris. Although I hadn’t checked it explicitly, I was quite surprised by this as I knew the code I had written should support them just fine.

A nice double t-spin opener.

The orange-coloured T-piece here will fall 2 more rows, at which point it can be rotated such that it will complete the bottom 2 rows. This ‘impossible’ rotation of T-pieces where in real life the piece would be locked into place is the premise of the T-spin.

For those not in the know, the Tetris Wikia T-spin page has some excellent explanations and diagrams of what T-spins are; namely, when you rotate a T-shaped Tetris piece in a way that would be impossible in real life (due to the corners being obstructed by other blocks) such that it fits snugly into a T or notch-shaped hole (see image).

Read more…

Advertisement

Tetris Revisited: Adding 2D Animations and Other Graphical Tweaks

October 12, 2012 1 comment

It’s game coding time with SimpleTetris again and this time we’re going to look at how to add a variety of different animations and useful graphical tweaks which can equally be applied to any game or demo.

We will create a simple animation class in C++ which takes care of tracking the position and timing of basic animations, and learn how to apply this to make changes in an object’s position, colour and alpha (transparency). We will examine how to use this class to animate several objects at different points in the path of a single animation, how to affect two paremeters of an object (eg. position and transparency) with a single animation function, and how to partly randomize the intermediate steps (interpolation) of an animation.

Although we are again using Simple2D here (and as such this article serves a tutorial on how to use it, and how SimpleTetris is evolving), the algorithms and principles can be mostly copy/pasted and are easily adaptable to other graphics libraries. The animation class works independently of Simple2D and does not depend on it.

Download: Source code (.cpp) | Source code (.h) | Executable

Read more…

Cutting Your Teeth on FMOD Part 1: Build environment, initialization and playing sounds

October 5, 2012 59 comments

FMOD by Firelight Technologies Pty Ltd. is a cross-platform audio API used in – among other things – many commercial games such as Crysis, Diablo 3, Guild Wars, Guitar Hero, League of Legends, Second Life, StarCraft II, Tomb Raider, World of Warcraft and many others. It is in many ways an indsutry-wide gold standard in audio engines, and as such, knowing how to use it will come in very handy if you are coding games professionally. In the first part of this series, we’ll look at how to get C++ applications using FMOD to compile, how to set up the sound system at the start of your application, and how to play MP3s (although you can use any supported format you wish).

Note: The SimpleFMOD library contains all of the source code and pre-compiled executables for the examples in this series.

Read more…

C++11: Using std::unique_ptr as a class member: initialization, move semantics and custom deleters

October 4, 2012 13 comments

Updated 23rd February 2013: Fixed typos in the code, incorrect use of const, clarified the use of the terms “initialization” and “assignment” to be semantically correct, corrected/clarified references to “constructor” and “assignment operator”, added an explanation of the default copy assignment operator and unique_ptr’s private copy constructor and copy assignment operator.

Updated 12th May 2013: Fixed a factual error regarding standard functions not working as custom deleters in all scenarios, fixed a factual error stating that std::shared_ptr doesn’t support custom deleters (it does), and corrected references to boost which should have referred to std. Expanded the custom deleter section with example cases of new’ing your own resource classes or fetching resources from an external API or factory function. Added text explaining the advantages of using the function object idiom for custom deleters.

The std::unique_ptr class is C++11’s replacement for the flawed std::auto_ptr smart pointer from C++03. std::unique_ptr provides single-ownership pointer semantics and is therefore especially useful when dealing with resource handles which should belong to a single object, and its custom deleter feature (which allows the call to delete to be replaced with an arbitrary function call when the object is destroyed, such as CloseHandle or IUnknown::Release) is great for ensuring resources are closed properly before their handles are freed.

I’m not going to explain how std::unique_ptr or move semantics work here – see the References section at the bottom for some introductory links – rather, I’m going to specifically look at how std::unique_ptr should be used in a non-copyable class (that is, one with a private, or – in C++ 11 – deleted, copy constructor), and specifically, how to make sure the resource is freed exactly and only once.

Read more…

%d bloggers like this: