Python: Add support for 256-color sprites
Jeffrey Pfau jeffrey@endrift.com
Tue, 18 Oct 2016 18:36:13 -0700
2 files changed,
20 insertions(+),
10 deletions(-)
M
src/platform/python/mgba/gba.py
→
src/platform/python/mgba/gba.py
@@ -24,8 +24,8 @@ self._native.video.renderer.cache = ffi.NULL
lib.mTileCacheDeinit(cache) class GBASprite(Sprite): - TILE_BASE = 0x800 - PALETTE_BASE = 0x10 + TILE_BASE = 0x800, 0x400 + PALETTE_BASE = 0x10, 1 def __init__(self, obj): self._a = obj.a@@ -35,11 +35,12 @@ 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._256Color = bool(self._a & 0x2000) self.width, self.height = lib.GBAVideoObjSizes[self._shape * 4 + self._size] self.tile = self._c & 0x3FF if self._256Color: self.paletteId = 0 + self.tile >>= 1 else: self.paletteId = self._c >> 12@@ -56,6 +57,5 @@ 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) + sprite.constitute(self._core.tiles, 0 if map1D else 0x20, int(sprite._256Color)) return sprite
M
src/platform/python/mgba/tile.py
→
src/platform/python/mgba/tile.py
@@ -26,22 +26,32 @@ self.core = core
self.cache = ffi.gc(ffi.new("struct mTileCache*"), core._deinitTileCache) core._initTileCache(self.cache) lib.mTileCacheSetPalette(self.cache, 0) + self.paletteSet = 0 def getTile(self, tile, palette): return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) + def setPalette(self, paletteSet): + if paletteSet > 1 or paletteSet < 0: + raise IndexError("Palette Set ID out of bounds") + lib.mTileCacheSetPalette(self.cache, paletteSet) + self.paletteSet = paletteSet + class Sprite(object): - TILE_BASE = 0 - PALETTE_BASE = 0 + TILE_BASE = 0, 0 + PALETTE_BASE = 0, 0 - def constitute(self, tileView, tilePitch): + def constitute(self, tileView, tilePitch, paletteSet): + oldPaletteSet = tileView.paletteSet + tileView.setPalette(paletteSet) i = image.Image(self.width, self.height) - tileId = self.tile + self.TILE_BASE + tileId = self.tile + self.TILE_BASE[paletteSet] for y in range(self.height // 8): for x in range(self.width // 8): - tile = tileView.getTile(tileId, self.paletteId + self.PALETTE_BASE) + tile = tileView.getTile(tileId, self.paletteId + self.PALETTE_BASE[paletteSet]) tile.composite(i, x * 8, y * 8) tileId += 1 if tilePitch: tileId += tilePitch - self.width // 8 self.image = i + tileView.setPalette(oldPaletteSet)