The Battle for Wesnoth  1.15.2+dev
rect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 #include "sdl/point.hpp"
16 #include "sdl/rect.hpp"
17 #include "sdl/window.hpp"
18 #include "video.hpp"
19 
20 namespace sdl
21 {
22 bool point_in_rect(int x, int y, const SDL_Rect& rect)
23 {
24  SDL_Point p {x, y};
25  return SDL_PointInRect(&p, &rect) != SDL_FALSE;
26 }
27 
28 bool point_in_rect(const point& point, const SDL_Rect& rect)
29 {
30  return point_in_rect(point.x, point.y, rect);
31 }
32 
33 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2)
34 {
35  return (rect1.x < rect2.x+rect2.w && rect2.x < rect1.x+rect1.w &&
36  rect1.y < rect2.y+rect2.h && rect2.y < rect1.y+rect1.h);
37 }
38 
39 SDL_Rect intersect_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
40 {
41  SDL_Rect res;
42  if(!SDL_IntersectRect(&rect1, &rect2, &res)) {
43  return empty_rect;
44  }
45 
46  return res;
47 }
48 
49 SDL_Rect union_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
50 {
51  SDL_Rect res;
52  SDL_UnionRect(&rect1, &rect2, &res);
53 
54  return res;
55 }
56 
57 void draw_rectangle(const SDL_Rect& rect, const color_t& color)
58 {
59  SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
60 
61  SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
62  SDL_RenderDrawRect(renderer, &rect);
63 }
64 
65 void fill_rectangle(const SDL_Rect& rect, const color_t& color)
66 {
67  SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
68 
69  SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
70  SDL_RenderFillRect(renderer, &rect);
71 }
72 
73 } // namespace sdl
74 
75 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
76 {
77  return SDL_RectEquals(&a, &b) != SDL_FALSE;
78 }
79 
80 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
81 {
82  return !operator==(a,b);
83 }
84 
85 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect)
86 {
87  s << "x: " << rect.x << ", y: " << rect.y << ", w: " << rect.w << ", h: " << rect.h;
88  return s;
89 }
SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the union of two rectangles.
Definition: rect.cpp:49
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:33
#define a
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:80
static CVideo & get_singleton()
Definition: video.hpp:43
SDL_Rect intersect_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:39
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
Definition: rect.cpp:85
-file util.hpp
int x
x coordinate.
Definition: point.hpp:44
#define b
void draw_rectangle(const SDL_Rect &rect, const color_t &color)
Draw a rectangle outline.
Definition: rect.cpp:57
uint8_t r
Red value.
Definition: color.hpp:177
uint8_t a
Alpha value.
Definition: color.hpp:186
sdl::window * get_window()
Returns a pointer to the underlying SDL window.
Definition: video.cpp:370
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:22
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:75
mock_party p
static map_location::DIRECTION s
Holds a 2D point.
Definition: point.hpp:23
Contains the SDL_Rect helper code.
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:31
void fill_rectangle(const SDL_Rect &rect, const color_t &color)
Draws a filled rectangle.
Definition: rect.cpp:65
Contains a wrapper class for the SDL_Window class.
uint8_t g
Green value.
Definition: color.hpp:180
uint8_t b
Blue value.
Definition: color.hpp:183
int y
y coordinate.
Definition: point.hpp:47