-
Notifications
You must be signed in to change notification settings - Fork 0
/
World.h
114 lines (97 loc) · 2.44 KB
/
World.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <d3dx9math.h>
#include "renderer\Mesh.h"
#include <assert.h>
typedef unsigned int MID;
const unsigned int INVALID_MID = UINT_MAX;
struct MeshArrayIndex {
MID id;
unsigned short index;
unsigned short next;
};
struct MeshArray {
int num;
int total;
MeshArrayIndex* indices;
MID* ids;
Vector3f* positions;
Vector3f* scales;
Vector3f* rotations;
D3DXMATRIX* worlds;
MeshData** data;
int* shaders;
char* buffer;
unsigned short free_enqueue;
unsigned short free_dequeue;
MeshArray() : num(0) , total(0) , buffer(0) {}
void clear() {
if ( buffer != 0 ) {
for ( unsigned short i = 0; i < total; ++i ) {
indices[i].id = i;
indices[i].next = i + 1;
}
num = 0;
free_dequeue = 0;
free_enqueue = total - 1;
}
}
MID create(const Vector3f& pos,MeshData* _data,int shader = -1) {
MeshArrayIndex &in = indices[free_dequeue];
free_dequeue = in.next;
in.index = num++;
ids[in.index] = in.id;
data[in.index] = _data;
positions[in.index] = pos;
scales[in.index] = Vector3f(1.0f,1.0f,1.0f);
rotations[in.index] = Vector3f(0.0f,0.0f,0.0f);
shaders[in.index] = shader;
return in.id;
}
void remove(MID id) {
MeshArrayIndex &in = indices[id];
assert(in.index != USHRT_MAX);
MID currentID = ids[num - 1];
MeshArrayIndex& next = indices[currentID];
ids[in.index] = ids[next.index];
data[in.index] = data[next.index];
positions[in.index] = positions[next.index];
scales[in.index] = scales[next.index];
rotations[in.index] = rotations[next.index];
shaders[in.index] = shaders[next.index];
--num;
indices[currentID].index = in.index;
in.index = USHRT_MAX;
indices[free_enqueue].next = id;
free_enqueue = id;
}
void translate(MID id,const Vector3f& p) {
MeshArrayIndex &in = indices[id];
assert(in.index != USHRT_MAX);
positions[in.index] = p;
}
void scale(MID id,const Vector3f& s) {
MeshArrayIndex &in = indices[id];
assert(in.index != USHRT_MAX);
scales[in.index] = s;
}
void rotate(MID id,const Vector3f& r) {
MeshArrayIndex &in = indices[id];
assert(in.index != USHRT_MAX);
rotations[in.index] = r;
}
};
class World {
public:
World();
~World(void);
MID create(const Vector3f& pos,MeshData* meshData,int shader = -1);
void remove(MID id);
void render();
void tick(float dt);
void translate(MID id,const Vector3f& p);
void scale(MID id,const Vector3f& p);
void rotate(MID id,const Vector3f& r);
private:
void allocate(int size);
MeshArray _array;
};