1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2017 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
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  */
24 
25 /**
26  * A vertex is an improved point. It has a position and other extra attributes
27  * that will be used for drawing: in DSFML, vertices also have a color and a
28  * pair of texture coordinates.
29  *
30  * The vertex is the building block of drawing. Everything which is visible on
31  * screen is made of vertices. They are grouped as 2D primitives (triangles,
32  * quads, ...), and these primitives are grouped to create even more complex 2D
33  * entities such as sprites, texts, etc.
34  *
35  * If you use the graphical entities of DSFML (sprite, text, shape) you won't
36  * have to deal with vertices directly. But if you want to define your own 2D
37  * entities, such as tiled maps or particle systems, using vertices will allow
38  * you to get maximum performances.
39  *
40  * Example:
41  * ---
42  * // define a 100x100 square, red, with a 10x10 texture mapped on it
43  * sf::Vertex vertices[] =
44  * [
45  *     Vertex(Vector2f(  0,   0), Color.Red, Vector2f( 0,  0)),
46  *     Vertex(Vector2f(  0, 100), Color.Red, Vector2f( 0, 10)),
47  *     Vertex(Vector2f(100, 100), Color.Red, Vector2f(10, 10)),
48  *     Vertex(Vector2f(100,   0), Color.Red, Vector2f(10,  0))
49  * ];
50  *
51  * // draw it
52  * window.draw(vertices, 4, PrimitiveType.Quads);
53  * ---
54  *
55  * $(PARA $(B Note): although texture coordinates are supposed to be an integer
56  * amount of pixels, their type is float because of some buggy graphics drivers
57  * that are not able to process integer coordinates correctly.)
58  *
59  * See_Also:
60  * $(VERTEXARRAY_LINK)
61  */
62 module dsfml.graphics.vertex;
63 
64 import dsfml.graphics.color;
65 import dsfml.system.vector2;
66 
67 /**
68  * Define a point with color and texture coordinates.
69  */
70 struct Vertex
71 {
72     /// 2D position of the vertex
73     Vector2f position = Vector2f(0,0);
74     /// Color of the vertex. Default is White.
75     Color color = Color.White;
76     /// 2D coordinates of the texture's pixel map to the vertex.
77     Vector2f texCoords = Vector2f(0,0);
78 
79     /**
80      * Construct the vertex from its position
81      *
82      * The vertex color is white and texture coordinates are (0, 0).
83      *
84      * Params:
85      * thePosition = Vertex position
86      */
87     this(Vector2f thePosition)
88     {
89         position = thePosition;
90     }
91 
92     /**
93      * Construct the vertex from its position and color
94      *
95      * The texture coordinates are (0, 0).
96      *
97      * Params:
98      *  thePosition = Vertex position
99      *  theColor    = Vertex color
100      */
101     this(Vector2f thePosition, Color theColor)
102     {
103         position = thePosition;
104         color = theColor;
105     }
106 
107     /**
108      * Construct the vertex from its position and texture coordinates
109      *
110      * The vertex color is white.
111      *
112      * Params:
113      *  thePosition  = Vertex position
114      *  theTexCoords = Vertex texture coordinates
115      */
116     this(Vector2f thePosition, Vector2f theTexCoords)
117     {
118         position = thePosition;
119         texCoords = theTexCoords;
120     }
121 
122     /**
123      * Construct the vertex from its position, color and texture coordinates
124      *
125      * Params:
126      *  thePosition  = Vertex position
127      *  theColor     = Vertex color
128      *  theTexCoords = Vertex texture coordinates
129      */
130     this(Vector2f thePosition, Color theColor, Vector2f theTexCoords)
131     {
132         position = thePosition;
133         color = theColor;
134         texCoords = theTexCoords;
135     }
136 }
137 
138 unittest
139 {
140     version(DSFML_Unittest_Graphics)
141     {
142         //not really needed, but implemented for code coverage later.
143         import std.stdio;
144 
145         writeln("Unit test for Vertex");
146 
147 
148         auto vertex = Vertex();
149 
150         vertex.position = Vector2f(1,1);
151 
152         vertex.color = Color.Blue;
153 
154         vertex.texCoords = Vector2f(20,10);
155 
156         writeln();
157     }
158 }