OSDN Git Service

[finished] gdx2d crap. Needs a little more testing, but it works for the first releas...
authorbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 26 Jan 2011 14:04:03 +0000 (14:04 +0000)
committerbadlogicgames <badlogicgames@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Wed, 26 Jan 2011 14:04:03 +0000 (14:04 +0000)
gdx/jni/gdx2d/gdx2d.c
gdx/src/com/badlogic/gdx/graphics/g2d/Gdx2DPixmap.java
tests/gdx-tests/src/com/badlogic/gdx/tests/Gdx2DTest.java

index a01088c..412be0a 100644 (file)
@@ -82,6 +82,18 @@ inline uint32_t to_format(uint32_t format, uint32_t color) {
        }\r
 }\r
 \r
+#define min(a, b) (a > b?b:a)\r
+\r
+inline uint32_t weight_RGBA8888(uint32_t color, float weight) {\r
+       uint32_t r, g, b, a;\r
+       r = min((uint32_t)(((color & 0xff000000) >> 24) * weight), 255);\r
+       g = min((uint32_t)(((color & 0xff0000) >> 16) * weight), 255);\r
+       b = min((uint32_t)(((color & 0xff00) >> 8) * weight), 255);\r
+       a = min((uint32_t)(((color & 0xff)) * weight), 255);\r
+\r
+       return (r << 24) | (g << 16) | (b << 8) | a;\r
+}\r
+\r
 inline uint32_t to_RGBA8888(uint32_t format, uint32_t color) {\r
        uint32_t r, g, b, a;\r
 \r
@@ -677,9 +689,143 @@ void blit_same_size(const gdx2d_pixmap* src_pixmap, const gdx2d_pixmap* dst_pixm
        }\r
 }\r
 \r
