diff --git a/base/backtrace.cpp b/base/backtrace.cpp new file mode 100644 index 0000000000..422b1e17b8 --- /dev/null +++ b/base/backtrace.cpp @@ -0,0 +1,17 @@ +#include "base/backtrace.h" + +#ifndef _MSC_VER + +// LINUX ONLY + +#include +#include + +static void *backtrace_buffer[128]; + +void PrintBacktraceToStderr() { + int num_addrs = backtrace(backtrace_buffer, 128); + backtrace_symbols_fd(backtrace_buffer, num_addrs, STDERR_FILENO); +} + +#endif \ No newline at end of file diff --git a/base/backtrace.h b/base/backtrace.h new file mode 100644 index 0000000000..4ff0917f30 --- /dev/null +++ b/base/backtrace.h @@ -0,0 +1,3 @@ +#pragma once + +void PrintBacktraceToStderr(); diff --git a/base/fastlist.h b/base/fastlist.h new file mode 100644 index 0000000000..78ba4e5512 --- /dev/null +++ b/base/fastlist.h @@ -0,0 +1,37 @@ +#pragma once + +// An inline fastlist is a fixed size array with a position counter. +// Objects stored in a fastlist must be copyable. +// [] returns the objects in consecutive order, up to and not including +// the value of .size(). +// Order is not preserved when removing objects. +template +class InlineFastList { + public: + InlineFastList() : count_(0) {} + ~InlineFastList() {} + + T& operator [](int index) { return data_[index]; } + const T& operator [](int index) const { return data_[index]; } + int size() const { return count_; } + + void Add(T t) { + data_[count_++] = t; + } + void RemoveAt(int index) { + data_[index] = data_[count_ - 1]; + count_--; + } + void Remove(T t) { // Requires operator== + for (int i = 0; i < count_; i++) { + if (data_[i] == t) { + RemoveAt(i); + return; + } + } + } + + private: + T data_[max_size]; + int count_; +}; diff --git a/base/fastlist_test.cpp b/base/fastlist_test.cpp new file mode 100644 index 0000000000..57064adcc4 --- /dev/null +++ b/base/fastlist_test.cpp @@ -0,0 +1,19 @@ +#include "base/fastlist.h" +#include "base/logging.h" + +/* +#include + +TEST(fastlist, AddRemove) { + InlineFastList list; + list.Add(8); + list.Remove(7); + EXPECT_EQ(1, list.size()); + list.Remove(8); + EXPECT_EQ(0, list.size()); + list.Add(1); + list.Add(2); + list.Add(3); + EXPECT_EQ(3, list.size()); +} +*/ \ No newline at end of file diff --git a/base/linked_ptr.h b/base/linked_ptr.h new file mode 100644 index 0000000000..4be7c3ddab --- /dev/null +++ b/base/linked_ptr.h @@ -0,0 +1,113 @@ +// From http://ootips.org/yonat/4dev/linked_ptr.h + +/* + * linked_ptr - simple reference linked pointer + * (like reference counting, just using a linked list of the references + * instead of their count.) + * + * The implementation stores three pointers for every linked_ptr, but + * does not allocate anything on the free store. + */ + +#pragma once + +/* For ANSI-challenged compilers, you may want to #define + * NO_MEMBER_TEMPLATES, explicit or mutable */ + +#ifdef _MSC_VER +#define NO_MEMBER_TEMPLATES +#endif + +template class linked_ptr +{ +public: + +#ifndef NO_MEMBER_TEMPLATES +# define TEMPLATE_FUNCTION template + TEMPLATE_FUNCTION friend class linked_ptr; +#else +# define TEMPLATE_FUNCTION + typedef X Y; +#endif + + typedef X element_type; + + explicit linked_ptr(X* p = 0) throw() + : itsPtr(p) {itsPrev = itsNext = this;} + ~linked_ptr() + {release();} + linked_ptr(const linked_ptr& r) throw() + {acquire(r);} + linked_ptr& operator=(const linked_ptr& r) + { + if (this != &r) { + release(); + acquire(r); + } + return *this; + } + +#ifndef NO_MEMBER_TEMPLATES + template friend class linked_ptr; + template linked_ptr(const linked_ptr& r) throw() + {acquire(r);} + template linked_ptr& operator=(const linked_ptr& r) + { + if (this != &r) { + release(); + acquire(r); + } + return *this; + } +#endif // NO_MEMBER_TEMPLATES + + X& operator*() const throw() {return *itsPtr;} + X* operator->() const throw() {return itsPtr;} + X* get() const throw() {return itsPtr;} + bool unique() const throw() {return itsPrev ? itsPrev==this : true;} + +private: + X* itsPtr; + mutable const linked_ptr* itsPrev; + mutable const linked_ptr* itsNext; + + void acquire(const linked_ptr& r) throw() + { // insert this to the list + itsPtr = r.itsPtr; + itsNext = r.itsNext; + itsNext->itsPrev = this; + itsPrev = &r; +#ifndef mutable + r.itsNext = this; +#else // for ANSI-challenged compilers + (const_cast*>(&r))->itsNext = this; +#endif + } + +#ifndef NO_MEMBER_TEMPLATES + template void acquire(const linked_ptr& r) throw() + { // insert this to the list + itsPtr = r.itsPtr; + itsNext = r.itsNext; + itsNext->itsPrev = this; + itsPrev = &r; +#ifndef mutable + r.itsNext = this; +#else // for ANSI-challenged compilers + (const_cast*>(&r))->itsNext = this; +#endif + } +#endif // NO_MEMBER_TEMPLATES + + void release() + { // erase this from the list, delete if unique + if (unique()) delete itsPtr; + else { + itsPrev->itsNext = itsNext; + itsNext->itsPrev = itsPrev; + itsPrev = itsNext = 0; + } + itsPtr = 0; + } +}; + diff --git a/math/lin/aabb.cpp b/math/lin/aabb.cpp index 4e73b512a0..6a04ddab14 100644 --- a/math/lin/aabb.cpp +++ b/math/lin/aabb.cpp @@ -7,8 +7,6 @@ #define LEFT 1 #define MIDDLE 2 -namespace lin { - static const float flt_plus_inf = -logf(0); // let's keep C and C++ compilers happy. AABB::AABB() : minB(0,0,0),maxB(0,0,0) { @@ -268,5 +266,3 @@ bool AABB::BehindPlane(const Plane &plane) const { return true; } - -} // namespace lin diff --git a/math/lin/aabb.h b/math/lin/aabb.h index 22f2a7b80d..6f887f876e 100644 --- a/math/lin/aabb.h +++ b/math/lin/aabb.h @@ -1,11 +1,8 @@ -#ifndef _MATH_LIN_AABB -#define _MATH_LIN_AABB +#pragma once #include "math/lin/vec3.h" -namespace lin { - -class Ray; +struct Ray; class Plane; struct AABB { @@ -41,7 +38,3 @@ struct AABB { bool BehindPlane(const Plane &plane) const; }; - -} // namespace lin - -#endif // _MATH_LIN_AABB diff --git a/math/lin/plane.cpp b/math/lin/plane.cpp index 31b52fa00d..67e8ef631a 100644 --- a/math/lin/plane.cpp +++ b/math/lin/plane.cpp @@ -1,13 +1,10 @@ #include "math/lin/matrix4x4.h" #include "math/lin/plane.h" -namespace lin { -Plane Plane::TransformByIT(const Matrix4x4 &m, Plane *out) { +void Plane::TransformByIT(const Matrix4x4 &m, Plane *out) { out->x = x * m.xx + y * m.yx + z * m.zx + d * m.wx; out->y = x * m.xy + y * m.yy + z * m.zy + d * m.wy; out->z = x * m.xz + y * m.yz + z * m.zz + d * m.wz; out->d = x * m.xw + y * m.yw + z * m.zw + d * m.ww; } - -} diff --git a/math/lin/plane.h b/math/lin/plane.h index 02c7ad22da..6466617c1b 100644 --- a/math/lin/plane.h +++ b/math/lin/plane.h @@ -3,8 +3,6 @@ #include "math/lin/vec3.h" -namespace lin { - class Matrix4x4; class Plane { @@ -24,7 +22,7 @@ class Plane { } void Normalize() { - float inv_length = 1.0 * sqrtf(x * x + y * y + z * z); + float inv_length = sqrtf(x * x + y * y + z * z); x *= inv_length; y *= inv_length; z *= inv_length; @@ -33,9 +31,7 @@ class Plane { // Matrix is the inverse transpose of the wanted transform. // out cannot be equal to this. - Plane TransformByIT(const Matrix4x4 &matrix, Plane *out); + void TransformByIT(const Matrix4x4 &matrix, Plane *out); }; -} - #endif diff --git a/math/lin/ray.h b/math/lin/ray.h index 5c9a5e91e0..2287460fbb 100644 --- a/math/lin/ray.h +++ b/math/lin/ray.h @@ -1,10 +1,7 @@ -#ifndef _MATH_LIN_RAY_H -#define _MATH_LIN_RAY_H +#pragma once #include "vec3.h" -namespace lin { - /* __declspec(align(16)) */ struct Ray { Vec3 origin; @@ -15,6 +12,3 @@ namespace lin { int pad3; }; -} // namespace lin - -#endif // _MATH_LIN_RAY_H diff --git a/math/lin/vec3.h b/math/lin/vec3.h index 5355f20977..e10fc0d3a9 100644 --- a/math/lin/vec3.h +++ b/math/lin/vec3.h @@ -22,6 +22,9 @@ public: Vec3() { } explicit Vec3(float f) {x=y=z=f;} + float operator [] (int i) const { return (&x)[i]; } + float &operator [] (int i) { return (&x)[i]; } + Vec3(const float _x, const float _y, const float _z) { x=_x; y=_y; z=_z; } diff --git a/math/math_util.h b/math/math_util.h index b6196f8240..71edd44a0b 100644 --- a/math/math_util.h +++ b/math/math_util.h @@ -1,8 +1,8 @@ #ifndef _MATHUTILS_H #define _MATHUTILS_H -#include -#include +#include +#include inline float sqr(float f) {return f*f;} inline float sqr_signed(float f) {return f<0 ? -f*f : f*f;} @@ -26,6 +26,9 @@ inline float Float16ToFloat(float16 ix) { #define PI 3.141592653589793f +#ifndef M_PI +#define M_PI 3.141592653589793f +#endif // The stuff in this file is from all over the web, esp. dspmusic.org. I think it's all public domain. // In any case, very little of it is used anywhere at the moment. diff --git a/native.vcxproj b/native.vcxproj index 909604b212..470181302e 100644 --- a/native.vcxproj +++ b/native.vcxproj @@ -97,12 +97,15 @@ + + + @@ -140,7 +143,9 @@ + + @@ -148,9 +153,12 @@ + + + @@ -167,6 +175,7 @@ + @@ -203,7 +212,9 @@ + + @@ -211,9 +222,11 @@ + + diff --git a/native.vcxproj.filters b/native.vcxproj.filters index e23e6418a1..a9609633e4 100644 --- a/native.vcxproj.filters +++ b/native.vcxproj.filters @@ -184,6 +184,30 @@ ui + + base + + + ui + + + base + + + base + + + math + + + math + + + util + + + util + @@ -333,6 +357,21 @@ ui + + ui + + + base + + + math + + + math + + + util + diff --git a/ui/list_screen.cpp b/ui/list_screen.cpp new file mode 100644 index 0000000000..c3cedcd59b --- /dev/null +++ b/ui/list_screen.cpp @@ -0,0 +1,6 @@ +#include "ui/list_screen.h" + +ListScreen::ListScreen() { + +} + diff --git a/ui/list_screen.h b/ui/list_screen.h new file mode 100644 index 0000000000..9dc08f17e2 --- /dev/null +++ b/ui/list_screen.h @@ -0,0 +1,10 @@ +#include "ui/screen.h" + + +class ListScreen { +public: + ListScreen(); + +private: + +}; \ No newline at end of file diff --git a/ui/screen.h b/ui/screen.h index cb168ec1d2..e2637599ff 100644 --- a/ui/screen.h +++ b/ui/screen.h @@ -56,7 +56,7 @@ private: Screen *currentScreen_; Screen *nextScreen_; - // Dialog stack. These are shown "on top" of base screens and back works as expected. + // Dialog stack. These are shown "on top" of base screens and the Android back button works as expected. // Used for options, in-game menus and other things you expect to be able to back out from onto something. std::list dialog_; }; diff --git a/ui/ui.cpp b/ui/ui.cpp index b4bda255a5..d6a0df757d 100644 --- a/ui/ui.cpp +++ b/ui/ui.cpp @@ -10,6 +10,8 @@ // TODO: UI should probably not own these. DrawBuffer ui_draw2d; DrawBuffer ui_draw2d_front; + +// This one, though, is OK. UIState uistate; // Theme. diff --git a/ui/ui.h b/ui/ui.h index 2849ff5690..78c16da1b5 100644 --- a/ui/ui.h +++ b/ui/ui.h @@ -5,6 +5,10 @@ // A common pattern is Adapter classes for changing how things are drawn // in lists, for example. // +// Immediate UI works great for overlay UI for games, for example, but is actually +// not really a good idea for full app UIs. Also, animations are difficult because +// there's not really any good place to store state. +// // hrydgard@gmail.com #pragma once diff --git a/util/random/perlin.cpp b/util/random/perlin.cpp index d0ee8a9fa4..e8a8a578df 100644 --- a/util/random/perlin.cpp +++ b/util/random/perlin.cpp @@ -47,14 +47,14 @@ inline float grad(int hash, float x, float y, float z) { return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } -float Noise(double z, double y, double x) { - double fx = floor(x), fy = floor(y), fz = floor(z); +float Noise(double iz, double iy, double ix) { + double fx = floor(ix), fy = floor(iy), fz = floor(iz); int X = (int)fx & 255, // FIND UNIT CUBE THAT Y = (int)fy & 255, // CONTAINS POINT. Z = (int)fz & 255; - x -= fx; // FIND RELATIVE X,Y,Z - y -= fy; // OF POINT IN CUBE. - z -= fz; + float x = (float)(ix - fx); // FIND RELATIVE X,Y,Z + float y = (float)(iy - fy); // OF POINT IN CUBE. + float z = (float)(iz - fz); float u = fade(x); // COMPUTE FADE CURVES float v = fade(y); // FOR EACH OF X,Y,Z. float w = fade(z); diff --git a/util/random/perlin.h b/util/random/perlin.h index ace8c521d3..ecda001c23 100644 --- a/util/random/perlin.h +++ b/util/random/perlin.h @@ -1,8 +1,6 @@ -#ifndef _PERLIN_H -#define _PERLIN_H +#pragma once // Implementation of "Improved Noise" // http://mrl.nyu.edu/~perlin/noise/ +// doubles are only used at the very start, not a big performance worry float Noise(double x, double y, double z); - -#endif // _PERLIN_H diff --git a/util/random/rng.h b/util/random/rng.h index 28369196a6..68e66d79b9 100644 --- a/util/random/rng.h +++ b/util/random/rng.h @@ -1,5 +1,4 @@ -#ifndef _RNG_H -#define _RNG_H +#pragma once #include "base/basictypes.h" @@ -22,5 +21,3 @@ class GMRng { uint32 m_w; uint32 m_z; }; - -#endif