The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rect.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SDL_RECT_HPP_INCLUDED
16 #define SDL_RECT_HPP_INCLUDED
17 
18 /**
19  * @file
20  * Contains the SDL_Rect helper code.
21  */
22 
23 #include <SDL_version.h>
24 #include "utils.hpp"
25 
26 #include <SDL_rect.h>
27 
28 #ifdef SDL_GPU
29 #include "gpu.hpp"
30 
31 class CVideo;
32 #endif
33 
34 namespace sdl
35 {
36 
37 extern const SDL_Rect empty_rect;
38 
39 /**
40  * Creates an empty SDL_Rect.
41  *
42  * Since SDL_Rect doesn't have a constructor it's not possible to create it as
43  * a temporary for a function parameter. This functions overcomes this limit.
44  */
45 SDL_Rect create_rect(const int x, const int y, const int w, const int h);
46 
47 #ifdef SDL_GPU
48 GPU_Rect create_gpu_rect(const float x, const float y, const float w,
49  const float h);
50 #endif
51 /**
52  * Tests whether a point is inside a rectangle.
53  *
54  * @param x The x coordinate of the point.
55  * @param y The y coordinate of the point.
56  * @param rect The rectangle.
57  *
58  * @return True if point (x;y) is inside or on the border
59  * of rect, false otherwise
60  */
61 bool point_in_rect(int x, int y, const SDL_Rect& rect);
62 
63 /**
64  * Tests whether two rectangles overlap.
65  *
66  * @param rect1 One rectangle.
67  * @param rect2 Another rectangle.
68  *
69  * @return True if rect1 and rect2 intersect, false if
70  * not. Touching borders don't overlap.
71  */
72 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2);
73 
74 /**
75  * Calculates the intersection of two rectangles.
76  *
77  * @param rect1 One rectangle.
78  * @param rect2 Another rectangle
79  * @return The intersection of rect1 and rect2, or
80  * empty_rect if they don't overlap.
81  */
82 SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2);
83 
84 /**
85  * Calculates the union of two rectangles. Note: "union" here doesn't mean the
86  * union of the sets of points of the two polygons, but rather the minimal
87  * rectangle that supersets both rectangles.
88  *
89  * @param rect1 One rectangle.
90  * @param rect2 Another rectangle.
91  *
92  * @return The union of rect1 and rect2.
93  */
94 SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2);
95 
96 /**
97  * Fills a specified area of a surface with a given color and opacity.
98  *
99  * @param rect The area that should be filled.
100  * @param color The color to fill with.
101  * @param alpha Opacity.
102  * @param target The surface to operate on.
103  */
104 void fill_rect_alpha(SDL_Rect &rect, Uint32 color, Uint8 alpha, surface target);
105 
106 /**
107  * Draw a colored rectangle on a surface.
108  *
109  * @param x The x coordinate of the rectangle.
110  * @param y The y coordinate of the rectangle.
111  * @param w The width of the rectangle.
112  * @param h The height of the rectangle.
113  * @param color The color of the rectangle.
114  * @param tg The surface to operate on.
115  */
116 void draw_rectangle(int x, int y, int w, int h, Uint32 color, surface tg);
117 
118 /**
119  * Fills a specified rectangle area of a surface with a given color and opacity.
120  * Shortcut for fill_rect_alpha().
121  *
122  * @param x The x coordinate of the rectangle.
123  * @param y The y coordinate of the rectangle.
124  * @param w The width of the rectangle.
125  * @param h The height of the rectangle.
126  * @param r The red value of the color to be used.
127  * @param g The green value of the color to be used.
128  * @param b The blue value of the color to be used.
129  * @param alpha Opacity for filling.
130  * @param target The surface to operate on.
131  */
132 void draw_solid_tinted_rectangle(int x, int y, int w, int h,
133  int r, int g, int b,
134  double alpha, surface target);
135 
136 /**
137  * Fill a rectangle on a given surface. Alias for SDL_FillRect.
138  *
139  * @param dst The surface to operate on.
140  * @param dst_rect The rectangle to fill.
141  * @param color Color of the rectangle.
142  */
143 inline void fill_rect(surface& dst, SDL_Rect* dst_rect, const Uint32 color)
144 {
145  SDL_FillRect(dst, dst_rect, color);
146 }
147 
148 #ifdef SDL_GPU
149 void fill_rect(CVideo &video, const SDL_Rect &rect, SDL_Color color);
150 
151 void fill_rect(CVideo &video, const SDL_Rect &rect, Uint8 r, Uint8 g,
152  Uint8 b, Uint8 a = SDL_ALPHA_OPAQUE);
153 
154 void draw_rect(CVideo &video, const SDL_Rect &rect, SDL_Color color);
155 
156 void draw_rect(CVideo &video, const SDL_Rect &rect, Uint8 r, Uint8 g,
157  Uint8 b, Uint8 a = SDL_ALPHA_OPAQUE);
158 #endif
159 } // namespace sdl
160 
161 bool operator==(const SDL_Rect& a, const SDL_Rect& b);
162 bool operator!=(const SDL_Rect& a, const SDL_Rect& b);
163 
164 #endif
SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:58
SDL_Rect union_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
Calculates the union of two rectangles.
Definition: rect.cpp:71
void fill_rect(surface &dst, SDL_Rect *dst_rect, const Uint32 color)
Fill a rectangle on a given surface.
Definition: rect.hpp:143
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:52
Definition: video.hpp:58
const SDL_Rect empty_rect
Definition: rect.cpp:26
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void draw_solid_tinted_rectangle(int x, int y, int w, int h, int r, int g, int b, double alpha, surface target)
Fills a specified rectangle area of a surface with a given color and opacity.
Definition: rect.cpp:117
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:162
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:167
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
GLenum GLenum dst
Definition: glew.h:2392
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1488
GLuint color
Definition: glew.h:5801
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:47
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
void fill_rect_alpha(SDL_Rect &rect, Uint32 color, Uint8 alpha, surface target)
Fills a specified area of a surface with a given color and opacity.
Definition: rect.cpp:83
void draw_rectangle(int x, int y, int w, int h, Uint32 color, surface target)
Draw a colored rectangle on a surface.
Definition: rect.cpp:103
GLenum target
Definition: glew.h:5190