Sign in to follow this  
  • entries
    132
  • comments
    99
  • views
    88618

Editable, Removable Tiles

Sign in to follow this  
Driv3MeFar

202 views

I've added preliminary support for removing and editing tiles from the map. I say preliminary because I'm not all that happy about the implementation, but at least I know it works.

Pic:


Code:
Renderer.h

#ifndef RENDERER_H
#define RENDERER_H

#define D3D_DEBUG_INFO
#include
#include
#include
#include

#include "../Header Files/GeometryBuffer.h"
#include "../Header Files/Texture.h"

#include "../../Main/Header Files/Entity.h"
#include "../../Main/Header Files/Types.h"
#include "Camera.h"

namespace ISO
{
namespace UI
{
class Button;
}

namespace Graphics
{
class Renderer : public ISO::Base::BaseEntity
{
public:
Renderer(HWND hWnd);
~Renderer();

void Render();

void InitProjection(float windowWidth, float windowHeight, float zNear, float zFar);

std::pair<int, int> AddTile(float bX, float bY, float bZ, float tX, float tY, float tZ);
int RemoveTile(int buffer, int index);
int EditTile(int buffer, int index, float bX, float bY, float bZ, float tX, float tY, float tZ);

int AddButton(UI::Button *b, float bX, float bY, float tX, float tY);

int AddTexture(Texture *tex);

virtual bool IsA(ISO::Base::TYPE t) { return t == ISO::Base::RENDERER; }

void DrawFrame(float, float);

LPDIRECT3DDEVICE9 GetDevice() const;
private:
Camera m_Cam;

LPDIRECT3D9 m_D3D;
LPDIRECT3DDEVICE9 m_D3DDevice;

D3DXMATRIX m_MatProj;

typedef std::vector< std::pair *, UI::Button *> > ButtonBuffer;

std::vector< GeometryBuffer *> m_GeometryBuffers;
ButtonBuffer m_UIBuffers;
std::vector m_TextureBuffer;
bool m_NeedToDraw;
};
}
}

#endif



Renderer.cpp

#define D3D_DEBUG_INFO
#include
#include
#include
#include
#include "../Header Files/Renderer.h"
#include "../Header Files/GeometryBuffer.h"
#include "../../Main/Header Files/Event.h"
#include "../../UI/Header Files/Button.h"

extern ISO::Base::EventManager *g_Events;

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

LPDIRECT3DTEXTURE9 tex;

ISO::Graphics::Renderer::Renderer(HWND hWnd)
: m_D3D(NULL),
m_D3DDevice(NULL),
m_NeedToDraw(false)
{
m_D3D = ::Direct3DCreate9(D3D_SDK_VERSION);
if (!m_D3D)
{
return;
}

D3DPRESENT_PARAMETERS params = {0};
params.Windowed = TRUE;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.BackBufferFormat = D3DFMT_UNKNOWN;
params.EnableAutoDepthStencil = TRUE;
params.AutoDepthStencilFormat = D3DFMT_D16;


m_D3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
¶ms,
&m_D3DDevice );
if (!m_D3DDevice)
{
m_D3D->Release();
}

D3DXCreateTextureFromFile(m_D3DDevice, "tex1.png", &tex);


m_D3DDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
m_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
//m_D3DDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
//m_D3DDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
//hack: fixed iso camera
//end hack

g_Events->RegisterEventHandler("draw", (Renderer*)this, &Renderer::DrawFrame);
}

