- updated common/utility.

This commit is contained in:
Christoph Oelckers 2020-04-19 21:46:37 +02:00
parent c403bc4a67
commit e828710072
7 changed files with 170 additions and 1 deletions

View file

@ -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;

View file

@ -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;
}
};

View file

@ -38,10 +38,12 @@
#include <string.h>
#if defined(__APPLE__)
#include <malloc/malloc.h>
#define _msize(p) malloc_size(p)
#elif defined(__solaris__) || defined(__OpenBSD__)
#define _msize(p) (*((size_t*)(p)-1))
#elif !defined(_WIN32)
#include <malloc.h>
#define _msize(p) malloc_usable_size(p) // from glibc/FreeBSD
#endif

View file

@ -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

View file

@ -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
{

View file

@ -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 T>
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;
};

View file

@ -40,6 +40,7 @@
#ifndef VECTORS_H
#define VECTORS_H
#include <cstddef>
#include <math.h>
#include <float.h>
#include <string.h>