Merge: Basename fix
[nit.git] / lib / mnit / opengles1.nit
index def8598..e2b02bc 100644 (file)
@@ -17,7 +17,7 @@
 # OpenGL ES1 general support (most of it)
 module opengles1 is pkgconfig("glesv1_cm", "egl")
 
-import mnit_display
+import mnit::display
 
 in "C header" `{
        #include <EGL/egl.h>
@@ -55,7 +55,9 @@ 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" `{
@@ -79,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;
@@ -93,8 +97,8 @@ 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) {
                        PRINT_ERROR("error loading image after malloc: %i", mnit_opengles_error_code);
@@ -112,14 +116,15 @@ in "C" `{
                        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) {
                        PRINT_ERROR("error loading image after glTexImage2D: %i", mnit_opengles_error_code);
                }
 
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
 
                if ((mnit_opengles_error_code = glGetError()) != GL_NO_ERROR) {
                        PRINT_ERROR("error loading image after gtTexParameter: %i", mnit_opengles_error_code);
@@ -138,7 +143,7 @@ class Opengles1Display
 
        init do extern_init
        fun midway_init( format: Int ) do end
-       fun extern_init: Bool is extern import midway_init `{
+       fun extern_init: Bool import midway_init `{
                /* initialize OpenGL ES and EGL */
                const EGLint attribs[] = {
                                EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
@@ -147,7 +152,7 @@ class Opengles1Display
                                EGL_RED_SIZE, 8,
                                EGL_NONE
                };
-               EGLint w, h, dummy, format;
+               EGLint w, h, format;
                EGLint numConfigs;
                EGLConfig config;
                EGLSurface surface;
@@ -180,7 +185,7 @@ class Opengles1Display
                }
 
                /* Used by Android to set buffer geometry */
-               Opengles1Display_midway_init(recv, format);
+               Opengles1Display_midway_init(self, format);
 
                surface = eglCreateWindowSurface(display, config, mnit_window, NULL);
                context = eglCreateContext(display, config, NULL, NULL);
@@ -211,7 +216,7 @@ class Opengles1Display
                return 0;
        `}
 
-       fun close is extern `{
+       fun close `{
                if ( mnit_display != EGL_NO_DISPLAY) {
                        eglMakeCurrent( mnit_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
                        if ( mnit_context != EGL_NO_CONTEXT) {
@@ -227,23 +232,23 @@ class Opengles1Display
                 mnit_surface = EGL_NO_SURFACE;
        `}
 
-       redef fun begin is extern `{
+       redef fun begin `{
                glClear(GL_COLOR_BUFFER_BIT);
                glLoadIdentity();
        `}
 
-       redef fun width: Int is extern `{
+       redef fun width: Int `{
                return mnit_width;
        `}
-       redef fun height: Int is extern `{
+       redef fun height: Int `{
                return mnit_height;
        `}
 
-       redef fun finish is extern `{
+       redef fun finish `{
                eglSwapBuffers( mnit_display, mnit_surface );
        `}
 
-       redef fun set_viewport( x, y, w, h ) is extern `{
+       redef fun set_viewport( x, y, w, h ) `{
                glLoadIdentity();
                glViewport(0,0, mnit_width, mnit_height );
                glMatrixMode(GL_PROJECTION);
@@ -401,12 +406,12 @@ class Opengles1Display
                }
        `}
 
-       redef fun clear( r, g, b: Float ) is extern `{
+       redef fun clear( r, g, b: Float ) `{
                glClearColor( r, g, b, 1.0 );
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        `}
 
-       fun clear_alpha( r, g, b, a: Float ) is extern `{
+       fun clear_alpha( r, g, b, a: Float ) `{
                glClearColor( r, g, b, a );
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        `}
@@ -423,41 +428,43 @@ end
 extern class Opengles1Image in "C" `{struct mnit_opengles_Texture *`}
        super Image
 
-    redef fun destroy is extern `{ free( recv ); `}
+    redef fun destroy `{ free( self ); `}
 
-    redef fun width: Int is extern `{ return recv->width; `}
-    redef fun height: Int is extern `{ return recv->height; `}
+    redef fun width: Int `{ return self->width; `}
+    redef fun height: Int `{ return self->height; `}
 
-       fun center_x: Int `{ return recv->center_x; `}
-       fun center_y: Int `{ return recv->center_y; `}
+       fun center_x: Int `{ return self->center_x; `}
+       fun center_y: Int `{ return self->center_y; `}
 
-    redef fun scale=( v: Float ) is extern `{
-               recv->scale = v;
-               recv->center_x = v*recv->width/2;
-               recv->center_y = v*recv->height/2;
+    redef fun scale=( v: Float ) `{
+               self->scale = v;
+               self->center_x = v*self->width/2;
+               self->center_y = v*self->height/2;
     `}
-    redef fun scale: Float is extern `{ return recv->scale; `}
+    redef fun scale: Float `{ return self->scale; `}
 
-    redef fun blended=( v: Bool ) is extern `{ recv->blended = v; `}
-    redef fun blended: Bool is extern `{ return recv->blended; `}
+    redef fun blended=( v: Bool ) `{ self->blended = v; `}
+    redef fun blended: Bool `{ return self->blended; `}
 
     # inherits scale and blend from source
-    redef fun subimage( x, y, w, h: Int ): Image is extern import Opengles1Image.as( Image ) `{
+    redef fun subimage( x, y, w, h: Int ): Image import Opengles1Image.as( Image ) `{
                struct mnit_opengles_Texture* image =
                        malloc( sizeof( struct mnit_opengles_Texture ) );
 
-               image->texture = recv->texture;
+               image->texture = self->texture;
                image->width = w;
                image->height = h;
-               image->center_x = recv->scale*w/2;
-               image->center_y = recv->scale*h/2;
-               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;
+               image->center_x = self->scale*w/2;
+               image->center_y = self->scale*h/2;
+               image->scale = self->scale;
+               image->blended = self->blended;
+
+               float r_dx = self->src_xi - self->src_xo;
+               float r_dy = self->src_yi - self->src_yo;
+               image->src_xo = self->src_xo + ((float)x)/self->width*r_dx;
+               image->src_yo = self->src_yo + ((float)y)/self->height*r_dy;
+               image->src_xi = self->src_xo + ((float)x+w)/self->width*r_dx;
+               image->src_yi = self->src_yo + ((float)y+h)/self->height*r_dy;
 
                return Opengles1Image_as_Image( image );
     `}