2003/10/16 05:39:20
[org.ibex.core.git] / src / org / xwt / plat / OpenGL.java
index 5367cb6..284fb62 100644 (file)
@@ -11,6 +11,7 @@ abstract class OpenGL {
     boolean hasRectangularTextures() { return rectangularTextures; }
     int maxTexSize;
     int maxRectTexSize;
+    float glVersion;
     String version;
     String renderer;
     String vendor;
@@ -26,13 +27,12 @@ abstract class OpenGL {
             return 1.1f; // just a guess
         }
     }
+    
     // This MUST be called after OpenGL is instansiated (and activateSharedContext is functioning)
     public void init() throws NotSupportedException {
         natInit();
-        float v = parseVersion(version);
-        // FEATURE: enable linear filtering for OpenGL >= 1.2
-        // If we disable linear filtering (and therefor GL_CLAMP_TO_EDGE) we could probably get by with less
-        if(v < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + v + ")");
+        glVersion = parseVersion(version);
+        if(glVersion < 1.1) throw new NotSupportedException("OpenGL 1.1 or greater is required. (you have: " + version +" - " + glVersion + ")");
         if(pretendToBeACrappyVideoCard) {
             maxTexSize = 512;
             maxRectTexSize = 0;
@@ -82,16 +82,16 @@ abstract class OpenGL {
         
         //public native void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2,
         //int sx1, int sy1, int sx2, int sy2, int rgb);
-        public void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2,
-            int sx1, int sy1, int sx2, int sy2, int rgb) { }
-
-        public void drawPicture(org.xwt.Picture source, int x, int y) {
-            activateContext();
-            GLPicture p = (GLPicture) source;
-            p.draw(x,y);
+        public void drawPictureAlphaOnly(Picture source, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, int rgb) {
+            drawPicture_(source,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,rgb);
+        }
+        public void drawPicture(Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) {
+            drawPicture_(source,dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2,0xffffffff);
         }
-        public void drawPicture(org.xwt.Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2) {
+
+        private void drawPicture_(Picture source, int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2, int color) {
             activateContext();
+            setColor(color);
             GLPicture p = (GLPicture) source;
             p.draw(dx1,dy1,dx2,dy2,sx1,sy1,sx2,sy2);
         }
@@ -103,10 +103,19 @@ abstract class OpenGL {
         return 0;
     }
         
+    private Picture _createPicture(Object data, int w, int h, boolean alphaOnly) {
+        if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,alphaOnly,this);
+        if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,alphaOnly,this);
+        return new MosaicGLPicture(data,w,h,alphaOnly,this);
+    }
+    
     public Picture createPicture(int[] data, int w, int h) {
-        if(rectangularTextures && w <= maxRectTexSize && h <= maxRectTexSize) new RectGLPicture(data,w,h,this);
-        if(w <= maxTexSize && h <= maxTexSize) return new SquareGLPicture(data,w,h,this);
-        return new MosaicGLPicture(data,w,h,this);
+        if(w*h > data.length) throw new Error("should never happen");
+        return _createPicture(data,w,h,false);
+    }
+    public Picture createAlphaOnlyPicture(byte[] data, int w, int h) {
+        if(w*h > data.length) throw new Error("should never happen");
+        return _createPicture(data,w,h,true);
     }
     
     private native void natDeleteTexture(int tex);
@@ -130,7 +139,6 @@ abstract class OpenGL {
             this.height = h;
         }
         
-        public void draw(int x, int y) { draw(x,y,x+width,y+height,0,0,width,height); }
         public abstract void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);
         protected abstract void finalize();
     }
@@ -139,12 +147,12 @@ abstract class OpenGL {
         private OpenGL gl;
         public int textureName;
                 
-        public native void natInit(int[] data);
+        public native void natInit(Object data, boolean alphaOnly);
         
-        public RectGLPicture(int[] data,int w, int h, OpenGL gl) {
+        public RectGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
             super(w,h);
             this.gl = gl;
-            natInit(data);
+            natInit(data,alphaOnly);
         }
     
         public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2);  
