1 /**
2 * Copyright (c) 2006-2011 LOVE Development Team
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty.  In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 *    claim that you wrote the original software. If you use this software
14 *    in a product, an acknowledgment in the product documentation would be
15 *    appreciated but is not required.
16 * 2. Altered source versions must be plainly marked as such, and must not be
17 *    misrepresented as being the original software.
18 * 3. This notice may not be removed or altered from any source distribution.
19 **/
20 
21 #include "Quad.h"
22 #include <common/Matrix.h>
23 
24 // STD
25 #include <cstring> // For memcpy
26 
27 namespace love
28 {
29 namespace graphics
30 {
31 namespace opengl
32 {
Quad(const Viewport & v,int sw,int sh)33 	Quad::Quad(const Viewport & v, int sw, int sh)
34 		: sw(sw), sh(sh)
35 	{
36 		memset(vertices, 255, sizeof(vertex)*4);
37 		refresh(v, sw, sh);
38 	}
39 
~Quad()40 	Quad::~Quad()
41 	{
42 	}
43 
refresh(const Viewport & v,int sw,int sh)44 	void Quad::refresh(const Viewport & v, int sw, int sh)
45 	{
46 		viewport = v;
47 
48 		vertices[0].x = 0;
49 		vertices[0].y = 0;
50 		vertices[1].x = 0;
51 		vertices[1].y = (float)v.h;
52 		vertices[2].x = (float)v.w;
53 		vertices[2].y = (float)v.h;
54 		vertices[3].x = (float)v.w;
55 		vertices[3].y = 0;
56 
57 		vertices[0].s = (float)v.x/(float)sw;
58 		vertices[0].t = (float)v.y/(float)sh;
59 		vertices[1].s = (float)v.x/(float)sw;
60 		vertices[1].t = (float)(v.y+v.h)/(float)sh;
61 		vertices[2].s = (float)(v.x+v.w)/(float)sw;
62 		vertices[2].t = (float)(v.y+v.h)/(float)sh;
63 		vertices[3].s = (float)(v.x+v.w)/(float)sw;
64 		vertices[3].t = (float)v.y/(float)sh;
65 	}
66 
setViewport(const Quad::Viewport & v)67 	void Quad::setViewport(const Quad::Viewport & v)
68 	{
69 		refresh(v, sw, sh);
70 	}
71 
getViewport() const72 	Quad::Viewport Quad::getViewport() const
73 	{
74 		return viewport;
75 	}
76 
flip(bool x,bool y)77 	void Quad::flip(bool x, bool y)
78 	{
79 		vertex temp[4];
80 		if (x)
81 		{
82 			memcpy(temp, vertices, sizeof(vertex)*4);
83 			vertices[0].s = temp[3].s; vertices[0].t = temp[3].t;
84 			vertices[1].s = temp[2].s; vertices[1].t = temp[2].t;
85 			vertices[2].s = temp[1].s; vertices[2].t = temp[1].t;
86 			vertices[3].s = temp[0].s; vertices[3].t = temp[0].t;
87 		}
88 		if (y)
89 		{
90 			memcpy(temp, vertices, sizeof(vertex)*4);
91 			vertices[0].s = temp[1].s; vertices[0].t = temp[1].t;
92 			vertices[1].s = temp[0].s; vertices[1].t = temp[0].t;
93 			vertices[2].s = temp[3].s; vertices[2].t = temp[3].t;
94 			vertices[3].s = temp[2].s; vertices[3].t = temp[2].t;
95 		}
96 	}
97 
getVertices() const98 	const vertex * Quad::getVertices() const
99 	{
100 		return vertices;
101 	}
102 
103 } // opengl
104 } // graphics
105 } // love
106