+void blit_bilinear(const gdx2d_pixmap* src_pixmap, const gdx2d_pixmap* dst_pixmap,\r
+                  int32_t src_x, int32_t src_y, uint32_t src_width, uint32_t src_height,\r
+                  int32_t dst_x, int32_t dst_y, uint32_t dst_width, uint32_t dst_height) {\r
+       set_pixel_func pset = set_pixel_func_ptr(dst_pixmap->format);\r
+       get_pixel_func pget = get_pixel_func_ptr(src_pixmap->format);\r
+       get_pixel_func dpget = get_pixel_func_ptr(dst_pixmap->format);\r
+       uint32_t sbpp = bytes_per_pixel(src_pixmap->format);\r
+       uint32_t dbpp = bytes_per_pixel(dst_pixmap->format);\r
+       uint32_t spitch = sbpp * src_pixmap->width;\r
+       uint32_t dpitch = dbpp * dst_pixmap->width;\r
+\r
+       float x_ratio = ((float)src_width - 1)/ dst_width;\r
+       float y_ratio = ((float)src_height - 1) / dst_height;\r
+       float x_diff = 0;\r
+       float y_diff = 0;\r
+\r
+       int dx = dst_x;\r
+       int dy = dst_y;\r
+       int sx = src_x;\r
+       int sy = src_y;\r
+       int i = 0;\r
+       int j = 0;\r
+\r
+       for(;i < dst_height; i++) {\r
+               sy = (int)(i * y_ratio) + src_y;\r
+               dy = i + dst_y;\r
+               y_diff = (y_ratio * i + src_y) - sy;\r
+               if(sy < 0 || dy < 0) continue;\r
+               if(sy >= src_pixmap->height || dy >= dst_pixmap->height) break;\r
+\r
+               for(j = 0 ;j < dst_width; j++) {\r
+                       sx = (int)(j * x_ratio) + src_x;\r
+                       dx = j + dst_x;\r
+                       x_diff = (x_ratio * j + src_x) - sx;\r
+                       if(sx < 0 || dx < 0) continue;\r
+                       if(sx >= src_pixmap->width || dx >= dst_pixmap->width) break;\r
+\r
+                       const void* dst_ptr = dst_pixmap->pixels + dx * dbpp + dy * dpitch;\r
+                       const void* src_ptr = src_pixmap->pixels + sx * sbpp + sy * spitch;\r
+                       uint32_t c1 = 0, c2 = 0, c3 = 0, c4 = 0;\r
+                       c1 = to_RGBA8888(src_pixmap->format, pget((void*)src_ptr));\r
+                       if(sx + 1 < src_width) c2 = to_RGBA8888(src_pixmap->format, pget((void*)(src_ptr + sbpp))); else c2 = c1;\r
+                       if(sy + 1< src_height) c3 = to_RGBA8888(src_pixmap->format, pget((void*)(src_ptr + spitch))); else c3 = c1;\r
+                       if(sx + 1< src_width && sy + 1 < src_height) c4 = to_RGBA8888(src_pixmap->format, pget((void*)(src_ptr + spitch + sbpp))); else c4 = c1;\r
+\r
+                       float ta = (1 - x_diff) * (1 - y_diff);\r
+                       float tb = (x_diff) * (1 - y_diff);\r
+                       float tc = (1 - x_diff) * (y_diff);\r
+                       float td = (x_diff) * (y_diff);\r
+\r
+                       uint32_t r = (uint32_t)(((c1 & 0xff000000) >> 24) * ta +\r
+                                                                       ((c2 & 0xff000000) >> 24) * tb +\r
+                                                                       ((c3 & 0xff000000) >> 24) * tc +\r
+                                                                       ((c4 & 0xff000000) >> 24) * td) & 0xff;\r
+                       uint32_t g = (uint32_t)(((c1 & 0xff0000) >> 16) * ta +\r
+                                                                       ((c2 & 0xff0000) >> 16) * tb +\r
+                                                                       ((c3 & 0xff0000) >> 16) * tc +\r
+                                                                       ((c4 & 0xff0000) >> 16) * td) & 0xff;\r
+                       uint32_t b = (uint32_t)(((c1 & 0xff00) >> 8) * ta +\r
+                                                                       ((c2 & 0xff00) >> 8) * tb +\r
+                                                                       ((c3 & 0xff00) >> 8) * tc +\r
+                                                                       ((c4 & 0xff00) >> 8) * td) & 0xff;\r
+                       uint32_t a = (uint32_t)((c1 & 0xff) * ta +\r
+                                                                       (c2 & 0xff) * tb +\r
+                                                                       (c3 & 0xff) * tc +\r
+                                                                       (c4 & 0xff) * td) & 0xff;\r
+\r
+                       uint32_t src_col = (r << 24) | (g << 16) | (b << 8) | a;\r
+\r
+                       if(gdx2d_blend) {\r
+                               uint32_t dst_col = to_RGBA8888(dst_pixmap->format, dpget((void*)dst_ptr));\r
+                               src_col = to_format(dst_pixmap->format, blend(src_col, dst_col));\r
+                       } else {\r
+                               src_col = to_format(dst_pixmap->format, src_col);\r
+                       }\r
+\r
+                       pset((void*)dst_ptr, src_col);\r
+               }\r
+       }\r
+}\r
+\r
+void blit_linear(const gdx2d_pixmap* src_pixmap, const gdx2d_pixmap* dst_pixmap,\r
+                  int32_t src_x, int32_t src_y, uint32_t src_width, uint32_t src_height,\r
+                  int32_t dst_x, int32_t dst_y, uint32_t dst_width, uint32_t dst_height) {\r
+       set_pixel_func pset = set_pixel_func_ptr(dst_pixmap->format);\r
+       get_pixel_func pget = get_pixel_func_ptr(src_pixmap->format);\r
+       get_pixel_func dpget = get_pixel_func_ptr(dst_pixmap->format);\r
+       uint32_t sbpp = bytes_per_pixel(src_pixmap->format);\r
+       uint32_t dbpp = bytes_per_pixel(dst_pixmap->format);\r
+       uint32_t spitch = sbpp * src_pixmap->width;\r
+       uint32_t dpitch = dbpp * dst_pixmap->width;\r
+\r
+       uint32_t x_ratio = (src_width << 16) / dst_width + 1;\r
+       uint32_t y_ratio = (src_height << 16) / dst_height + 1;\r
+\r
+       int dx = dst_x;\r
+       int dy = dst_y;\r
+       int sx = src_x;\r
+       int sy = src_y;\r
+       int i = 0;\r
+       int j = 0;\r
+\r
+       for(;i < dst_height; i++) {\r
+               sy = ((i * y_ratio) >> 16) + src_y;\r
+               dy = i + dst_y;\r
+               if(sy < 0 || dy < 0) continue;\r
+               if(sy >= src_pixmap->height || dy >= dst_pixmap->height) break;\r
+\r
+               for(j = 0 ;j < dst_width; j++) {\r
+                       sx = ((j * x_ratio) >> 16) + src_x;\r
+                       dx = j + dst_x;\r
+                       if(sx < 0 || dx < 0) continue;\r
+                       if(sx >= src_pixmap->width || dx >= dst_pixmap->width) break;\r
+\r
+                       const void* src_ptr = src_pixmap->pixels + sx * sbpp + sy * spitch;\r
+                       const void* dst_ptr = dst_pixmap->pixels + dx * dbpp + dy * dpitch;\r
+                       uint32_t src_col = to_RGBA8888(src_pixmap->format, pget((void*)src_ptr));\r
+\r
+                       if(gdx2d_blend) {\r
+                               uint32_t dst_col = to_RGBA8888(dst_pixmap->format, dpget((void*)dst_ptr));\r
+                               src_col = to_format(dst_pixmap->format, blend(src_col, dst_col));\r
+                       } else {\r
+                               src_col = to_format(dst_pixmap->format, src_col);\r
+                       }\r
+\r
+                       pset((void*)dst_ptr, src_col);\r
+               }\r
+       }\r
+}\r
+\r
 void blit(const gdx2d_pixmap* src_pixmap, const gdx2d_pixmap* dst_pixmap,\r
                                           int32_t src_x, int32_t src_y, uint32_t src_width, uint32_t src_height,\r
                                           int32_t dst_x, int32_t dst_y, uint32_t dst_width, uint32_t dst_height) {\r
+       if(gdx2d_scale == GDX2D_SCALE_NEAREST)\r
+               blit_linear(src_pixmap, dst_pixmap, src_x, src_y, src_width, src_height, dst_x, dst_y, dst_width, dst_height);\r
+       if(gdx2d_scale == GDX2D_SCALE_BILINEAR)\r
+               blit_bilinear(src_pixmap, dst_pixmap, src_x, src_y, src_width, src_height, dst_x, dst_y, dst_width, dst_height);\r
 }\r
 \r
 void gdx2d_draw_pixmap(const gdx2d_pixmap* src_pixmap, const gdx2d_pixmap* dst_pixmap,\r
index 058d07c..5b8bf4c 100644 (file)
@@ -33,6 +33,9 @@ public class Gdx2DPixmap {
        public static final int GDX2D_FORMAT_RGB565 = 5;\r
        public static final int GDX2D_FORMAT_RGBA4444 = 6;\r
        \r
+       public static final int GDX2D_SCALE_NEAREST = 0;\r
+       public static final int GDX2D_SCALE_LINEAR = 1;\r
+       \r
        final long basePtr;\r
        final int width;\r
        final int height;\r
@@ -111,7 +114,7 @@ public class Gdx2DPixmap {
                drawPixmap(src.basePtr, basePtr, srcX, srcY, width, height, dstX, dstY, width, height);\r
        }\r
        \r
-       private void drawPixmap(Gdx2DPixmap src, \r
+       public void drawPixmap(Gdx2DPixmap src, \r
                                                        int srcX, int srcY, int srcWidth, int srcHeight, \r
                                                        int dstX, int dstY, int dstWidth, int dstHeight) {\r
                drawPixmap(src.basePtr, basePtr, srcX, srcY, srcWidth, srcHeight, dstX, dstY, dstWidth, dstHeight);\r
index a5bc243..27dd353 100644 (file)
@@ -43,7 +43,7 @@ public class Gdx2DTest extends GdxTest {
                Gdx2DPixmap rgba4444 = createPixmap(32, 32, Gdx2DPixmap.GDX2D_FORMAT_RGBA4444);         \r
                Gdx2DPixmap rgb888 = createPixmap(32, 32, Gdx2DPixmap.GDX2D_FORMAT_RGB888);             \r
                Gdx2DPixmap rgba8888 = createPixmap(32, 32, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888);\r
-               Gdx2DPixmap composite = createPixmap(256, 32, Gdx2DPixmap.GDX2D_FORMAT_RGB565);\r
+               Gdx2DPixmap composite = createPixmap(256, 64, Gdx2DPixmap.GDX2D_FORMAT_RGB565);\r
                \r
                Gdx2DPixmap.setBlend(1);\r
                \r
@@ -108,6 +108,7 @@ public class Gdx2DTest extends GdxTest {
                rgba8888.fillCircle(16, 16, 6, Color.rgba8888(1, 0, 1, 0.5f));\r
                \r
                Gdx2DPixmap.setBlend(0);\r
+               Gdx2DPixmap.setScale(Gdx2DPixmap.GDX2D_SCALE_LINEAR);\r
                composite.clear(Color.rgba8888(1, 1, 1, 1));\r
                composite.drawPixmap(alpha, 0, 0, 0, 0, 32, 32);\r
                composite.drawPixmap(luminanceAlpha, 0, 0, 32, 0, 32, 32);\r
@@ -115,6 +116,11 @@ public class Gdx2DTest extends GdxTest {
                composite.drawPixmap(rgba4444, 0, 0, 96, 0, 32, 32);\r
                composite.drawPixmap(rgb888, 0, 0, 128, 0, 32, 32);\r
                composite.drawPixmap(rgba8888, 0, 0, 160, 0, 32, 32);\r
+               composite.drawPixmap(rgb888, 0, 0, 32, 32, 192, 0, 64, 64);\r
+               composite.drawPixmap(alpha, 0, 0, 32, 32, 192, 0, 16, 16);\r
+               composite.drawPixmap(luminanceAlpha, 0, 0, 32, 32, 256 - 16, 0, 16, 16);\r
+               composite.drawPixmap(rgb565, 0, 0, 32, 32, 192, 64 - 16, 16, 16);\r
+               composite.drawPixmap(rgba4444, 0, 0, 32, 32, 256 - 16, 64 - 16, 16, 16);\r
                        \r
 //             Format[] formats = { Format.Alpha, Format.RGB565, Format.RGBA4444, Format.RGBA8888 };\r
 //             int[] gdxFormats = { Gdx2DPixmap.GDX2D_FORMAT_ALPHA, Gdx2DPixmap.GDX2D_FORMAT_RGB565, Gdx2DPixmap.GDX2D_FORMAT_RGBA4444, Gdx2DPixmap.GDX2D_FORMAT_RGBA8888 };\r