September 2009

Uwaga! Informacje na tej stronie mają ponad 5 lat. Nadal je udostępniam, ale prawdopodobnie nie odzwierciedlają one mojej aktualnej wiedzy ani przekonań.

# C primer for C++ programmers

Sep 2009

I've never learnt pure C programming language, only C++. It does not mean I never use global variables or fopen function, but I it makes no sense for me to code in pure C when there is a C++ compiler available. But today I've decided to skim the classic book "The C Programming Language" by Brian W. Kernighan and Dennis Ritchie. Here is what I've found out:

You can actually code in C using Visual C++. To do this, you need to: (+) Create empty project, (+) Add a file with .c extension, (+) Enter project options, (+) Nagivate to Configuration Properties / C/C++ / Advanced, (+) Set "Compile As" to "Compile as C Code (/TC)".

The C language is similar to C++ except it lacks features such as:

There are some differences between C++ and C though. For example, you don't have to state type of function parameter or return type if you want to it to default to int. Following lines are equivalent:

int MyFunc1(int a, int b) { }
MyFunc2(a, b) { }

Compiler is generally more liberal when it comes to type checking and function calling. You can call function with more arguments than expected and it only generates warning. You can declare function with empty parameter list like MyFunc() which means unknown parameter list! To explicitly state that a function takes 0 parameters you have to write MyFunc(void).

Local variables can be defined only before any other statements in a block. You also cannot define a variable inside for loop. Example:

void Func(void)
  int a = 1; // OK
  int b = 2; // Error!
    int c = 3; // OK

C requires different syntax for using structures. If you define a structure like this:

struct Struct1 { int x; };

You have to prefix the name of this type with "struct" keyword each time you use it, just like this:

struct Struct1 *myObj;

But luckily typedef construct works in C, so the common solution to this problem is to use it like this:

typedef struct { int x; } Struct1;
Struct1 *myObj;

Comments | #c++ #c #visual studio Share

# What is C++/CLI ?

Sep 2009

Today I've started learning C++/CLI. It's a Microsoft technology that allows to write managed code for .NET platform in C++. But I can see it's not just a Microsoft idea to introduce custom language extensions. It's a massive piece of technology and even some C++ gurus were involved in its development, like Herb Sutter and Stanley Lippman. It's also approved as ECMA-372 standard.

So what exactly is C++/CLI? It's yet another language for .NET, just like C# or VB.NET, but it has the unique feature of being compatible with native C++ and being able to freely mix managed and unmanaged code.

For many people it may trigger some bad connotations with ugly syntax like __gc, __interface, __property etc., but that's not true. Syntax like this existed in Managed Extensions for C++, but C++/CLI is its new, improved version introduced in Visual Studio 2005.

Here is a screenshot from my first experiment, where I've created a property grid control from .NET next to a native Direct3D9 device initialized inside a panel control.

Just look at how old good native code can be mixed with managed one:

#include <d3dx9.h>
namespace ManagedCpp02 {
  using namespace System::Windows::Forms;


    PropertyGrid^ propertyGrid1 = gcnew PropertyGrid();
    IDirect3D9 *d3d = Direct3DCreate9(D3D_SDK_VERSION);
    System::Diagnostics::Debug::Assert(d3d != nullptr);

Microsoft wasn't afraid to introduce new keywords and operators to the language. These designed to operate on managed code work next to the native ones, so you can define and use native and managed classes next to each other just like this:

class NativeClass { };

// Allocate from unmanaged heap - memory must be freed.
NativeClass *nPtr = new NativeClass();
delete nPtr;

ref class ManagedClass { };

// Allocate from managed heap - garbage collected.
ManagedClass ^mPtr = gcnew ManagedClass();

Comments | #c++ #visual studio Share

# Learning PhysX

Sep 2009

Yesterday I've started learning NVIDIA PhysX. It's the first time I use a physics engine at home, but it seems quite easy for me. I like the PhysX library as it has pretty, object-oriented API.

Is using a physics engine really an advanced topic? Now I don't think so. Sure there are some scary looking terms like "inertia tensor" or "angular damping", but all you need for the start is to know a bit about vectors, matrices, quaternions and some school-level physics like the concept of mass, velocity and force.

PhysX is very powerful. It can simulate soft bodies, cloth, force fields, fluids and much more. But the foundation is just rigid body physics plus character controller. In fact it is so simple and powerful at the same time that I now think every beginner game programmer should learn this. Physics in games is not only to simulate boxes and ragdolls of killed enemies. Using such physics engine is many times easier than implementing algorithms for normal collision detection, sliding along walls, walking on the height field, checking if an object entered a trigger or casting rays. PhysX offers it all!

So how to start learning PhysX if you are interested in this subject? All you need to do is to go to the NVIDIA PhysX website, download and install the PhysX SDK. It includes all necessary files like headers, libraries, DLL-s, documentation and tutorials. I learn from the included tutorials in DOC format which can be found in the TrainingPrograms\Docs\ subdirectory.

Here is a small screenshots from my today experiments:

Comments | #libraries #physics Share

# About Graphics in Modern Games

Sep 2009

There are always some "trendy" styles that differentiate professional creations from amateur ones. CollegeHumor made a parody of professional-looking trailer for fictional movie Minesweeper [en] and LIMO cabaret did the same with movie Leszek Balcerowicz - Nieznana historia [pl].

What about games? Today people I'm following on Tweeter posted interesting links about graphics in modern games. First, this comic says much. Dark, brown and gray colors are common in today's games, especially in some genres like action games (opposite to colourful fantasy RPG and arcade games, like Trine). Here is also a funny picture. Second, Kayamon posted on his blog quite serious note about how render target size can be reduced by using only two channels to describe pixel colors. His experiment gave quite good results with screenshots from Gears of War :)

