File: widelands_geometry_io.cc

package info (click to toggle)
widelands 1:19+repack-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 370,608 kB
  • ctags: 20,609
  • sloc: cpp: 108,404; ansic: 18,695; python: 5,155; sh: 487; xml: 460; makefile: 233
file content (108 lines) | stat: -rw-r--r-- 3,006 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2006-2015 by the Widelands Development Team
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "logic/widelands_geometry_io.h"

#include "io/streamread.h"
#include "io/streamwrite.h"

namespace Widelands {

Direction read_direction_8(StreamRead* fr) {
	uint8_t const d = fr->unsigned_8();
	if (d == 0)
		throw DirectionIsNull();
	if (6 < d)
		throw DirectionInvalid(d);
	return d;
}

Direction read_direction_8_allow_null(StreamRead* fr) {
	uint8_t const d = fr->unsigned_8();
	if (6 < d)
		throw DirectionInvalid(d);
	return d;
}

MapIndex read_map_index_32(StreamRead* fr, const MapIndex max) {
	uint32_t const i = fr->unsigned_32();
	if (max <= i)
		throw ExceededMaxIndex(max, i);
	return i;
}

Coords read_coords_32(StreamRead* stream_read) {
	uint16_t const x = stream_read->unsigned_16();
	uint16_t const y = stream_read->unsigned_16();
	return Coords(x, y);
}

Coords read_coords_32(StreamRead* stream_read, const Extent& extent) {
	uint16_t const x = stream_read->unsigned_16();
	uint16_t const y = stream_read->unsigned_16();
	if (extent.w <= x)
		throw ExceededWidth(extent.w, x);
	if (extent.h <= y)
		throw ExceededHeight(extent.h, y);
	return Coords(x, y);
}

Coords read_coords_32_allow_null(StreamRead* fr, const Extent& extent) {
	uint16_t const x = fr->unsigned_16();
	uint16_t const y = fr->unsigned_16();
	const Coords result(x, y);
	if (result) {
		if (extent.w <= x)
			throw ExceededWidth(extent.w, x);
		if (extent.h <= y)
			throw ExceededHeight(extent.h, y);
	}
	return result;
}

Area<Coords, uint16_t> read_area_48(StreamRead* fr, const Extent& extent) {
	Coords const c = read_coords_32(fr, extent);
	uint16_t const r = fr->unsigned_16();
	return Area<Coords, uint16_t>(c, r);
}

void write_direction_8(StreamWrite* wr, Direction const d) {
	assert(0 < d);
	assert(d <= 6);
	wr->data(&d, 1);
}

void write_direction_8_allow_null(StreamWrite* wr, Direction const d) {
	assert(d <= 6);
	wr->data(&d, 1);
}

void write_coords_32(StreamWrite* wr, const Coords& c) {
	assert(static_cast<uint16_t>(c.x) < 0x8000 || c.x == -1);
	assert(static_cast<uint16_t>(c.y) < 0x8000 || c.y == -1);
	wr->unsigned_16(c.x);
	wr->unsigned_16(c.y);
}

void write_area_48(StreamWrite* wr, const Area<Coords, uint16_t>& area) {
	write_coords_32(wr, area);
	wr->unsigned_16(area.radius);
}

}  // namespace Widelands