Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ofTrueTypeFont.cpp to make it work with Emscripten #6764

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ var LibraryHTML5Audio = {
try {
// Fix up for prefixing
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var context = new AudioContext({
latencyHint: "interactive",
sampleRate: 44100
});

// Fix issue with chrome autoplay policy
document.addEventListener('mousedown', function cb(event) {
Expand Down
107 changes: 86 additions & 21 deletions addons/ofxEmscripten/src/ofxAppEmscriptenWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
EGLint majorVersion;
EGLint minorVersion;
EGLConfig config;
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
EGLint contextAttribs[] = { EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE, EGL_NONE };
std::vector <EGLint> attribList =
{
EGL_RED_SIZE, EGL_DONT_CARE,
Expand All @@ -68,12 +68,25 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
EGL_DEPTH_SIZE, EGL_DONT_CARE,
EGL_STENCIL_SIZE, EGL_DONT_CARE,
EGL_SAMPLE_BUFFERS, EGL_DONT_CARE,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT_KHR,
EGL_NONE
};

// We'll try these alpha sizes in order ending with EGL_DONT_CARE if we don't get anything higher.
std::vector <EGLint> alphaPreference = {8, EGL_DONT_CARE};

// Find the index for the value EGL_ALPHA_SIZE uses, so we can try a few different values till we get a successful config.
int attribListAlphaIndex = -1;
for(int i = 0; i < attribList.size(); i++){
if( attribList[i] == EGL_ALPHA_SIZE ){
attribListAlphaIndex = i+1;
break;
}
}

// We'll try these depth sizes in order ending with EGL_DONT_CARE if we don't get anything higher.
std::vector <EGLint> depthPreference = {24, 16, EGL_DONT_CARE};

// Find the index for the value EGL_DEPTH_SIZE uses, so we can try a few different values till we get a successful config.
int attribListDepthIndex = -1;
for(int i = 0; i < attribList.size(); i++){
Expand All @@ -83,6 +96,18 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
}
}

// We'll try these sample buffers in order ending with EGL_DONT_CARE if we don't get anything higher.
std::vector <EGLint> sampleBuffersPreference = {1, EGL_DONT_CARE};

// Find the index for the value EGL_SAMPLE_BUFFERS uses, so we can try a few different values till we get a successful config.
int attribListSampleBuffersIndex = -1;
for(int i = 0; i < attribList.size(); i++){
if( attribList[i] == EGL_SAMPLE_BUFFERS ){
attribListSampleBuffersIndex = i+1;
break;
}
}

// Get Display
display = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
if ( display == EGL_NO_DISPLAY ){
Expand All @@ -102,6 +127,27 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
return;
}

// Choose the config based on our attribute list
// Try higher EGL_ALPHA_SIZE first
for(int i = 0; i < alphaPreference.size(); i++){
// Set EGL_ALPHA_SIZE
attribList[attribListAlphaIndex] = alphaPreference[i];

// Try out that depth value
if ( !eglChooseConfig(display, &attribList[0], &config, 1, &numConfigs) ){

// Finally fail like we did before if no preference works
if( alphaPreference[i] == EGL_DONT_CARE ){
ofLogError() << "couldn't choose display";
return;
}

}else{
// Got a good configuration. Stop searching.
break;
}
}

// Choose the config based on our attribute list
// Try higher EGL_DEPTH_SIZE first
for(int i = 0; i < depthPreference.size(); i++){
Expand All @@ -122,6 +168,27 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
break;
}
}

// Choose the config based on our attribute list
// Try higher EGL_SAMPLE_BUFFERS first
for(int i = 0; i < sampleBuffersPreference.size(); i++){
// Set EGL_SAMPLE_BUFFERS
attribList[attribListSampleBuffersIndex] = sampleBuffersPreference[i];

// Try out that depth value
if ( !eglChooseConfig(display, &attribList[0], &config, 1, &numConfigs) ){

// Finally fail like we did before if no preference works
if( sampleBuffersPreference[i] == EGL_DONT_CARE ){
ofLogError() << "couldn't choose display";
return;
}

}else{
// Got a good configuration. Stop searching.
break;
}
}

// Create a surface
surface = eglCreateWindowSurface(display, config, NULL, NULL);
Expand All @@ -148,16 +215,16 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
_renderer = make_shared<ofGLProgrammableRenderer>(this);
((ofGLProgrammableRenderer*)_renderer.get())->setup(2,0);

emscripten_set_keydown_callback(0,this,1,&keydown_cb);
emscripten_set_keyup_callback(0,this,1,&keyup_cb);
emscripten_set_mousedown_callback(0,this,1,&mousedown_cb);
emscripten_set_mouseup_callback(0,this,1,&mouseup_cb);
emscripten_set_mousemove_callback(0,this,1,&mousemoved_cb);
emscripten_set_keydown_callback("#canvas",this,1,&keydown_cb);
emscripten_set_keyup_callback("#canvas",this,1,&keyup_cb);
emscripten_set_mousedown_callback("#canvas",this,1,&mousedown_cb);
emscripten_set_mouseup_callback("#canvas",this,1,&mouseup_cb);
emscripten_set_mousemove_callback("#canvas",this,1,&mousemoved_cb);

emscripten_set_touchstart_callback(0,this,1,&touch_cb);
emscripten_set_touchend_callback(0,this,1,&touch_cb);
emscripten_set_touchmove_callback(0,this,1,&touch_cb);
emscripten_set_touchcancel_callback(0,this,1,&touch_cb);
emscripten_set_touchstart_callback("#canvas",this,1,&touch_cb);
emscripten_set_touchend_callback("#canvas",this,1,&touch_cb);
emscripten_set_touchmove_callback("#canvas",this,1,&touch_cb);
emscripten_set_touchcancel_callback("#canvas",this,1,&touch_cb);
}

void ofxAppEmscriptenWindow::loop(){
Expand Down Expand Up @@ -229,9 +296,9 @@ int ofxAppEmscriptenWindow::mouseup_cb(int eventType, const EmscriptenMouseEvent

int ofxAppEmscriptenWindow::mousemoved_cb(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData){
if(ofGetMousePressed()){
instance->events().notifyMouseDragged(mouseEvent->canvasX,mouseEvent->canvasY,0);
instance->events().notifyMouseDragged(mouseEvent->targetX,mouseEvent->targetY,0);
}else{
instance->events().notifyMouseMoved(mouseEvent->canvasX,mouseEvent->canvasY);
instance->events().notifyMouseMoved(mouseEvent->targetX,mouseEvent->targetY);
}
return 0;

Expand Down Expand Up @@ -261,8 +328,8 @@ int ofxAppEmscriptenWindow::touch_cb(int eventType, const EmscriptenTouchEvent*
ofTouchEventArgs touchArgs;
touchArgs.type = touchArgsType;
touchArgs.id = i;
touchArgs.x = e->touches[i].canvasX;
touchArgs.y = e->touches[i].canvasY;
touchArgs.x = e->touches[i].targetX;
touchArgs.y = e->touches[i].targetY;
instance->events().notifyTouchEvent(touchArgs);
}
return 0;
Expand All @@ -282,21 +349,19 @@ void ofxAppEmscriptenWindow::setWindowPosition(int x, int y){
}

void ofxAppEmscriptenWindow::setWindowShape(int w, int h){
emscripten_set_canvas_element_size(NULL,w,h);
emscripten_set_canvas_size(w,h);
}



glm::vec2 ofxAppEmscriptenWindow::getWindowPosition(){
return glm::vec2(0,0);
}


glm::vec2 ofxAppEmscriptenWindow::getWindowSize(){
int width;
int height;
emscripten_get_canvas_element_size(NULL, &width, &height);
return glm::vec2(width,height);
int isFullscreen;
emscripten_get_canvas_size(&width, &height, &isFullscreen);
return glm::vec3(width,height,isFullscreen);
}

glm::vec2 ofxAppEmscriptenWindow::getScreenSize(){
Expand Down
7 changes: 6 additions & 1 deletion libs/openFrameworks/graphics/ofTrueTypeFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,12 @@ bool ofTrueTypeFont::loadFont(string filename, int fontSize, bool bAntiAliased,
//-----------------------------------------------------------
ofTrueTypeFont::glyph ofTrueTypeFont::loadGlyph(uint32_t utf8) const{
glyph aGlyph;
auto err = FT_Load_Glyph( face.get(), FT_Get_Char_Index( face.get(), utf8 ), settings.antialiased ? FT_LOAD_FORCE_AUTOHINT : FT_LOAD_DEFAULT );
bool autoHint = settings.antialiased;
#ifdef TARGET_EMSCRIPTEN
autoHint = false;
#endif

auto err = FT_Load_Glyph( face.get(), FT_Get_Char_Index( face.get(), utf8 ), autoHint ? FT_LOAD_FORCE_AUTOHINT : FT_LOAD_DEFAULT );
if(err){
ofLogError("ofTrueTypeFont") << "loadFont(): FT_Load_Glyph failed for utf8 code " << utf8 << ": FT_Error " << err;
return aGlyph;
Expand Down