[ Video ]
[ About ]
Day 28 Speaker
絵文字と物理エンジン(Box2d)の相性が良き
Pair speaker.
[ Source ]
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 |
#pragma once #include "ofMain.h" #include "ofxBox2d.h" class ofApp : public ofBaseApp { public: void setup(); void update(); void draw(); void keyPressed(int key) {}; void keyReleased(int key) {}; void mouseMoved(int x, int y) {}; void mouseDragged(int x, int y, int button) {}; void mousePressed(int x, int y, int button) {}; void mouseReleased(int x, int y, int button) {}; void windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; int size; ofTrueTypeFont font; ofxBox2d box2d; vector<shared_ptr<ofxBox2dCircle>> circles; vector<int> note_types; vector<ofColor> colors; vector<float> lifes; bool left; bool right; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetCircleResolution(30); ofSetLineWidth(2); //ofNoFill(); this->size = 32; ofTrueTypeFontSettings font_settings("fonts/EmojiSymbols-Regular.ttf", this->size); font_settings.antialiased = true; font_settings.addRanges(ofAlphabet::Emoji); this->font.load(font_settings); this->box2d.init(); this->box2d.setGravity(0, 5); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); } //-------------------------------------------------------------- void ofApp::update() { this->left = false; this->right = false; if (ofRandom(100) > 85) { if (ofRandom(2) > 1) { this->left = true; } else { this->right = true; } float radius = this->size * 0.5; auto circle = make_shared<ofxBox2dCircle>(); circle->setPhysics(1.0, 0.63, 0.1); circle->setup(this->box2d.getWorld(), this->left ? ofGetWidth() * 0.25 : ofGetWidth() * 0.75, ofGetHeight() * 0.5 + 100, radius); circle->addForce(ofVec2f(ofRandom(-10, 10), ofRandom(-10, 10)), 150.0); this->circles.push_back(circle); this->note_types.push_back(ofRandom(2)); ofColor color; color.setHsb(ofRandom(255), 255, 230); this->colors.push_back(color); this->lifes.push_back(255); } for (int i = 0; i < this->circles.size(); i++) { this->lifes[i]--; if (this->lifes[i] < 0) { this->circles[i].get()->destroy(); this->circles.erase(this->circles.begin() + i); this->note_types.erase(this->note_types.begin() + i); this->colors.erase(this->colors.begin() + i); this->lifes.erase(this->lifes.begin() + i); } } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { // Draw Speaker Left ofPushMatrix(); ofTranslate(ofGetWidth() * 0.25, ofGetHeight() * 0.5); ofSetColor(39); ofDrawRectangle(-150, -300, 300, 600); ofSetColor(139); ofDrawCircle(50, -100, 50); this->left ? ofDrawCircle(0, 100, 100) : ofDrawCircle(0, 100, 110); ofPopMatrix(); // Draw Speaker Right ofPushMatrix(); ofTranslate(ofGetWidth() * 0.75, ofGetHeight() * 0.5); ofSetColor(39); ofDrawRectangle(-150, -300, 300, 600); ofSetColor(139); ofDrawCircle(-50, -100, 50); this->right ? ofDrawCircle(0, 100, 100) : ofDrawCircle(0, 100, 110); ofPopMatrix(); // Draw Note for (int i = 0; i < this->circles.size(); i++) { ofPushMatrix(); ofTranslate(this->circles[i]->getPosition().x, this->circles[i]->getPosition().y); ofRotate(this->circles[i]->getRotation()); ofSetColor(this->colors[i], this->lifes[i] + 32); this->note_types[i] == 0 ? this->font.drawString(u8"🎵", this->circles[i]->getRadius() * -1, this->circles[i]->getRadius()) : this->font.drawString(u8"🎶", this->circles[i]->getRadius() * -1, this->circles[i]->getRadius()); ofPopMatrix(); } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |