Changeset 1135
- Timestamp:
- Jan 27, 2012, 9:05:00 AM (11 years ago)
- Location:
- trunk
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/monsterz/board.cpp
r1134 r1135 133 133 position = ivec3(24, 72, 1); 134 134 bbox[0] = position; 135 bbox[1] = bbox[0] + ivec3(384, 384, 0);135 bbox[1] = bbox[0] + vec3(384, 384, 0); 136 136 137 137 Input::TrackMouse(this); … … 189 189 if (cur_cell.x < 0 || cur_cell.x >= data->dim.x 190 190 || cur_cell.y < 0 || cur_cell.y >= data->dim.y 191 || (cur_pos - cur_cell * data->size).sqlen() > data->size.sqlen() / 8192 || (cur_cell - data->src_cell).sqlen() != 1)191 || sqlen(cur_pos - cur_cell * data->size) > sqlen(data->size) / 8 192 || sqlen(cur_cell - data->src_cell) != 1) 193 193 cur_cell = ivec2(-1); 194 194 /* If potential target changed, update our cache. */ … … 221 221 222 222 /* Mouse released, or exited window, or dragged too far. */ 223 if (!buttons[0] || mousepos.x == -1 224 ||(data->src_cell * data->size - data->grabbed.piece->GetPos()225 - data->grabbed.piece->GetOffset()) .sqlen()> 100 * 100)223 if (!buttons[0] || mousepos.x == -1 || 224 sqlen(data->src_cell * data->size - data->grabbed.piece->GetPos() 225 - data->grabbed.piece->GetOffset()) > 100 * 100) 226 226 { 227 227 ivec2 cell_a = data->src_cell; -
trunk/monsterz/interface.cpp
r1046 r1135 62 62 position = ivec3(0, 0, 1); 63 63 bbox[0] = position; 64 bbox[1] = bbox[0] + ivec3(640, 480, 0);64 bbox[1] = bbox[0] + vec3(640, 480, 0); 65 65 } 66 66 -
trunk/monsterz/piece.cpp
r1046 r1135 201 201 case PieceData::MOVE: 202 202 /* Only allow swaps when piece is close enough to its target cell. */ 203 if ( GetShift().sqlen() > data->size.sqlen() / 8)203 if (sqlen(GetShift()) > sqlen(data->size) / 8) 204 204 return 0; 205 205 /* Fall through */ … … 277 277 278 278 ivec2 trip = data->pos_dst - data->pos_src; 279 float pos_time = trip.len() / data->speed;279 float pos_time = len(trip) / data->speed; 280 280 float t = pos_time ? data->timer / pos_time : 1.0f; 281 281 if (t >= 1.0f) 282 282 t = 1.0f; 283 data->pos = data->pos_src+ t * vec2(trip) + vec2(0.5f);284 285 float off_time = data->off_src.len() / data->speed;283 data->pos = vec2(data->pos_src) + t * vec2(trip) + vec2(0.5f); 284 285 float off_time = len(data->off_src) / data->speed; 286 286 t = off_time ? data->timer / off_time : 1.0f; 287 287 if (t >= 1.0f) … … 354 354 break; 355 355 case PieceData::UNGRAB: 356 if ( (data->cell * data->size - data->pos_src).sqlen()357 > data->size.sqlen() / 8)356 if (sqlen(data->cell * data->size - data->pos_src) 357 > sqlen(data->size) / 8) 358 358 off = 7; 359 359 z = 8; -
trunk/monsterz/title.cpp
r1046 r1135 135 135 position = ivec3(0, 0, 1); 136 136 bbox[0] = position; 137 bbox[1] = bbox[0] + ivec3(640, 480, 0);137 bbox[1] = bbox[0] + vec3(640, 480, 0); 138 138 139 139 srand(rand() ^ time(NULL)); … … 145 145 146 146 data->ground = Tiler::Register(PNG_TITLE_GROUND, ivec2(384, 80), ivec2(0), 1.0f); 147 data->ground_pos = ivec2((bbox[1] - bbox[0]).xy() / ivec2(2, 4))147 data->ground_pos = ivec2((bbox[1] - bbox[0]).xy() / vec2(2, 4)) 148 148 - ivec2(192, 80); 149 149 150 150 data->rocks = Tiler::Register(PNG_TITLE_ROCKS, ivec2(640, 155), ivec2(0), 1.0f); 151 data->rocks_pos = ivec2((bbox[1] - bbox[0]).xy() / ivec2(2, 2))151 data->rocks_pos = ivec2((bbox[1] - bbox[0]).xy() / vec2(2, 2)) 152 152 - ivec2(320, 240); 153 153 -
trunk/src/debug/quad.cpp
r1110 r1135 201 201 /* Prepare our quad coordinates */ 202 202 ivec2 const layout(7, 5); 203 data->step = vec2(2.0f, -2.0f) / (4 * layout + ivec2(1));203 data->step = vec2(2.0f, -2.0f) / vec2(4 * layout + ivec2(1)); 204 204 data->orig = vec2(-1.0f, 1.0f) + data->step; 205 205 data->aa = data->orig; -
trunk/src/input.cpp
r1057 r1135 135 135 if (data->entities[n] == top) 136 136 { 137 data->entities[n]->mousepos = coord - top->bbox[0].xy();137 data->entities[n]->mousepos = coord - (ivec2)top->bbox[0].xy(); 138 138 if (top != data->lastfocus) 139 139 data->entities[n]->pressed = data->buttons; -
trunk/src/lol/math/matrix.h
r1134 r1135 50 50 VECTOR_TYPES(Mat4, mat4) 51 51 52 #define VECTOR_OP(op) \ 53 inline type_t operator op(type_t const &val) const \ 54 { \ 55 type_t ret; \ 56 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 57 ret[n] = (*this)[n] op val[n]; \ 58 return ret; \ 59 } \ 60 \ 61 inline type_t operator op##=(type_t const &val) \ 62 { \ 63 return *this = (*this) op val; \ 64 } 65 66 #define BOOL_OP(op, op2, ret) \ 67 inline bool operator op(type_t const &val) const \ 68 { \ 69 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 70 if (!((*this)[n] op2 val[n])) \ 71 return !ret; \ 72 return ret; \ 73 } 74 75 #define SCALAR_OP(op) \ 76 inline type_t operator op(T const &val) const \ 77 { \ 78 type_t ret; \ 79 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \ 80 ret[n] = (*this)[n] op val; \ 81 return ret; \ 82 } \ 83 \ 84 inline type_t operator op##=(T const &val) \ 85 { \ 86 return *this = (*this) op val; \ 87 } 88 89 #define LINEAR_OPS() \ 52 #define MEMBER_OPS() \ 90 53 inline T& operator[](int n) { return *(&x + n); } \ 91 54 inline T const& operator[](int n) const { return *(&x + n); } \ 92 55 \ 93 VECTOR_OP(-) \94 VECTOR_OP(+) \95 \96 BOOL_OP(==, ==, true) \97 BOOL_OP(!=, ==, false) \98 \99 SCALAR_OP(*) \100 SCALAR_OP(/) \101 \102 inline type_t operator -() const \103 { \104 type_t ret; \105 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \106 ret[n] = -(*this)[n]; \107 return ret; \108 } \109 \110 inline T sqlen() const \111 { \112 T acc = 0; \113 for (size_t n = 0; n < sizeof(*this) / sizeof(T); n++) \114 acc += (*this)[n] * (*this)[n]; \115 return acc; \116 } \117 \118 inline double len() const \119 { \120 using namespace std; \121 return sqrt((double)sqlen()); \122 } \123 \124 56 void printf() const; 125 57 … … 140 72 } \ 141 73 \ 142 inline T norm() const { return len( ); }74 inline T norm() const { return len(*this); } 143 75 144 76 #define QUATERNION_OPS() \ … … 168 100 ret[3] = (*this)[3]; \ 169 101 return ret; \ 170 } \ 171 \ 172 inline T norm() const { return sqlen(); } 173 174 #define OTHER_OPS(tname) \ 175 VECTOR_OP(*) \ 176 VECTOR_OP(/) \ 177 \ 178 BOOL_OP(<=, <=, true) \ 179 BOOL_OP(>=, >=, true) \ 180 BOOL_OP(<, <, true) \ 181 BOOL_OP(>, >, true) \ 182 \ 102 } 103 104 #define OTHER_MEMBER_OPS(tname) \ 183 105 template<typename U> \ 184 106 inline operator tname<U>() const \ … … 188 110 ret[n] = static_cast<U>((*this)[n]); \ 189 111 return ret; \ 190 } \ 191 \ 192 template<typename U> \ 193 friend U dot(tname<U>, tname<U>); 112 } 194 113 195 114 #define SWIZZLE2(e1, e2) \ … … 266 185 inline Vec2(T _x, T _y) { x = _x; y = _y; } 267 186 268 LINEAR_OPS()269 OTHER_ OPS(Vec2)187 MEMBER_OPS() 188 OTHER_MEMBER_OPS(Vec2) 270 189 271 190 SWIZZLE22(x); SWIZZLE22(y); … … 298 217 inline Cmplx(T _x, T _y) : x(_x), y(_y) { } 299 218 300 LINEAR_OPS() 219 MEMBER_OPS() 220 301 221 COMPLEX_OPS() 302 222 … … 312 232 static inline Cmplx<T> re(Cmplx<T> const &val) 313 233 { 314 return ~val / val.sqlen(); 315 } 316 317 template<typename T> 318 static inline Cmplx<T> operator /(T x, Cmplx<T> const &y) 319 { 320 return x * re(y); 321 } 322 323 template<typename T> 324 static inline Cmplx<T> operator /(Cmplx<T> x, Cmplx<T> const &y) 325 { 326 return x * re(y); 327 } 234 return ~val / sqlen(val); 235 } 236 237 template<typename T> 238 static inline Cmplx<T> operator /(T a, Cmplx<T> const &b) 239 { 240 return a * re(b); 241 } 242 243 template<typename T> 244 static inline Cmplx<T> operator /(Cmplx<T> a, Cmplx<T> const &b) 245 { 246 return a * re(b); 247 } 248 249 template<typename T> 250 static inline bool operator ==(Cmplx<T> const &a, T b) 251 { 252 return (a.x == b) && !a.y; 253 } 254 255 template<typename T> 256 static inline bool operator !=(Cmplx<T> const &a, T b) 257 { 258 return (a.x != b) || a.y; 259 } 260 261 template<typename T> 262 static inline bool operator ==(T a, Cmplx<T> const &b) { return b == a; } 263 264 template<typename T> 265 static inline bool operator !=(T a, Cmplx<T> const &b) { return b != a; } 328 266 329 267 /* … … 341 279 inline Vec3(T _x, Vec2<T> _yz) { x = _x; y = _yz.x; z = _yz.y; } 342 280 343 LINEAR_OPS()344 OTHER_ OPS(Vec3)281 MEMBER_OPS() 282 OTHER_MEMBER_OPS(Vec3) 345 283 346 284 SWIZZLE23(x); SWIZZLE23(y); SWIZZLE23(z); … … 382 320 inline Vec4(T _x, Vec3<T> _yzw) : x(_x), y(_yzw.x), z(_yzw.y), w(_yzw.z) { } 383 321 384 LINEAR_OPS()385 OTHER_ OPS(Vec4)322 MEMBER_OPS() 323 OTHER_MEMBER_OPS(Vec4) 386 324 387 325 SWIZZLE24(x); SWIZZLE24(y); SWIZZLE24(z); SWIZZLE24(w); … … 416 354 Quat(Mat4<T> const &m); 417 355 418 LINEAR_OPS() 356 MEMBER_OPS() 357 419 358 QUATERNION_OPS() 420 359 … … 428 367 429 368 template<typename T> 369 inline T norm(Quat<T> const &val) 370 { 371 return sqlen(val); 372 } 373 374 template<typename T> 430 375 static inline Quat<T> re(Quat<T> const &val) 431 376 { 432 return ~val / val.norm();377 return ~val / norm(val); 433 378 } 434 379 … … 449 394 */ 450 395 451 #define SCALAR_GLOBAL(tname, op, U) \ 452 template<typename T> \ 453 static inline tname<U> operator op(U const &val, tname<T> const &that) \ 396 #define VECTOR_OP(tname, op) \ 397 template<typename T> \ 398 inline tname<T> operator op(tname<T> const &a, tname<T> const &b) \ 399 { \ 400 tname<T> ret; \ 401 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 402 ret[n] = a[n] op b[n]; \ 403 return ret; \ 404 } \ 405 \ 406 template<typename T> \ 407 inline tname<T> operator op##=(tname<T> &a, tname<T> const &b) \ 408 { \ 409 return a = a op b; \ 410 } 411 412 #define BOOL_OP(tname, op, op2, ret) \ 413 template<typename T> \ 414 inline bool operator op(tname<T> const &a, tname<T> const &b) \ 415 { \ 416 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 417 if (!(a[n] op2 b[n])) \ 418 return !ret; \ 419 return ret; \ 420 } 421 422 #define SCALAR_OP(tname, op) \ 423 template<typename T> \ 424 inline tname<T> operator op##=(tname<T> &a, T const &val) \ 425 { \ 426 return a = a op val; \ 427 } \ 428 \ 429 template<typename T> \ 430 static inline tname<T> operator op(tname<T> const &a, T const &val) \ 431 { \ 432 tname<T> ret; \ 433 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 434 ret[n] = a[n] op val; \ 435 return ret; \ 436 } 437 438 #define SCALAR_PROMOTE_OP(tname, op, U) \ 439 template<typename T> \ 440 static inline tname<U> operator op(U const &val, tname<T> const &a) \ 454 441 { \ 455 442 tname<U> ret; \ 456 for (size_t n = 0; n < sizeof( that) / sizeof(that[0]); n++) \457 ret[n] = val op that[n]; \443 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 444 ret[n] = val op a[n]; \ 458 445 return ret; \ 459 446 } 460 447 461 #define SCALAR_GLOBAL2(tname, op) \462 SCALAR_GLOBAL(tname, op, int) \463 SCALAR_GLOBAL(tname, op, float) \464 SCALAR_GLOBAL(tname, op, double)465 466 448 #define GLOBALS(tname) \ 467 SCALAR_GLOBAL2(tname, *) \ 449 SCALAR_OP(tname, *) \ 450 SCALAR_OP(tname, /) \ 451 \ 452 SCALAR_PROMOTE_OP(tname, *, int) \ 453 SCALAR_PROMOTE_OP(tname, *, float) \ 454 SCALAR_PROMOTE_OP(tname, *, double) \ 455 \ 456 VECTOR_OP(tname, -) \ 457 VECTOR_OP(tname, +) \ 458 \ 459 BOOL_OP(tname, ==, ==, true) \ 460 BOOL_OP(tname, !=, ==, false) \ 461 \ 462 template<typename T> \ 463 inline tname<T> operator -(tname<T> const &a) \ 464 { \ 465 tname<T> ret; \ 466 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 467 ret[n] = -a[n]; \ 468 return ret; \ 469 } \ 470 \ 471 template<typename T> \ 472 inline T sqlen(tname<T> const &a) \ 473 { \ 474 T acc = 0; \ 475 for (size_t n = 0; n < sizeof(a) / sizeof(T); n++) \ 476 acc += a[n] * a[n]; \ 477 return acc; \ 478 } \ 479 \ 480 template<typename T> \ 481 inline double len(tname<T> const &a) \ 482 { \ 483 using namespace std; \ 484 return sqrt((double)sqlen(a)); \ 485 } \ 486 \ 487 /* dot() does not take const refs because the function is not inlined, \ 488 * so we try to avoid carrying a shitty pointer around. */ \ 489 template<typename T> \ 490 T dot(tname<T> a, tname<T> b); \ 468 491 \ 469 492 template<typename T> \ 470 493 static inline tname<T> normalize(tname<T> const &val) \ 471 494 { \ 472 T norm = val.len(); \473 return norm ? val / norm : val * 0; \495 T norm = len(val); \ 496 return norm ? val / norm : val * (T)0; \ 474 497 } 475 498 … … 479 502 GLOBALS(Vec4) 480 503 GLOBALS(Quat) 504 505 #define OTHER_OPS(tname) \ 506 VECTOR_OP(tname, *) \ 507 VECTOR_OP(tname, /) \ 508 \ 509 BOOL_OP(tname, <=, <=, true) \ 510 BOOL_OP(tname, >=, >=, true) \ 511 BOOL_OP(tname, <, <, true) \ 512 BOOL_OP(tname, >, >, true) 513 514 OTHER_OPS(Vec2) 515 OTHER_OPS(Vec3) 516 OTHER_OPS(Vec4) 481 517 482 518 /* -
trunk/src/matrix.cpp
r1052 r1135 277 277 { 278 278 mat4 ret(1.0f); 279 float n = q.norm();279 float n = norm(q); 280 280 281 281 if (!n) -
trunk/test/tutorial/tut03.cpp
r1104 r1135 72 72 else 73 73 m_window2world = 0.5 / m_window_size.x; 74 m_texel2world = (vec2)m_window_size / (vec2)m_size * m_window2world; 74 m_texel2world = (vec2)m_window_size / (vec2)m_size 75 * (vec2)m_window2world; 75 76 76 77 m_oldmouse = ivec2(0, 0); … … 369 370 z3 = z2 * z2 + r0; 370 371 z0 = z3 * z3 + r0; 371 if ( z0.sqlen() >= maxsqlen)372 if (sqlen(z0) >= maxsqlen) 372 373 break; 373 374 iter -= 4; … … 378 379 if (iter) 379 380 { 380 double n = z0.sqlen();381 382 if ( z1.sqlen() >= maxsqlen) { iter += 3; n = z1.sqlen(); }383 else if ( z2.sqlen() >= maxsqlen) { iter += 2; n = z2.sqlen(); }384 else if ( z3.sqlen() >= maxsqlen) { iter += 1; n = z3.sqlen(); }381 double n = sqlen(z0); 382 383 if (sqlen(z1) >= maxsqlen) { iter += 3; n = sqlen(z1); } 384 else if (sqlen(z2) >= maxsqlen) { iter += 2; n = sqlen(z2); } 385 else if (sqlen(z3) >= maxsqlen) { iter += 1; n = sqlen(z3); } 385 386 386 387 if (n > maxsqlen * maxsqlen) -
trunk/test/unit/quat.cpp
r1107 r1135 68 68 quat a(2.0f, -2.0f, -8.0f, 3.0f); 69 69 70 LOLUNIT_ASSERT_EQUAL( a.norm(), 81.0f);70 LOLUNIT_ASSERT_EQUAL(norm(a), 81.0f); 71 71 72 72 quat b = a * ~a; 73 quat c(0.0f, 0.0f, 0.0f, a.norm());73 quat c(0.0f, 0.0f, 0.0f, norm(a)); 74 74 75 75 LOLUNIT_ASSERT_EQUAL(b, c); … … 77 77 quat d(2.0f, 3.0f, -4.0f, -1.0f); 78 78 79 LOLUNIT_ASSERT_EQUAL( (a * d).norm(), a.norm() * d.norm());79 LOLUNIT_ASSERT_EQUAL(norm(a * d), norm(a) * norm(d)); 80 80 } 81 81 … … 87 87 quat one(0.0f, 0.0f, 0.0f, 1.0f); 88 88 89 LOLUNIT_ASSERT_EQUAL( i.norm(), 1.0f);90 LOLUNIT_ASSERT_EQUAL( j.norm(), 1.0f);91 LOLUNIT_ASSERT_EQUAL( k.norm(), 1.0f);92 LOLUNIT_ASSERT_EQUAL( one.norm(), 1.0f);89 LOLUNIT_ASSERT_EQUAL(norm(i), 1.0f); 90 LOLUNIT_ASSERT_EQUAL(norm(j), 1.0f); 91 LOLUNIT_ASSERT_EQUAL(norm(k), 1.0f); 92 LOLUNIT_ASSERT_EQUAL(norm(one), 1.0f); 93 93 94 94 LOLUNIT_ASSERT_EQUAL(i * i, -one); … … 110 110 quat b = normalize(a); 111 111 112 LOLUNIT_ASSERT_DOUBLES_EQUAL( b.norm(), 1.0, 1e-8);112 LOLUNIT_ASSERT_DOUBLES_EQUAL(norm(b), 1.0, 1e-8); 113 113 } 114 114
Note: See TracChangeset
for help on using the changeset viewer.