Skip to content

Commit

Permalink
Tried to wrap more D3D functions, and removed a lot of #ifdef linux
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed Jan 6, 2017
1 parent 95da496 commit a14e88e
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 264 deletions.
39 changes: 5 additions & 34 deletions 3D_Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1008,7 +1008,6 @@ struct TRANSFORMEDVERTEX
};
#define D3DFVF_TRANSFORMEDVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

#ifndef linux
static IDirect3DVertexBuffer9 *pPolygonVB = NULL;

HRESULT CreatePolygonVertexBuffer (IDirect3DDevice9 *pd3dDevice)
Expand All @@ -1028,67 +1027,39 @@ void FreePolygonVertexBuffer (void)
{
if (pPolygonVB) pPolygonVB->Release(), pPolygonVB = NULL;
}
#endif

// Draw flat polygon (no z information)
void DrawPolygon( POINT *pptr,
long sides)
{
long i;
#ifndef linux
IDirect3DDevice9 *pd3dDevice = DXUTGetD3DDevice();
#endif

TRANSFORMEDVERTEX *pVertices;

// finish if too many sides
if (sides > MAX_POLY_SIDES)
return;

#ifdef linux
#ifdef HAVE_GLES
pVertices = (TRANSFORMEDVERTEX*)malloc(sides*sizeof(TRANSFORMEDVERTEX));
if (!pVertices)
return;
#else
glBegin(GL_TRIANGLE_FAN);
#endif
#else
if( FAILED( pPolygonVB->Lock( 0, sides*sizeof(TRANSFORMEDVERTEX), (void**)&pVertices, 0 ) ) )
return;
#endif

for (i = 0; i < sides; i++)
{
#if defined(linux) && !defined(HAVE_GLES)
glColor4ubv((GLubyte*)&Fill_Colour);
glVertex2f((float)pptr[i].x, (float)pptr[i].y);
#else

pVertices[i].x = (float)pptr[i].x; // screen x
pVertices[i].y = (float)pptr[i].y; // screen y
pVertices[i].z = (float)0.5f; // not needed unless Z buffering
pVertices[i].rhw = (float)1.0f;
pVertices[i].color = Fill_Colour;
#endif

}
#ifdef linux
#ifdef HAVE_GLES
// setup arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(TRANSFORMEDVERTEX), &pVertices[0].color);
glVertexPointer(4, GL_FLOAT, sizeof(TRANSFORMEDVERTEX), &pVertices[0].x);
glDrawArrays(GL_TRIANGLE_FAN, 0, sides-2);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
#else
glEnd();
#endif
#else
pPolygonVB->Unlock();

pd3dDevice->SetStreamSource( 0, pPolygonVB, 0, sizeof(TRANSFORMEDVERTEX) );
pd3dDevice->SetFVF( D3DFVF_TRANSFORMEDVERTEX );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, sides-2 );
#endif

return;
}

Expand Down
2 changes: 0 additions & 2 deletions 3D_Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ extern void LockViewpointToTarget( long viewpoint_x,
long *viewpoint_x_angle,
long *viewpoint_y_angle );

#ifndef linux
extern HRESULT CreatePolygonVertexBuffer (IDirect3DDevice9 *pd3dDevice);
extern void FreePolygonVertexBuffer (void);
#endif

extern void DrawPolygon( POINT *pptr,
long sides );
Expand Down
78 changes: 0 additions & 78 deletions Car.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,19 +522,10 @@ static void DrawCarLeftWheelTread( long offset ) // offset into co-ordinates
/* */
/* Description: Draw the car using the supplied viewpoint */
/* ======================================================================================= */
#ifdef linux
static GLfloat *pCarVtx = NULL;
static GLfloat *pCarCol = NULL;
#else
static IDirect3DVertexBuffer9 *pCarVB = NULL;
#endif
static long numCarVertices = 0;

#ifdef linux
static void StoreCarTriangle( COORD_3D *c1, COORD_3D *c2, COORD_3D *c3, GLfloat *pVtx, GLfloat *pCol, DWORD colour )
#else
static void StoreCarTriangle( COORD_3D *c1, COORD_3D *c2, COORD_3D *c3, UTVERTEX *pVertices, DWORD colour )
#endif
{
D3DXVECTOR3 v1, v2, v3;//, edge1, edge2, surface_normal;

Expand All @@ -555,24 +546,6 @@ D3DXVECTOR3 v1, v2, v3;//, edge1, edge2, surface_normal;
D3DXVec3Normalize( &surface_normal, &surface_normal );
*/

#ifdef linux
GLfloat col[4];
col[0] = RGBA_GETRED(colour)/255.0f;
col[1] = RGBA_GETGREEN(colour)/255.0f;
col[2] = RGBA_GETBLUE(colour)/255.0f;
col[3] = RGBA_GETALPHA(colour)/255.0f;
memcpy(pVtx+numCarVertices*3, &v1, sizeof(GLfloat)*3);
memcpy(pCol+numCarVertices*3, col, sizeof(GLfloat)*4);
++numCarVertices;

memcpy(pVtx+numCarVertices*3, &v2, sizeof(GLfloat)*3);
memcpy(pCol+numCarVertices*3, col, sizeof(GLfloat)*4);
++numCarVertices;

memcpy(pVtx+numCarVertices*3, &v3, sizeof(GLfloat)*3);
memcpy(pCol+numCarVertices*3, col, sizeof(GLfloat)*4);
++numCarVertices;
#else
pVertices[numCarVertices].pos = v1;
// pVertices[numCarVertices].normal = surface_normal;
pVertices[numCarVertices].color = colour;
Expand All @@ -587,15 +560,10 @@ D3DXVECTOR3 v1, v2, v3;//, edge1, edge2, surface_normal;
// pVertices[numCarVertices].normal = surface_normal;
pVertices[numCarVertices].color = colour;
++numCarVertices;
#endif
}


