-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathofApp.cpp
More file actions
executable file
·159 lines (126 loc) · 3.49 KB
/
ofApp.cpp
File metadata and controls
executable file
·159 lines (126 loc) · 3.49 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup() {
ofDisableAntiAliasing();
ofBackgroundHex(0xfdefc2);
ofSetLogLevel(OF_LOG_NOTICE);
ofSetVerticalSync(true);
// Box2d
box2d.init();
box2d.setGravity(0, 30);
box2d.createGround();
box2d.setFPS(60.0);
// load the lines we saved...
std::ifstream f;
f.open(ofToDataPath("lines.txt").c_str());
vector <string> strLines;
while (!f.eof()) {
string ptStr;
getline(f, ptStr);
strLines.push_back(ptStr);
}
f.close();
for (int i=0; i<strLines.size(); i++) {
vector <string> pts = ofSplitString(strLines[i], ",");
if(pts.size() > 0) {
auto edge = make_shared<ofxBox2dEdge>();
for (int j=0; j<pts.size(); j+=2) {
if(pts[j].size() > 0) {
float x = ofToFloat(pts[j]);
float y = ofToFloat(pts[j+1]);
edge->addVertex(x, y);
}
}
edge->create(box2d.getWorld());
edges.push_back(edge);
}
}
}
//--------------------------------------------------------------
void ofApp::update() {
// add some circles every so often
if((int)ofRandom(0, 10) == 0) {
auto c = make_shared<ofxBox2dCircle>();
c->setPhysics(0.2, 0.2, 0.002);
c->setup(box2d.getWorld(), ofRandom(20, 50), -20, ofRandom(3, 10));
c->setVelocity(0, 15); // shoot them down!
circles.push_back(c);
}
box2d.update();
}
//--------------------------------------------------------------
void ofApp::draw() {
for (auto &circle : circles) {
ofFill();
ofSetHexColor(0xc0dd3b);
circle->draw();
}
ofSetHexColor(0x444342);
ofNoFill();
for (auto &line : lines) {
line.draw();
}
for (auto & edge : edges) {
edge->draw();
}
string info = "Draw a shape with the mouse\n";
info += "Press 1 to add some circles\n";
info += "Press c to clear everything\n";
ofSetHexColor(0x444342);
ofDrawBitmapString(info, 10, 15);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key) {
if(key == '1') {
auto c = make_shared<ofxBox2dCircle>();
c->setPhysics(1, 0.5, 0.5);
c->setup(box2d.getWorld(), mouseX, mouseY, 10);
circles.push_back(c);
}
if(key == 'c') {
lines.clear();
edges.clear();
}
/*
// want to save out some line...
if(key == ' ') {
std::ofstream f;
f.clear();
f.open(ofToDataPath("lines.txt").c_str());
for (int i=0; i<lines.size(); i++) {
for (int j=0; j<lines[i].size(); j++) {
float x = lines[i][j].x;
float y = lines[i][j].y;
f << x << "," << y << ",";
}
f << "\n";
}
f.close();lines.clear();
}*/
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ) {
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button) {
lines.back().addVertex(x, y);
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button) {
lines.push_back(ofPolyline());
lines.back().addVertex(x, y);
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button) {
auto edge = make_shared<ofxBox2dEdge>();
lines.back().simplify();
for (int i=0; i<lines.back().size(); i++) {
edge->addVertex(lines.back()[i]);
}
// edge->setPhysics(1, .2, 1); // uncomment this to see it fall!
edge->create(box2d.getWorld());
edges.push_back(edge);
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h) {
}