all repos — mgba @ a9ccb0fdd723cfa45a226925e2a059c05057389c

mGBA Game Boy Advance Emulator

src/platform/python/mgba/tile.py (view raw)

 1# Copyright (c) 2013-2016 Jeffrey Pfau
 2#
 3# This Source Code Form is subject to the terms of the Mozilla Public
 4# License, v. 2.0. If a copy of the MPL was not distributed with this
 5# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6from ._pylib import ffi, lib
 7from . import image
 8
 9class Tile:
10    def __init__(self, data):
11        self.buffer = data
12
13    def toImage(self):
14        i = image.Image(8, 8)
15        self.composite(i, 0, 0)
16        return i
17
18    def composite(self, i, x, y):
19        for iy in range(8):
20            for ix in range(8):
21                i.buffer[ix + x + (iy + y) * i.stride] = image.u16ToColor(self.buffer[ix + iy * 8])
22
23class TileView:
24    def __init__(self, core):
25        self.core = core
26        self.cache = ffi.gc(ffi.new("struct mTileCache*"), core._deinitTileCache)
27        core._initTileCache(self.cache)
28        lib.mTileCacheSetPalette(self.cache, 0)
29
30    def getTile(self, tile, palette):
31        return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))