Original Post
I''m working on an isometric engine, and I want to make sure my code is as clean as possible (which it isn''t) before I move on. I''ve left out most of the initilization functions, because I''m fine with them.
//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with the vertices
//-----------------------------------------------------------------------------
HRESULT CGame::InitVB(){
if(FAILED(D3DXCreateTextureFromFileEx(g_pD3DDevice, "test.bmp", D3DX_DEFAULT,
D3DX_DEFAULT, D3DX_DEFAULT, D3DUSAGE_RENDERTARGET, D3DFMT_UNKNOWN,
D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_DEFAULT,
D3DCOLOR_XRGB(0,0,0), NULL, NULL, &pTileTex)))
return E_FAIL;
charSprite.LoadTexture(g_pD3DDevice, "manatarms.bmp");
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: Scroll()
// Desc: Scrolls the map
//-----------------------------------------------------------------------------
void CGame::Scroll(){
if(dir == RIGHT){
scrollXOffset--;
if(scrollXOffset < 1){
scrollX++;
scrollXOffset = 64;
if(scrollX > 20)
dir = STATIONARY;
};
}
else if(dir == LEFT){
scrollXOffset++;
if(scrollXOffset > 64){
scrollX--;
scrollXOffset = 0;
if(scrollX < 1)
dir = STATIONARY;
};
}
else if(dir == UP){
scrollYOffset++;
if(scrollYOffset > 64){
scrollY--;
scrollYOffset = 0;
if(scrollY < 1)
dir = STATIONARY;
};
}
else if(dir == DOWN){
scrollYOffset--;
if(scrollYOffset < 1){
scrollY++;
scrollYOffset = 64;
if(scrollY > 20)
dir = STATIONARY;
};
}
else if(dir == STATIONARY){
scrollX=scrollX;
scrollXOffset=scrollXOffset;
};
DrawMap(scrollX, scrollY, scrollXOffset, scrollYOffset);
}
//-----------------------------------------------------------------------------
// Name: DrawMap()
// Desc: Draws the isometric map
//-----------------------------------------------------------------------------
void CGame::DrawMap(int worldX, int worldY, float offsetX, float offsetY)
{
float screenX; // Where are we currently on the screen?
float screenY=-16;
int whereX=worldX; // Where are we currently rendering on the map?
int whereY=worldY;
int i, j=0, counter=0;
static CUSTOMVERTEX vertices[4];
// Setup texture alignment
vertices[0].u = 0.0f;
vertices[0].v = 0.0f;
vertices[1].u = 1.0f;
vertices[1].v = 0.0f;
vertices[2].u = 0.0f;
vertices[2].v = 1.0f;
vertices[3].u = 1.0f;
vertices[3].v = 1.0f;
for(i=0; i<4; i++){
vertices[i].color = D3DCOLOR_XRGB(1, 1, 1);
vertices[i].z=1.0f;
vertices[i].rhw=1.0f;
}
g_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
// Begin the scene
g_pD3DDevice->BeginScene();
if((whereX % 2)==1)
screenY=-64;
else
screenY=-96;
while(screenY < SCREENH){
if((whereY % 2)==1)
screenX=-64;
else
screenX=-96;
while(screenX < SCREENW){
vertices[0].x = screenX + offsetX;
vertices[0].y = screenY+16 + offsetY;
vertices[1].x = screenX+32 + offsetX;
vertices[1].y = screenY + offsetY;
vertices[2].x = screenX+32 + offsetX;
vertices[2].y = screenY+32 + offsetY;
vertices[3].x = screenX+64 + offsetX;
vertices[3].y = screenY+16 + offsetY;
if(j==0){
g_pD3DDevice->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL);
j++;
}
VOID* pVertices;
if(FAILED(g_pVB->Lock(0, sizeof(vertices), (void**)&pVertices, 0)))
MessageBox(NULL, "EREORER", "MEMCPY", NULL);
memcpy(pVertices, vertices, sizeof(vertices));
g_pVB->Unlock();
Render();
// Make sure locations are correct
screenX+=64;
whereX++;
};
whereY++;
whereX=whereX;
screenY+=16;
};
g_pD3DDevice->EndScene();
// Present the backbuffer contents to the display
g_pD3DDevice->Present(NULL, NULL, NULL, NULL);
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
void CGame::Render()
{
ProcessKeyboard();
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
g_pD3DDevice->SetStreamSource(0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pD3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
g_pD3DDevice->SetTexture(0, pTileTex);
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
}