From 414a0896c9683f1bb71a346aa1c35f0cb6fe3ab5 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 30 Jul 2014 21:10:43 +1000 Subject: [PATCH] Began merging the jam branch into the development (read more) The list of changes committed: * Removed SimpleRNG * Added Timer (for debugging) * Added BoundingBox * Added floating point snapToBase() * Added error check to Vector2::Normalize() * Updated makefiles, project compiles --- client/makefile | 2 +- common/debugging/makefile | 37 +++++++++ .../simple_rng.hpp => debugging/timer.cpp} | 27 ++----- common/debugging/timer.hpp | 54 +++++++++++++ common/makefile | 1 + common/utilities/bounding_box.hpp | 77 +++++++++++++++++++ common/utilities/utility.cpp | 4 + common/utilities/utility.hpp | 2 + common/utilities/vector2.hpp | 2 + server/makefile | 2 +- 10 files changed, 186 insertions(+), 22 deletions(-) create mode 100644 common/debugging/makefile rename common/{utilities/simple_rng.hpp => debugging/timer.cpp} (70%) create mode 100644 common/debugging/timer.hpp create mode 100644 common/utilities/bounding_box.hpp diff --git a/client/makefile b/client/makefile index 97d8949..f318d52 100644 --- a/client/makefile +++ b/client/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. scenes ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/ui ../common/utilities +INCLUDES+=. scenes ../common/debugging ../common/gameplay ../common/graphics ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/ui ../common/utilities LIBS+=client.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) diff --git a/common/debugging/makefile b/common/debugging/makefile new file mode 100644 index 0000000..a4efd0c --- /dev/null +++ b/common/debugging/makefile @@ -0,0 +1,37 @@ +#config +INCLUDES+=. +LIBS+= +CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES)) + +#source +CXXSRC=$(wildcard *.cpp) + +#objects +OBJDIR=obj +OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) + +#output +OUTDIR=.. +OUT=$(addprefix $(OUTDIR)/,libcommon.a) + +#targets +all: $(OBJ) $(OUT) + ar -crs $(OUT) $(OBJ) + +$(OBJ): | $(OBJDIR) + +$(OUT): | $(OUTDIR) + +$(OBJDIR): + mkdir $(OBJDIR) + +$(OUTDIR): + mkdir $(OUTDIR) + +$(OBJDIR)/%.o: %.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) *.o *.a *.exe + +rebuild: clean all diff --git a/common/utilities/simple_rng.hpp b/common/debugging/timer.cpp similarity index 70% rename from common/utilities/simple_rng.hpp rename to common/debugging/timer.cpp index 52ca91b..be3636a 100644 --- a/common/utilities/simple_rng.hpp +++ b/common/debugging/timer.cpp @@ -19,24 +19,11 @@ * 3. This notice may not be removed or altered from any source * distribution. */ -#ifndef SIMPLERNG_HPP_ -#define SIMPLERNG_HPP_ +#include "timer.hpp" -//a simple, stateless, random number generator -class SimpleRNG { -public: - SimpleRNG() { SetSeed(8891.0); } - SimpleRNG(double x) { SetSeed(x); } - - double SetSeed(double s) { return seed = s; } - double GetSeed() { return seed; } - - double operator()(double x) { - return (x + seed) * 11235.0 + 81321.0; - }; - -private: - double seed; -}; - -#endif \ No newline at end of file +std::ostream& operator<<(std::ostream& os, Timer& t) { + os << t.GetName() << ": "; + os << std::chrono::duration_cast(t.GetTime()).count(); + os << "ns"; + return os; +} diff --git a/common/debugging/timer.hpp b/common/debugging/timer.hpp new file mode 100644 index 0000000..7f8f38b --- /dev/null +++ b/common/debugging/timer.hpp @@ -0,0 +1,54 @@ +/* Copyright: (c) Kayne Ruse 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef TIMER_HPP_ +#define TIMER_HPP_ + +#include +#include +#include + +class Timer { +public: + typedef std::chrono::high_resolution_clock Clock; + + Timer() = default; + Timer(std::string s) : name(s), start(Clock::now()) {}; + ~Timer() = default; + + inline void Start() { start = Clock::now(); } + inline void Stop() { time = Clock::now() - start; } + + //accessors and mutators + Clock::duration GetTime() { return time; } + + std::string SetName(std::string s) { return name = s; } + std::string GetName() { return name; } + +private: + std::string name; + Clock::time_point start; + Clock::duration time; +}; + +std::ostream& operator<<(std::ostream& os, Timer& t); + +#endif diff --git a/common/makefile b/common/makefile index b670b7d..bdb1406 100644 --- a/common/makefile +++ b/common/makefile @@ -1,4 +1,5 @@ all: + $(MAKE) -C debugging $(MAKE) -C gameplay $(MAKE) -C graphics $(MAKE) -C map diff --git a/common/utilities/bounding_box.hpp b/common/utilities/bounding_box.hpp new file mode 100644 index 0000000..1d32887 --- /dev/null +++ b/common/utilities/bounding_box.hpp @@ -0,0 +1,77 @@ +/* Copyright: (c) Kayne Ruse 2013, 2014 + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * + * 3. This notice may not be removed or altered from any source + * distribution. +*/ +#ifndef BOUNDINGBOX_HPP_ +#define BOUNDINGBOX_HPP_ + +#include +#include + +class BoundingBox { +public: + //This is explicitly a POD + int x, y; + int w, h; + + BoundingBox() = default; + BoundingBox(int i, int j, int k, int l): x(i), y(j), w(k), h(l) {}; + ~BoundingBox() = default; + BoundingBox& operator=(BoundingBox const&) = default; + + int Size() { + return std::max(w*h,0); + } + + bool CheckOverlap(BoundingBox rhs) { + return !( + x >= rhs.x + rhs.w || + y >= rhs.y + rhs.h || + rhs.x >= x + w || + rhs.y >= y + h); + } + + BoundingBox CalcOverlap(BoundingBox rhs) { + if (!CheckOverlap(rhs)) { + return {0, 0, 0, 0}; + } + BoundingBox ret; + ret.x = std::max(x, rhs.x); + ret.y = std::max(y, rhs.y); + ret.w = std::min(x+w, rhs.x+rhs.w) - ret.x; + ret.h = std::min(y+h, rhs.y+rhs.h) - ret.y; + return ret; + } +}; + +//This is explicitly a POD +static_assert(std::is_pod::value, "BoundingBox is not a POD"); + +#include "vector2.hpp" + +//operators +inline BoundingBox operator+(BoundingBox b, Vector2 v) { + return {b.x + (int)v.x, b.y + (int)v.y, b.w, b.h}; +} +inline BoundingBox operator+(Vector2 v, BoundingBox b) { + return b + v; +} + +#endif diff --git a/common/utilities/utility.cpp b/common/utilities/utility.cpp index a8a7270..bbba99c 100644 --- a/common/utilities/utility.cpp +++ b/common/utilities/utility.cpp @@ -32,6 +32,10 @@ int snapToBase(int base, int x) { return x / base * base; } +double snapToBase(double base, double x) { + return floor(x / base) * base; +} + std::string truncatePath(std::string pathname) { return std::string( std::find_if( diff --git a/common/utilities/utility.hpp b/common/utilities/utility.hpp index 3589c01..3346136 100644 --- a/common/utilities/utility.hpp +++ b/common/utilities/utility.hpp @@ -25,6 +25,8 @@ #include int snapToBase(int base, int x); +double snapToBase(double base, double x); + std::string truncatePath(std::string pathname); //fixing known bugs in g++ diff --git a/common/utilities/vector2.hpp b/common/utilities/vector2.hpp index 2be3d34..57c716c 100644 --- a/common/utilities/vector2.hpp +++ b/common/utilities/vector2.hpp @@ -43,6 +43,8 @@ public: } void Normalize() { double l = Length(); + if (l == 0) + throw(std::domain_error("Divide by zero")); x /= l; y /= l; } diff --git a/server/makefile b/server/makefile index 3d0de76..807cd13 100644 --- a/server/makefile +++ b/server/makefile @@ -1,5 +1,5 @@ #config -INCLUDES+=. accounts characters combat enemies mapgen mapgen/generators rooms ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities +INCLUDES+=. accounts characters combat enemies mapgen mapgen/generators rooms ../common/debugging ../common/gameplay ../common/map ../common/network ../common/network/packet ../common/network/serial ../common/utilities LIBS+=server.a ../libcommon.a -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL -llua -lsqlite3 CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))