All blog entries, ordered from most recent. Entry count: 1198.
# DirectXMath - A First Look
Sun
15
Apr 2012
Programmers using DirectX have access to the accompanying library full of routines that support various 3D math calculations. First it was D3DX - auxilliary library tightly coupled with DirectX itself. It's still there, but some time ago Microsoft released a new library called XNA Math. I've written about it here. This library also ships with latest versions of DirectX SDK (which was last updated in Jun 2010), but it is something completely new - a more independent, small, elegant and efficient library, portable between Windows (where it can use SSE2 or old FPU) and Xbox 360.
Now Microsoft comes up with another library called DirectXMath. See the official documentation and introductory blog post for details. As a successor of XNA Math, it looks very similar. Main differences are:
Sounds great? Well, I didn't tell yet how to get it. It's not shipped with DirectX SDK. You need the SDK for Windows 8. They say that:
The library is annotated using SAL2 (rather than the older VS-style or Windows-style annotations), so requires the standalone Windows SDK for Windows 8 Consumer Preview or some additional fixups to use with Visual C++ 2010.
So probably I'm not going to switch to this new library anytime soon :P
Comments | #directx #math Share
# unique_ptr in Visual C++ 2010
Sat
14
Apr 2012
Sure C++ doesn't have garbage collector, but the way of freeing memory and other resources recommended for this language is RAII idiom - creating classes (like smart pointers) that free pointed object in destructor. Standard library of old C++ provided only auto_ptr class for this, which had many flaws. Some programmers have been writing their own smart pointer classes or using these from Boost library - like scoped_ptr and shared_ptr.
The new C++11 standard (called C++0x before release) defines new smart pointers, similar to these from Boost. They are called unique_ptr and shared_ptr, they exist in std namespace and require #include <memory>. Microsoft Visual Studio 2010 / Visual C++ Express 2010 already implement parts of this new standard. Language features like r-value reference and move semantics make these smart pointers more powerful than before.
shared_ptr is for shared ownership and uses reference counting, so it's not needed very often in my opinion. More often we are dealing with a situation where there is one, clearly stated owner of a dynamically allocated object, like a local variable in some scope or a class member. So let's take a look at how unique_ptr can be used for this:
std::unique_ptr<MyClass> ptr1(new MyClass());
// ptr1 will automatically call destructor and free the object when going out of scope.
std::unique_ptr<MyClass> ptr2; // ptr2 is NULL
ptr2.reset(new MyClass(1)); // Object is created and passed to smart pointer.
ptr2->m_Number = 2; // Object can be dereferenced like with normal pointer.
ptr2.reset(new MyClass(3)); // New object is given to the pointer. First one is destroyed.
ptr2.reset(); // Second object is destroyed. ptr2 is now NULL.
unique_ptr can be used for arrays:
std::unique_ptr<MyClass[]> arrPtr(new MyClass[6]); // Smart pointer to array.
arrPtr[2].m_Number = 10; // Indexing array like with normal pointer.
// arrPtr will free the array with delete[] when going out of scope.
unique_ptr cannot be copied, but thanks to r-value references and move semantics introduced in C++11 it can be moved, so it can also be passed as parameter and returned by value, like this:
typedef std::unique_ptr<MyClass> MyClassPtr;
MyClassPtr ProducePtr() {
MyClassPtr ptr = MyClassPtr(new MyClass());
ptr->m_Number = 123;
return ptr;
}
void ConsumePtr(MyClassPtr ptr) {
printf("The number was %d\n", ptr->m_Number);
}
ConsumePtr(ProducePtr());
MyClassPtr ptr = ProducePtr();
ptr->m_Number = 456;
ConsumePtr(std::move(ptr));
Unlike old scoped_ptr from Boost, unique_ptr from C++11 can be used inside STL containers, e.g. std::vector. Reallocation that happens inside vector will not corrupt it.
std::vector<MyClassPtr> vec;
vec.push_back(MyClassPtr(new MyClass(1)));
vec.emplace_back(new MyClass(2)); // A new, better way of adding elements.
for(auto it = std::begin(vec); it != std::end(vec); ++it)
printf("%d\n", (*it)->m_Number);
And now the most interesting part - custom deleters! unique_ptr can be used to store any resources because you can provide it with your own code that will be used to free that resource. For example, you can print something to console before deleting object :) Deleter can be a functor passed as second template parameter:
struct MyDeleter {
void operator()(int* ptr) const {
printf("Deleting int!\n");
delete ptr;
}
};
std::unique_ptr<int, MyDeleter> ptr1(new int(1));
Deleter can also hold some state. This way you can associate additional information with the pointer, like a memory pool that the pointer object comes from. Now the sizeof(ptr3) will be 8 because it must hold deleter data next to the pointer.
class MyComplexDeleter {
public:
MyComplexDeleter(int memoryPool) : m_MemoryPool(memoryPool) {
}
void operator()(int* ptr) const {
printf("Deleting from memory pool %d\n", m_MemoryPool);
delete ptr;
}
private:
int m_MemoryPool;
};
MyComplexDeleter deleterForPool20(20);
std::unique_ptr<int, MyComplexDeleter> ptr3(new int(3), deleterForPool20);
Deleter can also be a normal function, like fclose:
std::unique_ptr<FILE, int(*)(FILE*)> filePtr(
fopen("Readme.txt", "rb"),
fclose);
unique_ptr<int> will contain value of type int*. What if we want to store a resource using unique_ptr that is not a pointer but some handle or identifier, so this automatically added * is undesirable? It turns out that the type of stored value can be changed by defining "pointer" type inside deleter.
struct CloseHandleDeleter {
typedef HANDLE pointer;
void operator()(HANDLE handle) const { CloseHandle(handle); }
};
std::unique_ptr<HANDLE, CloseHandleDeleter> file(CreateFile(
"Readme.txt", GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
// The first template parameter of unique_ptr seem to not have any menaing in this case.
Comments | #c++ #visual studio Share
# Demoscene: Revision 2012 and Moleman 2
Fri
13
Apr 2012
Last weekend - during Easter - there was a big demoscene party in Germany called Revision, the successor of Breakpoint. HD live videostream was available during the event and now all productions can be downloaded from Pouet.net (
also via FTP). Geeks3d.com summarizes most interesting prods and links to YouTube videos, but of course it's better to download and watch these amazing demos and intros in the highest quality, running on your machine.
Another interesting news related to demoscene is Moleman 2 - The Art of the Algorithms - a 1.5 h long documentary movie about demoscene, now available to download for free in HD and even Full HD quality. I really recommend this movie. I think it's very professional and perfectly explains what the demoscene is all about. Plus the interviews are interlaced with nice visuals and music from demos :)
Comments | #demoscene #events Share
# IGK-9'2012 - Photo Gallery
Tue
03
Apr 2012
Here are my photos from the IGK-9'2012 conference:
![]() |
| Main Gallery |
![]() |
| Games from Compo |
![]() |
| Slides about Sphere Tracing |
Comments | #igk #events #warsztat #gallery Share
# IGK-9'2012 - PrisonEscape Game
Mon
02
Apr 2012
Our team (called Rosyjscy wieśniacy - from Russian peasants multiplication algorithm) - that is Krzysztof Kluczek "Krzysiek K.", Karol Kuczmarski "Xion" and me - scored 1st place in the gamedev compo at IGK-9'2012 conference! Thanks for all your votes, I'm glad you like our game. The theme this year was "Escape", so we decided to make an oldschool looking 2D game with pixel art, where a prisoner tries to escape, runs through constantly scrolling map, avoids policemen etc. It is coded in C++ using Direct3D 9. Here you can download binaries:
IGK2012_PrisonEscape.zip (5.83 MB)
Comments | #igk #events #warsztat #competitions #productions #games Share
# IGK-9'2012 Starts Tomorrow
Thu
29
Mar 2012
9th Polish Game Engineering Conference IGK-9'2012 starts tomorrow in Siedlce, Poland. Just like in previous years, we expect interesting lectures on Friday evening and whole Saturday. I especially look forward to hear about NoSQL databases and sphere tracing, as these are subjects that I always wanted to learn about but I didn't find enought time to do it before. On Sunday there will be traditional competition where teams of up to 4 people have to develop a game from scratch during 8 hours. But first of all this is a great opportunity for passionate game developers to meet and socialize. So see you in Siedlce!
Comments | #warsztat #events #igk Share
# Ball Physics
Sat
17
Mar 2012
Checking collision between different kinds of 2D or 3D shapes is a subject I deal with for some time. It is useful in game development to determine if some object is visible or affected by light and this if it should be rendered. I have lots of function to check such collisions in the math module of CommonLib library.
But that is only the beginning if you want to make physics. Adding physical behavior to your game requires additional calculations to correct positions and apply forces to colliding bodies. I prepared a small code snippet that implements physical 2D collision between moving circle and another moving circle (calculated by CircleToCircleCollision function), static line (CircleToLineCollision function) and static axis-aligned rectangle (CircleToRectangleCollision function).
Download: ball_physics.cpp
The code uses some good practices (like fixed time step) as well as bad practices (like Euler integration). It should be enough for a simple physics in game like a platformer.
Comments | #math #physics Share
# Iterating Backward using Unsigned Numbers
Thu
01
Mar 2012
I've had a discussion recently with my colleagues about what is the best way of writing a loop in C++ that would iterate backward: Count-1, Count-2, ..., 0, using unsigned integer numbers. I'd like to share all possible solutions I managed to gather.
Iterating forward is simple:
unsigned count = 10;
// Forward: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
for(unsigned i = 0; i < count; ++i)
printf("%d ", i);
Iterating backward is equally simple and looks similar, if you use signed integers:
// Backward: 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
// Using signed integer
for(int i = count - 1; i >= 0; --i)
printf("%d ", i);
Same code cannot be used with unsigned integers, because it creates infinite loop, as unsigned number is always >= 0 and just wraps around to maximum value 0xFFFFFFFF:
// WRONG: Inifinte loop, unsigned int wraps around and is always >= 0.
for(unsigned i = count - 1; i >= 0; --i)
printf("%d ", i);
Changing condition to i > 0 is also invalid, because then loop is not executed for i == 0:
// WRONG: Not executed for i == 0.
for(unsigned i = count - 1; i > 0; --i)
printf("%d ", i);
Here comes a bunch of possible valid solutions. My favourite is following for loop:
for(unsigned i = count; i--; )
printf("%d ", i);
Following while loop does same thing. Some people say that it looks more clear. I don't think so, while I believe the for loop is better because it creates local scope for variable i, while the following code does not:
unsigned i = count;
while(i--)
printf("%d ", i);
Another way of implementing exactly same logic is to write the condition like this. It may look like C++ had the "goes to zero" arrow operator -->, but it is actually just (i-- > 0) :)
unsigned i = count;
while(i --> 0)
printf("%d ", i);
Another possible solution of loop condition (a one that does not use the postdecrementation operator) is to compare i with maximum integer value. One disadvantage of it is that you have to carefully match the value of the max constant with type of your iterator - whether it is unsigned int, unsigned short etc.
for(unsigned i = count - 1; i != UINT_MAX; --i)
printf("%d ", i);
Another way of doing this, without using maximum value, is:
for(unsigned i = count - 1; i < count; --i)
printf("%d ", i);
Finally there are solutions possible that introduce second iterator:
// Option 1 using second iterator:
// i goes 0...9, j is inverted i.
for(unsigned i = 0; i < count; ++i)
{
unsigned j = count - i - 1;
printf("%d ", j);
}
// Option 2 using second iterator:
// i goes 10...1, j is decramented i.
for(unsigned i = count; i > 0; --i)
{
unsigned j = i - 1;
printf("%d ", j);
}
Which is your favorite loop? Please leave a comment :)