ISO::Graphics::Renderer::~Renderer()
{
for (unsigned i = 0; i < m_GeometryBuffers.size(); ++i)
{
delete m_GeometryBuffers;
}

for (unsigned i = 0; i < m_UIBuffers.size(); ++i)
{
delete m_UIBuffers.first;
delete m_UIBuffers.second;
}

for (unsigned i = 0; i < m_TextureBuffer.size(); ++i)
{
delete m_TextureBuffer;
}

if (m_D3DDevice)
{
m_D3DDevice->Release();
m_D3DDevice = NULL;
}

if (m_D3D)
{
m_D3D->Release();
m_D3D = NULL;
}

}
void ISO::Graphics::Renderer::Render()
{
if( NULL == m_D3DDevice )
return;

if (m_NeedToDraw)
{
// Clear the backbuffer to a blue color
m_D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( m_D3DDevice->BeginScene() ) )
{
m_D3DDevice->SetTexture(0, tex);
m_D3DDevice->SetTransform(D3DTS_PROJECTION, &m_MatProj);
D3DXMATRIX view;
D3DXVECTOR3 eye = m_Cam.GetEyeVec();
D3DXVECTOR3 at = m_Cam.GetAtVec();
D3DXVECTOR3 up = m_Cam.GetUpVec();
D3DXMatrixLookAtLH(&view, &eye, &at, &up );
m_D3DDevice->SetTransform(D3DTS_VIEW, &view);
for(unsigned i = 0; i < m_GeometryBuffers.size(); ++i)
{
m_D3DDevice->SetFVF(m_GeometryBuffers->GetFVF());
m_D3DDevice->SetStreamSource(0, m_GeometryBuffers->GetVertexBufferPointer(), 0, sizeof(MapVertex));
m_D3DDevice->SetIndices(m_GeometryBuffers->GetIndexBufferPointer());
HRESULT hr = m_D3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
m_GeometryBuffers->GetNumVertices(),
0,
m_GeometryBuffers->GetNumPrimitives() );
if (FAILED(hr))
{
assert(0);
}
}

for (ButtonBuffer::iterator it = m_UIBuffers.begin(); it != m_UIBuffers.end(); ++it)
{
m_D3DDevice->SetTexture(0, m_TextureBuffer[(*it).second->GetTextureIndex()]->GetTexturePointer());
m_D3DDevice->SetFVF((*it).first->GetFVF());
m_D3DDevice->SetStreamSource(0, (*it).first->GetVertexBufferPointer(), 0, sizeof(UIVertex));
m_D3DDevice->SetIndices((*it).first->GetIndexBufferPointer());
HRESULT hr = m_D3DDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
0,
0,
(*it).first->GetNumVertices(),
0,
(*it).first->GetNumPrimitives() );
if (FAILED(hr))
{
assert(0);
}
}

// End the scene
m_D3DDevice->EndScene();
}

// Present the backbuffer contents to the display
m_D3DDevice->Present( NULL, NULL, NULL, NULL );

m_NeedToDraw = false;
}
}

void ISO::Graphics::Renderer::InitProjection(float windowWidth, float windowHeight, float zNear, float zFar)
{
D3DXMatrixOrthoLH( &m_MatProj,
windowWidth,
windowHeight,
zNear,
zFar );
}

std::pair<int, int> ISO::Graphics::Renderer::AddTile(float bX, float bY, float bZ, float tX, float tY, float tZ)
{
MapVertex verts[] =
{
{bX, bY, bZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 1.f},
{bX, tY, bZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .33f, .66f},
{bX, tY, tZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .33f, .33f},
{bX, bY, tZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 0.f},
{tX, bY, tZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 0.f},
{tX, bY, bZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 1.f},
{tX, tY, bZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .66f, .66f},
{tX, tY, tZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .66f, .33f}
};

short indices[] = { 0, 1, 2,
0, 2, 3,
3, 2, 7,
3, 7, 4,
4, 7, 6,
4, 6, 5,
5, 6, 1,
5, 1, 0,
6, 7, 2,
6, 2, 1,
4, 5, 0,
4, 0, 3 };

if (m_GeometryBuffers.empty() ||
m_GeometryBuffers.back()->GetNumVertices() > 792)
{
m_GeometryBuffers.push_back(new GeometryBuffer(m_D3DDevice, 800, 3200));
}

assert(!m_GeometryBuffers.empty());
assert(m_GeometryBuffers.back()->GetNumVertices() <= 792);

int i = m_GeometryBuffers.back()->AddGeometry(verts, 8, indices, 36);

return std::pair<int, int>((int)m_GeometryBuffers.size() - 1, i);
}

int ISO::Graphics::Renderer::RemoveTile(int buffer, int index)
{
int IBuffIndex = index * 4;
return m_GeometryBuffers[buffer]->RemoveGeometry(index, 8, IBuffIndex, 36);
}

int ISO::Graphics::Renderer::EditTile(int buffer, int index, float bX, float bY, float bZ, float tX, float tY, float tZ)
{
MapVertex verts[] =
{
{bX, bY, bZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 1.f},
{bX, tY, bZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .33f, .66f},
{bX, tY, tZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .33f, .33f},
{bX, bY, tZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 0.f},
{tX, bY, tZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 0.f},
{tX, bY, bZ, 0.f, 0.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 1.f},
{tX, tY, bZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .66f, .66f},
{tX, tY, tZ, 0.f, 1.f, 0.f, D3DCOLOR_XRGB(255, 255, 255), .66f, .33f}
};

return m_GeometryBuffers[buffer]->EditGeometry(index, verts, 8);
}

