refactored MainWindow and Engine, changed project structure
This commit is contained in:
15
src/common/UsefulException.cpp
Normal file
15
src/common/UsefulException.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "UsefulException.hpp"
|
||||
#include <sstream>
|
||||
|
||||
UsefulException_::UsefulException_(const std::string& _message, const std::string& _file, const std::string& _func, int _line_n)
|
||||
: message(_message), file(_file), function(_func), line_n(_line_n)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss<<message<<'\n';
|
||||
ss<<" at "<<file<<':'<<_line_n<<" in "<<function;
|
||||
complete_text = ss.str();
|
||||
}
|
||||
|
||||
char const* UsefulException_::what() const noexcept {
|
||||
return complete_text.c_str();
|
||||
}
|
||||
21
src/common/UsefulException.hpp
Normal file
21
src/common/UsefulException.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#define UsefulException(MSG) UsefulException_(MSG, __FILE__, __func__, __LINE__)
|
||||
|
||||
class UsefulException_ : public std::exception {
|
||||
std::string message;
|
||||
std::string file;
|
||||
std::string function;
|
||||
int line_n;
|
||||
std::string complete_text;
|
||||
|
||||
public:
|
||||
UsefulException_(const std::string& msg, const std::string& _file, const std::string& _func, int line_n);
|
||||
|
||||
virtual char const* what() const noexcept;
|
||||
};
|
||||
|
||||
#define useful_assert(EXPR, ERRMSG) if(!(EXPR)) throw UsefulException(ERRMSG);
|
||||
50
src/common/function_shared_ptr.hpp
Normal file
50
src/common/function_shared_ptr.hpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include "UsefulException.hpp"
|
||||
|
||||
|
||||
template<typename SignatureT> class function_shared_ptr;
|
||||
template<typename ReturnT, typename... ArgTypes>
|
||||
class function_shared_ptr<ReturnT(ArgTypes...)>
|
||||
{
|
||||
public:
|
||||
using func_t = std::function<ReturnT(ArgTypes...)>;
|
||||
using ptr_t = std::shared_ptr<func_t>;
|
||||
|
||||
protected:
|
||||
ptr_t func_ptr;
|
||||
|
||||
public:
|
||||
function_shared_ptr() = default;
|
||||
function_shared_ptr(const function_shared_ptr&) = default;
|
||||
function_shared_ptr(function_shared_ptr&&) = default;
|
||||
function_shared_ptr(const ptr_t& p) : func_ptr(p) { }
|
||||
function_shared_ptr(ptr_t&& p) : func_ptr(p) { }
|
||||
template<typename FunctionT>
|
||||
function_shared_ptr(FunctionT f){ func_ptr = std::make_shared<func_t>(f); }
|
||||
|
||||
function_shared_ptr& operator=(const function_shared_ptr&) = default;
|
||||
function_shared_ptr& operator=(function_shared_ptr&&) = default;
|
||||
|
||||
bool isNull() const { return func_ptr == nullptr; }
|
||||
|
||||
//TODO: make inline
|
||||
|
||||
// ReturnT is not void
|
||||
template<typename RT = ReturnT>
|
||||
std::enable_if_t<!std::is_void<RT>::value, RT> operator()(ArgTypes... args){
|
||||
if(func_ptr == nullptr)
|
||||
throw UsefulException("function_shared_ptr is null");
|
||||
return func_ptr->operator()(args...);
|
||||
}
|
||||
|
||||
// ReturnT is void
|
||||
template<typename RT = ReturnT>
|
||||
std::enable_if_t<std::is_void<RT>::value, RT> operator()(ArgTypes... args){
|
||||
if(func_ptr == nullptr)
|
||||
throw UsefulException("function_shared_ptr is null");
|
||||
func_ptr->operator()(args...);
|
||||
}
|
||||
};
|
||||
47
src/common/math.hpp
Normal file
47
src/common/math.hpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.hpp"
|
||||
#include <cmath>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_E 2.7182818284590452354
|
||||
#define M_LOG2E 1.4426950408889634074
|
||||
#define M_LOG10E 0.43429448190325182765
|
||||
#define M_LN2 0.69314718055994530942
|
||||
#define M_LN10 2.30258509299404568402
|
||||
#define M_PI 3.14159265358979323846
|
||||
#define M_PI_2 1.57079632679489661923
|
||||
#define M_PI_4 0.78539816339744830962
|
||||
#define M_1_PI 0.31830988618379067154
|
||||
#define M_2_PI 0.63661977236758134308
|
||||
#define M_2_SQRTPI 1.12837916709551257390
|
||||
#define M_SQRT2 1.41421356237309504880
|
||||
#define M_SQRT1_2 0.70710678118654752440
|
||||
#endif
|
||||
|
||||
struct Vec2 {
|
||||
f32 x = 0, y = 0;
|
||||
};
|
||||
|
||||
struct Vec3 {
|
||||
f32 x = 0, y = 0, z = 0;
|
||||
};
|
||||
|
||||
struct Vec4 {
|
||||
f32 x = 0, y = 0, z = 0, w = 0;
|
||||
};
|
||||
|
||||
/// radians
|
||||
typedef f32 angle_t;
|
||||
|
||||
static inline angle_t normalizeAngle(angle_t a){
|
||||
return std::fmodf(a + M_PI, 2*M_PI) - M_PI;
|
||||
}
|
||||
|
||||
static inline f32 angleToDegree(angle_t a){
|
||||
return (a / M_PI) * 180;
|
||||
}
|
||||
|
||||
static inline angle_t degreeToRadian(f32 d){
|
||||
return (d / 180) * M_PI;
|
||||
}
|
||||
65
src/common/ougge_format.cpp
Normal file
65
src/common/ougge_format.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <sstream>
|
||||
#include <stdarg.h>
|
||||
#include "ougge_format.hpp"
|
||||
#include "UsefulException.hpp"
|
||||
|
||||
std::string _ougge_format(const std::string& format_str, const size_t args_count, ...){
|
||||
va_list vl;
|
||||
va_start(vl, args_count);
|
||||
std::stringstream ss;
|
||||
|
||||
for(size_t i = 0; i < format_str.length(); i++){
|
||||
char c = format_str[i];
|
||||
|
||||
// format specifier
|
||||
if(c == '%'){
|
||||
c = format_str[++i];
|
||||
bool l = false;
|
||||
while(c == 'l'){
|
||||
l = true;
|
||||
c = format_str[++i];
|
||||
}
|
||||
|
||||
switch(c){
|
||||
case 'u':
|
||||
if(l) ss<<(u64)va_arg(vl, u64);
|
||||
else ss<<(u32)va_arg(vl, u32);
|
||||
break;
|
||||
case 'i':
|
||||
case 'd':
|
||||
if(l) ss<<(i64)va_arg(vl, i64);
|
||||
else ss<<(i32)va_arg(vl, i32);
|
||||
break;
|
||||
case 'f':
|
||||
// f32 is promoted to f64 when passed through '...'
|
||||
ss<<(f64)va_arg(vl, f64);
|
||||
break;
|
||||
case 'p':
|
||||
ss<<(void*)va_arg(vl, void*);
|
||||
break;
|
||||
case 'x':
|
||||
if(l) ss<<std::hex<<(u64)va_arg(vl, u64);
|
||||
else ss<<std::hex<<(u32)va_arg(vl, u32);
|
||||
break;
|
||||
case 's': {
|
||||
const char* cptr = va_arg(vl,char*);
|
||||
if(cptr != nullptr)
|
||||
ss<<cptr;
|
||||
else ss<<"<nullptr>";
|
||||
break;
|
||||
}
|
||||
case 'c':
|
||||
ss<<(char)va_arg(vl,int);
|
||||
break;
|
||||
default:
|
||||
throw UsefulException("invalid format cpecifier");
|
||||
}
|
||||
}
|
||||
|
||||
// regular character
|
||||
else ss<<c;
|
||||
}
|
||||
|
||||
va_end(vl);
|
||||
return ss.str();
|
||||
}
|
||||
6
src/common/ougge_format.hpp
Normal file
6
src/common/ougge_format.hpp
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.hpp"
|
||||
|
||||
std::string _ougge_format(const std::string& format_str, const size_t args_count, ...);
|
||||
#define ougge_format(FORMAT_STR, ARGS...) _ougge_format(FORMAT_STR, count_args(ARGS) ,##ARGS)
|
||||
62
src/common/std.hpp
Normal file
62
src/common/std.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
typedef int8_t i8;
|
||||
typedef uint8_t u8;
|
||||
typedef int16_t i16;
|
||||
typedef uint16_t u16;
|
||||
typedef int32_t i32;
|
||||
typedef uint32_t u32;
|
||||
typedef int64_t i64;
|
||||
typedef uint64_t u64;
|
||||
typedef float f32;
|
||||
typedef double f64;
|
||||
/// anonymous pointer without specified freeMembers() func
|
||||
typedef void* Pointer;
|
||||
|
||||
#define nameof(V) #V
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
||||
#define _CRT_SECURE_NO_WARNINGS 1
|
||||
#define EXPORT __declspec(dllexport)
|
||||
#define CALL __cdecl
|
||||
#elif defined(__GNUC__)
|
||||
#define EXPORT __attribute__((visibility("default")))
|
||||
#if __SIZEOF_POINTER__ == 4
|
||||
#define CALL __attribute__((__cdecl__))
|
||||
#else
|
||||
#define CALL
|
||||
#endif
|
||||
#ifndef typeof
|
||||
#define typeof(X) __typeof__(X)
|
||||
#endif
|
||||
#else
|
||||
#pragma GCC error "unknown compiler"
|
||||
#endif
|
||||
|
||||
#define __count_args( \
|
||||
a0, a1, a2, a3, a4, a5, a6, a7 , \
|
||||
a8, a9, a10,a11,a12,a13,a14,a15, \
|
||||
a16,a17,a18,a19,a20,a21,a22,a23, \
|
||||
a24,a25,a26,a27,a28,a29,a30,a31, \
|
||||
a32,a33,a34,a35,a36,a37,a38,a39, \
|
||||
a40,a41,a42,a43,a44,a45,a46,a47, \
|
||||
a48,a49,a50,a51,a52,a53,a54,a55, \
|
||||
a56,a57,a58,a59,a60,a61,a62,a63, \
|
||||
a64,...) a64
|
||||
// Macro for counting variadic arguments (max 64)
|
||||
// (see usage in kprint.h)
|
||||
#define count_args(ARGS...) __count_args( \
|
||||
ARGS, \
|
||||
64,63,62,61,60,59,58,57, \
|
||||
56,55,54,53,52,51,50,49, \
|
||||
48,47,46,45,44,43,42,41, \
|
||||
40,39,38,37,36,35,34,33, \
|
||||
32,31,30,29,28,27,26,25, \
|
||||
24,23,22,21,20,19,18,17, \
|
||||
16,15,14,13,12,11,10,9, \
|
||||
8, 7, 6, 5, 4, 3, 2, 1, 0)
|
||||
19
src/common/time.cpp
Normal file
19
src/common/time.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
// posix version definition to use clock_gettime
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#define _XOPEN_SOURCE 600
|
||||
#else
|
||||
#define _XOPEN_SOURCE 500
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "time.hpp"
|
||||
#include <ctime>
|
||||
#include "UsefulException.hpp"
|
||||
|
||||
nsec_t getMonotonicTimeNsec(){
|
||||
struct timespec t;
|
||||
if(clock_gettime(CLOCK_MONOTONIC, &t) != 0)
|
||||
throw UsefulException("clock_gettime(CLOCK_MONOTONIC) error");
|
||||
return (nsec_t)t.tv_sec * 1e9 + (nsec_t)t.tv_nsec;
|
||||
}
|
||||
20
src/common/time.hpp
Normal file
20
src/common/time.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include "std.hpp"
|
||||
|
||||
/// nanoseconds
|
||||
typedef i64 nsec_t;
|
||||
|
||||
/// can be used to measure delta time
|
||||
///@return time from some moment in nanoseconds.
|
||||
nsec_t getMonotonicTimeNsec();
|
||||
|
||||
#define optime(N, LABEL, CODE) {\
|
||||
nsec_t b = getMonotonicTimeNsec();\
|
||||
for(u32 i = 0; i < (u32)N; i++) {\
|
||||
CODE ;\
|
||||
}\
|
||||
nsec_t e = getMonotonicTimeNsec();\
|
||||
nsec_t t = e-b;\
|
||||
std::cout<<"operation '"<<LABEL<<"' took "<<t/1e6f<<" ms"<<std::endl;\
|
||||
}
|
||||
Reference in New Issue
Block a user