The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rect.cpp
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 #include "sdl/rect.hpp"
16 #include "sdl/alpha.hpp"
17 #include "sdl/utils.hpp"
18 
19 #ifdef SDL_GPU
20 #include "video.hpp"
21 #endif
22 
23 namespace sdl
24 {
25 
26 const SDL_Rect empty_rect = { 0, 0, 0, 0 };
27 
28 SDL_Rect create_rect(const int x, const int y, const int w, const int h)
29 {
30  SDL_Rect rect;
31  rect.x = x;
32  rect.y = y;
33  rect.w = w;
34  rect.h = h;
35  return rect;
36 }
37 
38 #ifdef SDL_GPU
39 GPU_Rect create_gpu_rect(const float x, const float y, const float w, const float h)
40 {
41  GPU_Rect result = {x, y, w, h};
42 
43  return result;
44 }
45 #endif
46 
47 bool point_in_rect(int x, int y, const SDL_Rect& rect)
48 {
49  return x >= rect.x && y >= rect.y && x < rect.x + rect.w && y < rect.y + rect.h;
50 }
51 
52 bool rects_overlap(const SDL_Rect& rect1, const SDL_Rect& rect2)
53 {
54  return (rect1.x < rect2.x+rect2.w && rect2.x < rect1.x+rect1.w &&
55  rect1.y < rect2.y+rect2.h && rect2.y < rect1.y+rect1.h);
56 }
57 
58 SDL_Rect intersect_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
59 {
60  SDL_Rect res;
61  res.x = std::max<int>(rect1.x, rect2.x);
62  res.y = std::max<int>(rect1.y, rect2.y);
63  int w = std::min<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
64  int h = std::min<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
65  if (w <= 0 || h <= 0) return empty_rect;
66  res.w = w;
67  res.h = h;
68  return res;
69 }
70 
71 SDL_Rect union_rects(SDL_Rect const &rect1, SDL_Rect const &rect2)
72 {
73  if (rect1.w == 0 || rect1.h == 0) return rect2;
74  if (rect2.w == 0 || rect2.h == 0) return rect1;
75  SDL_Rect res;
76  res.x = std::min<int>(rect1.x, rect2.x);
77  res.y = std::min<int>(rect1.y, rect2.y);
78  res.w = std::max<int>(rect1.x + rect1.w, rect2.x + rect2.w) - res.x;
79  res.h = std::max<int>(rect1.y + rect1.h, rect2.y + rect2.h) - res.y;
80  return res;
81 }
82 
83 void fill_rect_alpha(SDL_Rect &rect, Uint32 color, Uint8 alpha, surface target)
84 {
85  if(alpha == SDL_ALPHA_OPAQUE) {
86  sdl::fill_rect(target,&rect,color);
87  return;
88  } else if(alpha == SDL_ALPHA_TRANSPARENT) {
89  return;
90  }
91 
92  surface tmp(create_compatible_surface(target,rect.w,rect.h));
93  if(tmp == nullptr) {
94  return;
95  }
96 
97  SDL_Rect r = {0,0,rect.w,rect.h};
98  sdl::fill_rect(tmp,&r,color);
99  SDL_SetAlpha(tmp,SDL_SRCALPHA,alpha);
100  sdl_blit(tmp,nullptr,target,&rect);
101 }
102 
103 void draw_rectangle(int x, int y, int w, int h, Uint32 color, surface target)
104 {
105 
106  SDL_Rect top = create_rect(x, y, w, 1);
107  SDL_Rect bot = create_rect(x, y + h - 1, w, 1);
108  SDL_Rect left = create_rect(x, y, 1, h);
109  SDL_Rect right = create_rect(x + w - 1, y, 1, h);
110 
111  sdl::fill_rect(target,&top,color);
112  sdl::fill_rect(target,&bot,color);
113  sdl::fill_rect(target,&left,color);
114  sdl::fill_rect(target,&right,color);
115 }
116 
117 void draw_solid_tinted_rectangle(int x, int y, int w, int h,
118  int r, int g, int b,
119  double alpha, surface target)
120 {
121 
122  SDL_Rect rect = create_rect(x, y, w, h);
123  fill_rect_alpha(rect,SDL_MapRGB(target->format,r,g,b),Uint8(alpha*255),target);
124 }
125 
126 #ifdef SDL_GPU
127 void draw_rect(CVideo &video, const SDL_Rect &rect, Uint8 r, Uint8 g,
128  Uint8 b, Uint8 a)
129 {
130  video.set_texture_color_modulation(0, 0, 0, 0);
131  SDL_Color color = {r, g, b, a};
132  GPU_Rectangle(video.render_target(), rect.x, rect.y, rect.x + rect.w, rect.y + rect.h,
133  color);
134 }
135 
136 void draw_rect(CVideo &video, const SDL_Rect &rect, SDL_Color color)
137 {
138  video.set_texture_color_modulation(0, 0, 0, 0);
139  GPU_Rectangle(video.render_target(), rect.x, rect.y, rect.x + rect.w, rect.y + rect.h,
140  color);
141 }
142 
143 void fill_rect(CVideo &video, const SDL_Rect &rect, Uint8 r, Uint8 g,
144  Uint8 b, Uint8 a)
145 {
146  video.set_texture_color_modulation(0, 0, 0, 0);
147  SDL_Color color = {r, g, b, a};
148  GPU_RectangleFilled(video.render_target(), rect.x, rect.y, rect.x + rect.w,
149  rect.y + rect.h, color);
150 }
151 
152 void fill_rect(CVideo &video, const SDL_Rect &rect, SDL_Color color)
153 {
154  video.set_texture_color_modulation(0, 0, 0, 0);
155  GPU_RectangleFilled(video.render_target(), rect.x, rect.y, rect.x + rect.w,
156  rect.y + rect.h, color);
157 }
158 #endif
159 
160 } // namespace sdl
161 
162 bool operator==(const SDL_Rect& a, const SDL_Rect& b)
163 {
164  return a.x == b.x && a.y == b.y && a.w == b.w && a.h == b.h;
165 }
166 
167 bool operator!=(const SDL_Rect& a, const SDL_Rect& b)
168 {
169  return !operator==(a,b);
170 }
void GPU_RectangleFilled(GPU_Target *target, float x1, float y1, float x2, float y2, SDL_Color color)
void GPU_Rectangle(GPU_Target *target, float x1, float y1, float x2, float y2, SDL_Color color)
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
bool operator!=(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:167
#define h
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
-file util.hpp
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
GLuint64EXT * result
Definition: glew.h:10727
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
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
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
lu_byte right
Definition: lparser.cpp:1020
GLuint res
Definition: glew.h:9258
GLint left
Definition: glew.h:5907
bool operator==(const SDL_Rect &a, const SDL_Rect &b)
Definition: rect.cpp:162
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
#define SDL_SRCALPHA
Definition: alpha.hpp:28
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
Contains the SDL_Rect helper code.
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
#define g
Definition: glew.h:12730
surface create_compatible_surface(const surface &surf, int width, int height)
Definition: utils.cpp:2166
void sdl_blit(const surface &src, SDL_Rect *src_rect, surface &dst, SDL_Rect *dst_rect)
Definition: utils.hpp:112
int SDL_SetAlpha(SDL_Surface *surface, Uint32 flag, Uint8 alpha)
Definition: alpha.cpp:18
Compatibility layer for using SDL 1.2 and 2.0.
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