diff --git a/src/platform/python/mgba/core.py b/src/platform/python/mgba/core.py index 10b0ccd5d..8bf339846 100644 --- a/src/platform/python/mgba/core.py +++ b/src/platform/python/mgba/core.py @@ -4,6 +4,8 @@ # 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 tile +from cached_property import cached_property def find(path): core = lib.mCoreFind(path.encode('UTF-8')) @@ -43,6 +45,10 @@ class Core: self.cpu = LR35902Core(self._core.cpu) self.board = GB(self._core.board) + @cached_property + def tiles(self): + return tile.TileView(self) + def _deinit(self): self._core.deinit(self._core) @@ -112,4 +118,4 @@ if hasattr(lib, 'PLATFORM_GBA'): if hasattr(lib, 'PLATFORM_GB'): from .gb import GB from .lr35902 import LR35902Core - Core.PLATFORM_GB = lib.PLATFORM_GB \ No newline at end of file + Core.PLATFORM_GB = lib.PLATFORM_GB diff --git a/src/platform/python/mgba/gb.py b/src/platform/python/mgba/gb.py index 1d141e966..0b5f0f79d 100644 --- a/src/platform/python/mgba/gb.py +++ b/src/platform/python/mgba/gb.py @@ -8,3 +8,11 @@ from ._pylib import ffi, lib class GB: def __init__(self, native): self._native = ffi.cast("struct GB*", native) + + def _initTileCache(self, cache): + lib.GBVideoTileCacheInit(cache) + lib.GBVideoTileCacheAssociate(cache, ffi.addressof(self._native.video)) + + def _deinitTileCache(self, cache): + self._native.video.renderer.cache = ffi.NULL + lib.mTileCacheDeinit(cache) diff --git a/src/platform/python/mgba/gba.py b/src/platform/python/mgba/gba.py index a425c5b42..4a66222d9 100644 --- a/src/platform/python/mgba/gba.py +++ b/src/platform/python/mgba/gba.py @@ -8,3 +8,11 @@ from ._pylib import ffi, lib class GBA: def __init__(self, native): self._native = ffi.cast("struct GBA*", native) + + def _initTileCache(self, cache): + lib.GBAVideoTileCacheInit(cache) + lib.GBAVideoTileCacheAssociate(cache, ffi.addressof(self._native.video)) + + def _deinitTileCache(self, cache): + self._native.video.renderer.cache = ffi.NULL + lib.mTileCacheDeinit(cache) diff --git a/src/platform/python/mgba/tile.py b/src/platform/python/mgba/tile.py index 06a64366d..1728e8927 100644 --- a/src/platform/python/mgba/tile.py +++ b/src/platform/python/mgba/tile.py @@ -7,30 +7,25 @@ from ._pylib import ffi, lib from . import image class Tile: - def __init__(self, data): - self.buffer = data + def __init__(self, data): + self.buffer = data - def toImage(self): - i = image.Image(8, 8) - self.composite(i, 0, 0) - return i + 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]) + 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 __init__(self, core): + self.core = core + self.cache = ffi.gc(ffi.new("struct mTileCache*"), core.board._deinitTileCache) + core.board._initTileCache(self.cache) + lib.mTileCacheSetPalette(self.cache, 0) - def getTile(self, tile, palette): - return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) + def getTile(self, tile, palette): + return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) diff --git a/src/platform/python/setup.py.in b/src/platform/python/setup.py.in index 045e6de4e..1884778cf 100644 --- a/src/platform/python/setup.py.in +++ b/src/platform/python/setup.py.in @@ -14,7 +14,7 @@ setup(name="${BINARY_NAME}", url="http://github.com/mgba-emu/mgba/", packages=["mgba"], setup_requires=['cffi>=1.6'], - install_requires=['cffi>=1.6'], + install_requires=['cffi>=1.6', 'cached-property'], license="MPL 2.0", classifiers=classifiers - ) \ No newline at end of file + )