ClanLib  2.3.7
collision_outline.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Harry Storbacka
27 ** Magnus Norddahl
28 ** James Wynn
29 ** Emanuel Greisen
30 */
31 
34 
35 #pragma once
36 
37 #include "../api_display.h"
38 #include <vector>
39 #include "contour.h"
40 #include "outline_accuracy.h"
41 #include "../Render/graphic_context.h"
42 #include "../../Core/Resources/resource.h"
43 #include "../../Core/Math/cl_math.h"
44 #include "../../Core/Math/origin.h"
45 #include "../../Core/Math/circle.h"
46 #include "../../Core/IOData/virtual_directory.h"
47 
48 class CL_CollisionOutline_Generic;
49 class CL_ResourceManager;
50 class CL_OutputSourceProvider;
51 class CL_GraphicContext;
52 class CL_Color;
53 
59 {
62 
65 
67  bool is_entry;
68 
71 };
72 
78 {
81  bool inside;
82  std::vector<CL_CollisionPoint> points;
83 
88 
94  CL_CollidingContours(const CL_Contour *c1, const CL_Contour *c2, bool in=false) :
95  contour1(c1),
96  contour2(c2),
97  inside(in),
98  penetration_normal(0.0, 0.0),
99  penetration_depth(0.0),
100  contour1_deep_point(0.0, 0.0),
101  contour2_deep_point(0.0, 0.0)
102  {
103  points.clear();
104  }
105 };
106 
112 {
115 public:
118 
124  CL_CollisionOutline(const CL_PixelBuffer &pbuf, int alpha_limit=128, CL_OutlineAccuracy accuracy=accuracy_medium );
125 
132  CL_CollisionOutline(const CL_StringRef &fullname, int alpha_limit=128, CL_OutlineAccuracy accuracy=accuracy_medium, bool get_insides=true);
133 
141  CL_CollisionOutline(CL_IODevice &file, const CL_String &file_extension, int alpha_limit=128, CL_OutlineAccuracy accuracy=accuracy_medium, bool get_insides=true);
142 
150  CL_CollisionOutline(const CL_StringRef &filename, const CL_VirtualDirectory &directory, int alpha_limit=128, CL_OutlineAccuracy accuracy=accuracy_medium, bool get_insides=true);
151 
156  CL_CollisionOutline(const CL_StringRef &resource_id, CL_ResourceManager *manager );
157 
163  CL_CollisionOutline(std::vector<CL_Contour> contours, int width, int height);
164 
166 
170 public:
172  const CL_Contour &get_object_bounding_box() const;
173 
175 
176  CL_Circlef get_minimum_enclosing_disc() const;
177 
179  bool get_inside_test() const;
180 
182  std::vector<CL_Contour> &get_contours();
183  const std::vector<CL_Contour> &get_contours() const;
184 
186  CL_Pointf get_translation() const;
187 
189  CL_Pointf get_scale() const;
190 
192  float get_angle() const;
193 
195  unsigned int get_width() const;
196 
198  unsigned int get_height() const;
199 
201  void get_alignment( CL_Origin &origin, float &x, float &y ) const;
202 
204  void get_rotation_hotspot( CL_Origin &origin, float &x, float &y) const;
205 
207  const std::vector<CL_CollidingContours> &get_collision_info() const;
208 
210  void get_collision_info_state(bool &points, bool &normals, bool &metadata, bool &pendepth) const;
211 
214 
218 public:
222  void load(const CL_StringRef &fullname);
223 
228  void load(const CL_StringRef &filename, const CL_VirtualDirectory &directory);
229 
233  void load(CL_IODevice &file);
234 
236  CL_CollisionOutline &copy(const CL_CollisionOutline &other);
237 
239  CL_CollisionOutline clone() const;
240 
245  void optimize( unsigned char check_distance=3, float corner_angle = CL_PI / 5.0 );
246 
255  void draw(
256  float x,
257  float y,
258  const CL_Colorf &color,
259  CL_GraphicContext &gc);
260 
269  void draw_sub_circles(
270  float x,
271  float y,
272  const CL_Colorf &color,
273  CL_GraphicContext &gc);
274 
283  void draw_smallest_enclosing_disc(
284  float x,
285  float y,
286  const CL_Colorf &color,
287  CL_GraphicContext &gc);
288 
290  void set_alignment( CL_Origin origin, float x=0, float y=0 );
291 
293  void set_rotation_hotspot( CL_Origin origin, float x=0, float y=0 );
294 
296  void set_translation( float x, float y );
297 
299  void set_scale( float x, float y );
300 
302  void set_angle(const CL_Angle &angle);
303 
305  void rotate(const CL_Angle &angle);
306 
308  void set_inside_test( bool value );
309 
311  void enable_collision_info( bool points=true, bool normals=false, bool metadata=false, bool pen_depth=false);
312 
314  void set_collision_info(const std::vector<CL_CollidingContours> &colinfo);
315 
317  void clean_collision_info();
318 
320  void calculate_radius();
321 
323  void calculate_sub_circles(float radius_multiplier=3.5);
324 
326  void calculate_smallest_enclosing_discs();
327 
329  void calculate_convex_hulls();
330 
334  void save(const CL_StringRef &fullname) const;
335 
340  void save(const CL_StringRef &filename, CL_VirtualDirectory &directory) const;
341 
345  void save(CL_IODevice &file) const;
346 
351  bool collide( const CL_CollisionOutline &outline, bool remove_old_collision_info=true );
352 
354  static void calculate_penetration_depth(std::vector<CL_CollidingContours> &collision_info);
355 
359  bool point_inside( const CL_Pointf &point ) const;
360 
364 private:
365  CL_SharedPtr<CL_CollisionOutline_Generic> impl;
367 };
368