Skip to content

Commit 4287fb4

Browse files
committed
Implement DRAW_DrawLines in TRA
1 parent 01f65ef commit 4287fb4

File tree

7 files changed

+199
-28
lines changed

7 files changed

+199
-28
lines changed

src/cdc/render/PCPrimitivePool.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "PCPrimitivePool.h"
2+
#include "util/Hooking.h"
3+
4+
void* cdc::PCPrimitivePool::AllocateVertices(unsigned int numVerts)
5+
{
6+
unsigned int baseOffset;
7+
auto vertices = m_vertexPool.Create(numVerts, &baseOffset);
8+
auto offset = 0;
9+
10+
auto currentPrimitive = m_currentPrimitive->currentPrimitive;
11+
12+
if (currentPrimitive && currentPrimitive->vertexBuffer == vertices)
13+
{
14+
offset = currentPrimitive->numVertices * m_stride;
15+
currentPrimitive->numVertices += numVerts;
16+
}
17+
else
18+
{
19+
auto primitive = (PartialPrimitive*)m_pFrameHeap->Alloc(sizeof(PartialPrimitive));
20+
21+
primitive->baseOffset = baseOffset;
22+
primitive->next = nullptr;
23+
primitive->numVertices = numVerts;
24+
primitive->vertexBuffer = vertices;
25+
26+
if (m_currentPrimitive->currentPrimitive)
27+
{
28+
m_currentPrimitive->currentPrimitive->next = primitive;
29+
}
30+
else
31+
{
32+
m_currentPrimitive->firstPrimitive = primitive;
33+
}
34+
35+
m_currentPrimitive->currentPrimitive = primitive;
36+
}
37+
38+
m_currentPrimitive->totalNumVertices += numVerts;
39+
m_totalNumVertices += numVerts;
40+
41+
return (char*)vertices->m_pVertexData + offset;
42+
}

src/cdc/render/PCPrimitivePool.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#pragma once
2+
3+
#include <d3d9.h>
4+
5+
#include "TransientHeapAllocator.h"
6+
#include "PCVertexPool.h"
7+
8+
namespace cdc
9+
{
10+
class PCPrimitivePool
11+
{
12+
private:
13+
TransientHeapAllocator* m_pFrameHeap;
14+
15+
PCBasicVertexPool m_vertexPool;
16+
D3DPRIMITIVETYPE m_primType;
17+
18+
unsigned int m_primVerts;
19+
unsigned int m_totalNumVertices;
20+
unsigned int m_stride;
21+
22+
struct PartialPrimitive
23+
{
24+
PCDynamicVertexBuffer* vertexBuffer;
25+
unsigned int numVertices;
26+
unsigned int baseOffset;
27+
PartialPrimitive* next;
28+
};
29+
30+
struct Primitive
31+
{
32+
PartialPrimitive* firstPrimitive;
33+
PartialPrimitive* currentPrimitive;
34+
unsigned int totalNumVertices;
35+
};
36+
37+
Primitive* m_currentPrimitive;
38+
39+
public:
40+
void* AllocateVertices(unsigned int numVerts);
41+
};
42+
}

src/cdc/render/PCVertexPool.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "PCVertexPool.h"
2+
#include "util/Hooking.h"
3+
4+
cdc::PCDynamicVertexBuffer* cdc::PCBasicVertexPool::Create(unsigned int numVertices, unsigned int* baseOffsetInVerts)
5+
{
6+
auto addr = GET_ADDRESS(0xECDE60, 0x61F840, 0x000000);
7+
8+
return Hooking::ThisCallReturn<cdc::PCDynamicVertexBuffer*>(addr, this, numVertices, baseOffsetInVerts);
9+
}

