-
Notifications
You must be signed in to change notification settings - Fork 0
/
pencil.h
128 lines (113 loc) · 3.14 KB
/
pencil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#pragma once
#include "vectorExtendedPGE.hpp"
#include <vector>
#include <fstream>
#include <iomanip>
#include "physics.h"
#include "geometry.h"
#define dbgLine(VAR) Pencil::DrawDebugLine( std::string(#VAR) + "=" + std::to_string(VAR))
struct Arrow {
olc::vf2d tailLocation;
olc::vf2d vector;
olc::Pixel color;
float scale;
void Draw();
};
class Pencil {
// Singleton
// Trying to make this a buffered Draw, so that debug information can be requested to be drawn not every frame
// Allowing for simulation pausing.
public:
static olc::VxOLCPGE * host;
Pencil() = delete;
static std::vector<Arrow> arrows;
/*Pencil(olc::VxOLCPGE * Renderer) {
host = Renderer;
};*/
static inline void Draw(Line & L, olc::Pixel Color = 0) {
if (Color == 0) {
Color = L.color;
}
host->DrawLine(host->toScreen(L.p1()), host->toScreen(L.p2()), Color);
};
static inline void Draw(Circle & C, olc::Pixel Color = 0)
{
if (Color == 0) {
Color = C.color;
}
host->DrawCircle(host->toScreen(C.pos), (int)host->toScreen(C.radius), Color);
}
static inline void Draw(SegmentedCurve & S, olc::Pixel Color = 0)
{
if (Color == 0) {
Color = S.color;
}
for (int i = 0; i < (int)S.points.size() - 1; i++) {
Line L = { S.points.at(i),S.points.at(i + 1) };
host->DrawLine(host->toScreen(L.p1()),
host->toScreen(L.p2()));
}
if (S.type == S.CLOSED && S.points.size() > 2) {// draw last line
Line L = { S.points.at(0),S.points[S.points.size() - 1] };
host->DrawLine(host->toScreen(L.p2()), host->toScreen(L.p1()), Color);
};
}
static inline void DrawDebugLine(const std::string & debugLine){
host->DrawDebugLine(debugLine);
}
static inline void Draw(RigidBody & B, olc::Pixel Color = 0)
{
for (auto & C : B.circles_w) {
Draw(C);
}
for (auto & S : B.curves_w) {
Draw(S);
}
for (auto & L : B.lines_w) {
Draw(L);
}
}
static void AddArrow(olc::vf2d tailLocation, olc::vf2d vector, float scale = 1.0f, olc::Pixel color = olc::Pixel(255, 150, 150));
static inline void DrawNow() {
for (auto & A : arrows) {
// If they're getting this big, it takes forever to render, so why not just break before in an inifinite loop?
if (A.vector.mag() > 1000 || A.tailLocation.mag() > 1E6) {
continue;
}
else {
A.Draw();
}
}
};
static inline void clearDrawingQueue() {
arrows.clear();
};
// Debug logging -- TODO wrap this in debugLog Class
static std::vector<std::string> headers;
static std::vector<float> dataPoints;
static inline void log(float data) {
#ifdef _DEBUG
dataPoints.push_back(data);
#endif
}
static inline void writeLog() {
#ifdef _DEBUG
const int nFields = headers.size();
const int width = 25;
if (dataPoints.size() / nFields > 0) {
std::fstream output("log.txt", std::fstream::out | std::fstream::binary);
output << "#";
for (auto & Head : headers) {
output << std::setw(width) << Head;
}
output << std::endl;
for (int row = 0; row < dataPoints.size() / nFields; row++) {
for (int col = 0; col < nFields; col++) {
output << std::setprecision(10) << std::setw(width) << dataPoints.at(row*nFields + col);
}
output << std::endl;
}
}
#endif
}
};