src/platform/python/mgba/gb.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 .lr35902 import LR35902Core
8from .core import Core
9from .tile import Sprite
10
11class GB(Core):
12 def __init__(self, native):
13 super(GB, self).__init__(native)
14 self._native = ffi.cast("struct GB*", native.board)
15 self.sprites = GBObjs(self)
16 self.cpu = LR35902Core(self._core.cpu)
17
18 def _initTileCache(self, cache):
19 lib.GBVideoTileCacheInit(cache)
20 lib.GBVideoTileCacheAssociate(cache, ffi.addressof(self._native.video))
21
22 def _deinitTileCache(self, cache):
23 self._native.video.renderer.cache = ffi.NULL
24 lib.mTileCacheDeinit(cache)
25
26class GBSprite(Sprite):
27 PALETTE_BASE = 8,
28
29 def __init__(self, obj, core):
30 self.x = obj.x
31 self.y = obj.y
32 self.tile = obj.tile
33 self._attr = obj.attr
34 self.width = 8
35 lcdc = core._native.memory.io[0x40]
36 self.height = 16 if lcdc & 4 else 8
37 if core._native.model >= lib.GB_MODEL_CGB:
38 if self._attr & 8:
39 self.tile += 512
40 self.paletteId = self._attr & 7
41 else:
42 self.paletteId = (self._attr >> 4) & 1
43
44
45class GBObjs:
46 def __init__(self, core):
47 self._core = core
48 self._obj = core._native.video.oam.obj
49
50 def __len__(self):
51 return 40
52
53 def __getitem__(self, index):
54 if index >= len(self):
55 raise IndexError()
56 sprite = GBSprite(self._obj[index], self._core)
57 sprite.constitute(self._core.tiles, 0, 0)
58 return sprite