#ifdef linux
static void CreateCarInVB( GLfloat *pVtx, GLfloat *pCol )
#else
static void CreateCarInVB( UTVERTEX *pVertices )
#endif
{
static long first_time = TRUE;
// car co-ordinates
Expand Down Expand Up @@ -650,11 +618,7 @@ static COORD_3D car[16+8] = {
// rear left wheel
DWORD colour = SCRGB(SCR_BASE_COLOUR+0);
/**/
#ifdef linux
#define vertices pVtx, pCol
#else
#define vertices pVertices
#endif
// viewing from back
StoreCarTriangle(&car[0], &car[1], &car[2], vertices, colour);
StoreCarTriangle(&car[0], &car[2], &car[3], vertices, colour);
Expand Down Expand Up @@ -717,12 +681,6 @@ static COORD_3D car[16+8] = {

HRESULT CreateCarVertexBuffer (IDirect3DDevice9 *pd3dDevice)
{
#ifdef linux
if (pCarVtx == NULL)
pCarVtx = (GLfloat*)malloc(sizeof(GLfloat)*3*MAX_VERTICES_PER_CAR);
if (pCarCol == NULL)
pCarCol = (GLfloat*)malloc(sizeof(GLfloat)*4*MAX_VERTICES_PER_CAR);
#else
if (pCarVB == NULL)
{
if( FAILED( pd3dDevice->CreateVertexBuffer( MAX_VERTICES_PER_CAR*sizeof(UTVERTEX),
Expand All @@ -733,54 +691,19 @@ HRESULT CreateCarVertexBuffer (IDirect3DDevice9 *pd3dDevice)
UTVERTEX *pVertices;
if( FAILED( pCarVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )
return E_FAIL;
#endif
numCarVertices = 0;
#ifdef linux
CreateCarInVB(pCarVtx, pCarCol);
#else
CreateCarInVB(pVertices);
pCarVB->Unlock();
#endif
return S_OK;
}


void FreeCarVertexBuffer (void)
{
#ifdef linux
if (pCarVtx) {
free(pCarVtx);
pCarVtx = NULL;
}
if (pCarCol) {
free(pCarCol);
pCarCol = NULL;
}
#else
if (pCarVB) pCarVB->Release(), pCarVB = NULL;
#endif
}


#ifdef linux
void DrawCar (IDirect3DDevice9 *pd3dDevice)
{
pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
pd3dDevice->ActivateWorldMatrix();

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glColorPointer(4, GL_FLOAT, 0, pCarCol);
glVertexPointer(4, GL_FLOAT, 0, pCarVtx);
glDrawArrays(GL_TRIANGLES, 0, numCarVertices);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

pd3dDevice->DeactivateWorldMatrix();
}
#else
void DrawCar (IDirect3DDevice9 *pd3dDevice)
{
pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
Expand All @@ -790,5 +713,4 @@ void DrawCar (IDirect3DDevice9 *pd3dDevice)
pd3dDevice->SetFVF( D3DFVF_UTVERTEX );
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, numCarVertices/3 ); // 3 points per triangle
}
#endif

10 changes: 8 additions & 2 deletions StuntCarRacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,8 @@ void LoadTextures()
}
void CreateBuffers(IDirect3DDevice9 *pd3dDevice)
{
/*if (CreatePolygonVertexBuffer(pd3dDevice) != S_OK)
printf("Error creating PolygonVertexBuffer\n");*/
if (CreatePolygonVertexBuffer(pd3dDevice) != S_OK)
printf("Error creating PolygonVertexBuffer\n");
if (CreateTrackVertexBuffer(pd3dDevice) != S_OK)
printf("Error creating TrackVertexBuffer\n");
if (CreateShadowVertexBuffer(pd3dDevice) != S_OK)
Expand Down Expand Up @@ -1909,6 +1909,12 @@ int main(int argc, const char** argv)

IDirect3DDevice9 pd3dDevice;

glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDisable(GL_LIGHTING);
// Disable texture mapping by default (only DrawTrack() enables it)
pd3dDevice.SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_DISABLE );

sound_init();

double fLastTime = DXUTGetTime();
Expand Down
Loading

0 comments on commit a14e88e

Please sign in to comment.