File: netvariables.cpp

package info (click to toggle)
clanlib 0.5.4-1-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 10,320 kB
  • ctags: 10,893
  • sloc: cpp: 76,056; xml: 3,281; sh: 2,961; perl: 1,204; asm: 837; makefile: 775
file content (157 lines) | stat: -rw-r--r-- 4,047 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
/*
	$Id: netvariables.cpp,v 1.4 2001/05/18 11:58:37 mbn Exp $

	------------------------------------------------------------------------
	ClanLib, the platform independent game SDK.

	This library is distributed under the GNU LIBRARY GENERAL PUBLIC LICENSE
	version 2. See COPYING for details.

	For a total list of contributers see CREDITS.

	------------------------------------------------------------------------
*/

#include "API/Core/System/cl_assert.h"
#include "API/Network/netvariables.h"
#include "netvariables_generic.h"
#include <vector>

/////////////////////////////////////////////////////////////////////////////
// CL_NetVariables Construction:

CL_NetVariables::CL_NetVariables()
: impl(new CL_NetVariables_Generic)
{
}

CL_NetVariables::~CL_NetVariables()
{
	delete impl;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetVariables Attributes:

bool CL_NetVariables::is_different()
{
	std::list<CL_NetVariable *>::iterator it;
	for (it = impl->vars.begin(); it != impl->vars.end(); it++)
	{
		CL_NetVariable *var = *it;
		if (var->is_different()) return true;
	}

	return false;
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetVariables Operations:

void CL_NetVariables::add_bool(bool *var, int array)
{
	impl->vars.push_back(new CL_NetVariableT<bool>(var, array));
}

void CL_NetVariables::add_int(int *var, int array)
{
	impl->vars.push_back(new CL_NetVariableT<int>(var, array));
}

void CL_NetVariables::add_short(short *var, int array)
{
	impl->vars.push_back(new CL_NetVariableT<short>(var, array));
}

void CL_NetVariables::add_float(float *var, int array)
{
	impl->vars.push_back(new CL_NetVariableT<float>(var, array, false));
}

void CL_NetVariables::add_double(double *var, int array)
{
	impl->vars.push_back(new CL_NetVariableT<double>(var, array, false));
}

void CL_NetVariables::add_vars(CL_NetVariables *variables, int array)
{
	for (int i=0; i<array; i++)
	{
		std::list<CL_NetVariable *>::iterator it;
		for (it = variables[i].impl->vars.begin(); it != variables[i].impl->vars.end(); it++)
		{
			CL_NetVariable *var = *it;
			impl->vars.push_back(var->clone());
		}
	}
}

void CL_NetVariables::save_all(class CL_OutputSource *msg)
{
	std::list<CL_NetVariable *>::iterator it;
	for (it = impl->vars.begin(); it != impl->vars.end(); it++)
	{
		CL_NetVariable *var = *it;
		var->save(msg);
	}
}

void CL_NetVariables::load_all(class CL_InputSource *msg)
{
	std::list<CL_NetVariable *>::iterator it;
	for (it = impl->vars.begin(); it != impl->vars.end(); it++)
	{
		CL_NetVariable *var = *it;
		var->load(msg);
	}
}

void CL_NetVariables::save_diff(class CL_OutputSource *msg)
{
	std::vector<unsigned char> diff_bits;
	std::list<CL_NetVariable *>::iterator it;
	int bitpos;

	if (impl->vars.empty()) return;

	// Create diff bits.
	// For every 8 netvariables, we have a byte indicating which ones
	// have changed. If the bit is set, it means we saved the variable.

	diff_bits.resize((impl->vars.size()+7) / 8);
	for (it = impl->vars.begin(), bitpos = 0; it != impl->vars.end(); it++, bitpos++)
	{
		CL_NetVariable *var = *it;
		if (var->is_different()) diff_bits[bitpos/8] |= (1 << (bitpos % 8));
	}

	// Save the variables, and store diffbit for every eight variable.
	for (it = impl->vars.begin(), bitpos = 0; it != impl->vars.end(); it++, bitpos++)
	{
		if (bitpos % 8 == 0) msg->write_uchar8(diff_bits[bitpos/8]);

		CL_NetVariable *var = *it;
		if (var->is_different()) var->save(msg);
	}
}

void CL_NetVariables::load_diff(class CL_InputSource *msg)
{
	std::list<CL_NetVariable *>::iterator it;
	int bitpos;
	unsigned char diff_bits = 0;

	if (impl->vars.empty()) return;

	for (it = impl->vars.begin(), bitpos = 0; it != impl->vars.end(); it++, bitpos++)
	{
		if (bitpos % 8 == 0) diff_bits = msg->read_uchar8();

		CL_NetVariable *var = *it;
		if ((diff_bits & 1)) var->load(msg);
		diff_bits >>= 1;
	}
}

/////////////////////////////////////////////////////////////////////////////
// CL_NetVariables Implementation: