2D online game development (network) (9)
Author: akinggw
Foreword
Between the blink of an eye, I came to the ninth lesson of this series of tutorials. In advance, most of these articles come from the official website or routine of Raknet. If you feel that my article is not understood, please forgive me, because I am also with you, it is also the library. In this case, I still invite you to read it on the official website.
Bit Circle Introduction
In the previous article, we have explained what a packet is also involved in bitstream, but has not described in detail. And we will briefly explain the bitstream before explaining the receiving packets.
description
BitStream is a class under the namespace RakNet, which is to have three advantages for packed and unpacking the dynamic array of packets:
1.
Dynamically set up a packet
2.
compression
3.
Write bits
need to use
BITSTEAM,
You must first predefine your structure and convert them into
(char *)
form. Dependent and specific content, you can choose to write the data block when running.
BitStream
It is also possible to compress according to the type of packet.
Compression uses the following method, more simple:
1.
Half of the above is all
0
(
1
Indicates unsigned types)? (Translator Note: I don't know if I translate it correctly, I don't understand, I am sorry.)
TRUE -
Write one
1
False -
Half of the above
0
2
. Repeat the half of the following until half of the byte
This means that if your data is halfway in its own maximum range, you will save the bitstream. This way you can use
Writecompressed
Instead
Write
Use
Readcompressed
Instead
Reta
.
Finally, you can write bits. But most of the time, you won't notice this. No matter what, when writing
Bool
When value, you will only write only one bit. This is also useful to encrypt your data.
Write data
Establish a
BitStream
, Then create a write method for each of your data types. If your data type is
Raknet
Subbling, then the correct overload function will be called. If your data type is your own type, then you have to do it below.
BitStream.write ((char *) & mytype, sizeof (mytype);
This is a version of a parameter in the constructor.
Read data
Reading data is equally simple. Establish a
BitStream
, Then assign it to your data in the constructor.
BitStream.mybitstream (Packet-> Data, Packet-> Length, False);
Main function
Constructor
BitStream (int initialbytestoallocate);
The role of this constructor is to determine how much memory is pre-allocated.
BitStream
.
Constructor
BitStream (const char * _data, unsigned int lengthtes, bool _copydata);
This version of the constructor is given
BitStream
Some initial data. This is usually used to interpret the current data stream into
BitStream,
Last parameters
_CopyData
If
False
Then the pointer will point to the current data. If
True
Then you may want to change the data and want to make an internal copy.
Write data function
The write data function is used to convert the data into a bit stream at the end of the bitstream, you will use the opposite method.
Reta
Read the number back.
Write compressed data function
It is written to BitStream after compressed data.
You can also use the opposite method
Readcompressed
Read the data back.
Read data function
reta
The data function is used to read the data from the bitstream in order. If
Bitsteam
There is no data in the middle, return
False
.
Read compressed data function
As in front, it is only the operation of the compressed data.
GetNumberofbitsused
GetNumberofbytesused
Get the number of bytes or bit numbers of the bitstream.
GetData
Give you a pointer, point to internal data. You can access data directly.
OVER!
End
I was still intended to explain how to receive the packet, but I want to play.
3D
Oh, everyone is pitiful, let me play.
3D
Well
... ..
It doesn't matter it, let's play first.
For more information, please visit the Jinqiao Science website (
http://popul.jqcq.com
The game development section, if you need a book development, please refer to Jinqiao Book City Game Channel (
http://book.jqcq.com/category/1_70_740.html
).
If you have any good suggestions when you read this article, please let me, mine.
E_mail: akinggw@126.com.
If you are using
SDL
What is wrong with it, please go to Jinqiao Scot,
http://popul.jqcq.com
) The game development section, I will answer your questions in detail.