Comments | #video #humor #rendering #web Share

# Coding Texture Preview

Sep 2009

Today is the 256's day of the year, so it's the Programmers' Day. Thus I wish all the best, especially a good code and no bugs to all of you who code for fun or/and profit (hopefully "and" :)

I've recently started new home project called "BlueWay". There's noting special about it - just another time I start everyting from scratch :) But this time I have deferred shading, cascaded shadow mapping, new scene management (based on k-d tree), new component-based object system and asynchronous resource manager (resources are loaded in the background on separate thread). I'm not sure whether I can call D3DXCompileShaderFromFile from separate thread without D3DCREATE_MULTITHREADED, but it seems to work OK :)

What I want to show today is a code for previewing textures. When coding some complex effects, it's often handy to look at intermediate results in render-target textures. Of course we have famous PIX and NVIDIA PerfHUD, but I believe that such custom code for real-time in-game texture preview is useful anyway. I wanted to make it general and flexible and here is the way I do it.

Read full entry > | Comments | #rendering #directx Share

# New DirectX SDK and Total Commander

Sep 2009

There are two new software releases that are important to me. I know about them from Twitter (yet another good reason to join microblogging activity :)

First one is DirectX SDK August 2009. Updates relate mostly to DirectX 11, but hey, there was no new SDK version for a long time!

(I've just discovered there are changes in the error handling system that are not backward compatible. To compile my code I had to change #include <dxerr9.h> to <dxerr.h>, link with file dxerr.lib instead of dxerr9.lib and rename function calls DXGetErrorString9 and DXGetErrorDescription9 to DXGetErrorString and DXGetErrorDescription.)

Second new release is Total Commander 7.5 FINAL. I can't see any revolutionary changes (despite the new, Vista-like behavior of the current directory bar), but that's a good news as Total Commander is already great.

Comments | #directx #software Share

# Dualism of Data Types

Sep 2009

In 3D games programming we use structure type called "vector", like D3DXVECTOR3, but we can remember from school that there are two separate geometrical objects: point and vector. Although both contain just 3 real numbers, there actually is a difference between them. Point represents a position in the space of some coordinate system (it's absolute - we can imagine it as a dot), while vector represents a translation by some distance and direction (it's relative - we can image it as an arrow). If we had separate types for points and vectors, we should define their operators this way:

POINT + POINT doesn't exist
POINT - POINT = VECTOR (difference between points)

POINT + VECTOR = POINT (translated point)
POINT - VECTOR = POINT (point translated by inverted vector)

VECTOR - VECTOR = VECTOR (difference)

VECTOR * float = VECTOR (scaling length)
VECTOR / float = VECTOR (scaling length)

In my opinion it's good to express this difference by naming variables "position" (when we mean absolute position of an object) and "translation" (when we mean transformation of the object position), "size" (when we mean persisting size of the object) and "scale" (when we mean transforming object scale by some amount), "orientation" (when we mean how the object is currently rotated) and "rotation" (when we mean a transformation that is going to modify its orientation).

Although we never make such distinction by defining two separate data types for vectors or matrices, it happens sometimes in the analogical situation with date-time types. For example there is the DateTime structure in C# that represents an instant in time and the TimeSpan structure that represents a time interval.

Similar problem exists when we talk about any indices or pointers, especially if we want to use unsigned integers. Index or pointer cannot be negative and special "invalid" value can be defined as 0xffffffff as well as -1. I like to use unsigned type, e.g. because I can just check if (index < elemCount) instead of if (index >= 0 && index < elemCount). But what if we need a type to describe a relative offset? Obviously it should be signed because it can be negative. That's why C++ has types size_t (unsigned) and ptrdiff_t (signed). But things get complicated when we think about what type should the fseek function expect, as it can get either absolute (SEEK_SET) or relative position (SEEK_CUR, SEEK_END).

So all in all I think maybe we don't have to always define two separate types in our code, but we should always remember about this distinction when writing code.

Comments | #c++ Share

[Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2020