From 5e0641cb0e9b6b71c19954da443a8f7b5813c507 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Mon, 17 Oct 2016 22:37:54 -0700 Subject: [PATCH] Python: Add some tile manipulation --- src/platform/python/_builder.h | 3 +++ src/platform/python/_builder.py | 5 +++- src/platform/python/mgba/image.py | 43 +++++++++++++++++++++++++++++++ src/platform/python/mgba/tile.py | 36 ++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 src/platform/python/mgba/tile.py diff --git a/src/platform/python/_builder.h b/src/platform/python/_builder.h index 7ffdd422e..97539a639 100644 --- a/src/platform/python/_builder.h +++ b/src/platform/python/_builder.h @@ -22,6 +22,7 @@ void free(void*); #include #include "core/core.h" +#include "core/tile-cache.h" #include "platform/python/vfs-py.h" #include "platform/python/log.h" @@ -31,8 +32,10 @@ void free(void*); #ifdef M_CORE_GBA #include "arm/arm.h" #include "gba/gba.h" +#include "gba/renderers/tile-cache.h" #endif #ifdef M_CORE_GB #include "lr35902/lr35902.h" #include "gb/gb.h" +#include "gb/renderers/tile-cache.h" #endif diff --git a/src/platform/python/_builder.py b/src/platform/python/_builder.py index 97730cce7..666aefe33 100644 --- a/src/platform/python/_builder.py +++ b/src/platform/python/_builder.py @@ -10,10 +10,13 @@ ffi.set_source("mgba._pylib", """ #include "util/common.h" #include "core/core.h" #include "core/log.h" +#include "core/tile-cache.h" #include "arm/arm.h" #include "gba/gba.h" +#include "gba/renderers/tile-cache.h" #include "lr35902/lr35902.h" #include "gb/gb.h" +#include "gb/renderers/tile-cache.h" #include "util/png-io.h" #include "util/vfs.h" @@ -45,4 +48,4 @@ with open(os.path.join(os.getcwd(), "_builder.h")) as core: lines.append(line) ffi.cdef('\n'.join(lines)) -ffi.compile() \ No newline at end of file +ffi.compile() diff --git a/src/platform/python/mgba/image.py b/src/platform/python/mgba/image.py index 907077433..f76945b32 100644 --- a/src/platform/python/mgba/image.py +++ b/src/platform/python/mgba/image.py @@ -24,3 +24,46 @@ class Image: success = success and p.writePixels(self) p.writeClose() return success + +def u16ToU32(c): + r = c & 0x1F + g = (c >> 5) & 0x1F + b = (c >> 10) & 0x1F + a = (c >> 15) & 1 + abgr = r << 3 + abgr |= g << 11 + abgr |= b << 19 + abgr |= (a * 0xFF) << 24 + return abgr + +def u32ToU16(c): + r = (c >> 3) & 0x1F + g = (c >> 11) & 0x1F + b = (c >> 19) & 0x1F + a = c >> 31 + abgr = r + abgr |= g << 5 + abgr |= b << 10 + abgr |= a << 15 + return abgr + +if ffi.sizeof("color_t") == 2: + def colorToU16(c): + return c + + colorToU32 = u16ToU32 + + def u16ToColor(c): + return c + + u32ToColor = u32ToU16 +else: + def colorToU32(c): + return c + + colorToU16 = u32ToU16 + + def u32ToColor(c): + return c + + u16ToColor = u16ToU32 diff --git a/src/platform/python/mgba/tile.py b/src/platform/python/mgba/tile.py new file mode 100644 index 000000000..06a64366d --- /dev/null +++ b/src/platform/python/mgba/tile.py @@ -0,0 +1,36 @@ +# Copyright (c) 2013-2016 Jeffrey Pfau +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +from ._pylib import ffi, lib +from . import image + +class Tile: + def __init__(self, data): + self.buffer = data + + def toImage(self): + i = image.Image(8, 8) + self.composite(i, 0, 0) + return i + + def composite(self, i, x, y): + for iy in range(8): + for ix in range(8): + i.buffer[ix + x + (iy + y) * i.stride] = image.u16ToColor(self.buffer[ix + iy * 8]) + +class TileView: + def __init__(self, core): + self.core = core + self.cache = ffi.gc(ffi.new("struct mTileCache*"), lib.mTileCacheDeinit) + if core.platform() == core.PLATFORM_GBA: + lib.GBAVideoTileCacheInit(self.cache) + lib.GBAVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video")) + if core.platform() == core.PLATFORM_GB: + lib.GBVideoTileCacheInit(self.cache) + lib.GBVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video")) + lib.mTileCacheSetPalette(self.cache, 0) + + def getTile(self, tile, palette): + return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))