bitwise where

Pages: 12
Where did he get his numbers from e.g. 0x01, 0x02 etc.

0x1 is the value of an integer when only the least significant bit is set to 1, i.e.

00000001

0x2 is the value of an integer when only the second least significant bit is set to 1, i.e.

00000010

etc, etc.

They're not addresses, they're just hex numbers.
@technologist here there is an example that use enum

layermask.manager
1
2
3
4
5

ENTITY = 1,
FX,
TERRAIN,


main.cpp
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

class LayerManager
{
public:
	typedef signed __int8 tLayerMaskType;
	enum LayerMask : tLayerMaskType
	{
		#include "layermask.manager"
	};
};

class LayerableObject
{
protected:
	LayerManager::tLayerMaskType m_layerMask;
	LayerableObject() : m_layerMask(0x00000000){}
public:
	void addLayer(LayerManager::LayerMask layer)
	{
		if (layer > 31)	return;
		m_layerMask |= (0x1 << layer);
	}
	void removeLayer(LayerManager::LayerMask layer)
	{
		if (layer > 31)	return;
		m_layerMask &= ~(0x1 << layer);
	}
	void toggleLayer(LayerManager::LayerMask layer)
	{
		if (layer > 31)	return;
		m_layerMask ^= (0x1 << layer);
	}
	LayerManager::LayerMask getLayerMask() const
	{
		return (LayerManager::LayerMask)m_layerMask;
	}
};

class EntityModel 
	: public LayerableObject
{
public:
	EntityModel()
		: LayerableObject()
	{
	}
	void renderGeometry(const LayerManager::LayerMask cameraCullingMask)
	{
		if (cameraCullingMask & m_layerMask)
		{
                     //do something here
		}
	}
};

class Camera 
	: public LayerableObject
{
public:
	Camera()
		: LayerableObject()
	{
	}
};



int main()
{

	Camera mainCamera;

	mainCamera.addLayer(LayerManager::ENTITY);
	mainCamera.addLayer(LayerManager::TERRAIN);

	EntityModel model;

	model.addLayer(LayerManager::ENTITY);

	model.renderGeometry(mainCamera.getLayerMask());

	model.removeLayer(LayerManager::ENTITY);
	
	model.addLayer(LayerManager::TERRAIN);

	model.renderGeometry(mainCamera.getLayerMask());
	
	mainCamera.removeLayer(LayerManager::ENTITY);

	model.renderGeometry(mainCamera.getLayerMask());

	mainCamera.addLayer(LayerManager::FX);

	model.renderGeometry(mainCamera.getLayerMask());

	mainCamera.removeLayer(LayerManager::TERRAIN);

	model.renderGeometry(mainCamera.getLayerMask());

	return 0;
}
Last edited on
sorry.

where should this go?

layermask.manager

ENTITY = 1,
FX,
TERRAIN,
in a file called layermask.manager

you add the layers

is it a header file? something more generic? Where does it belong in the src environment.
Sorry
layermask.manager where does it go? Where do I put this file? This is a very good example.
Last edited on
among with the other files.

you might as well simply copy the content into the enum declaration.
If you're really interested, checkout the book "Hackers Delight". Its a whole book devoted to enumerating uses of bitwise operations to perform different functions.
Topic archived. No new replies allowed.
Pages: 12