Python: Reindent, fix up TileView interface

This commit is contained in:
Jeffrey Pfau 2016-10-18 14:32:17 -07:00
parent 5e0641cb0e
commit 303a7685a2
5 changed files with 42 additions and 25 deletions

View File

@ -4,6 +4,8 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this # 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/. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
from ._pylib import ffi, lib from ._pylib import ffi, lib
from . import tile
from cached_property import cached_property
def find(path): def find(path):
core = lib.mCoreFind(path.encode('UTF-8')) core = lib.mCoreFind(path.encode('UTF-8'))
@ -43,6 +45,10 @@ class Core:
self.cpu = LR35902Core(self._core.cpu) self.cpu = LR35902Core(self._core.cpu)
self.board = GB(self._core.board) self.board = GB(self._core.board)
@cached_property
def tiles(self):
return tile.TileView(self)
def _deinit(self): def _deinit(self):
self._core.deinit(self._core) self._core.deinit(self._core)
@ -112,4 +118,4 @@ if hasattr(lib, 'PLATFORM_GBA'):
if hasattr(lib, 'PLATFORM_GB'): if hasattr(lib, 'PLATFORM_GB'):
from .gb import GB from .gb import GB
from .lr35902 import LR35902Core from .lr35902 import LR35902Core
Core.PLATFORM_GB = lib.PLATFORM_GB Core.PLATFORM_GB = lib.PLATFORM_GB

View File

@ -8,3 +8,11 @@ from ._pylib import ffi, lib
class GB: class GB:
def __init__(self, native): def __init__(self, native):
self._native = ffi.cast("struct GB*", 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)

View File

@ -8,3 +8,11 @@ from ._pylib import ffi, lib
class GBA: class GBA:
def __init__(self, native): def __init__(self, native):
self._native = ffi.cast("struct GBA*", 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)

View File

@ -7,30 +7,25 @@ from ._pylib import ffi, lib
from . import image from . import image
class Tile: class Tile:
def __init__(self, data): def __init__(self, data):
self.buffer = data self.buffer = data
def toImage(self): def toImage(self):
i = image.Image(8, 8) i = image.Image(8, 8)
self.composite(i, 0, 0) self.composite(i, 0, 0)
return i return i
def composite(self, i, x, y): def composite(self, i, x, y):
for iy in range(8): for iy in range(8):
for ix in range(8): for ix in range(8):
i.buffer[ix + x + (iy + y) * i.stride] = image.u16ToColor(self.buffer[ix + iy * 8]) i.buffer[ix + x + (iy + y) * i.stride] = image.u16ToColor(self.buffer[ix + iy * 8])
class TileView: class TileView:
def __init__(self, core): def __init__(self, core):
self.core = core self.core = core
self.cache = ffi.gc(ffi.new("struct mTileCache*"), lib.mTileCacheDeinit) self.cache = ffi.gc(ffi.new("struct mTileCache*"), core.board._deinitTileCache)
if core.platform() == core.PLATFORM_GBA: core.board._initTileCache(self.cache)
lib.GBAVideoTileCacheInit(self.cache) lib.mTileCacheSetPalette(self.cache, 0)
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): def getTile(self, tile, palette):
return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))

View File

@ -14,7 +14,7 @@ setup(name="${BINARY_NAME}",
url="http://github.com/mgba-emu/mgba/", url="http://github.com/mgba-emu/mgba/",
packages=["mgba"], packages=["mgba"],
setup_requires=['cffi>=1.6'], setup_requires=['cffi>=1.6'],
install_requires=['cffi>=1.6'], install_requires=['cffi>=1.6', 'cached-property'],
license="MPL 2.0", license="MPL 2.0",
classifiers=classifiers classifiers=classifiers
) )