Platform.hpp: move and chunk parts into src/platform/
This commit is contained in:
parent
28d5b18448
commit
f2dd5300cc
116
src/Platform.hpp
116
src/Platform.hpp
@ -53,23 +53,13 @@
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "platform/FastRand.hpp"
|
||||
#include "platform/FourCC.hpp"
|
||||
#include "platform/Math.hpp"
|
||||
#include "platform/Types.hpp"
|
||||
|
||||
namespace Diggler {
|
||||
|
||||
using int32 = int32_t;
|
||||
using uint = uint32_t;
|
||||
using uint32 = uint32_t;
|
||||
using uint64 = uint64_t;
|
||||
using uint16 = uint16_t;
|
||||
using uint8 = uint8_t;
|
||||
using int64 = int64_t;
|
||||
using int16 = int16_t;
|
||||
using int8 = int8_t;
|
||||
using byte = uint8_t;
|
||||
using char32 = char32_t;
|
||||
using char16 = char16_t;
|
||||
|
||||
using String = std::string;
|
||||
|
||||
namespace proc {
|
||||
/// @returns The executable's absolute path
|
||||
std::string getExecutablePath();
|
||||
@ -121,102 +111,6 @@ std::string getAssetPath(const std::string &name);
|
||||
std::string getAssetPath(const std::string &type, const std::string &name);
|
||||
|
||||
|
||||
extern uint FastRand_Seed;
|
||||
#define FastRandSeed(x) FastRand_Seed=x;
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [0, 2^31-1]
|
||||
///
|
||||
inline int FastRand() {
|
||||
FastRand_Seed = (514229*((FastRand_Seed+4631018)>>1))^0x51d75169;
|
||||
return FastRand_Seed & 0x7FFFFFFF;
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [0, max]
|
||||
///
|
||||
inline int FastRand(int max) {
|
||||
return FastRand() % (max+1);
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [min, max]
|
||||
///
|
||||
inline int FastRand(int min, int max) {
|
||||
return min + (FastRand() % (max-min+1) );
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random float in range [0.0, 1.0[
|
||||
///
|
||||
inline float FastRandF() {
|
||||
return (float)FastRand() / 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
///
|
||||
/// Real Modulus
|
||||
/// @returns Real modulus operation result, as such mod(x,y) is always positive
|
||||
///
|
||||
constexpr int rmod(int x, int y) {
|
||||
const int ret = x % y;
|
||||
return (ret < 0) ? y+ret : ret;
|
||||
}
|
||||
float rmod(float x, float y);
|
||||
double rmod(double x, double y);
|
||||
|
||||
///
|
||||
/// Divide rounding down / Modulo quotient
|
||||
/// @returns x/y rounded down / Q in modulus' A=B×Q+R equation
|
||||
///
|
||||
constexpr inline int divrd(int x, uint y) {
|
||||
return (x < 0) ? (x+1)/(int)y-1 : x/(int)y;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns Floored value of f, as an integer
|
||||
/// @see ::std::floor For results as float or double
|
||||
///
|
||||
constexpr inline int floor(const float f) {
|
||||
return (f > 0) ? (int)f : ((int)f)-1;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns Ceiling value of f, as an integer
|
||||
/// @see ::std::ceil For results as float or double
|
||||
///
|
||||
constexpr inline int ceil(const float f) {
|
||||
return (f < 0) ? (int)f : ((int)f)+1;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns 1×[sign of v]. 0 if v == 0.
|
||||
///
|
||||
constexpr inline int signum(float v) {
|
||||
return (v > 0) ? 1 : (v < 0) ? -1 : 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns the smallest positive t such that s+t*ds is an integer.
|
||||
/// @see http://gamedev.stackexchange.com/questions/47362/cast-ray-to-select-block-in-voxel-game
|
||||
///
|
||||
constexpr float intbound(float s, float ds) {
|
||||
return (ds < 0) ? intbound(-s, -ds) : (1-rmod(s, 1.f))/ds;
|
||||
}
|
||||
|
||||
|
||||
using FourCC = uint32;
|
||||
constexpr inline FourCC MakeFourCC(char a, char b, char c, char d) {
|
||||
#if defined(BUILDINFO_LITTLE_ENDIAN)
|
||||
return d | c >> 8 | b >> 16 | a >> 24;
|
||||
#else
|
||||
return a | b >> 8 | c >> 16 | d >> 24;
|
||||
#endif
|
||||
}
|
||||
constexpr inline FourCC MakeFourCC(const char *s) {
|
||||
return MakeFourCC(s[0], s[1], s[2], s[3]);
|
||||
}
|
||||
|
||||
|
||||
namespace fs {
|
||||
|
||||
///
|
||||
|
45
src/platform/FastRand.hpp
Normal file
45
src/platform/FastRand.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
#ifndef DIGGLER_PLATFORM_FAST_RAND_HPP
|
||||
#define DIGGLER_PLATFORM_FAST_RAND_HPP
|
||||
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace Diggler {
|
||||
|
||||
// TODO: Make FastRand thread-safe
|
||||
|
||||
extern uint FastRand_Seed;
|
||||
#define FastRandSeed(x) FastRand_Seed=x;
|
||||
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [0, 2^31-1]
|
||||
///
|
||||
inline int FastRand() {
|
||||
FastRand_Seed = (514229*((FastRand_Seed+4631018)>>1))^0x51d75169;
|
||||
return FastRand_Seed & 0x7FFFFFFF;
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [0, max]
|
||||
///
|
||||
inline int FastRand(int max) {
|
||||
return FastRand() % (max+1);
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random integer in range [min, max]
|
||||
///
|
||||
inline int FastRand(int min, int max) {
|
||||
return min + (FastRand() % (max-min+1) );
|
||||
}
|
||||
///
|
||||
/// Fast pseudo-random number generator, very inaccurate
|
||||
/// @returns A random float in range [0.0, 1.0[
|
||||
///
|
||||
inline float FastRandF() {
|
||||
return (float)FastRand() / 0x7FFFFFFF;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* DIGGLER_PLATFORM_FAST_RAND_HPP */
|
23
src/platform/FourCC.hpp
Normal file
23
src/platform/FourCC.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef DIGGLER_PLATFORM_FOURCC_HPP
|
||||
#define DIGGLER_PLATFORM_FOURCC_HPP
|
||||
|
||||
#include "Types.hpp"
|
||||
|
||||
namespace Diggler {
|
||||
|
||||
using FourCC = uint32;
|
||||
|
||||
constexpr inline FourCC MakeFourCC(char a, char b, char c, char d) {
|
||||
#if defined(BUILDINFO_LITTLE_ENDIAN)
|
||||
return d | c >> 8 | b >> 16 | a >> 24;
|
||||
#else
|
||||
return a | b >> 8 | c >> 16 | d >> 24;
|
||||
#endif
|
||||
}
|
||||
constexpr inline FourCC MakeFourCC(const char *s) {
|
||||
return MakeFourCC(s[0], s[1], s[2], s[3]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* DIGGLER_PLATFORM_FOURCC_HPP */
|
58
src/platform/Math.hpp
Normal file
58
src/platform/Math.hpp
Normal file
@ -0,0 +1,58 @@
|
||||
#ifndef DIGGLER_PLATFORM_MATH_HPP
|
||||
#define DIGGLER_PLATFORM_MATH_HPP
|
||||
|
||||
namespace Diggler {
|
||||
|
||||
///
|
||||
/// Real Modulus
|
||||
/// @returns Real modulus operation result, as such mod(x,y) is always positive
|
||||
///
|
||||
constexpr int rmod(int x, int y) {
|
||||
const int ret = x % y;
|
||||
return (ret < 0) ? y+ret : ret;
|
||||
}
|
||||
float rmod(float x, float y);
|
||||
double rmod(double x, double y);
|
||||
|
||||
///
|
||||
/// Divide rounding down / Modulo quotient
|
||||
/// @returns x/y rounded down / Q in modulus' A=B×Q+R equation
|
||||
///
|
||||
constexpr inline int divrd(int x, uint y) {
|
||||
return (x < 0) ? (x+1)/(int)y-1 : x/(int)y;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns Floored value of f, as an integer
|
||||
/// @see ::std::floor For results as float or double
|
||||
///
|
||||
constexpr inline int floor(const float f) {
|
||||
return (f > 0) ? (int)f : ((int)f)-1;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns Ceiling value of f, as an integer
|
||||
/// @see ::std::ceil For results as float or double
|
||||
///
|
||||
constexpr inline int ceil(const float f) {
|
||||
return (f < 0) ? (int)f : ((int)f)+1;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns 1×[sign of v]. 0 if v == 0.
|
||||
///
|
||||
constexpr inline int signum(float v) {
|
||||
return (v > 0) ? 1 : (v < 0) ? -1 : 0;
|
||||
}
|
||||
|
||||
///
|
||||
/// @returns the smallest positive t such that s+t*ds is an integer.
|
||||
/// @see http://gamedev.stackexchange.com/questions/47362/cast-ray-to-select-block-in-voxel-game
|
||||
///
|
||||
constexpr float intbound(float s, float ds) {
|
||||
return (ds < 0) ? intbound(-s, -ds) : (1-rmod(s, 1.f))/ds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif /* DIGGLER_PLATFORM_MATH_HPP */
|
23
src/platform/Types.hpp
Normal file
23
src/platform/Types.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
#ifndef DIGGLER_PLATFORM_TYPES_HPP
|
||||
#define DIGGLER_PLATFORM_TYPES_HPP
|
||||
|
||||
namespace Diggler {
|
||||
|
||||
using int32 = int32_t;
|
||||
using uint = uint32_t;
|
||||
using uint32 = uint32_t;
|
||||
using uint64 = uint64_t;
|
||||
using uint16 = uint16_t;
|
||||
using uint8 = uint8_t;
|
||||
using int64 = int64_t;
|
||||
using int16 = int16_t;
|
||||
using int8 = int8_t;
|
||||
using byte = uint8_t;
|
||||
using char32 = char32_t;
|
||||
using char16 = char16_t;
|
||||
|
||||
using String = std::string;
|
||||
|
||||
}
|
||||
|
||||
#endif /* DIGGLER_PLATFORM_TYPES_HPP */
|
Loading…
x
Reference in New Issue
Block a user