all repos — mgba @ ba2a31c3f2ff5ef6fcc44570dfda7b4911704b25

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            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
35
36class Sprite(object):
37    def constitute(self, tileView, tilePitch):
38        i = image.Image(self.width, self.height, alpha=True)
39        tileId = self.tile
40        for y in range(self.height // 8):
41            for x in range(self.width // 8):
42                tile = tileView.getTile(tileId, self.paletteId)
43                tile.composite(i, x * 8, y * 8)
44                tileId += 1
45            if tilePitch:
46                tileId += tilePitch - self.width // 8
47        self.image = i