2011.9.23
HGE基本的渲染图元是hgeQuad (Quad is the basic HGE graphic primitive),其中有一个hgeVertex成员结构,它用来描述图元顶点信息。The hgeVertex structure is used to describe vertices of which HGE graphic primitives consist.两个结构信息如下:
struct hgeQuad { hgeVertex v[4]; HTEXTURE tex; int blend; };
struct hgeVertex { float x, y; float z; DWORD col; float tx, ty; };
其中,x,y被描述为屏幕坐标,tx,ty被描述为纹理坐标,col被描述为颜色。创建一组顶点,每个顶点包含了位置坐标,和纹理坐标(纹理坐标一般为0--1),还有颜色等信息。为什么会有屏幕坐标很纹理坐标呢,一个点在屏幕上有坐标,一个矩形区域需要把一张图片映射进来,如果采用纹理方式,就需要为每一个点指定一个二维的坐标,hgeQuad就采用了四个纹理坐标,存在hgeVertex中,这样就可以从一张纹理图中切割一部分来渲染。这四个顶点分别从左上角开始顺时针表示一个RECT
利用hgeQuad显示图片的过程:
1. 用Texture_Load载入外部文件作为纹理。hgeQuad quad; quad.tex=hge->Texture_Load("particles.png");
2. 设置hgeQuad的纹理坐标,窗口坐标,以及渲染模式。
quad.blend=BLEND_ALPHAADD | BLEND_COLORMUL| BLEND_ZWRITE;
3. 每一帧都调用 Gfx_RenderQuad函数,这个函数用hge->System_SetState()设置的。
通过小精灵hgeSprite和HTEXTURE配合也可以进行图形渲染,
hgeSprite方法:
Constructors | Create and initalize a hgeSprite object. |
Operators | hgeSprite operators. |
Render | Renders sprite to the screen. |
RenderEx | Renders sprite with scaling and rotation. |
RenderStretch | Renders stretched sprite. |
Render4V | Renders sprite into arbitrary quad on the screen. |
SetTexture | Sets the texture to use for the sprite. |
SetTextureRect | Sets the texture region to use for the sprite. |
SetColor | Sets tint and alpha for the specified vertex or entire sprite. |
SetZ | Sets Z-order for the specified vertex or entire sprite. |
SetBlendMode | Sets the sprite blending mode. |
SetHotSpot | Sets the sprite anchor point. |
SetFlip | Flips the sprite horizontally and/or vertically. |
GetTexture | Returns the current sprite texture. |
GetTextureRect | Returns the current texture region used for the sprite. |
GetColor | Returns color of the specified sprite vertex. |
GetZ | Returns Z-order of the specified sprite vertex. |
GetBlendMode | Returns the current sprite blending mode. |
GetHotSpot | Returns the sprite anchor point. |
GetFlip | Returns the current sprite flipping. |
GetWidth | Returns the sprite width. |
GetHeight | Returns the sprite height. |
GetBoundingBox | Returns the sprite bounding box. |
GetBoundingBoxEx | Returns the scaled and rotated sprite bounding box. |
typedef DWORD HTEXTURE;也就是说HTEXTURE实际上就是一个纹理的指针
渲染过程很简单,初始化:
HTEXTURE tex1; hgeSprite *spr;
tex1=hge->Texture_Load("1.jpg");
spr=new hgeSprite(tex1,0,0,800,600); //初始化图片精灵,后四个参数分别是,起始位置X,起始位置Y,图片宽,图片高。
在渲染函数中:
hge->Gfx_BeginScene(); //开始渲染
hge->Gfx_Clear(0xFFFFFFFF); //以某种颜色清屏幕
spr->Render(10,10); //在指定的位置上显示精灵
spr->SetColor(0xFFFF0000); //设置hgesprite的渲染颜色是红色,前两位是透明度a,后面一次是r,g,b
hge->Gfx_EndScene(); //结束渲染