summaryrefslogtreecommitdiffstats
path: root/src/ChunkBuffer.cpp
blob: 8e87d30496e3a3fa8d0879bbb413d0c0791433fb (plain) (blame)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146

#include "Globals.h"
#include "ChunkBuffer.h"

cChunkBuffer cChunkBuffer::Copy() const
{
	cChunkBuffer copy;
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		if(m_Sections[i])
		{
			copy.m_Sections[i] = Allocate();
			*copy.m_Sections[i] = *m_Sections[i];
		}
	}
	return copy;
}





void cChunkBuffer::CopyBlocks   (BLOCKTYPE * a_dest, size_t a_Idx, size_t length)  const
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length =  CHUNK_SECTION_HEIGHT * 16 * 16;
		if (a_Idx > 0) a_Idx = a_Idx > length ? a_Idx - length : 0;
		if (a_Idx == 0) 
		{
			size_t tocopy = length > segment_length ? segment_length : length;
			length -= tocopy;
			memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockTypes, sizeof(BLOCKTYPE) * length);
		}
	}
}





void cChunkBuffer::CopyMeta(NIBBLETYPE * a_dest) const
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockMeta, sizeof(NIBBLETYPE) * segment_length);
	}
}





void cChunkBuffer::CopyLight(NIBBLETYPE * a_dest) const
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockLight, sizeof(NIBBLETYPE) * segment_length);
	}
}





void cChunkBuffer::CopySkyLight(NIBBLETYPE * a_dest) const
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		memcpy(&a_dest[i * segment_length], &m_Sections[i]->m_BlockSkyLight, sizeof(NIBBLETYPE) * segment_length);
	}
}





void cChunkBuffer::SetBlocks(const BLOCKTYPE * a_src)
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		if (m_Sections[i])
		{
			memcpy(&m_Sections[i]->m_BlockTypes, &a_src[i * segment_length], sizeof(BLOCKTYPE) * segment_length);
		}
	} 
}




void cChunkBuffer::SetMeta(const NIBBLETYPE * a_src)
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		if (m_Sections[i])
		{
			memcpy(&m_Sections[i]->m_BlockMeta, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length);
		}
	} 
}




void cChunkBuffer::SetLight(const NIBBLETYPE * a_src)
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		if (m_Sections[i])
		{
			memcpy(&m_Sections[i]->m_BlockLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length);
		}
	} 
}




void cChunkBuffer::SetSkyLight  (const NIBBLETYPE * a_src)
{
	for (int i = 0; i < CHUNK_SECTION_NUM; i++)
	{
		const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16 / 2;
		if (m_Sections[i])
		{
			memcpy(&m_Sections[i]->m_BlockSkyLight, &a_src[i * segment_length], sizeof(NIBBLETYPE) * segment_length);
		}
	} 
}





cChunkBuffer::sChunkSection * cChunkBuffer::Allocate() const
{
	// TODO: use a allocation pool
	return new cChunkBuffer::sChunkSection;
}