Getting Started with DirectX game Programming-Part II (Game Programming Toolbox)-the Wizard of Transparent Wizard programming

Source: Internet
Author: User
Tags file info image filter transparent color transparent image

This series of articles by Net_assassin finishing, reproduced please indicate the source.

http://blog.csdn.net/net_assassin/article/category/1100363


Author: Net_assassin e-mail: net_assassin@hotmail.com look forward to interacting with like-minded friends

ID3DXSprite objects are a great surprise to programmers who plan to use Direct3D to write 2D games.

The advantage of this is that the full 3D renderer is ready to serve you at the same time as the previous implementation (DirectDraw) as fast as the function. By processing sprites into textures and rendering them in rectangles (two triangles), we can transform the sprite to initialize the sprite renderer ID3DXSprite object is a sprite processor that contains functions that draw sprites from textures.

Lpd3dxsprite  spriteobj = NULL;

Attaching the Sprite processor to the Direct3D device

HRESULT WINAPI d3dxcreatesprite (
         lpdirect3ddevice9  pdevice,
         lpd3dxsprite          *ppsprite
);

After the start wizard renderer has called BeginScene in the main D3D device, you can start drawing sprites. Spriteobj->begin (D3dxsprite_alphablend); Drawing Sprites

HRESULT Draw (
    lpdirect3dtexture9  ptexture,  //Specifies the texture of the source image used by the sprite
    CONST RECT  *psrcrect,//Can be captured from the source image " The image unit "
    const D3DXVECTOR3 *pcenter,//Specifies the center point where the rotation takes place
    CONST D3dxvector3 *pposition,//The location of the specified sprite
    d3dcolor Color  //Specifies the color changes that are made to the sprite image when it is drawn (without affecting transparency). Color key
)

Stop Sprite Renderer Spriteobj->end (); Transparent Elf programBefore adding a change to our template: Add the declaration of two functions to the MyDirectX.h file and add the corresponding function implementation in the MyDirectX.cpp file.
D3dxvector2 getbitmapsize (string filename);
Lpdirect3dtexture9 loadtexture (string filename, d3dcolor transcolor = D3dcolor_xrgb (0,0,0));
D3dxvector2 getbitmapsize (string filename) {D3dximage_info INFO;

    D3dxvector2 size = D3dxvector2 (0.0f,0.0f);
    
    HRESULT result = D3dxgetimageinfofromfile (Filename.c_str (), &info); if (result = = D3D_OK) size = D3dxvector2 ((float) info. Width, (float) info.
    Height); else size = D3dxvector2 ((float) info. Width, (float) info.

    Height);
return size;

    } lpdirect3dtexture9 loadtexture (std::string filename, d3dcolor transcolor) {lpdirect3dtexture9 texture = NULL;
    Get width and height from bitmap file D3dximage_info INFO;
    HRESULT result = D3dxgetimageinfofromfile (Filename.c_str (), &info);

    if (Result! = D3D_OK) return NULL; Create the new texture by loading a bitmap image file D3DXCreateTextureFromFileEx (D3ddev,//di Rect3D device Object Filename.c_str (),//bitmap filename info. width,//bitmap image width info.
    Height,//bitmap image Height    1,//mip-map levels (1 for no chain) D3dpool_default,//the type of surface (Standar 
        d) D3dfmt_unknown,//surface format (default) D3dpool_default,//memory class for the texture            D3dx_default,//image filter d3dx_default,//MIP filter Transcolor,                  Color key for transparency &info,//bitmap file info (from loaded file) NULL,            color palette &texture);

	Destination Texture//make sure the bitmap textre was loaded correctly if (result! = D3D_OK) return NULL;
return texture; }
Modify Direct3d_init and Direct3d_shutdown to automatically create and destroy Sprite objects. Modify MyGame.cpp to add in the Game_init function:
Load non-transparent image
    Image_notrans = loadtexture ("shuttle_notrans.bmp");
    if (!image_notrans) return false;

    Load color-keyed transparent image
    Image_colorkey = loadtexture ("Shuttle_colorkey.bmp", D3dcolor_xrgb ( 255,0,255));
    if (!image_colorkey) return false;

    Load Alpha transparent image
    Image_alpha = loadtexture ("Shuttle_alpha.tga");
    if (!image_alpha) return false;


    You can use this function to get the size of the image
    D3dxvector2 size = getbitmapsize ("Shuttle_alpha.tga");
In the Game_run function, add:
Start Drawing
        spriteobj->begin (d3dxsprite_alphablend);

        Draw the Sprite
		D3dxvector3 pos1 (ten, 0);
		Spriteobj->draw (Image_notrans, NULL, NULL, &POS1, D3dcolor_xrgb (255,255,255));

		D3dxvector3 pos2 (0);
		Spriteobj->draw (image_colorkey, NULL, NULL, &POS2, D3dcolor_xrgb (255,255,255));

		D3dxvector3 pos3 (0);
		Spriteobj->draw (image_alpha, NULL, NULL, &POS3, D3dcolor_xrgb (255,255,255));

        Stop drawing
        spriteobj->end ();
Freeing Resources in Game_end:
Free memory and shut down
    image_notrans->release ();
    Image_colorkey->release ();
    Image_alpha->release ();


Note: From the above figure, we can see that the first picture part is hollow, not the original black, the reason is ID3DXSprite::D raw By default using black as the color key.

If a transparent color key is specified when the texture is loaded, ID3DXSprite::D Raw will use this color key, even if there is an alpha channel in the image.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.