File: widelands_geometry_io.h

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 (113 lines) | stat: -rw-r--r-- 3,976 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
109
110
111
112
113
/*
 * 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.
 *
 */

#ifndef WL_LOGIC_WIDELANDS_GEOMETRY_IO_H
#define WL_LOGIC_WIDELANDS_GEOMETRY_IO_H

#include "io/fileread.h"
#include "logic/widelands.h"
#include "logic/widelands_geometry.h"

namespace Widelands {

struct DirectionIsNull : public FileRead::DataError {
	DirectionIsNull()
	   : DataError("direction is 0 but must be one of {1 (northeast), 2 (east), 3 "
	               "(southeast), 4 (southwest), 5 (west), 6 (northwest)}") {
	}
};

struct DirectionInvalid : public FileRead::DataError {
	DirectionInvalid(Direction const D)
	   : DataError("direction is %u but must be one of {0 (idle), 1 (northeast), 2 "
	               "(east), 3 (southeast), 4 (southwest), 5 (west), 6 (northwest)}",
	               D),
	     direction(D) {
	}
	Direction direction;
};
struct ExceededMaxIndex : public FileRead::DataError {
	ExceededMaxIndex(MapIndex const Max, MapIndex const I)
	   : DataError("index is %u but max index is only %u", I, Max), max(Max), i(I) {
	}
	MapIndex const max, i;
};
struct ExceededWidth : public FileRead::DataError {
	ExceededWidth(uint16_t const W, const uint16_t X)
	   : DataError("x coordinate is %i but width is only %u", X, W), w(W), x(X) {
	}
	uint16_t const w;
	uint16_t const x;
};
struct ExceededHeight : public FileRead::DataError {
	ExceededHeight(uint16_t const H, const int16_t Y)
	   : DataError("y coordinate is %i but height is only %u", Y, H), h(H), y(Y) {
	}
	uint16_t h;
	int16_t y;
};

/// Read a Direction from the file. Use this when the result can only be a
/// direction.
///
/// \throws direction_is_null if the direction is 0.
/// \throws direction_invalid if direction is > 6.
Direction read_direction_8(StreamRead* fr);

/// Read a Direction from the file. Use this when the result can only be a
/// direction or 0 (none).
///
/// \throws direction_invalid if direction is > 6.
Direction read_direction_8_allow_null(StreamRead* fr);

MapIndex read_map_index_32(StreamRead* fr, MapIndex max);

/// Read a Coords from the file. Use this when the result can only be a
/// coordinate pair referring to a node.
///
/// \throws width_exceeded  if extent.w is <= the x coordinate.
/// \throws height_exceeded if extent.h is <= the y coordinate.
/// Both coordinates are read from the file before checking and possibly
/// throwing, so in case such an exception is thrown, it is guaranteed that
/// the whole coordinate pair has been read.
Coords read_coords_32(StreamRead* stream_read);

Coords read_coords_32(StreamRead* stream_read, const Extent& extent);

/// Like Coords32 but the result can have the special value indicating
/// invalidity, as defined by Coords::null.
Coords read_coords_32_allow_null(StreamRead* fr, const Extent& extent);

Area<Coords, uint16_t> read_area_48(StreamRead* fr, const Extent& extent);

// Writes 'd' to 'wr'.
void write_direction_8(StreamWrite* wr, Direction d);

// Writes 'd' into 'wr'.
void write_direction_8_allow_null(StreamWrite* wr, Direction d);

// Writes 'c' to 'wr'.
void write_coords_32(StreamWrite* wr, const Coords& c);

// Writes 'area' to 'wr'.
void write_area_48(StreamWrite* wr, const Area<Coords, uint16_t>& area);

}  // namespace Widelands

#endif  // end of include guard: WL_LOGIC_WIDELANDS_GEOMETRY_IO_H