lib/mnit: expand images to the nearest power of 2 for OpenGL ES 1.0
[nit.git] / lib / mnit / opengles1.nit
index 56c6b29..24a904b 100644 (file)
 # limitations under the License.
 
 # OpenGL ES1 general support (most of it)
-module opengles1 is pkgconfig("glesv1_cm", "x11", "egl")
+module opengles1 is pkgconfig("glesv1_cm", "egl")
 
 import mnit_display
 
 in "C header" `{
        #include <EGL/egl.h>
        #include <GLES/gl.h>
-       #define GL_GLEXT_PROTOTYPES 1
-       #include <GLES/glext.h>
-       #include <errno.h>
 
        EGLDisplay mnit_display;
        EGLSurface mnit_surface;
@@ -32,7 +29,6 @@ in "C header" `{
        EGLConfig mnit_config;
        int32_t mnit_width;
        int32_t mnit_height;
-       float mnit_zoom;
 
        struct mnit_opengles_Texture {
                GLuint texture;
@@ -59,17 +55,12 @@ in "C header" `{
 
        GLenum mnit_opengles_error_code;
 
-       struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha );
+       struct mnit_opengles_Texture *mnit_opengles_load_image(
+               const uint_least32_t *pixels, int width, int height,
+               int width_pow2, int height_pow2, int has_alpha);
 `}
 
 in "C" `{
-       #define LOGW(...) ((void)fprintf(stderr, "# warn: %s (%i)\n", __VA_ARGS__))
-       #ifdef DEBUG
-               #define LOGI(...) ((void)fprintf(stderr, "# info: %s (%i)\n", __VA_ARGS__))
-       #else
-               #define LOGI(...) (void)0
-       #endif
-
        extern NativeWindowType mnit_window;
        extern EGLNativeDisplayType mnit_native_display;
 
@@ -90,7 +81,9 @@ in "C" `{
                {1.0f, 0.0f}
        };
 
-       struct mnit_opengles_Texture *mnit_opengles_load_image( const uint_least32_t *pixels, int width, int height, int has_alpha )
+       struct mnit_opengles_Texture *mnit_opengles_load_image(
+               const uint_least32_t *pixels, int width, int height,
+               int width_pow2, int height_pow2, int has_alpha)
        {
                struct mnit_opengles_Texture *image = malloc(sizeof(struct mnit_opengles_Texture));
                int format = has_alpha? GL_RGBA: GL_RGB;
@@ -104,36 +97,36 @@ in "C" `{
 
                image->src_xo = 0;
                image->src_yo = 0;
-               image->src_xi = 1.0;
-               image->src_yi = 1.0;
+               image->src_xi = ((float)width)/width_pow2;
+               image->src_yi = ((float)height)/height_pow2;
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                       LOGW("error loading image after malloc", mnit_opengles_error_code);
+                       PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
                }
 
                glGenTextures(1, &image->texture);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                       LOGW("error loading image after glGenTextures", mnit_opengles_error_code);
+                       PRINT_ERROR("error loading image after glGenTextures: %i", mnit_opengles_error_code);
                }
 
                glBindTexture(GL_TEXTURE_2D, image->texture);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                       LOGW("error loading image glBindTexture", mnit_opengles_error_code);
+                       PRINT_ERROR("error loading image glBindTexture: %i", mnit_opengles_error_code);
                }
 
-               glTexImage2D(   GL_TEXTURE_2D, 0, format, width, height,
+               glTexImage2D(   GL_TEXTURE_2D, 0, format, width_pow2, height_pow2,
                                                0, format, GL_UNSIGNED_BYTE, (GLvoid*)pixels);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                       LOGW("error loading image after glTexImage2D", mnit_opengles_error_code);
+                       PRINT_ERROR("error loading image after glTexImage2D: %i", mnit_opengles_error_code);
                }
 
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                       LOGW("error loading image after gtTexParameter", mnit_opengles_error_code);
+                       PRINT_ERROR("error loading image after gtTexParameter: %i", mnit_opengles_error_code);
                }
 
                return image;
@@ -166,27 +159,27 @@ class Opengles1Display
 
                EGLDisplay display = eglGetDisplay(mnit_native_display);
                if ( display == EGL_NO_DISPLAY) {
-                       LOGW("Unable to eglGetDisplay", 0);
+                       PRINT_ERROR("Unable to eglGetDisplay");
                        return -1;
                }
 
                if ( eglInitialize(display, 0, 0) == EGL_FALSE) {
-                       LOGW("Unable to eglInitialize", 0);
+                       PRINT_ERROR("Unable to eglInitialize");
                        return -1;
                }
 
                if ( eglChooseConfig(display, attribs, &config, 1, &numConfigs) == EGL_FALSE) {
-                       LOGW("Unable to eglChooseConfig", 0);
+                       PRINT_ERROR("Unable to eglChooseConfig");
                        return -1;
                }
 
                if ( numConfigs == 0 ) {
-                       LOGW("No configs available for egl", 0);
+                       PRINT_ERROR("No configs available for egl");
                        return -1;
                }
 
                if ( eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format) == EGL_FALSE) {
-                       LOGW("Unable to eglGetConfigAttrib", 0);
+                       PRINT_ERROR("Unable to eglGetConfigAttrib");
                        return -1;
                }
 
@@ -197,7 +190,7 @@ class Opengles1Display
                context = eglCreateContext(display, config, NULL, NULL);
 
                if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
-                       LOGW("Unable to eglMakeCurrent", 0);
+                       PRINT_ERROR("Unable to eglMakeCurrent");
                        return -1;
                }
 
@@ -210,12 +203,6 @@ class Opengles1Display
                mnit_config = config;
                mnit_width = w;
                mnit_height = h;
-               mnit_zoom = 1.0f;
-
-               LOGI("surface", (int)surface);
-               LOGI("display", (int)display);
-               LOGI("width", w);
-               LOGI("height", h);
 
                glViewport(0, 0, mnit_width, mnit_height);
                glMatrixMode(GL_PROJECTION);
@@ -266,12 +253,13 @@ class Opengles1Display
                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrthof(x, x+w, y+h, y, 0.0f, 1.0f);
-               mnit_zoom = ((float)w)/mnit_width;
                glMatrixMode(GL_MODELVIEW);
                glFrontFace( GL_CW );
        `}
 
-       redef fun blit( image, x, y ) is extern  `{
+       redef fun blit(image, x, y) do native_blit(image, x.to_f, y.to_f)
+
+       private fun native_blit(image: Opengles1Image, x, y: Float)  `{
                GLfloat texture_coord[4][2] =
                {
                        {image->src_xo, image->src_yi},
@@ -308,18 +296,20 @@ class Opengles1Display
                glDisable(GL_TEXTURE_2D);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                  LOGW("error drawing", mnit_opengles_error_code);
+                  PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
                }
        `}
 
-    redef fun blit_centered( img, x, y )
+    redef fun blit_centered(img, x, y)
     do
-               x = x - img.center_x
-               y = y - img.center_y
-               blit( img, x, y )
+               x = x.sub(img.center_x)
+               y = y.sub(img.center_y)
+               blit(img, x, y)
     end
 
-       redef fun blit_rotated( image, x, y, angle ) is extern  `{
+       redef fun blit_rotated(image, x, y, angle) do native_blit_rotated(image, x.to_f, y.to_f, angle)
+
+       private fun native_blit_rotated(image: Opengles1Image, x, y, angle: Float) `{
                GLfloat texture_coord[4][2] =
                {
                        {image->src_xo, image->src_yi},
@@ -356,12 +346,19 @@ class Opengles1Display
                glDisable(GL_TEXTURE_2D);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                  LOGW("error drawing", mnit_opengles_error_code);
+                  PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
                }
        `}
 
        # a = top left, b = bottom left, c = bottom right, d = top right
-       redef fun blit_stretched( image, ax, ay, bx, by, cx, cy, dx, dy ) is extern  `{
+       redef fun blit_stretched(image, ax, ay, bx, by, cx, cy, dx, dy)
+       do
+               native_blit_stretched(image,
+                       ax.to_f, ay.to_f, bx.to_f, by.to_f,
+                       cx.to_f, cy.to_f, dx.to_f, dy.to_f)
+       end
+
+       private fun native_blit_stretched(image: I, ax, ay, bx, by, cx, cy, dx, dy: Float) `{
                GLfloat texture_coord[4][2] =
                {
                        {image->src_xo, image->src_yi},
@@ -404,7 +401,7 @@ class Opengles1Display
                glDisable(GL_TEXTURE_2D);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
-                  LOGW("error drawing", mnit_opengles_error_code);
+                  PRINT_ERROR("error drawing: %i", mnit_opengles_error_code);
                }
        `}
 
@@ -461,10 +458,12 @@ extern class Opengles1Image in "C" `{struct mnit_opengles_Texture *`}
                image->scale = recv->scale;
                image->blended = recv->blended;
 
-               image->src_xo = ((float)x)/recv->width;
-               image->src_yo = ((float)y)/recv->height;
-               image->src_xi = ((float)x+w)/recv->width;
-               image->src_yi = ((float)y+h)/recv->height;
+               float r_dx = recv->src_xi - recv->src_xo;
+               float r_dy = recv->src_yi - recv->src_yo;
+               image->src_xo = recv->src_xo + ((float)x)/recv->width*r_dx;
+               image->src_yo = recv->src_yo + ((float)y)/recv->height*r_dy;
+               image->src_xi = recv->src_xo + ((float)x+w)/recv->width*r_dx;
+               image->src_yi = recv->src_yo + ((float)y+h)/recv->height*r_dy;
 
                return Opengles1Image_as_Image( image );
     `}