-
-
Notifications
You must be signed in to change notification settings - Fork 991
/
rect.hpp
156 lines (133 loc) · 5.13 KB
/
rect.hpp
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
/*
Copyright (C) 2014 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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.
See the COPYING file for more details.
*/
#ifndef SDL_RECT_HPP_INCLUDED
#define SDL_RECT_HPP_INCLUDED
/**
* @file
* Contains the SDL_Rect helper code.
*/
#include <SDL_version.h>
#include "utils.hpp"
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include <SDL_rect.h>
#else
#include <SDL_video.h>
#endif
namespace sdl
{
extern const SDL_Rect empty_rect;
/**
* Creates an empty SDL_Rect.
*
* Since SDL_Rect doesn't have a constructor it's not possible to create it as
* a temporary for a function parameter. This functions overcomes this limit.
*/
SDL_Rect create_rect(const int x, const int y, const int w, const int h);
/**
* Tests whether a point is inside a rectangle.
*
* @param x The x coordinate of the point.
* @param y The y coordinate of the point.
* @param rect The rectangle.
*
* @return True if point (x;y) is inside or on the border
* of rect, false otherwise
*/
bool point_in_rect(int x, int y, const SDL_Rect& rect);
/**
* Tests whether two rectangles overlap.
*
* @param rect1 One rectangle.
* @param rect2 Another rectangle.
*
* @return True if rect1 and rect2 intersect, false if
* not. Touching borders don't overlap.
*/
bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2);
/**
* Calculates the intersection of two rectangles.
*
* @param rect1 One rectangle.
* @param rect2 Another rectangle
* @return The intersection of rect1 and rect2, or
* empty_rect if they don't overlap.
*/
SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2);
/**
* Calculates the union of two rectangles. Note: "union" here doesn't mean the
* union of the sets of points of the two polygons, but rather the minimal
* rectangle that supersets both rectangles.
*
* @param rect1 One rectangle.
* @param rect2 Another rectangle.
*
* @return The union of rect1 and rect2.
*/
SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2);
/**
* Fills a specified area of a surface with a given color and opacity.
*
* @param rect The area that should be filled.
* @param color The color to fill with.
* @param alpha Opacity.
* @param target The surface to operate on.
*/
void fill_rect_alpha(SDL_Rect &rect, Uint32 color, Uint8 alpha, surface target);
/**
* Draw a colored rectangle on a surface.
*
* @param x The x coordinate of the rectangle.
* @param y The y coordinate of the rectangle.
* @param w The width of the rectangle.
* @param h The height of the rectangle.
* @param color The color of the rectangle.
* @param tg The surface to operate on.
*/
void draw_rectangle(int x, int y, int w, int h, Uint32 color, surface tg);
/**
* Fills a specified rectangle area of a surface with a given color and opacity.
* Shortcut for fill_rect_alpha().
*
* @param x The x coordinate of the rectangle.
* @param y The y coordinate of the rectangle.
* @param w The width of the rectangle.
* @param h The height of the rectangle.
* @param r The red value of the color to be used.
* @param g The green value of the color to be used.
* @param b The blue value of the color to be used.
* @param alpha Opacity for filling.
* @param target The surface to operate on.
*/
void draw_solid_tinted_rectangle(int x, int y, int w, int h,
int r, int g, int b,
double alpha, surface target);
/**
* Fill a rectangle on a given surface. Alias for SDL_FillRect.
*
* @param dst The surface to operate on.
* @param dst_rect The rectangle to fill.
* @param color Color of the rectangle.
*/
inline void fill_rect(surface& dst, SDL_Rect* dst_rect, const Uint32 color)
{
SDL_FillRect(dst, dst_rect, color);
}
#if SDL_VERSION_ATLEAST(2,0,0)
void fill_rect(SDL_Renderer *rnd, const SDL_Rect *rect, Uint8 r, Uint8 g,
Uint8 b, Uint8 a);
void draw_rect(SDL_Renderer *rnd, const SDL_Rect *rect, Uint8 r, Uint8 g,
Uint8 b, Uint8 a);
#endif
} // namespace sdl
bool operator==(const SDL_Rect& a, const SDL_Rect& b);
bool operator!=(const SDL_Rect& a, const SDL_Rect& b);
#endif