int ISO::Graphics::Renderer::AddButton(ISO::UI::Button *b, float bX, float bY, float tX, float tY)
{
UIVertex verts[] =
{
{bX, bY, 0.5f, 1.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 0.f},
{bX, tY, 0.5f, 1.f, D3DCOLOR_XRGB(255, 255, 255), 0.f, 1.f},
{tX, tY, 0.5f, 1.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 1.f},
{tX, bY, 0.5f, 1.f, D3DCOLOR_XRGB(255, 255, 255), 1.f, 0.f}
};

short indices[] = { 0, 1, 2,
0, 2, 3 };

GeometryBuffer *buffer = new GeometryBuffer(m_D3DDevice, 4, 6);
buffer->AddGeometry(verts, 4, indices, 6);

m_UIBuffers.push_back(ButtonBuffer::value_type(buffer, b));

return (int)m_UIBuffers.size() - 1;
}



int ISO::Graphics::Renderer::AddTexture(ISO::Graphics::Texture *tex)
{
if (std::find(m_TextureBuffer.begin(), m_TextureBuffer.end(), tex) != m_TextureBuffer.end())
{
//find curr index
for (unsigned i = 0; i < m_TextureBuffer.size(); ++i)
{
if (m_TextureBuffer == tex)
{
return static_cast<int>(i);
}
}
}
m_TextureBuffer.push_back(tex);
return static_cast<int>(m_TextureBuffer.size() - 1);
}

void ISO::Graphics::Renderer::DrawFrame(float, float)
{
m_NeedToDraw = true;
}

LPDIRECT3DDEVICE9 ISO::Graphics::Renderer::GetDevice() const
{
return m_D3DDevice;
}



VertexBuffer.h

#ifndef VERTEXBUFFER_H
#define VERTEXBUFFER_H

#include
#include
#include

namespace ISO
{
namespace Graphics
{
struct MapVertex
{
float x, y, z; //position
float Nx, Ny, Nz; //normal
D3DCOLOR color; //diffuse color
float u, v; //tex coords
static const DWORD FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1;
};

struct UIVertex
{
float x, y, z, w;
D3DCOLOR color; //diffuse color
float u, v;
static const DWORD FVF = D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1;
};

template <class T>
class VertexBuffer
{
public:
VertexBuffer(LPDIRECT3DDEVICE9 device, int size)
: m_VertexBuffer(NULL),
m_Index(0)
{
assert(size > 0);
HRESULT hr = device->CreateVertexBuffer( size * sizeof(T),
D3DUSAGE_WRITEONLY,
T::FVF,
D3DPOOL_DEFAULT,
&m_VertexBuffer,
NULL );
if (FAILED(hr))
{
return;
}
}
~VertexBuffer()
{
if (m_VertexBuffer)
{
m_VertexBuffer->Release();
}
}

int AddVertices(T *verts, int numVerts)
{
assert(m_VertexBuffer);

T *buffer;
HRESULT hr = m_VertexBuffer->Lock(m_Index * sizeof(T), numVerts * sizeof(T), (void**)&buffer, 0);

if (FAILED(hr))
{
return -1;
}

std::copy(verts, verts + numVerts, buffer);

hr = m_VertexBuffer->Unlock();
if (FAILED(hr))
{
return -1;
}

int retVal = m_Index;
m_Index += numVerts;

return retVal;
}

int RemoveVertices(int index, int numVerts)
{
assert (m_VertexBuffer);

T* buffer;
HRESULT hr = m_VertexBuffer->Lock(index * sizeof(T), (m_Index - index) * sizeof(T), (void**)&buffer, 0);

std::copy(buffer + numVerts, buffer + (m_Index - index), buffer);

hr = m_VertexBuffer->Unlock();

m_Index -= numVerts;

return m_Index;
}

int EditVertices(int index, T* verts, int numVerts)
{
assert (m_VertexBuffer);

T* buffer;
HRESULT hr = m_VertexBuffer->Lock(index * sizeof(T), numVerts * sizeof(T), (void**)&buffer, 0);

std::copy(verts, verts + numVerts, buffer);

hr = m_VertexBuffer->Unlock();

return index;
}

LPDIRECT3DVERTEXBUFFER9 GetVertexBufferPointer()
{
assert(m_VertexBuffer);
return m_VertexBuffer;
}

const int GetCurrentIndex()
{
return m_Index;
}

private:
LPDIRECT3DVERTEXBUFFER9 m_VertexBuffer;
int m_Index;
};
}
}

#endif

Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now