ScummVM API documentation
Rectangle.h
1/* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22/*
23 * This code is based on the CRAB engine
24 *
25 * Copyright (c) Arvind Raja Yadav
26 *
27 * Licensed under MIT
28 *
29 */
30
31#ifndef CRAB_RECTANGLE_H
32#define CRAB_RECTANGLE_H
33
34#include "crab/Line.h"
35#include "crab/vectors.h"
36
37namespace Crab {
38
39//------------------------------------------------------------------------
40// Purpose: A simple rectangle class
41//------------------------------------------------------------------------
42struct Rect {
43
44 int x;
45 int y;
46 int w;
47 int h;
48
49 Rect(int X = 0, int Y = 0, int W = 0, int H = 0) {
50 x = X;
51 y = Y;
52 w = W;
53 h = H;
54 }
55
56 bool load(rapidxml::xml_node<char> *node, const bool &echo = true, const Common::String &x_name = "x",
57 const Common::String &y_name = "y", const Common::String &w_name = "w", const Common::String &h_name = "h");
58
59 // Is this rectangle colliding with another rectangle?
60 bool collide(Rect box) const;
61
62 // Extend this rectangle to fully contain another rectangle
63 void extend(Rect box);
64
65 // Resolving a collision means we need to correct the position of the target rectangle to just outside of the reference rectangle
66 // To do this, first we need to determine which edges of the rectangles are colliding
67
68 // Resolve a collision in the X plane
69 Direction resolveX(Rect collider);
70
71 // Resolve a collision in the Y plane
72 Direction resolveY(Rect collider);
73
74 // Does this rectangle contain a point?
75 bool contains(int x1, int y1) {
76 return (x1 > x && x1 < x + w && y1 > y && y1 < y + h);
77 }
78
79 bool contains(Vector2i v) {
80 return contains(v.x, v.y);
81 }
82
83 // Does this rectangle contain another rectangle?
84 bool contains(Rect box) {
85 return (x < box.x && x + w > box.x + box.w && y < box.y && y + h > box.y + box.h);
86 }
87
88 // Flip a rectangle with respect to an axis
89 void flip(const TextureFlipType &flip, const Vector2i &axis);
90
91 // Draw the borders of the rectangle
92 void draw(const int &xOffset = 0, const int &yOffset = 0, const uint8 &r = 0, const uint8 &g = 0, const uint8 &b = 0, const uint8 &a = 255);
93
94 // Check if a rectangle is the same as another
95 bool operator==(const Rect &r) { return r.x == x && r.y == y && r.w == w && r.h == h; }
96
97 // Save to file
98 void saveState(rapidxml::xml_document<> &doc, rapidxml::xml_node<char> *root, const char *name);
99};
100
101} // End of namespace Crab
102
103#endif // CRAB_RECTANGLE_H
Definition: str.h:59
Definition: Rectangle.h:42