oFで日本語テキストをタレ流しするaddon
日本語をoFでタレ流しする超地味アドオン書いてた。 ofxTrueTypeFontUC使ってます。(製作者は多分研究室の先輩)
同じクラス名のアドオンがofxaddonsにあったけどとりあえず気にしない。。
ofxTextWriter.h
#include "ofMain.h" #include "ofxTrueTypeFontUC.h" static const int DISAPEAR_TIME = 5; class ofxTextWriter{ public: void setup(string fontPath, int size, float s); void update(); void draw(); void setString(string s, ofPoint pos, ofColor c); private: struct stringGroup{ string str; string drawStr; vector<int> charsizeList; int nChar; int nDrawChar; float time; ofPoint position; ofColor color; }; ofxTrueTypeFontUC font; int fontSize; float speed; vector<stringGroup> strings; vector<int> getCharsizeList(const string &input); int getUTF8Byte(unsigned char input); };
ofxTextWriter.cpp
#include "ofxTextWriter.h" template<typename T> void remove(std::vector<T>& vector, unsigned int index) { vector.erase(vector.begin() + index); } void ofxTextWriter::setup(string fontPath, int size, float s = 0.3){ fontSize = size; speed = s; font.loadFont(fontPath, fontSize); } void ofxTextWriter::update(){ float t = ofGetElapsedTimef(); for(int i=0;i<strings.size();i++){ if(strings[i].nDrawChar < strings[i].nChar){ if(t-strings[i].time > speed){ strings[i].nDrawChar ++; int dByte = 0; for(int c=0;c<strings[i].nDrawChar;c++) dByte += strings[i].charsizeList[c]; strings[i].drawStr = strings[i].str.substr(0, dByte); strings[i].time = ofGetElapsedTimef(); } } else{ if(t - strings[i].time > DISAPEAR_TIME){ strings[i].drawStr = strings[i].str; if(strings[i].color.a > 0){ strings[i].color.a *= cos((t -strings[i].time - DISAPEAR_TIME)); } else{ remove(strings, i); } } } } } void ofxTextWriter::draw(){ for(int i=0;i<strings.size();i++){ ofSetColor(strings[i].color); font.drawString(strings[i].drawStr, strings[i].position.x, strings[i].position.y); } ofSetColor(255); ofDrawBitmapString("numStr: " +ofToString(strings.size()), 10., 10); ofDrawBitmapString("FPS:" + ofToString(ofGetFrameRate()), 10, 20); } void ofxTextWriter::setString(string s, ofPoint pos, ofColor c){ stringGroup strGroup; strGroup.str = s; strGroup.drawStr = ""; strGroup.charsizeList = getCharsizeList(s); strGroup.nChar = strGroup.charsizeList.size(); strGroup.nDrawChar = 0; strGroup.time = ofGetElapsedTimef(); strGroup.position = pos; strGroup.color = c; strings.push_back(strGroup); } vector<int> ofxTextWriter::getCharsizeList(const string &input){ vector<int> sizeList; int target = 0; while (target < input.size()) { int iByte = getUTF8Byte(input[target]); sizeList.push_back(iByte); target += iByte; } return sizeList; } int ofxTextWriter::getUTF8Byte(unsigned char input){ int iByte; if ((input >= 0x00) && (input <= 0x7f)) { iByte = 1; } else if ((input >= 0xc2) && (input <= 0xdf)) { iByte = 2; } else if ((input >= 0xe0) && (input <= 0xef)) { iByte = 3; } else if ((input >= 0xf0) && (input <= 0xf7)) { iByte = 4; } else if ((input >= 0xf8) && (input <= 0xfb)) { iByte = 5; } else if ((input >= 0xfc) && (input <= 0xfd)) { iByte = 6; } else { iByte = 0; } return iByte; }