Python: Add GBA sprite accessors
Jeffrey Pfau jeffrey@endrift.com
Tue, 18 Oct 2016 15:31:21 -0700
2 files changed,
59 insertions(+),
3 deletions(-)
M
src/platform/python/mgba/gba.py
→
src/platform/python/mgba/gba.py
@@ -4,13 +4,16 @@ # 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 core, arm +from .arm import ARMCore +from .core import Core +from .tile import Sprite -class GBA(core.Core): +class GBA(Core): def __init__(self, native): super(GBA, self).__init__(native) self._native = ffi.cast("struct GBA*", native.board) - self.cpu = arm.ARMCore(self._core.cpu) + self.sprites = GBAObjs(self) + self.cpu = ARMCore(self._core.cpu) def _initTileCache(self, cache): lib.GBAVideoTileCacheInit(cache)@@ -19,3 +22,40 @@
def _deinitTileCache(self, cache): self._native.video.renderer.cache = ffi.NULL lib.mTileCacheDeinit(cache) + +class GBASprite(Sprite): + TILE_BASE = 0x800 + PALETTE_BASE = 0x10 + + def __init__(self, obj): + self._a = obj.a + self._b = obj.b + self._c = obj.c + self.x = self._b & 0x1FF + self.y = self._a & 0xFF + self._shape = self._a >> 14 + self._size = self._b >> 14 + self._256Color = bool(self._b & 0x2000) + self.width, self.height = lib.GBAVideoObjSizes[self._shape * 4 + self._size] + self.tile = self._c & 0x3FF + if self._256Color: + self.paletteId = 0 + else: + self.paletteId = self._c >> 12 + +class GBAObjs: + def __init__(self, core): + self._core = core + self._obj = core._native.video.oam.obj + + def __len__(self): + return 128 + + def __getitem__(self, index): + if index >= len(self): + raise IndexError() + sprite = GBASprite(self._obj[index]) + map1D = bool(self._core._native.memory.io[0] & 0x40) + # TODO: 256 colors + sprite.constitute(self._core.tiles, 0 if map1D else 0x20) + return sprite
M
src/platform/python/mgba/tile.py
→
src/platform/python/mgba/tile.py
@@ -29,3 +29,19 @@ lib.mTileCacheSetPalette(self.cache, 0)
def getTile(self, tile, palette): return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) + +class Sprite(object): + TILE_BASE = 0 + PALETTE_BASE = 0 + + def constitute(self, tileView, tilePitch): + i = image.Image(self.width, self.height) + tileId = self.tile + self.TILE_BASE + for y in range(self.height // 8): + for x in range(self.width // 8): + tile = tileView.getTile(tileId, self.paletteId + self.PALETTE_BASE) + tile.composite(i, x * 8, y * 8) + tileId += 1 + if tilePitch: + tileId += tilePitch - self.width // 8 + self.image = i