This commit is contained in:
Ilusha 2024-03-15 23:42:10 +03:00 committed by Ilya Shurupov
parent b8bcf58a29
commit afab59eb21
34 changed files with 288 additions and 264 deletions

View file

@ -5,14 +5,6 @@
using namespace tp;
static bool init(const ModuleManifest* self) {
gEnvironment.log();
return true;
}
static ModuleManifest* deps[] = { nullptr };
ModuleManifest tp::gModuleBase = ModuleManifest("Common", init, nullptr, deps);
ModuleManifest::ModuleManifest(const char* aModuleName, ModuleInit aInit, ModuleDeinit aDeinit, ModuleManifest** aDependencies) {
mInit = aInit;
mDeinit = aDeinit;

View file

@ -0,0 +1,67 @@
#include <chrono>
#include <thread>
#include "Timing.hpp"
#include "Utils.hpp"
#define GETTIMEMSC() (time_ms)(std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()).time_since_epoch()).count())
#define THREAD_SLEEP(time_ms) std::this_thread::sleep_for(std::chrono::milliseconds(time_ms))
tp::time_ms tp::gCurrentTime = tp::get_time();
namespace tp {
time_ms get_time() {
gCurrentTime = GETTIMEMSC();
return gCurrentTime;
}
void sleep(time_ms mDuration) { THREAD_SLEEP(mDuration); }
Timer::Timer() {
mDuration = 0;
mStart = GETTIMEMSC();
}
Timer::Timer(time_ms mDuration) {
mStart = GETTIMEMSC();
this->mDuration = mDuration;
}
bool Timer::isTimeout() { return mDuration < GETTIMEMSC() - mStart; }
void Timer::reset() { mStart = GETTIMEMSC(); }
time_ms Timer::timePassed() { return GETTIMEMSC() - mStart; }
time_ms Timer::remainder() { return mDuration - (GETTIMEMSC() - mStart); }
time_ms Timer::start() { return mStart; }
time_ms Timer::duration() { return mDuration; }
void Timer::setDuration(time_ms dur) { mDuration = dur; }
void Timer::wait() {
if (!isTimeout()) {
sleep(remainder());
}
}
// TODO remove
float Timer::easeIn(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((1.1f * x) / (x + 0.1f), 0.f, 1.f);
}
// TODO remove
float Timer::easeOut(time_ms pDuration) {
if (!pDuration) {
pDuration = mDuration;
}
float x = (1.f / pDuration) * timePassed();
return clamp((0.1f * (1 - x)) / (x + 0.1f), 0.f, 1.f);
}
}

116
Modules/private/Utils.cpp Normal file
View file

@ -0,0 +1,116 @@
#include "Utils.hpp"
#include <ctime>
#include <random>
void initializeCallStackCapture();
void deinitializeCallStackCapture();
static bool initialize(const tp::ModuleManifest*) {
initializeCallStackCapture();
std::srand(std::time(nullptr));
return true;
}
static void deinitialize(const tp::ModuleManifest*) {
deinitializeCallStackCapture();
}
namespace tp {
void memSetVal(void* p, uhalni byteSize, uint1 val) {
alni alignedVal = val;
alignedVal = (alignedVal << 8) | alignedVal;
alignedVal = (alignedVal << 16) | alignedVal;
alignedVal = (alignedVal << 32) | alignedVal;
ualni alignedLen = byteSize / sizeof(alni);
for (ualni idx = 0; idx < alignedLen; idx++) {
((alni*) p)[idx] = alignedVal;
}
ualni unalignedLen = byteSize - (alignedLen * sizeof(alni));
for (ualni idx = 0; idx < unalignedLen; idx++) {
((uint1*) p)[byteSize - idx - 1] = val;
}
}
void memCopy(void* left, const void* right, uhalni len) {
ualni alignedLen = len / sizeof(alni);
for (ualni idx = 0; idx < alignedLen; idx++) {
((alni*) left)[idx] = ((alni*) right)[idx];
}
ualni unalignedLen = len - (alignedLen * sizeof(alni));
for (ualni idx = 0; idx < unalignedLen; idx++) {
((uint1*) left)[len - idx - 1] = ((uint1*) right)[len - idx - 1];
}
}
int1 memCompare(const void* left, const void* right, uhalni len) {
if (!len) return 0;
ualni alignedLength = len / sizeof(alni);
for (ualni idx = 0; idx < alignedLength; idx++) {
if (((alni*) left)[idx] == ((alni*) right)[idx]) {
continue;
}
if (((alni*) left)[idx] > ((alni*) right)[idx]) {
return 1;
}
return -1;
}
ualni unalignedLength = len - (alignedLength * sizeof(alni));
for (ualni idx = 0; idx < unalignedLength; idx++) {
if (((uint1*) left)[len - idx - 1] == ((uint1*) right)[len - idx - 1]) {
continue;
}
if (((uint1*) left)[len - idx - 1] > ((uint1*) right)[len - idx - 1]) {
return 1;
}
return -1;
}
return 0;
}
bool memEqual(const void* left, const void* right, uhalni len) { return memCompare(left, right, len) == 0; }
int1 memCompareVal(const void* left, uhalni len, uint1 val) {
if (!len) return 0;
alni valAligned = val;
valAligned = (valAligned << 8) | valAligned;
valAligned = (valAligned << 16) | valAligned;
valAligned = (valAligned << 32) | valAligned;
ualni alignedLength = len / sizeof(alni);
for (ualni idx = 0; idx < alignedLength; idx++) {
if (((alni*) left)[idx] == valAligned) {
continue;
}
if (((alni*) left)[idx] > valAligned) {
return 1;
}
return -1;
}
ualni unalignedLength = len - (alignedLength * sizeof(alni));
for (ualni idx = 0; idx < unalignedLength; idx++) {
if (((uint1*) left)[len - idx - 1] == val) {
continue;
}
if (((uint1*) left)[len - idx - 1] > val) {
return 1;
}
return -1;
}
return 0;
}
alnf randomFloat() {
alnf r = static_cast<alnf>(std::rand()) / static_cast<alnf>(RAND_MAX);
return r;
}
}

View file

@ -30,6 +30,4 @@ namespace tp {
uhalni mInitCount = 0;
void* mCustomData = nullptr;
};
extern ModuleManifest gModuleBase;
};

