all repos — mgba @ 5e0641cb0e9b6b71c19954da443a8f7b5813c507

mGBA Game Boy Advance Emulator

Python: Add some tile manipulation
Jeffrey Pfau jeffrey@endrift.com
Mon, 17 Oct 2016 22:37:54 -0700
commit

5e0641cb0e9b6b71c19954da443a8f7b5813c507

parent

35658689366d3b78ce819a73cb3e475b2fb4c061

M src/platform/python/_builder.hsrc/platform/python/_builder.h

@@ -22,6 +22,7 @@

#include <limits.h> #include "core/core.h" +#include "core/tile-cache.h" #include "platform/python/vfs-py.h" #include "platform/python/log.h"

@@ -31,8 +32,10 @@ #endif

#ifdef M_CORE_GBA #include "arm/arm.h" #include "gba/gba.h" +#include "gba/renderers/tile-cache.h" #endif #ifdef M_CORE_GB #include "lr35902/lr35902.h" #include "gb/gb.h" +#include "gb/renderers/tile-cache.h" #endif
M src/platform/python/_builder.pysrc/platform/python/_builder.py

@@ -10,10 +10,13 @@ ffi.set_source("mgba._pylib", """

#include "util/common.h" #include "core/core.h" #include "core/log.h" +#include "core/tile-cache.h" #include "arm/arm.h" #include "gba/gba.h" +#include "gba/renderers/tile-cache.h" #include "lr35902/lr35902.h" #include "gb/gb.h" +#include "gb/renderers/tile-cache.h" #include "util/png-io.h" #include "util/vfs.h"

@@ -45,4 +48,4 @@ continue

lines.append(line) ffi.cdef('\n'.join(lines)) -ffi.compile()+ffi.compile()
M src/platform/python/mgba/image.pysrc/platform/python/mgba/image.py

@@ -24,3 +24,46 @@ success = p.writeHeader(self)

success = success and p.writePixels(self) p.writeClose() return success + +def u16ToU32(c): + r = c & 0x1F + g = (c >> 5) & 0x1F + b = (c >> 10) & 0x1F + a = (c >> 15) & 1 + abgr = r << 3 + abgr |= g << 11 + abgr |= b << 19 + abgr |= (a * 0xFF) << 24 + return abgr + +def u32ToU16(c): + r = (c >> 3) & 0x1F + g = (c >> 11) & 0x1F + b = (c >> 19) & 0x1F + a = c >> 31 + abgr = r + abgr |= g << 5 + abgr |= b << 10 + abgr |= a << 15 + return abgr + +if ffi.sizeof("color_t") == 2: + def colorToU16(c): + return c + + colorToU32 = u16ToU32 + + def u16ToColor(c): + return c + + u32ToColor = u32ToU16 +else: + def colorToU32(c): + return c + + colorToU16 = u32ToU16 + + def u32ToColor(c): + return c + + u16ToColor = u16ToU32
A src/platform/python/mgba/tile.py

@@ -0,0 +1,36 @@

+# Copyright (c) 2013-2016 Jeffrey Pfau +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +from ._pylib import ffi, lib +from . import image + +class Tile: + def __init__(self, data): + self.buffer = data + + def toImage(self): + i = image.Image(8, 8) + self.composite(i, 0, 0) + return i + + def composite(self, i, x, y): + for iy in range(8): + for ix in range(8): + i.buffer[ix + x + (iy + y) * i.stride] = image.u16ToColor(self.buffer[ix + iy * 8]) + +class TileView: + def __init__(self, core): + self.core = core + self.cache = ffi.gc(ffi.new("struct mTileCache*"), lib.mTileCacheDeinit) + if core.platform() == core.PLATFORM_GBA: + lib.GBAVideoTileCacheInit(self.cache) + lib.GBAVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video")) + if core.platform() == core.PLATFORM_GB: + lib.GBVideoTileCacheInit(self.cache) + lib.GBVideoTileCacheAssociate(self.cache, ffi.addressof(self.core.board._native, "video")) + lib.mTileCacheSetPalette(self.cache, 0) + + def getTile(self, tile, palette): + return Tile(lib.mTileCacheGetTile(self.cache, tile, palette))