1 /**
2 * @file size.c
3 * 2D sizes.
4 *
5 * @authors Copyright © 2013 Daniel Swanson <danij@dengine.net>
6 *
7 * @par License
8 * GPL: http://www.gnu.org/licenses/gpl.html
9 *
10 * <small>This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version. This program is distributed in the hope that it
14 * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 * Public License for more details. You should have received a copy of the GNU
17 * General Public License along with this program; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
19 * 02110-1301 USA</small>
20 */
21
22 #include "de/size.h"
23 #include "de/memory.h"
24
25 struct size2_s {
26 /// Use a "raw" Size natively so that we can simply copy when called upon.
27 Size2Raw raw;
28 };
29
30 struct size2f_s {
31 /// Use a "raw" Size natively so that we can simply copy when called upon.
32 Size2Rawf raw;
33 };
34
Size2_New(void)35 Size2 *Size2_New(void)
36 {
37 Size2 *s = M_Calloc(sizeof *s);
38 return s;
39 }
40
Size2_NewWithDimensions(int width,int height)41 Size2 *Size2_NewWithDimensions(int width, int height)
42 {
43 Size2 *s = Size2_New();
44 Size2_SetWidthHeight(s, width, height);
45 return s;
46 }
47
Size2_NewFromRaw(Size2Raw const * rawSize)48 Size2 *Size2_NewFromRaw(Size2Raw const *rawSize)
49 {
50 DENG_ASSERT(rawSize);
51 return Size2_NewWithDimensions(rawSize->width, rawSize->height);
52 }
53
Size2_Delete(Size2 * s)54 void Size2_Delete(Size2 *s)
55 {
56 DENG_ASSERT(s);
57 M_Free(s);
58 }
59
Size2_IsNull(Size2 const * s)60 dd_bool Size2_IsNull(Size2 const *s)
61 {
62 DENG_ASSERT(s);
63 return s->raw.width == 0 && s->raw.height == 0;
64 }
65
Size2_Raw(Size2 const * s,Size2Raw * rawSize)66 Size2Raw *Size2_Raw(Size2 const *s, Size2Raw *rawSize)
67 {
68 DENG_ASSERT(s);
69 if (!rawSize) return NULL;
70 memcpy(rawSize, &s->raw, sizeof(*rawSize));
71 return rawSize;
72 }
73
Size2_Width(Size2 const * s)74 int Size2_Width(Size2 const *s)
75 {
76 DENG_ASSERT(s);
77 return s->raw.width;
78 }
79
Size2_Height(Size2 const * s)80 int Size2_Height(Size2 const *s)
81 {
82 DENG_ASSERT(s);
83 return s->raw.height;
84 }
85
Size2_SetWidth(Size2 * s,int width)86 void Size2_SetWidth(Size2 *s, int width)
87 {
88 DENG_ASSERT(s);
89 s->raw.width = width;
90 }
91
Size2_SetHeight(Size2 * s,int height)92 void Size2_SetHeight(Size2 *s, int height)
93 {
94 DENG_ASSERT(s);
95 s->raw.height = height;
96 }
97
Size2_WidthHeight(Size2 const * s)98 int const *Size2_WidthHeight(Size2 const *s)
99 {
100 DENG_ASSERT(s);
101 return s->raw.widthHeight;
102 }
103
Size2_SetWidthHeight(Size2 * s,int width,int height)104 void Size2_SetWidthHeight(Size2 *s, int width, int height)
105 {
106 DENG_ASSERT(s);
107 s->raw.width = width;
108 s->raw.height = height;
109 }
110
Size2_Sum(Size2 * s,Size2 const * other)111 void Size2_Sum(Size2 *s, Size2 const *other)
112 {
113 DENG_ASSERT(s && other);
114 s->raw.width += Size2_Width(other);
115 s->raw.height += Size2_Height(other);
116 }
117
Size2_Equality(Size2 const * s,Size2 const * other)118 dd_bool Size2_Equality(Size2 const *s, Size2 const *other)
119 {
120 DENG_ASSERT(s && other);
121 return s == other || (s->raw.width == Size2_Width(other) &&
122 s->raw.height == Size2_Height(other));
123 }
124
Size2f_New(void)125 Size2f *Size2f_New(void)
126 {
127 Size2f *s = M_Calloc(sizeof *s);
128 return s;
129 }
130
Size2f_NewWithDimensions(double width,double height)131 Size2f *Size2f_NewWithDimensions(double width, double height)
132 {
133 Size2f *s = Size2f_New();
134 Size2f_SetWidthHeight(s, width, height);
135 return s;
136 }
137
Size2f_NewFromRaw(Size2Rawf const * rawSize)138 Size2f *Size2f_NewFromRaw(Size2Rawf const *rawSize)
139 {
140 DENG_ASSERT(rawSize);
141 return Size2f_NewWithDimensions(rawSize->width, rawSize->height);
142 }
143
Size2f_Delete(Size2f * s)144 void Size2f_Delete(Size2f *s)
145 {
146 DENG_ASSERT(s);
147 M_Free(s);
148 }
149
Size2f_IsNull(Size2f const * s)150 dd_bool Size2f_IsNull(Size2f const *s)
151 {
152 DENG_ASSERT(s);
153 return s->raw.width == 0 && s->raw.height == 0;
154 }
155
Size2f_Raw(Size2f const * s,Size2Rawf * rawSize)156 Size2Rawf *Size2f_Raw(Size2f const *s, Size2Rawf *rawSize)
157 {
158 DENG_ASSERT(s);
159 if (!rawSize) return NULL;
160 memcpy(rawSize, &s->raw, sizeof(*rawSize));
161 return rawSize;
162 }
163
Size2f_Width(Size2f const * s)164 double Size2f_Width(Size2f const *s)
165 {
166 DENG_ASSERT(s);
167 return s->raw.width;
168 }
169
Size2f_Height(Size2f const * s)170 double Size2f_Height(Size2f const *s)
171 {
172 DENG_ASSERT(s);
173 return s->raw.height;
174 }
175
Size2f_SetWidth(Size2f * s,double width)176 void Size2f_SetWidth(Size2f *s, double width)
177 {
178 DENG_ASSERT(s);
179 s->raw.width = width;
180 }
181
Size2f_SetHeight(Size2f * s,double height)182 void Size2f_SetHeight(Size2f *s, double height)
183 {
184 DENG_ASSERT(s);
185 s->raw.height = height;
186 }
187
Size2f_WidthHeight(Size2f const * s)188 double const *Size2f_WidthHeight(Size2f const *s)
189 {
190 DENG_ASSERT(s);
191 return s->raw.widthHeight;
192 }
193
Size2f_SetWidthHeight(Size2f * s,double width,double height)194 void Size2f_SetWidthHeight(Size2f *s, double width, double height)
195 {
196 DENG_ASSERT(s);
197 s->raw.width = width;
198 s->raw.height = height;
199 }
200
Size2f_Sum(Size2f * s,Size2f const * other)201 void Size2f_Sum(Size2f *s, Size2f const *other)
202 {
203 DENG_ASSERT(s && other);
204 s->raw.width += Size2f_Width(other);
205 s->raw.height += Size2f_Height(other);
206 }
207
Size2f_Equality(Size2f const * s,Size2f const * other)208 dd_bool Size2f_Equality(Size2f const *s, Size2f const *other)
209 {
210 DENG_ASSERT(s && other);
211 return s == other || (s->raw.width == Size2f_Width(other) &&
212 s->raw.height == Size2f_Height(other));
213 }
214