1 /* 2 DSFML - The Simple and Fast Multimedia Library for D 3 4 Copyright (c) 2013 - 2015 Jeremy DeHaan (dehaan.jeremiah@gmail.com) 5 6 This software is provided 'as-is', without any express or implied warranty. 7 In no event will the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial applications, 10 and to alter it and redistribute it freely, subject to the following restrictions: 11 12 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. 13 If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 14 15 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 16 17 3. This notice may not be removed or altered from any source distribution 18 */ 19 20 module dsfml.graphics.convexshape; 21 22 import dsfml.system.vector2; 23 import dsfml.graphics.shape; 24 25 /++ 26 + Specialized shape representing a convex polygon. 27 + 28 + This class inherits all the functions of Transformable (position, rotation, scale, bounds, ...) as well as the functions of Shape (outline, color, texture, ...). 29 + 30 + It is important to keep in mind that a convex shape must always be... convex, otherwise it may not be drawn correctly. Moreover, the points must be defined in order; using a random order would result in an incorrect shape. 31 + 32 + Authors: Laurent Gomila, Jeremy DeHaan 33 + See_Also: http://www.sfml-dev.org/documentation/2.0/classsf_1_1ConvexShape.php#a4f4686f57622bfbbe419ac1420b1432a 34 +/ 35 class ConvexShape : Shape 36 { 37 private Vector2f[] m_points; 38 39 /// Params: pointCount = Number of points on the polygon 40 this(uint thePointCount = 0) 41 { 42 this.pointCount = thePointCount; 43 update(); 44 } 45 46 ~this() 47 { 48 import dsfml.system.config; 49 mixin(destructorOutput); 50 } 51 52 /// The number of points on the polygon 53 @property 54 { 55 uint pointCount(uint newPointCount) 56 { 57 m_points.length = newPointCount; 58 update(); 59 return newPointCount; 60 } 61 override uint pointCount() 62 { 63 import std.algorithm; 64 return cast(uint)min(m_points.length, uint.max); 65 } 66 } 67 68 /** 69 * Get the position of a point. 70 * 71 * The result is undefined if index is out of the valid range. 72 * 73 * Params: 74 * index = Index of the point to get, in range [0 .. pointCount - 1]. 75 * 76 * Returns: Index-th point of the shape. 77 */ 78 override Vector2f getPoint(uint index) const 79 { 80 return m_points[index]; 81 } 82 83 /** 84 * Set the position of a point. 85 * 86 * Don't forget that the polygon must remain convex, and the points need to stay ordered! pointCount must be changed first in order to set the total number of points. The result is undefined if index is out of the valid range. 87 * 88 * Params: 89 * index = Index of the point to change, in range [0 .. pointCount - 1]. 90 * point = New position of the point 91 */ 92 void setPoint(uint index, Vector2f point) 93 { 94 m_points[index] = point; 95 } 96 97 /** 98 * Add a point to the polygon. 99 * 100 * Don't forget that the polygon must remain convex, and the points need to stay ordered! 101 * 102 * Params: 103 * point = Position of the new point. 104 */ 105 void addPoint(Vector2f point) 106 { 107 m_points ~=point; 108 update(); 109 } 110 } 111 112 unittest 113 { 114 version(DSFML_Unittest_Graphics) 115 { 116 import std.stdio; 117 import dsfml.graphics; 118 119 writeln("Unit test for ConvexShape"); 120 auto window = new RenderWindow(VideoMode(800,600), "ConvexShape unittest"); 121 122 auto convexShape = new ConvexShape(); 123 124 convexShape.addPoint(Vector2f(0,20)); 125 convexShape.addPoint(Vector2f(10,10)); 126 convexShape.addPoint(Vector2f(20,20)); 127 convexShape.addPoint(Vector2f(20,30)); 128 convexShape.addPoint(Vector2f(10,40)); 129 convexShape.addPoint(Vector2f(0,30)); 130 131 convexShape.fillColor = Color.Blue; 132 133 convexShape.outlineColor = Color.Green; 134 135 auto clock = new Clock(); 136 137 138 while(window.isOpen()) 139 { 140 Event event; 141 142 while(window.pollEvent(event)) 143 { 144 //no events gonna do stuffs! 145 } 146 147 //draws the shape for a while before closing the window 148 if(clock.getElapsedTime().total!"seconds" > 1) 149 { 150 window.close(); 151 } 152 153 window.clear(); 154 window.draw(convexShape); 155 window.display(); 156 } 157 158 writeln(); 159 } 160 }