Search code examples
androidc++opengl-esnative

How to use OpenGL to draw a text over a Android MediaPlayer?


I have a cpp code implementing a media player behavior on Android.

I'm using the media player for playing a mp4 file however, I need to draw text above this.

For testing purposes, I've already tried to do as drawText() function from BootAnimation.cpp however without success.

I'm guessing there is some OpenGL calls I'm missing. Is there some call to be added inside drawText() for it to draw above the mp4?

void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
    glEnable(GL_BLEND);  // Allow us to draw on top of the animation
    glBindTexture(GL_TEXTURE_2D, font.texture.name);
    const int len = strlen(str);
    const int strWidth = font.char_width * len;
    if (*x == TEXT_CENTER_VALUE) {
        *x = (mWidth - strWidth) / 2;
    } else if (*x < 0) {
        *x = mWidth + *x - strWidth;
    }
    if (*y == TEXT_CENTER_VALUE) {
        *y = (mHeight - font.char_height) / 2;
    } else if (*y < 0) {
        *y = mHeight + *y - font.char_height;
    }
    int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
    for (int i = 0; i < len; i++) {
        char c = str[i];
        if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
            c = '?';
        }
        // Crop the texture to only the pixels in the current glyph
        const int charPos = (c - FONT_BEGIN_CHAR);  // Position in the list of valid characters
        const int row = charPos / FONT_NUM_COLS;
        const int col = charPos % FONT_NUM_COLS;
        cropRect[0] = col * font.char_width;  // Left of column
        cropRect[1] = row * font.char_height * 2; // Top of row
        // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
        cropRect[1] += bold ? 2 * font.char_height : font.char_height;
        glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
        glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
        *x += font.char_width;
    }
    glDisable(GL_BLEND);  // Return to the animation's default behaviour
    glBindTexture(GL_TEXTURE_2D, 0);
}

PS: this is no android app, so it won't be done in app layer.


Solution

  • The Bootanimation.cpp use of OpenGL ES changed a bit and now it's using a more modern way to deal with graphics.

    That being said, I found that my case would need a some abstraction as done here. Basic OpenGL manipulation, as use of common vertex and fragment shaders (position and color, really nothing different from fundamentals) and VBO/VAO for data buffering and glDrawArrays is enough for my usage.

    I still need to understand and apply some texture and understand the best way (in my scenario) for manipulate text, however I think that is the all.