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