@@ -157,15 +165,15 @@ abstract class OpenGL {
         private OpenGL gl;
         public int textureName;
                 
-        public native void natInit(int[] data);
+        public native void natInit(Object data, boolean alphaOnly);
                 
-        public SquareGLPicture(int[] data,int w, int h, OpenGL gl) {
+        public SquareGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
             super(w,h);
             this.gl = gl;
             if(w > 0x7fffffff || h > 0x7fffffff) throw new Error("insane texture size: " + w + "x" + h);
             texHeight = roundToPowerOf2(h);
             texWidth = roundToPowerOf2(w);
-            natInit(data);
+            natInit(data,alphaOnly);
         }
         
         public native void draw(int dx1, int dy1, int dx2, int dy2,int sx1, int sy1, int sx2, int sy2); 
@@ -184,21 +192,31 @@ abstract class OpenGL {
             return 1.0f-(float)(w*h)/(float)(w2*h2);
         }
         
+        private static Object subData(Object data, int x, int y, int w, int h, int rowSize) {
+            if(data instanceof byte[]) return subData((byte[])data,x,y,w,h,rowSize);
+            if(data instanceof int[]) return subData((int[])data,x,y,w,h,rowSize);
+            throw new Error("not reached");
+        }
+        
         private static int[] subData(int[] data, int x, int y, int w, int h, int rowSize) {
             int[] sub = new int[w*h];
             int row = y;
-            try {
             for(int i=0;i<h;i++,row++)
                 for(int j=0;j<w;j++)
                     sub[i*w+j] = data[row*rowSize+j+x];
-            } catch(ArrayIndexOutOfBoundsException e) {
-                Log.log(MosaicGLPicture.class,"subData(data("+data.length+"),"+x+","+y+","+w+","+h+","+rowSize);
-                throw e;
-            }
             return sub;
         }
         
-        public MosaicGLPicture(int[] data,int w, int h, OpenGL gl) {
+        private static byte[] subData(byte[] data, int x, int y, int w, int h, int rowSize) {
+            byte[] sub = new byte[w*h];
+            int row = y;
+            for(int i=0;i<h;i++,row++)
+                for(int j=0;j<w;j++)
+                    sub[i*w+j] = data[row*rowSize+j+x];
+            return sub;
+        }
+        
+        public MosaicGLPicture(Object data,int w, int h, boolean alphaOnly, OpenGL gl) {
             super(w,h);
             psize = gl.maxTexSize;
             while(wastedSpace(w,h,psize) > 0.40) psize/=2;
@@ -211,14 +229,14 @@ abstract class OpenGL {
             pics = new GLPicture[rows][cols];
             for(int i=0;i<rows-1;i++)
                 for(int j=0;j<cols-1;j++)
-                    pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,gl);
+                    pics[i][j] = new SquareGLPicture(subData(data,j*psize,i*psize,psize,psize,w),psize,psize,alphaOnly,gl);
             tmp = (rows-1)*psize;
             for(int i=0;i<cols-1;i++)
-                pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,gl);
+                pics[rows-1][i] = new SquareGLPicture(subData(data,i*psize,tmp,psize,h-tmp,w),psize,h-tmp,alphaOnly,gl);
             tmp2 = (cols-1)*psize;
             for(int i=0;i<rows-1;i++)
-                pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,gl);
-            pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,gl);
+                pics[i][cols-1] = new SquareGLPicture(subData(data,tmp2,i*psize,w-tmp2,psize,w),w-tmp2,psize,alphaOnly,gl);
+            pics[rows-1][cols-1] = new SquareGLPicture(subData(data,tmp2,tmp,w-tmp2,h-tmp,w),w-tmp2,h-tmp,alphaOnly,gl);
         }
         protected void finalize() {  }