This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <format> | |
| int main() { | |
| int sum = 0; | |
| for(int i = 0; i <= 16; i++) { | |
| std::cout << std::format("{:>2} : {:>3}\n", i, sum); | |
| sum += i + i + 1; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Windows.h> | |
| #include <mmdeviceapi.h> | |
| #include <endpointvolume.h> | |
| int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { | |
| CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); | |
| IMMDeviceEnumerator* deviceEnumerator; | |
| CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| std::string stringf(const char* format, ...) { | |
| va_list args; | |
| va_start(args, format); | |
| std::vector<char> buffer(_vscprintf(format, args) + 1); | |
| _vsnprintf_s(buffer.data(), buffer.size(), buffer.size(), format, args); | |
| va_end(args); | |
| return buffer.data(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <chrono> | |
| class Timer { | |
| public: | |
| Timer() { prev = now(); } | |
| //get seconds elapsed since last getDT (or construction) | |
| float getDT() { | |
| auto cur = now(); | |
| std::chrono::duration<float> seconds = cur - prev; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type> | |
| T getUserValue(const std::string& requestString) { | |
| T retval; | |
| while(true) { | |
| std::cout << requestString; | |
| std::cin >> retval; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <functional> | |
| class OnScopeExit { | |
| public: | |
| OnScopeExit(std::function<void()> func) : func(func) {} | |
| ~OnScopeExit() { func(); } | |
| private: | |
| std::function<void()> func; | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Note that this is slow compared to the traditional allocate-and-read method. | |
| //When testing a 230MB file on my machine this was around 4 sec while the traditional method | |
| //was 0.9s. That may be acceptable in some cases, but in 'debug' build in VS it was 113 seconds, | |
| //so honestly this is probably better used for small files or just for showing off. | |
| std::string loadFile(const std::string& fname) { | |
| std::ifstream file(fname); | |
| if(!file) { throw std::runtime_error("loadFile() - File could not be opened."); } | |
| std::stringstream ss; | |
| ss << file.rdbuf(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //written to test http://stackoverflow.com/questions/36090269/finding-height-of-point-on-height-map-triangles | |
| float calcZ(float x, float y, Vec a, Vec b, Vec c) { | |
| //most of these are reused so just solve | |
| //them all first and store the results | |
| float cxbx = c.x - b.x; | |
| float xcx = x - c.x; | |
| float ycy = y - c.y; | |
| float bycy = b.y - c.y; | |
| float axcx = a.x - c.x; |