source:
network-game/common/Player.cpp@
60776f2
| Last change on this file since 60776f2 was 60776f2, checked in by , 13 years ago | |
|---|---|
|
|
| File size: 1.1 KB | |
| Rev | Line | |
|---|---|---|
| [8e540f4] | 1 | #include "Player.h" |
| [2488852] | 2 | |
| 3 | #include <iostream> | |
| [3b8adee] | 4 | #include <sstream> |
| 5 | #include <cstring> | |
| [2488852] | 6 | |
| 7 | using namespace std; | |
| 8 | ||
| [59061f6] | 9 | Player::Player(string name, string password) |
| 10 | { | |
| 11 | this->name = name; | |
| 12 | this->password = password; | |
| [edfd1d0] | 13 | this->pos.x = 200; |
| 14 | this->pos.y = 200; | |
| [59061f6] | 15 | |
| 16 | cout << "Created new player: " << this->name << endl; | |
| 17 | } | |
| 18 | ||
| [8e540f4] | 19 | Player::Player(string name, sockaddr_in addr) |
| [2488852] | 20 | { |
| 21 | this->name = name; | |
| [59061f6] | 22 | this->password = ""; |
| [edfd1d0] | 23 | this->pos.x = 200; |
| 24 | this->pos.y = 200; | |
| [2488852] | 25 | this->addr = addr; |
| 26 | ||
| 27 | cout << "Created new played: " << this->name << endl; | |
| 28 | } | |
| 29 | ||
| [8e540f4] | 30 | Player::~Player() |
| [2488852] | 31 | { |
| 32 | } | |
| 33 | ||
| [3b8adee] | 34 | void Player::serialize(char* buffer) |
| [59061f6] | 35 | { |
| [3b8adee] | 36 | ostringstream oss; |
| 37 | ||
| 38 | oss << this->name; | |
| [60776f2] | 39 | oss.write((char*)&(this->pos.x), sizeof(int)); |
| 40 | oss.write((char*)&(this->pos.y), sizeof(int)); | |
| [3b8adee] | 41 | |
| [60776f2] | 42 | memcpy(buffer, oss.str().c_str(), this->name.length()+1+2*sizeof(int)); |
| [59061f6] | 43 | } |
| [edfd1d0] | 44 | |
| [3b8adee] | 45 | void Player::deserialize(char* buffer) |
| [edfd1d0] | 46 | { |
| [3b8adee] | 47 | istringstream iss; |
| [60776f2] | 48 | iss.str(buffer); |
| [3b8adee] | 49 | |
| 50 | iss >> this->name; | |
| [60776f2] | 51 | iss.read((char*)&(this->pos.x), sizeof(int)); |
| 52 | iss.read((char*)&(this->pos.y), sizeof(int)); | |
| [3b8adee] | 53 | } |
| 54 | ||
| 55 | void Player::setAddr(sockaddr_in addr) | |
| 56 | { | |
| 57 | this->addr = addr; | |
| [edfd1d0] | 58 | } |
Note:
See TracBrowser
for help on using the repository browser.
![(please configure the [header_logo] section in trac.ini)](/medieval/chrome/site/your_project_logo.png)