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 # pylint: disable=no-name-in-module
7from . import image
8
9
10class Tile:
11 def __init__(self, data):
12 self.buffer = data
13
14 def to_image(self):
15 i = image.Image(8, 8)
16 self.composite(i, 0, 0)
17 return i
18
19 def composite(self, i, x, y):
20 for iy in range(8):
21 ffi.memmove(ffi.addressof(i.buffer, x + (iy + y) * i.stride), ffi.addressof(self.buffer, iy * 8), 8 * ffi.sizeof("color_t"))
22
23
24class CacheSet:
25 def __init__(self, core):
26 self.core = core
27 self.cache = ffi.gc(ffi.new("struct mCacheSet*"), core._deinit_cache)
28 core._init_cache(self.cache)
29
30
31class TileView:
32 def __init__(self, cache):
33 self.cache = cache
34
35 def get_tile(self, tile, palette):
36 return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))
37
38
39class MapView:
40 def __init__(self, cache):
41 self.cache = cache
42
43 @property
44 def width(self):
45 return 1 << lib.mMapCacheSystemInfoGetTilesWide(self.cache.sysConfig)
46
47 @property
48 def height(self):
49 return 1 << lib.mMapCacheSystemInfoGetTilesHigh(self.cache.sysConfig)
50
51 @property
52 def image(self):
53 i = image.Image(self.width * 8, self.height * 8, alpha=True)
54 for y in range(self.height * 8):
55 if not y & 7:
56 lib.mMapCacheCleanRow(self.cache, y >> 3)
57 row = lib.mMapCacheGetRow(self.cache, y)
58 ffi.memmove(ffi.addressof(i.buffer, i.stride * y), row, self.width * 8 * ffi.sizeof("color_t"))
59 return i
60
61
62class Sprite(object):
63 def constitute(self, tileView, tilePitch):
64 i = image.Image(self.width, self.height, alpha=True)
65 tileId = self.tile
66 for y in range(self.height // 8):
67 for x in range(self.width // 8):
68 tile = tileView.getTile(tileId, self.paletteId)
69 tile.composite(i, x * 8, y * 8)
70 tileId += 1
71 if tilePitch:
72 tileId += tilePitch - self.width // 8
73 self.image = i