View file

@ -0,0 +1,95 @@
#pragma once
#include "Utils.hpp"
namespace tp {
template <typename Type>
inline bool compare(const Type& val1, const Type& val2) {
return val1 > val2;
}
struct SortMerge {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
mergeSort(buff, 0, length - 1, grater);
}
private:
template <typename Type>
static void merge(Type* buff, int left, int middle, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
int n1 = middle - left + 1;
int n2 = right - middle;
Type* Left = new Type[n1];
Type* Right = new Type[n2];
for (int i = 0; i < n1; i++) {
Left[i] = buff[left + i];
}
for (int j = 0; j < n2; j++) {
Right[j] = buff[middle + 1 + j];
}
int i = 0;
int j = 0;
int k = left;
while (i < n1 && j < n2) {
if (!(grater(Left[i], Right[j]))) {
buff[k] = Left[i];
i++;
} else {
buff[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
buff[k] = Left[i];
i++;
k++;
}
while (j < n2) {
buff[k] = Right[j];
j++;
k++;
}
delete[] Left;
delete[] Right;
}
template <typename Type>
static void mergeSort(Type* buff, int left, int right, bool (*grater)(const Type& obj1, const Type& obj2)) {
if (left >= right) {
return;
}
int middle = left + (right - left) / 2;
mergeSort(buff, left, middle, grater);
mergeSort(buff, middle + 1, right, grater);
merge(buff, left, middle, right, grater);
}
};
struct SortInsert {
template <typename Type>
static void sort(Type* buff, int length, bool (*grater)(const Type& obj1, const Type& obj2) = &compare) {
for (int i = 0; i < length; i++) {
for (int j = i + 1; j < length; j++) {
if (grater(*buff[i], *buff[j])) {
swap(buff[i], buff[j]);
}
}
}
}
};
}

58
Modules/public/Timing.hpp Normal file
View file

@ -0,0 +1,58 @@
#pragma once
#include "Environment.hpp"
namespace tp {
typedef alni time_ms;
typedef alni time_ns;
extern time_ms gCurrentTime;
class Timer {
time_ms mStart;
time_ms mDuration;
public:
Timer();
explicit Timer(time_ms time);
time_ms start();
time_ms duration();
void setDuration(time_ms dur);
bool isTimeout();
void reset();
time_ms timePassed();
time_ms remainder();
void wait();
float easeIn(time_ms duration = 0);
float easeOut(time_ms duration = 0);
};
void sleep(time_ms duration);
time_ms get_time();
struct FpsCounter {
halni frames = 0;
Timer time;
halni fps = 0;
FpsCounter() :
time(1000) {}
void update(bool log = true) {
frames++;
if (time.isTimeout()) {
fps = frames;
if (log) {
// printf("fps %i \n", fps);
}
frames = 0;
time.reset();
}
}
};
}

96
Modules/public/Utils.hpp Normal file
View file

@ -0,0 +1,96 @@
#pragma once
#include "Module.hpp"
#define PTR_OFFSET(first, offset) (*((&first) + offset))
#define MEMBER_OFFSET(s, m) (alni(&(((s*) 0)->m)))
namespace tp {
void memSetVal(void* p, uhalni byteSize, uint1 val);
void memCopy(void* left, const void* right, uhalni len);
int1 memCompare(const void* left, const void* right, uhalni len);
bool memEqual(const void* left, const void* right, uhalni len);
int1 memCompareVal(const void* left, uhalni len, uint1 val);
}
namespace tp {
[[nodiscard]] alnf randomFloat();
}
namespace tp {
template <typename Type = alni>
class Bits {
Type mFlags = 0;
public:
Bits() = default;
explicit Bits(Type val) { mFlags = val; }
explicit Bits(bool val) {
for (int bit = 0; bit < sizeof(Type); bit++) {
set(bit, val);
}
}
bool get(int1 idx) { return mFlags & (1l << idx); }
void set(int1 idx, bool val) {
if (val) {
mFlags |= (1l << idx);
} else {
mFlags &= ~(1l << idx);
}
}
};
template <typename tType = ualni>
class Range {
public:
class Iterator {
public:
tType mIndex;
explicit Iterator(tType pStartIndex) :
mIndex(pStartIndex) {}
tType index() const { return mIndex; }
inline void operator++() { mIndex++; }
inline operator tType() const { return mIndex; }
inline bool operator==(Iterator pIndex) { return mIndex == pIndex.mIndex; }
inline bool operator!=(Iterator pIndex) { return mIndex != pIndex.mIndex; }
inline const Iterator& operator*() { return *this; }
};
tType mBegin{};
tType mEnd{};
Range() = default;
explicit Range(tType pEndIndex) :
mBegin(0),
mEnd(pEndIndex) {}
Range(tType pStartIndex, tType pEndIndex) :
mBegin(pStartIndex),
mEnd(pEndIndex) {}
bool valid() const { return mBegin < mEnd; }
tType idxBegin() const { return mBegin; }
tType idxEnd() const { return mEnd; }
tType idxDiff() const { return mEnd - mBegin; }
Iterator begin() const { return Iterator(mBegin); }
Iterator end() const { return Iterator(mEnd); }
};
}