vvopensource
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Groups Pages
VVBasicMacros.h
1 #import <Foundation/Foundation.h>
2 
3 // macros for checking to see if something is nil, and if it's not releasing and setting it to nil
4 #define VVRELEASE(item) {if (item != nil) { \
5  [item release]; \
6  item = nil; \
7 }}
8 #define VVAUTORELEASE(item) {if (item != nil) { \
9  [item autorelease]; \
10  item = nil; \
11 }}
12 
13 
14 
15 
16 // NSRect/Point/Size/etc and CGRect/Point/Size are functionally identical, but cast differently. these macros provide a single interface for this functionality to simplify things.
17 #if IPHONE
18 #define VVPOINT CGPoint
19 #define VVMAKEPOINT CGPointMake
20 #define VVZEROPOINT CGPointZero
21 #define VVRECT CGRect
22 #define VVMAKERECT CGRectMake
23 #define VVZERORECT CGRectZero
24 #define VVINTERSECTSRECT CGRectIntersectsRect
25 #define VVINTERSECTIONRECT CGRectIntersection
26 #define VVINTEGRALRECT CGRectIntegral
27 #define VVUNIONRECT CGRectUnion
28 #define VVPOINTINRECT(a,b) CGRectContainsPoint((b),(a))
29 #define VVSIZE CGSize
30 #define VVMAKESIZE CGSizeMake
31 #else
32 #define VVPOINT NSPoint
33 #define VVMAKEPOINT NSMakePoint
34 #define VVZEROPOINT NSZeroPoint
35 #define VVRECT NSRect
36 #define VVMAKERECT NSMakeRect
37 #define VVZERORECT NSZeroRect
38 #define VVINTERSECTSRECT NSIntersectsRect
39 #define VVINTERSECTIONRECT NSIntersectionRect
40 #define VVINTEGRALRECT NSIntegralRect
41 #define VVUNIONRECT NSUnionRect
42 #define VVPOINTINRECT NSPointInRect
43 #define VVSIZE NSSize
44 #define VVMAKESIZE NSMakeSize
45 #endif
46 
47 
48 
49 
50 // macros for calculating rect coords
51 /*
52 #define VVMINX(r) (r.origin.x)
53 #define VVMAXX(r) (r.origin.x+r.size.width)
54 #define VVMINY(r) (r.origin.y)
55 #define VVMAXY(r) (r.origin.y+r.size.height)
56 #define VVMIDX(r) (r.origin.x+(r.size.width/2.0))
57 #define VVMIDY(r) (r.origin.y+(r.size.height/2.0))
58 
59 #define VVMINX(r) (fmin(r.origin.x,(r.origin.x+r.size.width)))
60 #define VVMAXX(r) (fmax(r.origin.x,(r.origin.x+r.size.width)))
61 #define VVMINY(r) (fmin(r.origin.y,(r.origin.y+r.size.height)))
62 #define VVMAXY(r) (fmax(r.origin.y,(r.origin.y+r.size.height)))
63 */
64 #define VVMINX(r) ((r.size.width>=0) ? (r.origin.x) : (r.origin.x+r.size.width))
65 #define VVMAXX(r) ((r.size.width>=0) ? (r.origin.x+r.size.width) : (r.origin.x))
66 #define VVMINY(r) ((r.size.height>=0) ? (r.origin.y) : (r.origin.y+r.size.height))
67 #define VVMAXY(r) ((r.size.height>=0) ? (r.origin.y+r.size.height) : (r.origin.y))
68 #define VVMIDX(r) (r.origin.x+(r.size.width/2.0))
69 #define VVMIDY(r) (r.origin.y+(r.size.height/2.0))
70 #define VVTOPLEFT(r) (VVMAKEPOINT(VVMINX(r),VVMAXY(r)))
71 #define VVTOPRIGHT(r) (VVMAKEPOINT(VVMAXX(r),VVMAXY(r)))
72 #define VVBOTLEFT(r) (VVMAKEPOINT(VVMINX(r),VVMINY(r)))
73 #define VVBOTRIGHT(r) (VVMAKEPOINT(VVMAXX(r),VVMINY(r)))
74 #define VVCENTER(r) (VVMAKEPOINT(VVMIDX(r),VVMIDY(r)))
75 #define VVADDPOINT(a,b) (VVMAKEPOINT((a.x+b.x),(a.y+b.y)))
76 #define VVSUBPOINT(a,b) (VVMAKEPOINT((a.x-b.x),(a.y-b.y)))
77 #define VVADDSIZE(a,b) (VVMAKESIZE(a.width+b.width, a.height+b.height))
78 #define VVSUBSIZE(a,b) (VVMAKESIZE(a.width-b.width, a.height-b.height))
79 #define VVEQUALRECTS(a,b) ((a.origin.x==b.origin.x && a.origin.y==b.origin.y && a.size.width==b.size.width && a.size.height==b.size.height) ? YES : NO)
80 #define VVEQUALSIZES(a,b) ((a.width==b.width)&&(a.height==b.height))
81 #define VVEQUALPOINTS(a,b) ((a.x==b.x)&&(a.y==b.y))
82 #define VVISZERORECT(a) ((a.size.width==0.0 && a.size.height==0.0) ? YES : NO)
83 
84 // macro for clipping a val to the normalized range (0.0 - 1.0)
85 #define CLIPNORM(n) (((n)<0.0)?0.0:(((n)>1.0)?1.0:(n)))
86 #define CLIPTORANGE(n,l,h) (((n)<(l))?(l):(((n)>(h))?(h):(n)))
87 
88 
89 
90 
91 
92 
93 // macros for making a CGRect from an NSRect
94 #define NSMAKECGRECT(n) CGRectMake(n.origin.x, n.origin.y, n.size.width, n.size.height)
95 #define NSMAKECGPOINT(n) CGPointMake(n.x, n.y)
96 #define NSMAKECGSIZE(n) CGSizeMake(n.width, n.height)
97 // macros for making an NSRect from a CGRect
98 #define CGMAKENSRECT(n) NSMakeRect(n.origin.x, n.origin.y, n.size.width, n.size.height)
99 #define CGMAKENSSIZE(n) NSMakeSize(n.width,n.height)
100 
101 // macro for quickly printing out the dimensions of a rect (and a name/id so you can distinguish between them)
102 #define NSRectLog(n,r) NSLog(@"%@, (%0.2f,%0.2f) : %0.2fx%0.2f",n,r.origin.x,r.origin.y,r.size.width,r.size.height)
103 #define NSPointLog(n,r) NSLog(@"%@, (%0.2f,%0.2f)",n,r.x,r.y)
104 #define NSSizeLog(n,s) NSLog(@"%@, %0.2fx%0.2f",n,s.width,s.height)
105 
106 #define VVRectLog(n,r) NSLog(@"%@, (%0.2f,%0.2f) : %0.2fx%0.2f",n,r.origin.x,r.origin.y,r.size.width,r.size.height)
107 #define VVPointLog(n,r) NSLog(@"%@, (%0.2f,%0.2f)",n,r.x,r.y)
108 #define VVSizeLog(n,s) NSLog(@"%@, %0.2fx%0.2f",n,s.width,s.height)
109 
110 // macros for quickly making numbers and values
111 #define NUMINT(i) [NSNumber numberWithInt:i]
112 #define NUMUINT(i) [NSNumber numberWithUnsignedInteger:i]
113 #define NUMLONG(i) [NSNumber numberWithLong:i]
114 #define NUMU64(i) [NSNumber numberWithUnsignedLongLong:i]
115 #define NUM64(i) [NSNumber numberWithLongLong:i]
116 #define NUMFLOAT(f) [NSNumber numberWithFloat:f]
117 #define NUMBOOL(b) [NSNumber numberWithBool:b]
118 #define NUMDOUBLE(d) [NSNumber numberWithDouble:d]
119 #define VALSIZE(s) [NSValue valueWithSize:s]
120 #define VALRECT(r) [NSValue valueWithRect:r]
121 
122 // macro for quickly archiving and object
123 #define ARCHIVE(a) [NSKeyedArchiver archivedDataWithRootObject:a]
124 #define UNARCHIVE(a) [NSKeyedUnarchiver unarchiveObjectWithData:a]
125 
126 // macro for quickly making colors
127 #define VVDEVCOLOR(r,g,b,a) [NSColor colorWithDeviceRed:r green:g blue:b alpha:a]
128 #define VVCALCOLOR(r,g,b,a) [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a]
129 
130 // nice little macro for strings
131 #define VVSTRING(n) ((NSString *)[NSString stringWithString:n])
132 #define VVFMTSTRING(f, ...) ((NSString *)[NSString stringWithFormat:f, ##__VA_ARGS__])
133 #define VVDATASTRING(n) ((NSData *)[[NSString stringWithString:n] dataUsingEncoding:NSUTF8StringEncoding])
134 #define VVDATAFMTSTRING(f, ...) ((NSData *)[[NSString stringWithFormat:f, ##__VA_ARGS__] dataUsingEncoding:NSUTF8StringEncoding])
135 
136 // macros for quickly making arrays because.....well, it's the wiimote, and i'm fucking tired of typing. so there.
137 #define OBJARRAY(f) [NSArray arrayWithObject:f]
138 #define OBJSARRAY(f, ...) [NSArray arrayWithObjects:f, ##__VA_ARGS__, nil]
139 #define MUTARRAY [NSMutableArray arrayWithCapacity:0]
140 
141 // macros for quickly making dicts
142 #define OBJDICT(o,k) [NSDictionary dictionaryWithObject:o forKey:k]
143 #define OBJSDICT(o, ...) [NSDictionary dictionaryWithObjectsAndKeys:o,#__VA_ARGS__, nil]
144 #define MUTDICT [NSMutableDictionary dictionaryWithCapacity:0]
145 
146 // calculating the distance between two NSPoints or similar structs
147 #define POINTDISTANCE(a,b) fabs(sqrtf((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)))
148 
149 
150 
151 
152 // not actually a macro- a function to replace NSRunAlertPanel, which is deprecated in 10.10
153 NSInteger VVRunAlertPanel(NSString *title, NSString *msg, NSString *btnA, NSString *btnB, NSString *btnC);
154 
155 
156 
157 
158 // this macro is from the GL red book
159 #define BUFFER_OFFSET(bytes) ((GLubyte*)NULL + (bytes))
160 // this is a macro for drawing an NSRect in opengl
161 #define GLDRAWRECT(r) \
162 { \
163  GLfloat vvMacroVertices[]={ \
164  r.origin.x, r.origin.y, 0.0, \
165  r.origin.x, r.origin.y+r.size.height, 0.0, \
166  r.origin.x+r.size.width, r.origin.y+r.size.height, 0.0, \
167  r.origin.x+r.size.width, r.origin.y, 0.0}; \
168  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
169  glDrawArrays(GL_QUADS,0,4); \
170 }
171 
172 
173 #define GLDRAWRECT_TRISTRIP(r) \
174 { \
175  GLfloat vvMacroVertices[]={ \
176  r.origin.x, r.origin.y, 0.0, \
177  r.origin.x, r.origin.y+r.size.height, 0.0, \
178  r.origin.x+r.size.width, r.origin.y+r.size.height, 0.0, \
179  r.origin.x+r.size.width, r.origin.y, 0.0}; \
180  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
181  glDrawArrays(GL_TRIANGLE_STRIP,0,4); \
182 }
183 
184 
185 
186 #define GLDRAWRECT_TRISTRIP_COLOR(rect,r,g,b,a) \
187 { \
188  GLfloat vvMacroVertices[]={ \
189  rect.origin.x, rect.origin.y, 0.0, \
190  rect.origin.x, rect.origin.y+rect.size.height, 0.0, \
191  rect.origin.x+rect.size.width, rect.origin.y, 0.0, \
192  rect.origin.x+rect.size.width, rect.origin.y+rect.size.height, 0.0}; \
193  GLfloat vvMacroColors[]={ \
194  r, g, b, a, \
195  r, g, b, a, \
196  r, g, b, a, \
197  r, g, b, a }; \
198  glEnableVertexAttribArray(GLKVertexAttribPosition); \
199  glEnableVertexAttribArray(GLKVertexAttribColor); \
200  glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, vvMacroVertices); \
201  glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, vvMacroColors); \
202  glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); \
203  glDisableVertexAttribArray(GLKVertexAttribPosition); \
204  glDisableVertexAttribArray(GLKVertexAttribColor); \
205 }
206 
207 
208 
209 // this is a macro for stroking an NSRect in opengl
210 #define GLSTROKERECT(r) \
211 { \
212  GLfloat vvMacroVertices[]={ \
213  r.origin.x+0.5, r.origin.y+0.5, 0.0, \
214  r.origin.x+r.size.width-0.5, r.origin.y+0.5, 0.0, \
215  r.origin.x+r.size.width-0.5, r.origin.y+0.5, 0.0, \
216  r.origin.x+r.size.width-0.5, r.origin.y+r.size.height-0.5, 0.0, \
217  r.origin.x+r.size.width-0.5, r.origin.y+r.size.height-0.5, 0.0, \
218  r.origin.x+0.5, r.origin.y+r.size.height-0.5, 0.0, \
219  r.origin.x+0.5, r.origin.y+r.size.height-0.5, 0.0, \
220  r.origin.x+0.5, r.origin.y+0.5, 0.0}; \
221  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
222  glDrawArrays(GL_LINES,0,8); \
223 }
224 /*
225 #define GLSTROKERECT(r) \
226 { \
227  GLfloat vvMacroVertices[]={ \
228  r.origin.x, r.origin.y, 0.0, \
229  r.origin.x+r.size.width, r.origin.y, 0.0, \
230  r.origin.x+r.size.width, r.origin.y, 0.0, \
231  r.origin.x+r.size.width, r.origin.y+r.size.height, 0.0, \
232  r.origin.x+r.size.width, r.origin.y+r.size.height, 0.0, \
233  r.origin.x, r.origin.y+r.size.height, 0.0, \
234  r.origin.x, r.origin.y+r.size.height, 0.0, \
235  r.origin.x, r.origin.y, 0.0}; \
236  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
237  glDrawArrays(GL_LINES,0,8); \
238 }
239 */
240 #define GLSTROKERECT_COLOR(tmpLocalRect,r,g,b,a) \
241 { \
242  GLfloat vvMacroVertices[]={ \
243  tmpLocalRect.origin.x+0.5, tmpLocalRect.origin.y+0.5, 0.0, \
244  tmpLocalRect.origin.x+tmpLocalRect.size.width-0.5, tmpLocalRect.origin.y+0.5, 0.0, \
245  tmpLocalRect.origin.x+tmpLocalRect.size.width-0.5, tmpLocalRect.origin.y+0.5, 0.0, \
246  tmpLocalRect.origin.x+tmpLocalRect.size.width-0.5, tmpLocalRect.origin.y+tmpLocalRect.size.height-0.5, 0.0, \
247  tmpLocalRect.origin.x+tmpLocalRect.size.width-0.5, tmpLocalRect.origin.y+tmpLocalRect.size.height-0.5, 0.0, \
248  tmpLocalRect.origin.x+0.5, tmpLocalRect.origin.y+tmpLocalRect.size.height-0.5, 0.0, \
249  tmpLocalRect.origin.x+0.5, tmpLocalRect.origin.y+tmpLocalRect.size.height-0.5, 0.0, \
250  tmpLocalRect.origin.x+0.5, tmpLocalRect.origin.y+0.5, 0.0}; \
251  GLfloat vvMacroColors[]={ \
252  r, g, b, a, \
253  r, g, b, a, \
254  r, g, b, a, \
255  r, g, b, a, \
256  r, g, b, a, \
257  r, g, b, a, \
258  r, g, b, a, \
259  r, g, b, a, }; \
260  glEnableVertexAttribArray(GLKVertexAttribPosition); \
261  glEnableVertexAttribArray(GLKVertexAttribColor); \
262  glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, vvMacroVertices); \
263  glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, vvMacroColors); \
264  glDrawArrays(GL_LINES, 0, 8); \
265  glDisableVertexAttribArray(GLKVertexAttribPosition); \
266  glDisableVertexAttribArray(GLKVertexAttribColor); \
267 }
268 
269 // this is a macro for drawing a line connecting two points
270 #define GLDRAWLINE(p,q) \
271 { \
272  GLfloat vvMacroVertices[]={ \
273  p.x, p.y, 0.0, \
274  q.x, q.y, 0.0}; \
275  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
276  glDrawArrays(GL_LINES,0,2); \
277 }
278 
279 
280 
281 // this is a macro for drawing a diamond specified by a point and radius in opengl
282 #define GLDRAWDIAMOND(p,r) \
283 { \
284  GLfloat vvMacroVertices[] = { \
285  p.x-r, p.y, 0.0, \
286  p.x, p.y+r, 0.0, \
287  p.x+r, p.y, 0.0, \
288  p.x, p.y-r, 0.0}; \
289  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
290  glDrawArrays(GL_QUADS,0,4); \
291 }
292 
293 
294 
295 // this is a macro for stroking an diamond around a point in opengl
296 #define GLSTROKEDIAMOND(p,r) \
297 { \
298  GLfloat vvMacroVertices[] = { \
299  p.x-r, p.y, 0.0, \
300  p.x, p.y+r, 0.0, \
301  p.x, p.y+r, 0.0, \
302  p.x+r, p.y, 0.0, \
303  p.x+r, p.y, 0.0, \
304  p.x, p.y-r, 0.0, \
305  p.x, p.y-r, 0.0, \
306  p.x-r, p.y, 0.0}; \
307  glVertexPointer(3,GL_FLOAT,0,vvMacroVertices); \
308  glDrawArrays(GL_LINE_LOOP,0,8); \
309 }
310 
311 // NOTE: this macro will not function correctly if you forget to glEnable(texTarget) or glEnableClientState() for GL_VERTEX_ARRAY and GL_TEXTURE_COORD_ARRAY.
312 // 'texName' is the texture name (from glGenTextures())
313 // 'texTarget' is the target (GL_TEXTURE_RECTANGLE_EXT or GL_TEXTURE_2D
314 // 'texFlipped' is BOOL- whether or not tex is flipped vertically
315 // 'srcRect' is the coords of the tex to draw. REMEMBER: GL_TEXTURE_2D coords are NORMALIZED!
316 // 'dstRect' are the coords of the rect to draw the tex in.
317 #define GLDRAWTEXQUADMACRO(texName,texTarget,texFlipped,src,dst) \
318 { \
319  GLuint localMacroTexTarget=texTarget; \
320  NSRect localMacroSrc=src; \
321  NSRect localMacroDst=dst; \
322  BOOL localMacroFlip=texFlipped; \
323  GLfloat vvMacroVerts[]={ \
324  VVMINX(localMacroDst), VVMINY(localMacroDst), 0.0, \
325  VVMAXX(localMacroDst), VVMINY(localMacroDst), 0.0, \
326  VVMAXX(localMacroDst), VVMAXY(localMacroDst), 0.0, \
327  VVMINX(localMacroDst), VVMAXY(localMacroDst), 0.0}; \
328  GLfloat vvMacroTexs[]={ \
329  VVMINX(localMacroSrc), (localMacroFlip ? VVMAXY(localMacroSrc) : VVMINY(localMacroSrc)), \
330  VVMAXX(localMacroSrc), (localMacroFlip ? VVMAXY(localMacroSrc) : VVMINY(localMacroSrc)), \
331  VVMAXX(localMacroSrc), (localMacroFlip ? VVMINY(localMacroSrc) : VVMAXY(localMacroSrc)), \
332  VVMINX(localMacroSrc), (localMacroFlip ? VVMINY(localMacroSrc) : VVMAXY(localMacroSrc))}; \
333  glVertexPointer(3,GL_FLOAT,0,vvMacroVerts); \
334  glTexCoordPointer(2,GL_FLOAT,0,vvMacroTexs); \
335  glBindTexture(localMacroTexTarget,texName); \
336  glDrawArrays(GL_QUADS,0,4); \
337  glBindTexture(localMacroTexTarget,0); \
338 }
339 
340 
341 
342 
343 // this is a macro for using an NSColor to set a GL color!
344 #define NSTOGLCLEARCOLOR(c) { \
345  if (c!=nil) { \
346  CGFloat comps[4]; \
347  [c getComponents:comps]; \
348  glClearColor(comps[0],comps[1],comps[2],comps[3]); \
349  } \
350 }
351 #define NSTOGLCOLOR(c) { \
352  if (c!=nil) { \
353  CGFloat comps[4]; \
354  [c getComponents:comps]; \
355  glColor4f(comps[0],comps[1],comps[2],comps[3]); \
356  } \
357 }
358