File: viewport.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (221 lines) | stat: -rw-r--r-- 6,779 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "ags/engine/ac/draw.h"
#include "ags/engine/ac/game_state.h"
#include "ags/engine/debugging/debug_log.h"
#include "ags/shared/game/room_struct.h"
#include "ags/engine/game/viewport.h"
#include "ags/globals.h"

namespace AGS3 {

using namespace AGS::Shared;

void Camera::SetID(int id) {
	_id = id;
}

// Returns Room camera position and size inside the room (in room coordinates)
const Rect &Camera::GetRect() const {
	return _position;
}

// Sets explicit room camera's orthographic size
void Camera::SetSize(const Size cam_size) {
	// TODO: currently we don't support having camera larger than room background
	// (or rather - looking outside of the room background); look into this later
	const Size real_room_sz = (_G(displayed_room) >= 0 && (_GP(thisroom).Width > 0 && _GP(thisroom).Height > 0)) ?
							  Size(data_to_game_coord(_GP(thisroom).Width), data_to_game_coord(_GP(thisroom).Height)) :
							  Size(INT32_MAX, INT32_MAX);

	const Size real_size = Size::Clamp(cam_size, Size(1, 1), real_room_sz);
	if (_position.GetWidth() == real_size.Width && _position.GetHeight() == real_size.Height)
		return;

	_position.SetWidth(real_size.Width);
	_position.SetHeight(real_size.Height);
	SetAt(_position.Left, _position.Top); // readjust in case went off-room after size changed
	for (auto vp = _viewportRefs.begin(); vp != _viewportRefs.end(); ++vp) {
		auto locked_vp = vp->lock();
		if (locked_vp)
			locked_vp->AdjustTransformation();
	}
	_hasChangedSize = true;
}

// Puts room camera to the new location in the room
void Camera::SetAt(int x, int y) {
	int cw = _position.GetWidth();
	int ch = _position.GetHeight();
	int room_width = data_to_game_coord(_GP(thisroom).Width);
	int room_height = data_to_game_coord(_GP(thisroom).Height);
	x = Math::Clamp(x, 0, room_width - cw);
	y = Math::Clamp(y, 0, room_height - ch);
	if (_position.Left == x && _position.Top == y)
		return;
	_position.MoveTo(Point(x, y));
	_hasChangedPosition = true;
}

// Tells if camera is currently locked at custom position
bool Camera::IsLocked() const {
	return _locked;
}

// Locks room camera at its current position
void Camera::Lock() {
	debug_script_log("Room camera locked");
	_locked = true;
}

// Similar to SetAt, but also locks camera preventing it from following player character
void Camera::LockAt(int x, int y) {
	debug_script_log("Room camera locked to %d,%d", x, y);
	SetAt(x, y);
	_locked = true;
}

// Releases camera lock, letting it follow player character
void Camera::Release() {
	_locked = false;
	debug_script_log("Room camera released back to engine control");
}

// Link this camera to a new viewport; this does not unlink any linked ones
void Camera::LinkToViewport(ViewportRef viewport) {
	auto new_locked = viewport.lock();
	if (!new_locked)
		return;
	for (auto vp = _viewportRefs.begin(); vp != _viewportRefs.end(); ++vp) {
		auto old_locked = vp->lock();
		if (old_locked->GetID() == new_locked->GetID())
			return;
	}
	_viewportRefs.push_back(viewport);
}

// Unlinks this camera from a given viewport; does nothing if link did not exist
void Camera::UnlinkFromViewport(int id) {
	for (auto vp = _viewportRefs.begin(); vp != _viewportRefs.end(); ++vp) {
		auto locked = vp->lock();
		if (locked && locked->GetID() == id) {
			_viewportRefs.erase(vp);
			return;
		}
	}
}

const std::vector<ViewportRef> &Camera::GetLinkedViewports() const {
	return _viewportRefs;
}

void Viewport::SetID(int id) {
	_id = id;
}

void Viewport::SetRect(const Rect &rc) {
	// TODO: consider allowing size 0,0, in which case viewport is considered not visible
	Size fix_size = rc.GetSize().IsNull() ? Size(1, 1) : rc.GetSize();
	Rect new_pos = RectWH(rc.Left, rc.Top, fix_size.Width, fix_size.Height);
	if (new_pos == _position)
		return;
	_position = new_pos;
	AdjustTransformation();
	_hasChangedPosition = true;
	_hasChangedSize = true;
}

void Viewport::SetSize(const Size sz) {
	// TODO: consider allowing size 0,0, in which case viewport is considered not visible
	Size fix_size = sz.IsNull() ? Size(1, 1) : sz;
	if (_position.GetWidth() == fix_size.Width && _position.GetHeight() == fix_size.Height)
		return;
	_position = RectWH(_position.Left, _position.Top, fix_size.Width, fix_size.Height);
	AdjustTransformation();
	_hasChangedSize = true;
}

void Viewport::SetAt(int x, int y) {
	if (_position.Left == x && _position.Top == y)
		return;
	_position.MoveTo(Point(x, y));
	AdjustTransformation();
	_hasChangedPosition = true;
}

void Viewport::SetVisible(bool on) {
	_visible = on;
	_hasChangedVisible = true;
}

void Viewport::SetZOrder(int zorder) {
	_zorder = zorder;
	_hasChangedVisible = true;
}

void Viewport::AdjustTransformation() {
	auto locked_cam = _camera.lock();
	if (locked_cam)
		_transform.Init(locked_cam->GetRect().GetSize(), _position);
}

PCamera Viewport::GetCamera() const {
	return _camera.lock();
}

void Viewport::LinkCamera(PCamera cam) {
	_camera = cam;
	AdjustTransformation();
}

VpPoint Viewport::RoomToScreen(int roomx, int roomy, bool clip) const {
	auto cam = _camera.lock();
	if (!cam)
		return std::make_pair(Point(), -1);
	const Rect &camr = cam->GetRect();
	Point screen_pt = _transform.Scale(Point(roomx - camr.Left, roomy - camr.Top));
	if (clip && !_position.IsInside(screen_pt))
		return std::make_pair(Point(), -1);
	return std::make_pair(screen_pt, _id);
}

VpPoint Viewport::ScreenToRoom(int scrx, int scry, bool clip, bool convert_cam_to_data) const {
	Point screen_pt(scrx, scry);
	if (clip && !_position.IsInside(screen_pt))
		return std::make_pair(Point(), -1);
	auto cam = _camera.lock();
	if (!cam)
		return std::make_pair(Point(), -1);

	const Rect &camr = cam->GetRect();
	Point p = _transform.UnScale(screen_pt);
	if (convert_cam_to_data) {
		p.X += game_to_data_coord(camr.Left);
		p.Y += game_to_data_coord(camr.Top);
	} else {
		p.X += camr.Left;
		p.Y += camr.Top;
	}
	return std::make_pair(p, _id);
}

} // namespace AGS3