My brain makes things explode.
Posts tagged 2D
C++/D3D Pong Tutorial 04: Adding Keyboard Input
Apr 25th
Tutorial Description
In the last tutorial, we established a rendering engine and got scene objects working, with basic physics. In this tutorial, we are going to implement the initial Input manager, which will be focusing on the keyboard. I was planning to include mouse input, but it turns out that DirectInput only handles the mouse in relative positioning – which is perfect for three-dimensional first-person shooter games that need to track the movement of the mouse – but it’s poor for two-dimensional casual games. For those of you that do wish to use mouse input, I will be covering it a later tutorial – the methods of getting absolute positioning will require a bit of modification to our engine.
Downloads
Please note that I’ve switched to using 7-zip in place of WinRAR for compression
Binary Only (20.4KB)
Full VS2010 Project Dir (26.8KB)
SVN Repository
As of this tutorial, I have created and added a Subversion repository on Assembla to store the code for the tutorial series. I will continue to upload the source for each tutorial – the SVN will merely contain the code as I write it, so you can feel free to play around with the code that I’m working on before I release a tutorial. Mostly, this is to help people play around with things if there’s a point where I’m slow to create the tutorial following the code (as was the case with this tutorial).
SVN URL: http://svn2.assembla.com/svn/d3dpong
Starting Words
Notice: This tutorial continues from where “C++/D3D Pong Tutorial 03” article left off. Please read it if you have not already.
C++/D3D Pong Maintenance: The Game Loop/Frame Delta
Mar 25th
Tutorial Description
While running the game/engine that we’ve been developing throughout this tutorial series, you may have noticed that the engine kind of runs… like crap. So, before moving onto the next tutorial in the series (the code for which is completed), which will cover keyboard and mouse input, I feel that it’s somewhat important to fix this. It’s just going to be some small updates to CGameEngine.cpp, so it won’t be hard.
Code Listing 1: CGameEngine.cpp -> CGameEngine::CGameEengine(void)
First, we’re going to correct an error with our game timer, which we never actually created an instance of in code – causing the GetRunTime feature to fail due to a member variable not being initialized (CGameTimer::_start). Replace the constructor for CGameEngine with the following, in CGameEngine.cpp
CGameEngine::CGameEngine(void)
: _windowHandle(NULL), _windowTitle(L"D3DPong"),
_screenWidth(640), _screenHeight(480), _colorDepth(32),
_gameRunning(true), _gameTimer(new CGameTimer())
{
}
Code Listing 2: CGameEngine.cpp -> CGameEngine::Update(void)
Our new Update method is much smaller than it was before. We’re still using fast-as-possible rendering and updating, so this isn’t going to fix the minor graphics tearing that you’ve no doubt noticed before this, but it is going to make things run a little better.
void CGameEngine::Update(void)
{
float delta = this->_gameTimer->GetRunTime();
GameUpdate(delta);
this->_gameTimer->Reset();
this->BeginRender();
GameRender(delta);
this->EndRender();
}
Nicer, cleaner, shorter. This was caused by a bit of an inconsistency and laziness on my part, but things should run a bit smoother now. In the next tutorial, we’re going to be adding input, and will put the beginning pieces of our game into effect. I’ll cover enabling VSync at a later point, because I’m too lazy to deal with it right now. It’ll probably be in another “Maintenance” mini-tutorial.
Expect Tutorial 4 soon!
C++/D3D Pong Tutorial 03: Scene Objects and Sprite Rendering
Mar 21st
Tutorial Description
In the last tutorial, we finished getting Direct3D set up, implemented a timer system to allow us to calculate the time between each frame, and set up the framework that will grant us access to our game without interfering with the core engine. In this tutorial, we are going to implement a reusable Scene Object class, which will be the parent for all future classes that we wish to enable in a scene manager. The scope of this tutorial series makes a full-fledged instance management system overkill, so we will not be building one in here. However, as this tutorial series will act as the base for future tutorials that will implement a scene manager, I am going to implement the scene object class here.
Downloads
Binary Only (18.8KB)
Full VS2010 Project Dir (28.4KB)
Starting Words
I just wanted to point out that this tutorial series borrows a lot of the structure of Jonathan Harbour’s Advanced2D game engine, from the book “Advanced 2D Game Development“. While I certainly believe it to be a wonderful book, and an excellent starting point for beginners (in fact, the reason for me borrowing the format for his engine is because I find the method well-suited for beginners), I find that I have a lot of personal quirks with Jonathan’s methods and programming habits, and there are a number of inconsistencies in it. Furthermore, there are some topics that are covered in the book that just aren’t well-suited for beginners, and can cause things to be a bit confusing and convoluted. In this tutorial series, I don’t plan to implement three-dimensional rendering, which he does cover briefly, and have no intention of including game scripting, though several more advanced tutorials will cover these aspects at a later date (read: when I stop being lazy). Anybody looking at learning to program two-dimensional games with DirectX and C++ should look into the book, but should remember that there will always be better ways to implement certain systems than what is shown to you in a book – or an online tutorial. Anybody that wants to gain a solid understanding and truly grasp the depth of these programming systems is highly encouraged to look up information on any subjects that they are unfamiliar with. I strongly recommend Safari Books Online – I read a lot of books there, and it’s certainly helped me improve myself. Advanced 2D Game Development is available on SBO, as well.
Notice: This tutorial continues from where “C++/D3D Pong Tutorial 02” article left off. Please read it if you have not already.
Torque Game Builder/Box2D Integration
Feb 20th
I’ve been looking at Box2D a lot recently, mostly because Torque 2D (Torque Game Builder’s replacement) is going to be using it. It’s a damn good, and powerful, two-dimensional physics engine -- it’s what powered Crayon Physics, and several similar physics-oriented games. Box2D is a pretty beautiful thing, from what I’ve looked at, and it’s been ported to a lot of systems. Recently, I found a Torque Game Builder integration for it, and figured I’d look into it. Currently, it’s integrated into the Torque Game Builder editor, and still allows for the use of the standard TGB physics (you cannot have both Box2D and TGB physics on the same object, however). The system comes in the form of a class that needs to be set on the Scene itself, and behaviors that you add to objects. The system uses the object’s convex collision polygon, as well.
Video
Screenshots of the Pyramid Test
Stacked Pyramid
Tumbling Pyramid
TGB/Box2D Integration Links
Torque Game Builder 1.7.4
Box2D implementation
C++/D3D Pong Tutorial 02: Setting up Direct3D and our Engine Framework
Feb 15th
Tutorial Description
In the last tutorial, we set up the basics required for the Windows API to open our window. While basic, the task was also essential, though many people who are familiar with Win32 Programming will likely have gained nothing from it. Here, we are going to do several tasks. In no particular order, they are as follows:
1) Set up Direct3D rendering
2) Set up the framework for our game engine
3) Implementing game timing
Downloads
Binary Only (10.4KB)
Full VS2010 Project Dir (13.1MB)
Starting Words
Notice: This tutorial continues from where “C++/D3D Pong Tutorial 01” article left off. Please read it if you have not already.
