The Battle for Wesnoth  1.17.0-dev
rect.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "sdl/point.hpp"
17 #include "sdl/rect.hpp"
18 #include "sdl/window.hpp"
19 #include "video.hpp"
20 
21 namespace sdl
22 {
23 bool point_in_rect(int x, int y, const SDL_Rect& rect)
24 {
25  SDL_Point p {x, y};
26  return SDL_PointInRect(&p, &rect) != SDL_FALSE;
27 }
28 
29 bool point_in_rect(const point& point, const SDL_Rect& rect)
30 {
31  return point_in_rect(point.x, point.y, rect);
32 }
33 
34 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2)
35 {
36  return (rect1.x < rect2.x+rect2.w && rect2.x < rect1.x+rect1.w &&
37  rect1.y < rect2.y+rect2.h && rect2.y < rect1.y+rect1.h);
38 }
39 
40 SDL_Rect intersect_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
41 {
42  SDL_Rect res;
43  if(!SDL_IntersectRect(&rect1, &rect2, &res)) {
44  return empty_rect;
45  }
46 
47  return res;
48 }
49 
50 SDL_Rect union_rects(const SDL_Rect& rect1, const SDL_Rect& rect2)
51 {
52  SDL_Rect res;
53  SDL_UnionRect(&rect1, &rect2, &res);
54 
55  return res;
56 }
57 
58 void draw_rectangle(const SDL_Rect& rect, const color_t& color)
59 {
60  SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
61 
62  SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
63  SDL_RenderDrawRect(renderer, &rect);
64 }
65 
66 void fill_rectangle(const SDL_Rect& rect, const color_t& color)
67 {
68  SDL_Renderer* renderer = *CVideo::get_singleton().get_window();
69 
70  SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
71  SDL_RenderFillRect(renderer, &rect);
72 }
73 
74 } // namespace sdl
75 
76 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
77 {
78  return SDL_RectEquals(&a, &b) != SDL_FALSE;
79 }
80 
81 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
82 {
83  return !operator==(a,b);
84 }
85 
86 std::ostream& operator<<(std::ostream& s, const SDL_Rect& rect)
87 {
88  s << "x: " << rect.x << ", y: " << rect.y << ", w: " << rect.w << ", h: " << rect.h;
89  return s;
90 }
SDL_Rect union_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the union of two rectangles.
Definition: rect.cpp:50
bool rects_overlap(const SDL_Rect &rect1, const SDL_Rect &rect2)
Tests whether two rectangles overlap.
Definition: rect.cpp:34
#define a
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:81
static CVideo & get_singleton()
Definition: video.hpp:49
SDL_Rect intersect_rects(const SDL_Rect &rect1, const SDL_Rect &rect2)
Calculates the intersection of two rectangles.
Definition: rect.cpp:40
std::ostream & operator<<(std::ostream &s, const SDL_Rect &rect)
Definition: rect.cpp:86
int x
x coordinate.
Definition: point.hpp:45
#define b
void draw_rectangle(const SDL_Rect &rect, const color_t &color)
Draw a rectangle outline.
Definition: rect.cpp:58
uint8_t r
Red value.
Definition: color.hpp:178
uint8_t a
Alpha value.
Definition: color.hpp:187
sdl::window * get_window()
Returns a pointer to the underlying SDL window.
Definition: video.cpp:354
bool point_in_rect(int x, int y, const SDL_Rect &rect)
Tests whether a point is inside a rectangle.
Definition: rect.cpp:23
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:76
mock_party p
static map_location::DIRECTION s
Holds a 2D point.
Definition: point.hpp:24
Contains the SDL_Rect helper code.
constexpr const SDL_Rect empty_rect
Definition: rect.hpp:32
void fill_rectangle(const SDL_Rect &rect, const color_t &color)
Draws a filled rectangle.
Definition: rect.cpp:66
Contains a wrapper class for the SDL_Window class.
uint8_t g
Green value.
Definition: color.hpp:181
uint8_t b
Blue value.
Definition: color.hpp:184
int y
y coordinate.
Definition: point.hpp:48