Sprite Technique Delayed In BMP Images

Sprite Technique Delayed In BMP Images

If you read the article & Elegant Game Programming & quot; You must be ready to write a game for yourself. However, to write a game also requires some basic techniques. Please introduce Sprite technique & quot; Remove excess in BMP image & quot; When using pictures for Game.

As you know to make a game, the interface must be beautiful. That means that you can not draw a scene or character yourself, but use graphical programs to create images and use them to display them. For 3D programs, it is 3DMAX or MAYA, and 2D as we are discussing is Photoshop or some other program. The image file we use will be the standard BMP format of Windows. However, there was a problem that when I started I had to think a lot to find a solution. That is, most of the images in the game are not rectangular. But a bitmap image is a rectangle so we have to have some way to draw its real shape without the whole rectangle.
There is a flying saucer as follows:
When we draw 10 flying saucers will look like this:

The images will be lost due to the overlapping rectangles. Meanwhile, what we need is not to overlap. So how do you now 1 Have you got a solution yet? Here are two ways to share with you. Basically there are two common algorithms applied to solve this problem chroma key & nbsp; alpha channel

Color or technical removal techniques chroma key is a well known and easy-to-use Bitmap image or scanned image as a method of storing digital images. It turns the image pixels into binary numbers and displays the data in rows and columns of the matrix, so the bitmap image must be rectangular.

00 01 10 FF FE 12 15 11
11 12 11 16 18 90 11 C0
00 00 01 02 03 04 05 06
11 12 11 15 16 D0 ED DD
00 01 10 FF FE 12 15 11
11 12 11 16 18 90 11 C0
00 00 01 02 03 04 05 06
11 12 11 15 16 D0 ED DD

With the method chroma key We will remove the color (red color of the image) when we paint the image on the display device. The image to be displayed will be of the form:

As we see pink is not in the colors of the UFO object. So we choose pink as the color of the image. We remove the redundant part of the main image with this key with the following code:

for (nRow = 0; nRow <nheight;> {for (nCol = 0; nCol <nwidth;>
dwPixel = GetPixel (hMemDC, nCol, nRow);
if (dwPixel! = dwTransparentColor)

SetPixel (hDC, nPosX + nCol, nPosY + nRow, dwPixel);

That's the Chroma key method, but the drawback of this method is that it does not always have the special color that makes the key color. By the alpha chanel method we will be more capable, first we first create the UFO image into two images as follows:

This method uses a mask image as a black and white image to remove the excess in the bitmap image. The following sprite algorithm performs the removal of the excess in the UFO pattern

Sprite algorithm:

first. & nbsp; A bitmap object and a mask image

& nbsp; INVENTORY (INVERT) image mask

& nbsp; Put the mask image on the display

& nbsp; Use OR bitmap object images with the screen display

The code looks like this:

void PutSprite (HDC hDC, int nPosX, int nPosY, int nCX, int nCY, HBITMAP hObj, HBITMAP hMask)
HDC hMemDC, hMaskDC;
hMemDC = CreateCompatibleDC (hDC);
hMaskDC = CreateCompatibleDC (hDC);
SelectObject (hMemDC, hObj);
SelectObject (hMaskDC, hMask);
BitBlt (hMemDC, 0,0, nCX, nCY, hMask, 0,0, SRCAND);
PatBlt (hMaskDC, 0,0, nCX, nCY, DSTINVERT);
BitBlt (hDC, nPosX, nPosY, nCX, nCY, hMaskDC, 0,0, SRCAND);
BitBlt (hDC, nPosX, nPosY, nCX, nCY, hMemDC, 0,0, SRCPAINT);
DeleteDC (hMemDC);
DeleteDC (hMaskDC);

Consider the following example:

#include & quot; windows.h & quot;
100 BITMAP ufo.bmp
& nbsp;

// replace to startup.c
LRESULT MainWndProc (HWND hWnd, UINT message, WPARAM uParam, LPARAM lParam)

PAINTSTRUCT ps;
HDC hDC, hMemDC;
HBITMAP hBitmap;
int nCnt;
switch (message)

case WM_PAINT:
hDC = BeginPaint (hWnd, & ps;
hBitmap = LoadBitmap (g_hInst, MAKEINTRESOURCE (100));
hMemDC = CreateCompatibleDC (hDC);
SelectObject (hMemDC, hBitmap);
for (nCnt = 10; nCnt & lt; 100; nCnt + = 5)
TransparentImage (hDC, 10 + nCnt, 10 + nCnt, 41.38, hMemDC, 0,0,41,38,

RGB (255,0,255));
DeleteDC (hMemDC);
DeleteObject (hBitmap);
EndPaint (hWnd, & ps;)
break;
case WM_LBUTTONDOWN:
DestroyWindow (hWnd);
break;
default:
DefWindowProc (hWnd, message, uParam, lParam);

return 0;

We will get the same result as the algorithm. In addition to this technique we need some other technique such as OffScreen, Paloma, Collision, Tile. . . which we will discuss later.

KienDN - thuynt Dohoavietnam