cleanup build. add demo project. add timing features to sync. switch to more std::string and std::vector usage over custom, hacky Array template

This commit is contained in:
2025-09-01 21:56:43 -05:00
parent b4e90dce4d
commit 75e72ba9ca
25 changed files with 663 additions and 1759 deletions

View File

@@ -2,6 +2,7 @@
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/glm.hpp>
#include <string>
#include "util.hpp"
struct Body {
@@ -11,11 +12,11 @@ struct Body {
uint vao;
uint vbo;
uint shader;
Array<float> verts;
Array<int> faces;
std::vector<float> verts;
std::vector<int> faces;
glm::vec4 color;
};
bool load_body(Body* out_body, const char* obj_filepath);
bool load_body(Body* out_body, std::string filepath);
void draw_body(const Body& b);
void create_new_sphere(Body* b, float scale=1.0f);

View File

@@ -1,5 +1,3 @@
#pragma once
#include "util.hpp"
bool parse_poses(Array<glm::mat2x3>* locs_out, const char* filepath);
bool parse_poses(std::vector<glm::mat2x3>* locs_out, const char* filepath);

10
inc/logger.hpp Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
#include <string>
void log(const char* msg);
void log(std::string msg);
void log(char*, int len);
void flush(const char* filepath);
void start_auto_flush_thread(const char* filepath, double period);
void start_auto_flush_thread();

180
inc/sync.hpp Normal file
View File

@@ -0,0 +1,180 @@
#pragma once
#ifdef _WIN32
#include "windows.h"
#endif
namespace sync {
#ifdef _WIN32
typedef CRITICAL_SECTION Mutex;
typedef CONDITION_VARIABLE ConditionVar;
typedef HANDLE Semaphore;
typedef HANDLE Thread;
typedef LARGE_INTEGER TimeSpan;
typedef DWORD (WINAPI *ThreadFunc)(_In_ LPVOID lpParameter);
typedef LPVOID ThreadArg;
const TimeSpan infinite_ts = LLONG_MAX;
static const LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
#endif
Thread make_thread(ThreadFunc t, ThreadArg a);
void join(Thread t);
Mutex make_mutex();
void lock(Mutex &m);
bool trylock(Mutex &m);
void unlock(Mutex &m);
void dispose(Mutex &m);
ConditionVar make_condition_var();
void wait(ConditionVar &c, Mutex &m, TimeSpan ts);
void wake_one(ConditionVar &c);
void wake_all(ConditionVar &c);
void dispose(ConditionVar &c);
Semaphore make_semaphore(int initial, int max);
void wait(Semaphore &s);
void post(Semaphore &s);
void dispose(Semaphore &s);
TimeSpan from_ms(double milliseconds);
TimeSpan from_s(double seconds);
TimeSpan from_min(double minutes);
TimeSpan from_hours(double hours);
TimeSpan now();
TimeSpan operator-(const TimeSpan &a, const TimeSpan &b);
double to_ms(TimeSpan &sp);
double to_s(TimeSpan &sp);
double to_min(TimeSpan &sp);
double to_hours(TimeSpan &sp);
#ifdef _WIN32
Thread make_thread(ThreadFunc f, ThreadArg a) {
DWORD tid;
return CreateThread(NULL, 0, f, a, 0, &tid);
}
void join(Thread t) {
WaitForSingleObject(t, infinite_ts);
}
Mutex make_mutex() {
Mutex m;
InitializeCriticalSection(&m);
return m;
}
void lock(Mutex &m) {
EnterCriticalSection(&m);
}
bool trylock(Mutex &m) {
return TryEnterCriticalSection(&m);
}
void unlock(Mutex &m) {
LeaveCriticalSection(&m);
}
void dispose(Mutex &m) {
DeleteCriticalSection(&m);
}
ConditionVar make_condition_var() {
ConditionVar c;
InitializeConditionVariable(&c);
return c;
}
void wait(ConditionVar &c, Mutex &m, TimeSpan ts) {
SleepConditionVariableCS(&c, &m, ts);
}
void wake_one(ConditionVar &c) {
WakeConditionVariable(&c);
}
void wake_all(ConditionVar &c) {
WakeAllConditionVariable(&c);
}
void dispose(ConditionVar &c) {
return; // Windows doesn't have a delete condition variable func
}
Semaphore make_semaphore(int initial, int max) {
return CreateSemaphoreA(NULL, (long)initial, (long)max, NULL);
}
void wait(Semaphore &s) {
WaitForSingleObject(s, infinite_ts);
}
void post(Semaphore &s) {
ReleaseSemaphore(s, 1, NULL);
}
void dispose(Semaphore &s) {
CloseHandle(s);
}
TimeSpan from_ms(double milliseconds) {
TimeSpan ts;
ts.QuadPart = static_cast<int64_t>(milliseconds/1000.0)*freq.QuadPart;
return ts;
}
TimeSpan from_s(double seconds) {
TimeSpan ts;
ts.QuadPart = static_cast<int64_t>(seconds)*freq.QuadPart;
return ts;
}
TimeSpan from_min(double minutes) {
TimeSpan ts;
ts.QuadPart = static_cast<int64_t>(minutes*60.0)*freq.QuadPart;
return ts;
}
TimeSpan from_hours(double hours) {
TimeSpan ts;
ts.QuadPart = static_cast<int64_t>(hours*60.0*60.0)*freq.QuadPart;
return ts;
}
TimeSpan now() {
TimeSpan ts;
QueryPerformanceCounter(&ts);
return ts;
}
TimeSpan operator-(const TimeSpan &a, TimeSpan &b) {
TimeSpan ts;
ts.QuadPart = a.QuadPart - b.QuadPart;
return ts;
}
double to_ms(TimeSpan &sp) {
return static_cast<double>(sp.QuadPart*1000)/static_cast<double>(freq.QuadPart);
}
double to_s(TimeSpan &sp) {
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart);
}
double to_min(TimeSpan &sp) {
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart*60);
}
double to_hours(TimeSpan &sp) {
return static_cast<double>(sp.QuadPart)/static_cast<double>(freq.QuadPart*60*60);
}
#endif
} // namespace sync

View File

@@ -2,30 +2,18 @@
#include <stdio.h>
#include <cstdlib>
#include <string>
#include <fstream>
#include <sstream>
#include <glm/ext/matrix_float4x4.hpp>
#include <glm/ext/quaternion_float.hpp>
#include <glm/ext/vector_float4.hpp>
typedef unsigned int uint;
template<class T> struct Array {
T* data;
size_t len;
size_t cap;
inline T& operator[](int i) { return data[i]; }
};
template<class T> void append(Array<T>& a, T el);
template<class T> T pop(Array<T>& a);
template<class T> void resize(Array<T>& a, size_t new_cap);
template<class T> void clear(Array<T>& a);
bool read_file(Array<char>* out, const char* filepath);
Array<char*> split_str(const char* s, char delimiter);
Array<char*> split_str(const char* s);
bool read_file(std::string* s, const char* filepath);
std::vector<std::string> split_str(std::string s, char delim);
glm::mat4 quat_to_mat4(glm::quat q);
float randf();
template<class T> T lerp(T start, T end, float t) { return t * end + (1 - t) * start; }
template<class T> float ilerp(T start, T end, T pos) { return (pos - start) / (end - start); }
float randf(); // returns a float between 0 and 1
template<class T> float ilerp(T start, T end, T pos) { return (pos - start) / (end - start); }