[work 70] Pixel Font
Movie
Source code
about
- フォントを読み込んで文字ごとにピクセル化する
file
- 上部にあるファイル名が表示されているボタンを押すと、表示されるファイルが切り替わります
- 別ウィンドウ表示したい時や行番号などが無いRawMode表示したい時は、コード内右上のボタンを押してください(ボタンはマウスオーバーすると表示されます)
#include "ofMain.h" #include "ofApp.h" //================================ int main( ){ // 4K:4096x2160 // 2K:2048x1080 // FullHD:1920x1080 // HD:1440x1080 // HD720p:1280x720 // DVD:720x480 // setup the GL context ofSetupOpenGL(1280, 720, OF_WINDOW); // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp( new ofApp()); }
#pragma once #include "ofMain.h" #include "Box.hpp" class ofApp : public ofBaseApp{ public: ofApp(); ~ofApp(); 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 mouseEntered(int x, int y); void mouseExited(int x, int y); void windowResized(int w, int h); void dragEvent(ofDragInfo dragInfo); void gotMessage(ofMessage msg); private: std::shared_ptr<Box> box; };
#include "ofApp.h" ofApp::ofApp(){ } ofApp::~ofApp(){ } //-------------------------------------------------------------- void ofApp::setup(){ double fps = 30; ofSetFrameRate(fps); ofBackground(255); ofSetBackgroundAuto(true); ofSetVerticalSync(true); box = make_shared<Box>(); box->setup(); } //-------------------------------------------------------------- void ofApp::update(){ box->update(); } //-------------------------------------------------------------- void ofApp::draw(){ box->display(); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if (key == 's') { ofImage img; img.grabScreen(0, 0, ofGetWidth(), ofGetHeight()); img.save("screenshot.png"); } }
#ifndef Box_hpp #define Box_hpp #include <stdio.h> #include "ofMain.h" class Box { public: Box(); ~Box(); void setup(); void update(); void display(); private: void drawBox(ofVec2f c); void setOffset(char c); std::vector<ofVec2f> box; std::vector<ofVec2f> orgPos; std::vector<bool> fill; int angle; int index; ofTrueTypeFont font; }; #endif /* Box_hpp */
#include "Box.hpp" Box::Box() { } Box::~Box() { } void Box::setup() { ofVec2f d = ofVec2f(0, 1); float len = 50; int num = 6; box.push_back(ofVec2f(0, 0)); for (int i = 0; i < num; i++) { ofVec2f org = d; box.push_back(org.rotate((360 / num) * i) * len); } ofVec2f s = ofVec2f(ofGetWidth() / 2 + 100, 150); for (int i = 0; i < 7; i++) { ofVec2f p = s; for (int j = 0; j < 13; j++) { orgPos.push_back(p); p += box.at(2); } s += box.at(6); } font.load("font/misaki_gothic_2nd.ttf", 10); angle = 0; index = 0; } void Box::update() { string message("HELLOWORLD!"); if ((angle == 0) && (index < message.size())) { char c = message.at(index); setOffset(c); index++; } angle = (angle + 6) % 360; } void Box::display() { int idx = 0; for (ofVec2f p : orgPos) { ofVec2f offset = ofVec2f(0, 0); float rad = TWO_PI * ((float)angle / 360); if (fill.at(idx) != false) { offset.y = ofMap(std::sin(rad), -1, 1, 200, -200, true); } drawBox(p + offset); idx++; } } void Box::drawBox(ofVec2f c) { ofFill(); ofSetColor(3, 91, 114); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(1)); ofVertex(c + box.at(2)); ofVertex(c + box.at(3)); ofEndShape(); ofSetColor(153, 196, 222); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(3)); ofVertex(c + box.at(4)); ofVertex(c + box.at(5)); ofEndShape(); ofSetColor(255, 255, 217); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(5)); ofVertex(c + box.at(6)); ofVertex(c + box.at(1)); ofEndShape(); ofNoFill(); ofSetLineWidth(3); ofSetColor(0); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(1)); ofVertex(c + box.at(2)); ofVertex(c + box.at(3)); ofEndShape(); ofSetColor(0); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(3)); ofVertex(c + box.at(4)); ofVertex(c + box.at(5)); ofEndShape(); ofSetColor(0); ofBeginShape(); ofVertex(c + box.at(0)); ofVertex(c + box.at(5)); ofVertex(c + box.at(6)); ofVertex(c + box.at(1)); ofEndShape(); } void Box::setOffset(char c) { ofTexture tex; ofPixels pix; ofFbo fbo; string str(1, c); tex = font.getStringTexture(str); fbo.allocate(tex.getWidth(), tex.getHeight()); fbo.begin(); ofClear(0); tex.draw(0, 0); fbo.end(); fbo.readToPixels(pix); fill.clear(); for (int i = 0; i < pix.getWidth(); i++) { std::vector<bool> tmp; for (int j = 0; j < pix.getHeight(); j++) { if (pix.getColor(i, j) != ofColor(0, 0, 0, 0)) { if (j != pix.getHeight() - 1) { tmp.push_back(true); } else { auto itr = tmp.begin(); tmp.insert(itr, true); } } else { if (j != pix.getHeight() - 1) { tmp.push_back(false); } else { auto itr = tmp.begin(); tmp.insert(itr, false); } } } if (i != pix.getWidth() - 1) { auto itr = fill.end(); fill.insert(itr, tmp.begin(), tmp.end()); } else { auto itr = fill.begin(); fill.insert(itr, tmp.begin(), tmp.end()); } } }
Link to the reference page
ソースコードで使用したAPIの中から要点になりそうなものをいくつか選んでリストアップしました。
category | API/Lib |
openframeworks | ofTrueTypeFont |
openframeworks | ofFbo |
openframeworks | ofTexture |
openframeworks | ofPixels |
Development environment
- openframeworks 0.10.1
- c++
- macOS
- Xcode