summaryrefslogtreecommitdiff
path: root/game.cpp
blob: 0ebefed430bed8d4c25947c7d427b8deec2f64c5 (plain)
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
#include "game.h"
#include "video.h"
#include "messages.h"

#include <boost/lexical_cast.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>

Game* Game::game = NULL;

Game::Game() : socket(io_service) {
	scene = new Scene();
}

Game::~Game() {
	delete scene;
}

void Game::run(const std::string host, const unsigned int port) {
	run(host, boost::lexical_cast<std::string>(port));
}

void Game::run(const std::string host, const std::string port) {
	SDL_WarpMouse(video::width/2, video::height/2);
	scene->last_time = SDL_GetTicks();
	scene->update();

	{
		boost::asio::ip::tcp::resolver resolver(io_service);
		boost::asio::ip::tcp::resolver::query query(host, port);
		boost::asio::ip::tcp::resolver::iterator iterator = resolver.resolve(query);
		socket.connect(boost::asio::ip::tcp::endpoint(*iterator));

		message::Hello h(1);
		h.send(socket);
	}

	async_read();

	unsigned int last = SDL_GetTicks();
	while(scene->running) {
		if(SDL_GetTicks() - last >= 1000) {
			message::Pos pos(scene->pos.x, scene->pos.y, scene->pos.z);
			pos.send(socket);
			last = SDL_GetTicks();
		}
		io_service.poll_one();
		scene->events();
		scene->render();

		SDL_Delay(1);
	}
}

void Game::async_read() {
	uint8_t *t = new uint8_t;
	boost::asio::async_read(socket, boost::asio::buffer(t, sizeof(uint8_t)),
			boost::asio::transfer_all(),
			boost::bind(&Game::handle_type, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, t));
}

void Game::handle_type(const boost::system::error_code& error, std::size_t bytes_transferred, uint8_t *type) {
	if(error) {
		std::cerr << error << std::endl;
		delete type;
		scene->running = false;
		return;
	}

	switch(*type) {
		case message::MSG_TYPE_CHUNK:
			handle_chunk();
			break;
		case message::MSG_TYPE_MSG:
			handle_message();
			break;
		default:
			std::cout << "unknown type: " << (int)*type << std::endl;
	}

	delete type;
	async_read();
}

void Game::handle_chunk() {
	message::Chunk m;

	// coords
	m.read(socket);
	int64_t x, y;
	m.get_coords(x, y);

	// data
	m.read(socket);
	float *data = m.get_data();
	scene->terrain->tc->add_chunk(data, x, y, Terrain::chunk_size_total, Terrain::chunk_size_total);

	Terrain::Chunk *chunk = NULL;
	try {
		chunk = new Terrain::Chunk(scene->terrain, x, y);
	} catch(...) {
	}
	if(chunk)
		scene->terrain->chunks.push_back(chunk);
}

void Game::handle_message() {
	message::Message m;

	m.read(socket);
	m.get_len();

	m.read(socket);
	std::string s = m.get_str();

	scene->chat->add_line(s);
}

Game& Game::get_instance() {
	if(!game)
		game = new Game();
	return *game;
}