From e828710072781a93f7d6fa0ac6e0274a1869de2e Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 19 Apr 2020 21:46:37 +0200 Subject: [PATCH] - updated common/utility. --- source/common/utility/cmdlib.cpp | 2 +- source/common/utility/floatrect.h | 21 ++++++ source/common/utility/m_alloc.h | 2 + source/common/utility/palette.cpp | 41 ++++++++++++ source/common/utility/palutil.h | 1 + source/common/utility/refcounted.h | 103 +++++++++++++++++++++++++++++ source/common/utility/vectors.h | 1 + 7 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 source/common/utility/floatrect.h create mode 100644 source/common/utility/refcounted.h diff --git a/source/common/utility/cmdlib.cpp b/source/common/utility/cmdlib.cpp index f3b5a2226..c25ade8b9 100644 --- a/source/common/utility/cmdlib.cpp +++ b/source/common/utility/cmdlib.cpp @@ -73,7 +73,7 @@ static inline bool IsSeperator (int c) if (c == '/') return true; #ifdef _WIN32 - if (c == '\\' || c == ':') + if (c == '\\') return true; #endif return false; diff --git a/source/common/utility/floatrect.h b/source/common/utility/floatrect.h new file mode 100644 index 000000000..8274b8498 --- /dev/null +++ b/source/common/utility/floatrect.h @@ -0,0 +1,21 @@ +#pragma once + +struct FloatRect +{ + float left,top; + float width,height; + + + void Offset(float xofs,float yofs) + { + left+=xofs; + top+=yofs; + } + void Scale(float xfac,float yfac) + { + left*=xfac; + width*=xfac; + top*=yfac; + height*=yfac; + } +}; diff --git a/source/common/utility/m_alloc.h b/source/common/utility/m_alloc.h index 3da124bff..76c4947bc 100644 --- a/source/common/utility/m_alloc.h +++ b/source/common/utility/m_alloc.h @@ -38,10 +38,12 @@ #include #if defined(__APPLE__) +#include #define _msize(p) malloc_size(p) #elif defined(__solaris__) || defined(__OpenBSD__) #define _msize(p) (*((size_t*)(p)-1)) #elif !defined(_WIN32) +#include #define _msize(p) malloc_usable_size(p) // from glibc/FreeBSD #endif diff --git a/source/common/utility/palette.cpp b/source/common/utility/palette.cpp index 5be3f6a45..f4876b4b9 100644 --- a/source/common/utility/palette.cpp +++ b/source/common/utility/palette.cpp @@ -471,6 +471,47 @@ void MakeGoodRemap(uint32_t* BaseColors, uint8_t* Remap) // 256 entries are different. :-) } +//=========================================================================== +// +// Gets the average color of a texture for use as a sky cap color +// +//=========================================================================== + +PalEntry averageColor(const uint32_t* data, int size, int maxout) +{ + int i; + unsigned int r, g, b; + + // First clear them. + r = g = b = 0; + if (size == 0) + { + return PalEntry(255, 255, 255); + } + for (i = 0; i < size; i++) + { + b += BPART(data[i]); + g += GPART(data[i]); + r += RPART(data[i]); + } + + r = r / size; + g = g / size; + b = b / size; + + int maxv = MAX(MAX(r, g), b); + + if (maxv && maxout) + { + r = ::Scale(r, maxout, maxv); + g = ::Scale(g, maxout, maxv); + b = ::Scale(b, maxout, maxv); + } + return PalEntry(255, r, g, b); +} + + + //========================================================================== // // V_GetColorFromString diff --git a/source/common/utility/palutil.h b/source/common/utility/palutil.h index 077c4c036..b1dcca40a 100644 --- a/source/common/utility/palutil.h +++ b/source/common/utility/palutil.h @@ -30,6 +30,7 @@ FString V_GetColorStringByName(const char* name, FScriptPosition* sc = nullptr); // Tries to get color by name, then by string int V_GetColor(const uint32_t* palette, const char* str, FScriptPosition* sc = nullptr); int V_GetColor(const uint32_t* palette, FScanner& sc); +PalEntry averageColor(const uint32_t* data, int size, int maxout); enum { diff --git a/source/common/utility/refcounted.h b/source/common/utility/refcounted.h new file mode 100644 index 000000000..b0c87d934 --- /dev/null +++ b/source/common/utility/refcounted.h @@ -0,0 +1,103 @@ +#pragma once + +// Simple lightweight reference counting pointer alternative for std::shared_ptr which stores the reference counter in the handled object itself. + + // Base class for handled objects +class RefCountedBase +{ +public: + void IncRef() { refCount++; } + void DecRef() { if (--refCount <= 0) delete this; } +private: + int refCount = 0; +protected: + virtual ~RefCountedBase() = default; +}; + + // The actual pointer object +template +class RefCountedPtr +{ +public: + ~RefCountedPtr() + { + if (ptr) ptr->DecRef(); + } + + RefCountedPtr() : ptr(nullptr) + {} + + explicit RefCountedPtr(T* p) : ptr(p) + { + if (ptr) ptr->IncRef(); + } + + RefCountedPtr & operator=(const RefCountedPtr& r) + { + if (ptr != r.ptr) + { + if (ptr) ptr->DecRef(); + ptr = r.ptr; + if (ptr) ptr->IncRef(); + } + return *this; + } + + RefCountedPtr& operator=(T* r) + { + if (ptr != r) + { + if (ptr) ptr->DecRef(); + ptr = r; + if (ptr) ptr->IncRef(); + } + return *this; + } + + RefCountedPtr & operator=(const RefCountedPtr&& r) + { + if (ptr) ptr->DecRef(); + ptr = r.ptr; + r.ptr = nullptr; + return *this; + } + + bool operator==(T* p) const + { + return ptr == p; + } + + bool operator!=(T* p) const + { + return ptr != p; + } + + bool operator==(const RefCountedPtr &p) const + { + return ptr == p.ptr; + } + + bool operator!=(const RefCountedPtr& p) const + { + return ptr != p.ptr; + } + + T& operator* () const + { + return *ptr; + } + + T* operator-> () const + { + return ptr; + } + + T* get() const + { + return ptr; + } + +private: + + T * ptr; +}; \ No newline at end of file diff --git a/source/common/utility/vectors.h b/source/common/utility/vectors.h index b56b36e7b..e8c39fc28 100644 --- a/source/common/utility/vectors.h +++ b/source/common/utility/vectors.h @@ -40,6 +40,7 @@ #ifndef VECTORS_H #define VECTORS_H +#include #include #include #include