-
Notifications
You must be signed in to change notification settings - Fork 0
/
CS123Common.h
167 lines (131 loc) · 3.62 KB
/
CS123Common.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*!
@file CS123Common.h
@author Travis Fischer (fisch0920@gmail.com)
@author Ben Herila (ben@herila.net)
@date Fall 2010
@brief Contains data structures and macros commonly used in CS123.
*/
#pragma once
#ifndef __CS123COMMON_H__
#define __CS123COMMON_H__
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
// from http://en.wikipedia.org/wiki/Assertion_(computing)
#define COMPILE_TIME_ASSERT(pred) switch(0){case 0:case pred:;}
typedef float REAL;
//! A structure for a color. Each channel is 8 bits [0-255].
struct BGRA
{
BGRA() : b(0), g(0), r(0), a(0) {}
BGRA(unsigned char red, unsigned char green, unsigned char blue, unsigned char alpha = 255) : b(blue), g(green), r(red), a(alpha) {}
// C++ TIP:
// A union struct. Essentially, this makes b <==> channels[0],
// g <==> channels[1], etc. In other words: b, g, r, and a are
// stored at the same memory location as channels[4]. Note that
// sizeof(b)+sizeof(g)+sizeof(r)+sizeof(a) = sizeof(channels)
// so the memory overlaps **exactly**.
//
// You might want to take advantage of union structs in later
// assignments, although we don't require or expect you to do so.
//
union {
struct { unsigned char b, g, r, a; };
unsigned char channels[4];
};
// @TODO: [OPTIONAL] You can implement some operators here for color arithmetic.
};
//! A structure to store a rectangle on the screen.
struct CS123Rectangle {
int minX;
int maxX;
int minY;
int maxY;
};
// ----------------------
// Deallocation Utilities
// ----------------------
#define safeFree(p) \
do { \
if ((p)) { \
free((p)); \
(p) = NULL; \
} \
} while(0)
#define safeDelete(p) \
do { \
if ((p)) { \
delete ((p)); \
(p) = NULL; \
} \
} while(0)
#define safeDeleteArray(p) \
do { \
if ((p)) { \
delete[] ((p)); \
(p) = NULL; \
} \
} while(0)
// ---------------------
// Common math utilities
// ---------------------
#ifdef MIN
#undef MIN
#endif
#ifdef MAX
#undef MAX
#endif
#ifdef MIN3
#undef MIN3
#endif
#ifdef MAX3
#undef MAX3
#endif
#ifdef ABS
#undef ABS
#endif
#define MIN(x, y) ({ \
typeof (x) x_ = (x); \
typeof (y) y_ = (y); \
((x_ < y_) ? x_ : y_); \
})
#define MAX(x, y) ({ \
typeof (x) _x_ = (x); \
typeof (y) _y_ = (y); \
((_x_ > _y_) ? _x_ : _y_); \
})
#define MIN3(x, y, z) ({ \
typeof (x) x__ = (x); \
typeof (y) y__ = (y); \
typeof (z) z__ = (z); \
((x__ < y__) ? (x__ < z__ ? x__ : z__) : (y__ < z__ ? y__ : z__)); \
})
#define MAX3(x, y, z) ({ \
typeof (x) __x = (x); \
typeof (y) __y = (y); \
typeof (z) __z = (z); \
((__x > __y) ? (__x > __z ? __x: __z) : (__y > __z ? __y : __z)); \
})
#define ABS(x) ({ \
register typeof(x) ___tx = (x); \
(___tx >= 0 ? ___tx : -___tx); \
})
#ifndef M_PI
#define M_PI 3.14159265f
#endif
#define _EPSILON_ 1e-8
#define EPSILON _EPSILON_
#define EQ(a, b) (fabs((a) - (b)) < _EPSILON_)
#define NEQ(a, b) (fabs((a) - (b)) > _EPSILON_)
#define NYI(f) { \
char ss[999]; \
(sprintf(ss, "Not yet implemented: %s, file %s, line %d\n", \
f, __FILE__, __LINE__)); \
QMessageBox mb; \
mb.setText(QString(ss)); \
mb.setIcon(QMessageBox::Critical); \
mb.exec(); \
exit(0xf); \
}
#endif