diff --git a/CMakeLists.txt b/CMakeLists.txt index 1af81a8..6704793 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,7 +34,7 @@ set(SOURCES_GRAPHICS src/graphics/color.cpp) set(SOURCES_FILESYSTEM src/system/fs/filesystem.cpp) set(SOURCES_SERIALIZERS) set(SOURCES_SYSTEM ${SOURCES_FILESYSTEM} src/system/os.cpp src/system/terminal.cpp src/system/system.cpp src/system/uri.cpp) -set(SOURCES_BASE src/lambdacommon.cpp src/serializable.cpp src/lstring.cpp src/path.cpp src/resources.cpp) +set(SOURCES_BASE src/lambdacommon.cpp src/serializable.cpp src/lstring.cpp src/path.cpp src/resources.cpp src/maths.cpp) set(SOURCES_FILES ${SOURCES_CONNECTION} ${SOURCES_DOCUMENT} ${SOURCES_GRAPHICS} ${SOURCES_SERIALIZERS} ${SOURCES_SYSTEM} ${SOURCES_BASE}) # Now build the library diff --git a/c_wrapper/CMakeLists.txt b/c_wrapper/CMakeLists.txt index c0a442f..933e4c8 100644 --- a/c_wrapper/CMakeLists.txt +++ b/c_wrapper/CMakeLists.txt @@ -7,11 +7,11 @@ include_directories(../include) # All files # This is the C headers files. set(C_HEADERS_SYSTEM include/clambdacommon/system/os.h include/clambdacommon/system/terminal.h include/clambdacommon/system/system.h) -set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h) +set(C_HEADERS_BASE include/clambdacommon/clambdacommon.h include/clambdacommon/lstring.h include/clambdacommon/maths.h) set(C_HEADERS_FILES ${C_HEADERS_SYSTEM} ${C_HEADERS_BASE}) # This is the C sources files. set(C_SOURCES_SYSTEM src/system/os.cpp src/system/terminal.cpp src/system/system.cpp) -set(C_SOURCES_BASE src/clambdacommon.cpp src/lstring.cpp) +set(C_SOURCES_BASE src/clambdacommon.cpp src/lstring.cpp src/maths.cpp) set(C_SOURCES_FILES ${C_SOURCES_SYSTEM} ${C_SOURCES_BASE}) # Now build the library diff --git a/c_wrapper/include/clambdacommon/clambdacommon.h b/c_wrapper/include/clambdacommon/clambdacommon.h index f6acefc..ddcd0ae 100644 --- a/c_wrapper/include/clambdacommon/clambdacommon.h +++ b/c_wrapper/include/clambdacommon/clambdacommon.h @@ -39,6 +39,8 @@ # define CLAMBDA_CYGWIN #endif +#include "maths.h" + #ifdef __cplusplus extern "C" { #endif diff --git a/c_wrapper/include/clambdacommon/maths.h b/c_wrapper/include/clambdacommon/maths.h new file mode 100644 index 0000000..56cc08b --- /dev/null +++ b/c_wrapper/include/clambdacommon/maths.h @@ -0,0 +1,38 @@ +/* + * Copyright © 2018 AperLambda + * + * This file is part of λcommon. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +#ifndef CLAMBDACOMMON_MATHS_H +#define CLAMBDACOMMON_MATHS_H + +#define LCOMMON_PI 3.14159265359 + +// Define every functions that include templates as preprocessor defines. +#define lcommon_maths_abs(number) (number < 0 ? -(number) : number) +#define lcommon_maths_min(a, b) (a < b ? a : b) +#define lcommon_maths_max(a, b) (a > b ? a : b) +#define lcommon_maths_clamp(number, min, max) lcommon_maths_min(lcommon_maths_max(number, min), max) //maths::min(maths::max(number, min), max) + +#ifdef __cplusplus +extern "C" +{ +#endif + +double lcommon_maths_radians(double degrees); + +float lcommon_maths_fradians(float degrees); + +double lcommon_maths_degrees(double radians); + +float lcommon_maths_fdegrees(float radians); + +#ifdef __cplusplus +} +#endif + +#endif //CLAMBDACOMMON_MATHS_H \ No newline at end of file diff --git a/c_wrapper/include/clambdacommon/system/terminal.h b/c_wrapper/include/clambdacommon/system/terminal.h index 074a996..c9ca4f1 100644 --- a/c_wrapper/include/clambdacommon/system/terminal.h +++ b/c_wrapper/include/clambdacommon/system/terminal.h @@ -21,56 +21,56 @@ enum lcommon_TermFormatting /* * RESET */ - RESET = 0, + LC_TERM_RESET = 0, /* * SET */ - BOLD = 1, - DIM = 2, - UNDERLINED = 4, - BLINK = 5, - REVERSE = 7, - HIDDEN = 8, + LC_TERM_BOLD = 1, + LC_TERM_DIM = 2, + LC_TERM_UNDERLINED = 4, + LC_TERM_BLINK = 5, + LC_TERM_REVERSE = 7, + LC_TERM_HIDDEN = 8, /* * Foreground */ - DEFAULT_FCOLOR = 39, - BLACK = 30, - RED = 31, - GREEN = 32, - YELLOW = 33, - BLUE = 34, - MAGENTA = 35, - CYAN = 36, - LIGHT_GRAY = 37, - DARK_GRAY = 90, - LIGHT_RED = 91, - LIGHT_GREEN = 92, - LIGHT_YELLOW = 93, - LIGHT_BLUE = 94, - LIGHT_MAGENTA = 95, - LIGHT_CYAN = 96, - WHITE = 97, + LC_TERM_DEFAULT_FCOLOR = 39, + LC_TERM_BLACK = 30, + LC_TERM_RED = 31, + LC_TERM_GREEN = 32, + LC_TERM_YELLOW = 33, + LC_TERM_BLUE = 34, + LC_TERM_MAGENTA = 35, + LC_TERM_CYAN = 36, + LC_TERM_LIGHT_GRAY = 37, + LC_TERM_DARK_GRAY = 90, + LC_TERM_LIGHT_RED = 91, + LC_TERM_LIGHT_GREEN = 92, + LC_TERM_LIGHT_YELLOW = 93, + LC_TERM_LIGHT_BLUE = 94, + LC_TERM_LIGHT_MAGENTA = 95, + LC_TERM_LIGHT_CYAN = 96, + LC_TERM_WHITE = 97, /* * Background */ - DEFAULT_BCOLOR = 49, - B_BLACK = 40, - B_RED = 41, - B_GREEN = 42, - B_YELLOW = 43, - B_BLUE = 44, - B_MAGENTA = 45, - B_CYAN = 46, - B_LIGHT_GRAY = 47, - B_DARK_GRAY = 100, - B_LIGHT_RED = 101, - B_LIGHT_GREEN = 102, - B_LIGHT_YELLOW = 103, - B_LIGHT_BLUE = 104, - B_LIGHT_MAGENTA = 105, - B_LIGHT_CYAN = 106, - B_WHITE = 107 + LC_TERM_DEFAULT_BCOLOR = 49, + LC_TERM_B_BLACK = 40, + LC_TERM_B_RED = 41, + LC_TERM_B_GREEN = 42, + LC_TERM_B_YELLOW = 43, + LC_TERM_B_BLUE = 44, + LC_TERM_B_MAGENTA = 45, + LC_TERM_B_CYAN = 46, + LC_TERM_B_LIGHT_GRAY = 47, + LC_TERM_B_DARK_GRAY = 100, + LC_TERM_B_LIGHT_RED = 101, + LC_TERM_B_LIGHT_GREEN = 102, + LC_TERM_B_LIGHT_YELLOW = 103, + LC_TERM_B_LIGHT_BLUE = 104, + LC_TERM_B_LIGHT_MAGENTA = 105, + LC_TERM_B_LIGHT_CYAN = 106, + LC_TERM_B_WHITE = 107 }; typedef enum lcommon_TermFormatting lcommon_TermFormatting; diff --git a/c_wrapper/src/maths.cpp b/c_wrapper/src/maths.cpp new file mode 100644 index 0000000..56fc70c --- /dev/null +++ b/c_wrapper/src/maths.cpp @@ -0,0 +1,31 @@ +/* + * Copyright © 2018 AperLambda + * + * This file is part of λcommon. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +#include "../include/clambdacommon/maths.h" +#include + +double lcommon_maths_radians(double degrees) +{ + return lambdacommon::maths::degrees(degrees); +} + +float lcommon_maths_fradians(float degrees) +{ + return lambdacommon::maths::degrees(degrees); +} + +double lcommon_maths_degrees(double radians) +{ + return lambdacommon::maths::radians(radians); +} + +float lcommon_maths_fdegrees(float radians) +{ + return lambdacommon::maths::radians(radians); +} \ No newline at end of file diff --git a/c_wrapper/tests/test.c b/c_wrapper/tests/test.c index 8644237..1aae966 100644 --- a/c_wrapper/tests/test.c +++ b/c_wrapper/tests/test.c @@ -6,39 +6,49 @@ void printWithColor(const char *print, lcommon_TermFormatting formatting); +#define TEST(NAME, COUNT, INDEX, FUNC) printf("TESTING %s...\n RESULT: ", NAME); \ +COUNT++; \ +if (FUNC) \ +{ \ + INDEX++; \ + printWithColor("OK.\n", LC_TERM_LIGHT_GREEN); \ +} \ +else \ + printWithColor("FAILED.\n", LC_TERM_LIGHT_RED); \ + int main() { lcommon_term_setup(); lcommon_term_setTerminalTitle(u8"cλcommon - tests"); printf("Starting clambdacommon-tests with "); - printWithColor("lambdacommon", CYAN); + printWithColor("lambdacommon", LC_TERM_CYAN); printf(" (C wrapper) v%s\n\n", lcommon_getVersion()); printf("OS running: "); - printWithColor(lcommon_sys_getOSName(), LIGHT_YELLOW); + printWithColor(lcommon_sys_getOSName(), LC_TERM_LIGHT_YELLOW); printf("\n\n"); printf("Computer DATA:\n"); printf(" Computer Name: "); - lcommon_term_setFormat(LIGHT_YELLOW); + lcommon_term_setFormat(LC_TERM_LIGHT_YELLOW); printf("%s\n", lcommon_sys_getHostName()); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" User Name: "); - lcommon_term_setFormat(LIGHT_YELLOW); + lcommon_term_setFormat(LC_TERM_LIGHT_YELLOW); printf("%s\n", lcommon_sys_getUserName()); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" User Directory: "); - lcommon_term_setFormats(2, LIGHT_BLUE, BOLD); + lcommon_term_setFormats(2, LC_TERM_LIGHT_BLUE, LC_TERM_BOLD); printf("%s\n", lcommon_sys_getUserDirectoryStr()); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" Is run as root: "); - lcommon_term_setFormat(LIGHT_RED); + lcommon_term_setFormat(LC_TERM_LIGHT_RED); printf("%s\n", lcommon_sys_isProcessRunningAsRoot() ? "true" : "false"); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" CPU: %s (%u cores)\n", lcommon_sys_getProcessorName(), lcommon_sys_getProcessorCores()); @@ -46,17 +56,36 @@ int main() uint64_t usedMem = lcommon_sys_getMemoryUsed(); uint64_t availableMem = lcommon_sys_getMemoryAvailable(); printf(" OS Physical Memory: "); - lcommon_term_setFormat(LIGHT_GREEN); + lcommon_term_setFormat(LC_TERM_LIGHT_GREEN); printf("%uMB (%lfGB)\n", (uint32_t) (totalMem / 1048576), totalMem / 1073741824.0); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" OS Available Memory: "); - lcommon_term_setFormat(LIGHT_GREEN); + lcommon_term_setFormat(LC_TERM_LIGHT_GREEN); printf("%uMB (%lfGB)\n", (uint32_t) (availableMem / 1048576), availableMem / 1073741824.0); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); printf(" OS Used Memory: "); - lcommon_term_setFormat(LIGHT_GREEN); + lcommon_term_setFormat(LC_TERM_LIGHT_GREEN); printf("%uMB (%lfGB)\n\n", (uint32_t) (usedMem / 1048576), usedMem / 1073741824.0); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); + + uint32_t testsCount = 0; + uint32_t testsPassed = 0; + + printf("===== MATHS SECTION =====\n"); + + TEST("lcommon_maths_abs((int) 42)", testsCount, testsPassed, lcommon_maths_abs((int) 42) == 42); + + TEST("lcommon_maths_abs((float) -64.0f)", testsCount, testsPassed, lcommon_maths_abs(-64.f) == 64.f); + + TEST("lcommon_maths_min(42, 64)", testsCount, testsPassed, lcommon_maths_min(42, 64) == 42); + + TEST("lcommon_maths_clamp(128, 0, 255)", testsCount, testsPassed, lcommon_maths_clamp(128, 0, 255) == 128); + + TEST("lcommon_maths_clamp(32.f, 0.f, 1.f)", testsCount, testsPassed, lcommon_maths_clamp(32.f, 0.f, 1.f) == 1.f); + + printf("Tests results: %u/%u\n", testsPassed, testsCount); + if (testsPassed != testsCount) + return 1; return 0; } @@ -65,5 +94,5 @@ void printWithColor(const char *print, lcommon_TermFormatting formatting) { lcommon_term_setFormat(formatting); printf("%s", print); - lcommon_term_setFormat(RESET); + lcommon_term_setFormat(LC_TERM_RESET); } \ No newline at end of file diff --git a/include/lambdacommon/lambdacommon.h b/include/lambdacommon/lambdacommon.h index 0f92568..9266d59 100644 --- a/include/lambdacommon/lambdacommon.h +++ b/include/lambdacommon/lambdacommon.h @@ -42,7 +42,10 @@ #define LAMBDACOMMON_VERSION_MAJOR 1 #define LAMBDACOMMON_VERSION_MINOR 6 -#define LAMBDACOMMON_VERSION_PATCH 10 +#define LAMBDACOMMON_VERSION_PATCH 11 + +// Deletes the pointer and sets the variable to null. It's just simpler to write like this. +#define LCOMMON_DELETE_POINTER(pointer) delete pointer; pointer = nullptr; namespace lambdacommon { diff --git a/include/lambdacommon/lstring.h b/include/lambdacommon/lstring.h index 15007c0..c49cd75 100644 --- a/include/lambdacommon/lstring.h +++ b/include/lambdacommon/lstring.h @@ -126,8 +126,6 @@ namespace lambdacommon */ extern long LAMBDACOMMON_API parseLong(const std::string &longNumber, int base = 10); -#ifdef LAMBDA_WINDOWS - /** * Converts a std::wstring to a std::string. * @param wstring The std::wstring to convert. @@ -141,8 +139,6 @@ namespace lambdacommon * @return The converted wstring. */ extern std::wstring LAMBDACOMMON_API convertStringToWString(std::string string); - -#endif } } diff --git a/include/lambdacommon/maths.h b/include/lambdacommon/maths.h index 2d01c15..354aeda 100644 --- a/include/lambdacommon/maths.h +++ b/include/lambdacommon/maths.h @@ -10,8 +10,16 @@ #ifndef LAMBDACOMMON_MATHS_H #define LAMBDACOMMON_MATHS_H +/* + * maths.h + * + * Note: Never use `using namespace maths;` with this header, else the std and this will be conflicting. + */ + #include "lambdacommon.h" +#define LCOMMON_PI 3.14159265359 + namespace lambdacommon { namespace maths @@ -117,6 +125,62 @@ namespace lambdacommon { return maths::min(maths::max(number, min), max); } + + /* + * Trigonometric functions. + */ + + /*! + * Converts a degree value to a radian value. + * @param degrees Value representing an angle, expressed in degrees. + * @return The radian value. + */ + double radians(double degrees); + + /*! + * Converts a degree value to a radian value. + * @param degrees Value representing an angle, expressed in degrees. + * @return The radian value. + */ + float radians(float degrees); + + /*! + * Converts a degree value to a radian value. + * @tparam N The type of the value. + * @param degrees Value representing an angle, expressed in degrees. + * @return The radian value. + */ + template + double radians(N degrees) + { + return radians(static_cast(degrees)); + } + + /*! + * Converts a radian value to a radian value. + * @param degrees Value representing an angle, expressed in radians. + * @return The degree value. + */ + double degrees(double radians); + + /*! + * Converts a radian value to a radian value. + * @param degrees Value representing an angle, expressed in radians. + * @return The degree value. + */ + float degrees(float radians); + + /*! + * Converts a radian value to a radian value. + * @tparam N The type of the value. + * @param degrees Value representing an angle, expressed in radians. + * @return The degree value. + */ + template + double degrees(N radians) + { + return degrees(static_cast(degrees)); + } } } diff --git a/src/lstring.cpp b/src/lstring.cpp index b009061..ab26d21 100644 --- a/src/lstring.cpp +++ b/src/lstring.cpp @@ -179,7 +179,11 @@ namespace lambdacommon #ifdef LAMBDA_WINDOWS #ifndef __GNUC__ +#define CONVERT_WSTRING_WINDOWS_WAY +#endif +#endif +#ifdef CONVERT_WSTRING_WINDOWS_WAY #include std::string LAMBDACOMMON_API convertWStringToString(std::wstring wstring) @@ -219,7 +223,6 @@ namespace lambdacommon return out; } -#endif #endif } diff --git a/src/maths.cpp b/src/maths.cpp new file mode 100644 index 0000000..d5e6b0d --- /dev/null +++ b/src/maths.cpp @@ -0,0 +1,43 @@ +/* + * Copyright © 2018 AperLambda + * + * This file is part of λcommon. + * + * Licensed under the MIT license. For more information, + * see the LICENSE file. + */ + +#include "../include/lambdacommon/maths.h" + +namespace lambdacommon +{ + namespace maths + { + /* + * Trigonometric functions + */ + +#define RADIANS(TYPE, VALUE) VALUE * (static_cast(LCOMMON_PI) / static_cast(180.0)) +#define DEGREES(TYPE, VALUE) VALUE * (static_cast(180.0) / static_cast(LCOMMON_PI)) + + double radians(double degrees) + { + return RADIANS(double, degrees); + } + + float radians(float degrees) + { + return RADIANS(float, degrees); + } + + double degrees(double radians) + { + return DEGREES(double, radians); + } + + float degrees(float radians) + { + return DEGREES(float, radians); + } + } +} \ No newline at end of file diff --git a/test.sh b/test.sh index 45504e5..f2a3ca7 100644 --- a/test.sh +++ b/test.sh @@ -14,6 +14,5 @@ return 1; fi cd build/ -cp liblambdacommon.so tests -cd tests -./lambdacommon_test \ No newline at end of file +./tests/lambdacommon_test +./c_wrapper/tests/clambdacommon_test \ No newline at end of file