all repos — mgba @ 5e0641cb0e9b6b71c19954da443a8f7b5813c507

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*"), lib.mTileCacheDeinit)
27		if core.platform() == core.PLATFORM_GBA:
28			lib.GBAVideoTileCacheInit(self.cache)
29			lib.GBAVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video"))
30		if core.platform() == core.PLATFORM_GB:
31			lib.GBVideoTileCacheInit(self.cache)
32			lib.GBVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video"))
33		lib.mTileCacheSetPalette(self.cache, 0)
34
35	def getTile(self, tile, palette):
36		return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))