Last active
May 5, 2019 19:40
-
-
Save shiyuugohirao/20d8f70dc13c179c8a3be078b9056e2f to your computer and use it in GitHub Desktop.
my snippets for oepnFrameworks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ofSnippets.h | |
// | |
// Created by shugohirao on 2018/11/01~. | |
// | |
/*================================================== | |
setup | |
==================================================*/ | |
static void loadImg2Tex(ofTexture &tex, string path, float scale=1.0) { | |
try{ | |
ofImage img; | |
if (!img.load(path)) throw ("~~~ failed to load ("+path+")"); | |
img.resize(img.getWidth()*scale, img.getHeight()*scale); | |
if (!img.getTexture().isAllocated()) throw "~~~ failed to get texture ("+path+")"; | |
tex = img.getTexture(); | |
ofLogNotice("loadImg2Tex")<<"Success to set texture ("<<path<<")"; | |
} | |
catch(string err){ | |
ofSystemAlertDialog(err); | |
} | |
}; | |
static void loadImg2Tex(vector<ofTexture> &tex, string path, float scale=1.0){ | |
ofDirectory dir; | |
dir.allowExt("png"); | |
dir.allowExt("jpg"); | |
dir.listDir(path); | |
dir.sort(); | |
for (int i = 0; i < dir.size(); i++) { | |
tex.resize(tex.size()+1); | |
loadImg2Tex(tex.back(),dir.getPath(i),scale); | |
} | |
}; | |
/*================================================== | |
update | |
==================================================*/ | |
//--- sin with time | |
sin(ofGetElapsedTimef()*TWO_PI / cycleMillis); | |
//--- EASING ---// | |
enum EASE_TYPE{ | |
LINEAR, EASE_IN, EASE_OUT, EASE_IO_IN, EASE_IO_OUT, | |
}; | |
const vector<string> EASE_TYPE_NAME = { | |
"LINEAR", "EASE_IN", "EASE_OUT", "EASE_IO_IN", "EASE_IO_OUT", | |
}; | |
//[*] t= 0.~1. | |
static float easeCalc(float t, EASE_TYPE type, float easeVal){ | |
switch(type){ | |
case LINEAR: return t; | |
case EASE_IN: return pow(t,easeVal); | |
case EASE_OUT: return pow(t,1/easeVal); | |
case EASE_IO_IN: return (t<0.5) ? pow(t*2,easeVal)*0.5 : 1-pow(2-t*2, easeVal)*0.5; | |
case EASE_IO_OUT: return (t<0.5) ? pow(t*2,1/easeVal)*0.5 : 1-pow(2-t*2, 1/easeVal)*0.5; | |
} | |
}; | |
/*================================================== | |
draw | |
==================================================*/ | |
//--- drawRing ver01 | |
void drawRing(float x, float y, float r, float w, ofColor color, bool TOPLEFT=false){ | |
ofPushStyle(); | |
ofPushMatrix(); | |
ofPath curve; | |
curve.setCircleResolution(60); | |
ofPoint center; | |
center = TOPLEFT ? ofPoint(x+r,y+r) : ofPoint(x,y); | |
curve.arc(center, r,r, 0, 360); | |
curve.arcNegative(center, r-w,r-w, 360, 0); | |
curve.close(); | |
curve.setFillColor(color); | |
curve.setFilled(true); | |
curve.draw(); | |
ofPopMatrix(); | |
ofPopStyle(); | |
} | |
//--- drawFillPolyline | |
ofVboMesh vboMesh; | |
ofTessellator tess; | |
tess.tessellateToMesh(ofPolyline, ofPolyWindingMode::OF_POLY_WINDING_ODD, vboMesh, true); | |
vboMesh.draw(); | |
/*================================================== | |
shader | |
==================================================*/ | |
void setupShaderScript(){ | |
#define STRINGIFY(A) #A | |
string shaderProgram = "#version 120\n" | |
STRINGIFY( | |
uniform vec2 resolution; | |
uniform sampler2DRect tex; | |
void main(){ | |
vec2 st = gl_TexCoord[0].st / resolution; | |
vec4 col = texture2DRect(tex, gl_TexCoord[0].st); | |
gl_FragColor = col; | |
} | |
); | |
string status = | |
shader.setupShaderFromSource(GL_FRAGMENT_SHADER, shaderProgram) && shader.linkProgram() ? "OK":"ERROR"; | |
cout<<("shader link! -> " + status) <<endl; | |
} | |
/*================================================== | |
debug | |
==================================================*/ | |
//--- show fps on windowTitleBar | |
ofSetWindowTitle(ofToString(ofGetFrameRate(),2)); | |
//--- get process time | |
int t = ofGetElapsedTimeMicros(); | |
ofLogNotice("ProcessTime") << ((ofGetElapsedTimeMicros() - t)*0.000001) <<"s"; | |
/*================================================== | |
ofTrueTypeFont | |
==================================================*/ | |
//--- loadJapnese | |
ofTrueTypeFont* font; | |
font = new ofTrueTypeFont(); | |
ofTrueTypeFontSettings settings("KozGoPr6N-ExtraLight.otf", 30); | |
settings.addRanges(ofAlphabet::Japanese); | |
font->load(settings); | |
//--- drawWithAnchor | |
//[*] anchor(0:center 1:LT 2:RT 3:LB 4:RB), bRect(show debugRectangle) | |
void drawString(ofTrueTypeFont *font, const string &msg, int x, int y, int anchor=0, bool bRect=false){ | |
float w = font->stringWidth(msg); | |
float h = font->stringHeight(msg); | |
switch(anchor){ | |
case 0:{ x-=w*0.5; y+=h*0.5; } break; | |
case 1:{ y+=h; } break; | |
case 2:{ x-=w; y+=h; } break; | |
case 4:{ x-=w; } break; | |
default: break; | |
} | |
font->drawString(msg,x,y); | |
if(bRect){ | |
ofPushStyle(); | |
ofNoFill(); | |
ofSetColor(255, 0, 0,127); | |
ofDrawRectangle(font->getStringBoundingBox(msg, x, y)); | |
ofPopStyle(); | |
} | |
} | |
/*================================================== | |
ofImage | |
==================================================*/ | |
//--- threadedSaveImage | |
static void threadedSaveImage(const ofPixels &pix, std::filesystem::path filePath){ | |
if(pix.isAllocated() && filePath.size()>0){ | |
thread imageThread([=]{ | |
int t = ofGetElapsedTimeMicros(); | |
ofSaveImage(pix, filePath); | |
ofLogVerbose("threadedSaveImage") << " - about"+to_string((ofGetElapsedTimeMicros() - t)*0.000001) <<"sec"; | |
}); | |
ofLogNotice("threadedSaveImage")<< " - save image (" + ofToString(imageThread.get_id()) + ")"; | |
imageThread.detach(); | |
}else{ | |
ofLogError("threadedSaveImage")<<"failed to play sound file ("<<filePath<<")"; | |
} | |
} | |
static void threadedSaveImage(const ofTexture &tex, std::filesystem::path filePath){ | |
ofPixels pix; | |
tex.readToPixels(pix); | |
threadedSaveImage(pix, filePath); | |
} | |
/*================================================== | |
ofSoundPlayer | |
==================================================*/ | |
//--- threadedPlaySE | |
static void threadedPlaySE(string soundPath){ | |
if(ofFile::doesFileExist(soundPath)){ | |
thread soundThread([=]{ | |
ofSoundPlayer sp; | |
sp.load(soundPath); | |
sp.setLoop(false); | |
ofLogVerbose("threadedPlaySE")<< "play SE"; | |
int t = ofGetElapsedTimeMicros(); | |
sp.play(); | |
while(sp.isLoaded() && sp.isPlaying()){} | |
ofLogVerbose("threadedPlaySE") << " - about"+to_string((ofGetElapsedTimeMicros() - t)*0.000001) <<"sec"; | |
}); | |
soundThread.detach(); | |
}else{ | |
ofLogError("threadedPlaySE")<<"failed to play sound file ("<<soundPath<<")"; | |
} | |
} | |
/*================================================== | |
other (tiny class etc..) | |
==================================================*/ | |
//--- getRandomT by controled rate | |
template <typename T> | |
class MIKUJI { | |
private: | |
map<T,float> kujiset; //[*] {kuji,rate} | |
public: | |
MIKUJI(){} | |
MIKUJI(map<T,float> kujiset){ | |
setup(kujiset); | |
} | |
void setup(map<T,float> kujiset){ | |
float sumRate=0.; | |
for(auto kuji:kujiset) sumRate += kuji.second; | |
ofLogWarning("MIKUJI")<<"sumRate:"<<sumRate<<" - sumRate should be 1.00"; | |
this->kujiset = kujiset; | |
} | |
T divine(){ | |
float r = ofRandom(1); | |
float f=0; | |
for(auto kuji:kujiset) { | |
f += kuji.second; | |
if(r < f) return kuji.first; | |
} | |
} | |
//[*] ho to use | |
// map<Type,float> set={{typeA,0.5},{typeB,0.3},{typeC,0.2},}; | |
// MIKUJI<Type> kuji(set); | |
// Type result = kuji.divine(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment