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 ffi.memmove(ffi.addressof(i.buffer, x + (iy + y) * i.stride), ffi.addressof(self.buffer, iy * 8), 8 * ffi.sizeof("color_t"))
21
22class CacheSet:
23 def __init__(self, core):
24 self.core = core
25 self.cache = ffi.gc(ffi.new("struct mCacheSet*"), core._deinitCache)
26 core._initCache(self.cache)
27
28class TileView:
29 def __init__(self, cache):
30 self.cache = cache
31
32 def getTile(self, tile, palette):
33 return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))
34
35class MapView:
36 def __init__(self, cache):
37 self.cache = cache
38
39 @property
40 def width(self):
41 return 1 << lib.mMapCacheSystemInfoGetTilesWide(self.cache.sysConfig)
42
43 @property
44 def height(self):
45 return 1 << lib.mMapCacheSystemInfoGetTilesHigh(self.cache.sysConfig)
46
47 @property
48 def image(self):
49 i = image.Image(self.width * 8, self.height * 8, alpha=True)
50 for y in range(self.height * 8):
51 if not y & 7:
52 lib.mMapCacheCleanRow(self.cache, y >> 3)
53 row = lib.mMapCacheGetRow(self.cache, y)
54 ffi.memmove(ffi.addressof(i.buffer, i.stride * y), row, self.width * 8 * ffi.sizeof("color_t"))
55 return i
56
57class Sprite(object):
58 def constitute(self, tileView, tilePitch):
59 i = image.Image(self.width, self.height, alpha=True)
60 tileId = self.tile
61 for y in range(self.height // 8):
62 for x in range(self.width // 8):
63 tile = tileView.getTile(tileId, self.paletteId)
64 tile.composite(i, x * 8, y * 8)
65 tileId += 1
66 if tilePitch:
67 tileId += tilePitch - self.width // 8
68 self.image = i