src/cdc/render/PCVertexPool.h

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma once
2+
3+
#include <d3d9.h>
4+
5+
#include "TransientHeapAllocator.h"
6+
7+
namespace cdc
8+
{
9+
class PCVertexFormat
10+
{
11+
};
12+
13+
class PCVertexBufferMemPool
14+
{
15+
};
16+
17+
class PCVertexBuffer
18+
{
19+
public:
20+
virtual ~PCVertexBuffer() = 0;
21+
22+
virtual unsigned int GetBaseVertexIndex() = 0;
23+
virtual IDirect3DVertexBuffer9* GetD3DVertexBuffer() = 0;
24+
virtual IDirect3DVertexDeclaration9* GetD3DVertexDeclaration() = 0;
25+
virtual unsigned int GetNumVertices() = 0;
26+
virtual unsigned __int16 GetStride() = 0;
27+
};
28+
29+
class PCDynamicVertexBuffer : public PCVertexBuffer
30+
{
31+
public:
32+
IDirect3DVertexBuffer9* m_pD3DVertexBuffer;
33+
void* m_pVertexData;
34+
PCVertexFormat* m_pVertexFormat;
35+
unsigned int m_baseVertexIndex;
36+
unsigned int m_numVertices;
37+
};
38+
39+
class PCBasicVertexPool
40+
{
41+
private:
42+
PCVertexFormat* m_pVertexFormat;
43+
cdc::PCVertexBufferMemPool* m_pVertexBufferMemPool;
44+
TransientHeapAllocator* m_pFrameHeap;
45+
IDirect3DVertexBuffer9* m_pLastVB;
46+
cdc::PCDynamicVertexBuffer* m_pLastSharedVB;
47+
unsigned int m_nVertices;
48+
49+
public:
50+
PCDynamicVertexBuffer* Create(unsigned int numVertices, unsigned int* baseOffsetInVerts);
51+
52+
virtual ~PCBasicVertexPool() { };
53+
};
54+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "TransientHeapAllocator.h"
2+
3+
char* cdc::TransientHeapAllocator::Alloc(unsigned int size)
4+
{
5+
auto ptr = m_freePtr;
6+
7+
if (ptr + size > m_size)
8+
{
9+
return nullptr;
10+
}
11+
12+
m_freePtr += size;
13+
return &m_pHeap[ptr];
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
3+
namespace cdc
4+
{
5+
class TransientHeapAllocator
6+
{
7+
private:
8+
char* m_pHeap;
9+
unsigned int m_size;
10+
unsigned int m_freePtr;
11+
12+
public:
13+
char* Alloc(unsigned int size);
14+
};
15+
}

src/render/Draw.cpp

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include "Draw.h"
22
#include "util/Hooking.h"
33

4+
#ifdef TRAE
5+
#include "cdc/render/PCDeviceManager.h"
6+
#include "cdc/render/PCPrimitivePool.h"
7+
#endif
8+
49
void TRANS_TransToDrawVertexV4f(DRAWVERTEX* v, cdc::Vector3* vec)
510
{
611
auto addr = GET_ADDRESS(0x402EF0, 0x402F20, 0x49F990);
@@ -40,9 +45,27 @@ void DRAW_DrawQuads(int flags, int tpage, DRAWVERTEX* verts, int numquads)
4045

4146
void DRAW_DrawLines(LINEVERTEX* verts, int numlines)
4247
{
48+
#ifndef TRAE
4349
auto addr = GET_ADDRESS(0x406120, 0x000000, 0x5BFCD0);
4450

4551
Hooking::Call(addr, verts, numlines);
52+
#else
53+
// The DRAW_DrawLines method has been optimized away in Anniversary
54+
// the code below is a reimplementation of the code from Legend
55+
if (cdc::PCDeviceManager::s_pInstance->IsStatusOk())
56+
{
57+
auto linePool = *(cdc::PCPrimitivePool**)0x7545E0;
58+
auto vertices = (LINEVERTEX*)linePool->AllocateVertices(numlines * 2);
59+
60+
for (int i = 0; i < numlines * 2; i++)
61+
{
62+
vertices[i] = verts[i];
63+
64+
// Swap RGBA to ARGB
65+
vertices[i].color = (verts[i].color << 24) | (verts[i].color >> 8);
66+
}
67+
}
68+
#endif
4669
}
4770

4871
void DRAW_DrawTriangles(int flags, int tpage, DRAWVERTEX* verts, int numtris)
@@ -111,33 +134,6 @@ void DrawPlane(cdc::Vector3* v0, cdc::Vector3* v1, int color)
111134

112135
void DrawLine(cdc::Vector3* v0, cdc::Vector3* v1, int color)
113136
{
114-
#ifdef TRAE
115-
DRAWVERTEX verts[6];
116-
117-
auto v2 = *v1;
118-
auto v3 = *v0;
119-
120-
v2.z += 20.f;
121-
v3.z += 20.f;
122-
v2.y += 20.f;
123-
v3.y += 20.f;
124-
125-
TransformToDrawVertex(verts, v0);
126-
TransformToDrawVertex(&verts[1], &v2);
127-
TransformToDrawVertex(&verts[2], v1);
128-
TransformToDrawVertex(&verts[3], &v3);
129-
TransformToDrawVertex(&verts[4], v1);
130-
TransformToDrawVertex(&verts[5], v0);
131-
132-
verts[0].color = color;
133-
verts[1].color = color;
134-
verts[2].color = color;
135-
verts[3].color = color;
136-
verts[4].color = color;
137-
verts[5].color = color;
138-
139-
DRAW_DrawTriangles(2, 0, verts, 2);
140-
#else
141137
LINEVERTEX lines[2];
142138

143139
TransformToLineVertex(lines, v0);
@@ -147,7 +143,6 @@ void DrawLine(cdc::Vector3* v0, cdc::Vector3* v1, int color)
147143
lines[1].color = color;
148144

149145
DRAW_DrawLines(lines, 1);
150-
#endif
151146
}
152147

153148
void DrawBoundingBox(cdc::Vector3* v0, cdc::Vector3* v1, int color)

0 commit comments

Comments
 (0)