?? gameswf_render_handler_d3d.cpp
字號:
if (m_pVB2)
{
m_pVB2->Release();
m_pVB2 = 0;
}
if (m_pd3dDevice)
m_pd3dDevice->Release();
}
// Style state.
enum style_index
{
LEFT_STYLE = 0,
RIGHT_STYLE,
LINE_STYLE,
STYLE_COUNT
};
fill_style m_current_styles[STYLE_COUNT];
gameswf::bitmap_info* create_bitmap_info_alpha(int w, int h, unsigned char* data)
{
return new bitmap_info_d3d(w, h, data);
}
gameswf::bitmap_info* create_bitmap_info_rgb(image::rgb* im)
// Given an image, returns a pointer to a bitmap_info struct
// that can later be passed to fill_styleX_bitmap(), to set a
// bitmap fill style.
{
return new bitmap_info_d3d(im);
}
gameswf::bitmap_info* create_bitmap_info_rgba(image::rgba* im)
// Given an image, returns a pointer to a bitmap_info struct
// that can later be passed to fill_style_bitmap(), to set a
// bitmap fill style.
//
// This version takes an image with an alpha channel.
{
return new bitmap_info_d3d(im);
}
gameswf::bitmap_info* create_bitmap_info_empty()
// Creates and returns an empty bitmap_info structure. Image data
// can be bound to this info later, via set_alpha_image().
{
assert(0); //pk this function not implemented.
//pk return new bitmap_info_d3d(gameswf::bitmap_info::empty);
return NULL;
}
void set_alpha_image(gameswf::bitmap_info* bi, int w, int h, Uint8* data)
// Set the specified bitmap_info so that it contains an alpha
// texture with the given data (1 byte per texel).
//
// Munges *data (in order to make mipmaps)!!
{
assert(bi);
assert(0); // not tested
//pk bi->set_alpha_image(w, h, data);
}
void delete_bitmap_info(gameswf::bitmap_info* bi)
// Delete the given bitmap info struct.
{
delete bi;
}
void prepare_vertex_buffer(const Sint16* coords, int vertex_count)
{
HRESULT hr;
if( vertex_count>(int)m_nMaxVertices )
{
// resize mesh
m_pVB->Release();
m_nMaxVertices = vertex_count;
hr = m_pd3dDevice->CreateVertexBuffer( m_nMaxVertices*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY|D3DUSAGE_DYNAMIC
, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB
#if DIRECT3D_VERSION >= 0x0900
, NULL
#endif
);
assert(hr==S_OK);
}
CUSTOMVERTEX* verts;
hr = m_pVB->Lock( 0, 0, (tLock**)&verts, D3DLOCK_DISCARD );
assert(hr==S_OK);
int coord_count = vertex_count * 2;
for(int i=0; i<coord_count; i+=2)
{
verts->x = coords[i];
verts->y = coords[i+1];
verts->z = Z_DEPTH;
verts++;
}
hr = m_pVB->Unlock();
assert(hr==S_OK);
hr = m_pd3dDevice->SetStreamSource( 0, m_pVB,
#if DIRECT3D_VERSION >= 0x0900
0,
#endif
sizeof(CUSTOMVERTEX) );
assert(hr==S_OK);
#if DIRECT3D_VERSION < 0x0900
hr = m_pd3dDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);
#else
hr = m_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
#endif
assert(hr==S_OK);
}
void begin_display(
gameswf::rgba background_color,
int viewport_x0, int viewport_y0,
int viewport_width, int viewport_height,
float x0, float x1, float y0, float y1)
// Set up to render a full frame from a movie and fills the
// background. Sets up necessary transforms, to scale the
// movie to fit within the given dimensions. Call
// end_display() when you're done.
//
// The rectangle (viewport_x0, viewport_y0, viewport_x0 +
// viewport_width, viewport_y0 + viewport_height) defines the
// window coordinates taken up by the movie.
//
// The rectangle (x0, y0, x1, y1) defines the pixel
// coordinates of the movie that correspond to the viewport
// bounds.
{
HRESULT hr;
D3DXMatrixIdentity(&m_ModelViewMatrix);
D3DXMatrixIdentity(&m_ProjMatrix);
// invert coordinate system from lower left to upper left
float gsWidthDiv = 1.0f / viewport_width;
float gsHeightDiv = 1.0f / viewport_height;
m_ModelViewMatrix._11 = 2.0f / (x1 - x0);
m_ModelViewMatrix._22 = -2.0f / (y1 - y0);
m_ModelViewMatrix._41 = -((x1 + x0) / (x1 - x0));
m_ModelViewMatrix._42 = ((y1 + y0) / (y1 - y0));
m_ProjMatrix._11 = viewport_width * gsWidthDiv;
m_ProjMatrix._22 = viewport_height * gsHeightDiv;
m_ProjMatrix._41 = -1.0f + viewport_x0 * 2.0f * gsWidthDiv + viewport_width * gsWidthDiv;
m_ProjMatrix._42 = 1.0f - viewport_y0 * 2.0f * gsHeightDiv - viewport_height * gsHeightDiv;
// Matrix setup
D3DXMATRIX ident;
D3DXMatrixIdentity(&ident);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &m_ModelViewMatrix);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &m_ProjMatrix);
m_pd3dDevice->SetTransform(D3DTS_WORLD, &ident);
// turn on alpha bending.
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);
if( NULL == m_pd3dDevice )
{
assert(0);
return;
}
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE);
// Viewport.
hr = m_pd3dDevice->GetViewport(&m_origVP);
assert(SUCCEEDED(hr)); // pure device?
D3DVIEWPORT vp;
vp.X = viewport_x0;
vp.Y = viewport_y0;
vp.Width = viewport_width;
vp.Height = viewport_height;
vp.MinZ = 0.0f;
vp.MaxZ = 1.0f; // minZ = maxZ = 0 forces all polys to be in foreground
m_pd3dDevice->SetViewport(&vp);
// Matrix to map from SWF movie (TWIPs) coords to
// viewport coordinates.
// Clear the backbuffer to a blue color
// m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
if( FAILED( m_pd3dDevice->BeginScene() ) )
{
// Rendering of scene objects can happen here
// End the scene
m_pd3dDevice->EndScene();
assert(0);
}
// Blending renderstates
//pk not sure if we need these.
m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND, D3DBLEND_SRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA );
m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_BLENDOP, D3DBLENDOP_ADD );
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_TFACTOR);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR);
// Textures off by default.
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG2);
// @@ for sanity's sake, let's turn of backface culling...
m_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);//xxxxx
m_pd3dDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS); //xxxxx
// Vertex format.
#if DIRECT3D_VERSION >= 0x0900
m_pd3dDevice->SetVertexShader(NULL);
#endif
// No pixel shader.
m_pd3dDevice->SetPixelShader(NULL);
// Clear the background, if background color has alpha > 0.
if (background_color.m_a > 0)
{
if( background_color.m_a==255 )
{
D3DRECT rect = {LONG(x0),LONG(y0),LONG(x1),LONG(y1)};
m_pd3dDevice->Clear(1, &rect,D3DCLEAR_TARGET,
D3DCOLOR_XRGB(background_color.m_r, background_color.m_g, background_color.m_b),
0.0f, 0 );
}
else
{
const Sint16 backgroundCoords[] = {
(Sint16)x0,(Sint16)y0,
(Sint16)x1,(Sint16)y0,
(Sint16)x0,(Sint16)y1,
(Sint16)x1,(Sint16)y1};
apply_color(background_color);
set_matrix(gameswf::matrix::identity);
apply_matrix(m_current_matrix);
prepare_vertex_buffer(backgroundCoords, 4);
HRESULT hr = m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
assert(hr==S_OK);
}
}
}
void end_display()
// Clean up after rendering a frame. Client program is still
// responsible for calling glSwapBuffers() or whatever.
{
// End the scene
m_pd3dDevice->EndScene();
// Present the backbuffer contents to the display
D3DXMATRIX mat;
D3DXMatrixIdentity( &mat );
m_pd3dDevice->SetTransform(D3DTS_VIEW, &mat);
m_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
#if DIRECT3D_VERSION >= 0x0900
m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_WRAP);
m_pd3dDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_WRAP);
#else
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_WRAP);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_WRAP);
#endif
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_PASSTHRU );
m_pd3dDevice->SetViewport(&m_origVP);
}
void set_matrix(const gameswf::matrix& m)
// Set the current transform for mesh & line-strip rendering.
{
m_current_matrix = m;
}
void set_cxform(const gameswf::cxform& cx)
// Set the current color transform for mesh & line-strip rendering.
{
m_current_cxform = cx;
}
void apply_matrix(const gameswf::matrix& m)
// Set the given transformation matrix.
{
D3DXMATRIX mat;
D3DXMatrixIdentity( &mat );
// row 0
mat._11 = m.m_[0][0]; mat._12 = m.m_[1][0]; mat._13 = 0.00f; mat._14 = 0.00f;
mat._21 = m.m_[0][1]; mat._22 = m.m_[1][1]; mat._23 = 0.00f; mat._24 = 0.00f;
mat._31 = 0.00f; mat._32 = 0.00f; mat._33 = 1.00f; mat._34 = 0.00f;
mat._41 = m.m_[0][2]; mat._42 = m.m_[1][2]; mat._43 = 0.00f; mat._44 = 1.00f;
m_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
}
static void apply_color(const gameswf::rgba& c)
// Set the given color.
{
HRESULT hr = m_pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, D3DCOLOR_ARGB(c.m_a, c.m_r, c.m_g, c.m_b));
assert(hr==S_OK);
}
void fill_style_disable(int fill_side)
// Don't fill on the {0 == left, 1 == right} side of a path.
{
assert(fill_side >= 0 && fill_side < 2);
m_current_styles[fill_side].disable();
}
void line_style_disable()
// Don't draw a line on this path.
{
m_current_styles[LINE_STYLE].disable();
}
void fill_style_color(int fill_side, gameswf::rgba color)
// Set fill style for the left interior of the shape. If
// enable is false, turn off fill for the left interior.
{
assert(fill_side >= 0 && fill_side < 2);
m_current_styles[fill_side].set_color(m_current_cxform.transform(color));
}
void line_style_color(gameswf::rgba color)
// Set the line style of the shape. If enable is false, turn
// off lines for following curve segments.
{
m_current_styles[LINE_STYLE].set_color(m_current_cxform.transform(color));
}
void fill_style_bitmap(int fill_side, const gameswf::bitmap_info* bi, const gameswf::matrix& m, bitmap_wrap_mode wm)
{
assert(fill_side >= 0 && fill_side < 2);
m_current_styles[fill_side].set_bitmap(bi, m, wm, m_current_cxform);
}
void line_style_width(float width)
{
// WK: what to do here???
}
void draw_mesh_strip(const void* coords, int vertex_count)
{
// Set up current style.
m_current_styles[LEFT_STYLE].apply(m_current_matrix);
apply_matrix(m_current_matrix);
prepare_vertex_buffer((Sint16*)coords, vertex_count);
HRESULT hr = m_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, vertex_count - 2);
assert(hr==S_OK);
if (m_current_styles[LEFT_STYLE].needs_second_pass())
{
// 2nd pass, if necessary.
m_current_styles[LEFT_STYLE].apply_second_pass();
hr = m_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, vertex_count-2);
assert(hr==S_OK);
m_current_styles[LEFT_STYLE].cleanup_second_pass();
}
}
void draw_line_strip(const void* coords, int vertex_count)
// Draw the line strip formed by the sequence of points.
{
// Set up current style.
m_current_styles[LINE_STYLE].apply(m_current_matrix);
apply_matrix(m_current_matrix);
prepare_vertex_buffer((Sint16*)coords, vertex_count);
HRESULT hr = m_pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, vertex_count-1);
assert(hr==S_OK);
}
void draw_bitmap(
const gameswf::matrix& m,
const gameswf::bitmap_info* bi,
const gameswf::rect& coords,
const gameswf::rect& uv_coords,
gameswf::rgba color)
// Draw a rectangle textured with the given bitmap, with the
// given color. Apply given transform; ignore any currently
// set transforms.
//
// Intended for textured glyph rendering.
{
assert(bi);
apply_color(color);
D3DXMATRIX mat;
D3DXMatrixIdentity(&mat);
m_pd3dDevice->SetTransform(D3DTS_WORLD, &mat);
gameswf::point a, b, c, d;
m.transform(&a, gameswf::point(coords.m_x_min, coords.m_y_min));
m.transform(&b, gameswf::point(coords.m_x_max, coords.m_y_min));
m.transform(&c, gameswf::point(coords.m_x_min, coords.m_y_max));
d.m_x = b.m_x + c.m_x - a.m_x;
d.m_y = b.m_y + c.m_y - a.m_y;
// Set texture.
m_pd3dDevice->SetTexture(0, m_d3d_textures[bi->m_texture_id]);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2);
m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA);
m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -