Skip to content

Instantly share code, notes, and snippets.

View SimpleRepos's full-sized avatar

Richard Brass SimpleRepos

View GitHub Profile
#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;
}
@SimpleRepos
SimpleRepos / masterVolume.cpp
Created September 28, 2017 17:53
Set Master Volume in Windows via WinAPI
#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));
@SimpleRepos
SimpleRepos / stringf.cpp
Created May 6, 2017 19:52
Utility for generating std::strings from printf-style format strings.
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();
}
@SimpleRepos
SimpleRepos / Timer.cpp
Created March 29, 2017 00:04
Unembellished frame timer using <chrono>.
#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;
@SimpleRepos
SimpleRepos / num_cin.cpp
Created March 29, 2017 00:04
A little love for std::cin (very little)
#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;
@SimpleRepos
SimpleRepos / OnScopeExit.cpp
Created March 29, 2017 00:03
Simple utility class to run a function when the scope exits.
#include <functional>
class OnScopeExit {
public:
OnScopeExit(std::function<void()> func) : func(func) {}
~OnScopeExit() { func(); }
private:
std::function<void()> func;
};
@SimpleRepos
SimpleRepos / load_to_string.cpp
Created March 29, 2017 00:03
Load a file into a std::string
//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();
@SimpleRepos
SimpleRepos / heightmapHeight.cpp
Created March 29, 2017 00:02
Find height of x,y coord in height map (z is "up")
//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;