Btk
rect.hpp
1 #if !defined(_BTK_RECT_HPP_)
2 #define _BTK_RECT_HPP_
3 #include <SDL2/SDL_rect.h>
4 #include "defs.hpp"
5 namespace Btk{
10  struct Rect:public SDL_Rect{
11  Rect() = default;
12  Rect(const SDL_Rect &r){
13  x = r.w;
14  h = r.h;
15  x = r.x;
16  y = r.y;
17  }
18  Rect(int x,int y,int w,int h){
19  this->x = x;
20  this->y = y;
21  this->w = w;
22  this->h = h;
23  }
24  //is empty?
25  bool empty() const noexcept{
26  return SDL_RectEmpty(this);
27  }
28  //Point in rect
29  bool has_point(const SDL_Point &p) const noexcept{
30  return SDL_PointInRect(&p,this);
31  }
32  bool has_point(int x,int y) const noexcept{
33  SDL_Point p{
34  x,
35  y
36  };
37  return SDL_PointInRect(&p,this);
38  }
39  //cmp rect
40  bool operator ==(const SDL_Rect& r) const noexcept{
41  return SDL_RectEquals(this,&r);
42  }
43  bool operator !=(const SDL_Rect& r) const noexcept{
44  return not SDL_RectEquals(this,&r);
45  }
46  };
47  struct FPoint{
48  float x,y;
49  };
50  struct FRect{
51  FRect() = default;
52  FRect(const FRect &) = default;
53  FRect(float x,float y,float w,float h){
54  this->x = x;
55  this->y = y;
56  this->w = w;
57  this->h = h;
58  }
59  float x, y;
60  float w, h;
61 
62  bool empty() const noexcept{
63  return w <= 0 or h <= 0;
64  }
65  operator Rect() const noexcept{
66  return Rect{
67  static_cast<int>(x),
68  static_cast<int>(y),
69  static_cast<int>(w),
70  static_cast<int>(h)
71  };
72  }
73  bool operator ==(const FRect &r){
74  return x == r.x and y == r.y and w == r.w and h == r.h;
75  }
76  bool operator !=(const FRect &r){
77  return not operator ==(r);
78  }
79  };
80  //Define Point
81  struct Point:public SDL_Point{
82  Point() = default;
83  Point(int x,int y){
84  this->x = x;
85  this->y = y;
86  };
87  Point(const SDL_Point &p){
88  x = p.x;
89  y = p.y;
90  }
91  bool operator ==(const SDL_Point &p) const noexcept{
92  return x == p.x and y == p.y;
93  }
94  bool operator !=(const SDL_Point &p) const noexcept{
95  return x != p.x or y != p.y;
96  }
97  };
98  typedef Point Vec2;
99  typedef FPoint FVec2;
104  struct Size{
105  int w;
106  int h;
107  };
108 };
109 #endif // _BTK_RECT_HPP_
a SDL_Rect with methods
Definition: rect.hpp:10
Definition: rect.hpp:50
This header include many useful containers.
Definition: async.hpp:7
Definition: rect.hpp:47
Definition: rect.hpp:81
Size of a Widget or Window.
Definition: rect.hpp:104