Python: Add some tile manipulation

This commit is contained in:
Jeffrey Pfau 2016-10-17 22:37:54 -07:00
parent 3565868936
commit 5e0641cb0e
4 changed files with 86 additions and 1 deletions

View File

@ -22,6 +22,7 @@ void free(void*);
#include <limits.h> #include <limits.h>
#include "core/core.h" #include "core/core.h"
#include "core/tile-cache.h"
#include "platform/python/vfs-py.h" #include "platform/python/vfs-py.h"
#include "platform/python/log.h" #include "platform/python/log.h"
@ -31,8 +32,10 @@ void free(void*);
#ifdef M_CORE_GBA #ifdef M_CORE_GBA
#include "arm/arm.h" #include "arm/arm.h"
#include "gba/gba.h" #include "gba/gba.h"
#include "gba/renderers/tile-cache.h"
#endif #endif
#ifdef M_CORE_GB #ifdef M_CORE_GB
#include "lr35902/lr35902.h" #include "lr35902/lr35902.h"
#include "gb/gb.h" #include "gb/gb.h"
#include "gb/renderers/tile-cache.h"
#endif #endif

View File

@ -10,10 +10,13 @@ ffi.set_source("mgba._pylib", """
#include "util/common.h" #include "util/common.h"
#include "core/core.h" #include "core/core.h"
#include "core/log.h" #include "core/log.h"
#include "core/tile-cache.h"
#include "arm/arm.h" #include "arm/arm.h"
#include "gba/gba.h" #include "gba/gba.h"
#include "gba/renderers/tile-cache.h"
#include "lr35902/lr35902.h" #include "lr35902/lr35902.h"
#include "gb/gb.h" #include "gb/gb.h"
#include "gb/renderers/tile-cache.h"
#include "util/png-io.h" #include "util/png-io.h"
#include "util/vfs.h" #include "util/vfs.h"
@ -45,4 +48,4 @@ with open(os.path.join(os.getcwd(), "_builder.h")) as core:
lines.append(line) lines.append(line)
ffi.cdef('\n'.join(lines)) ffi.cdef('\n'.join(lines))
ffi.compile() ffi.compile()

View File

@ -24,3 +24,46 @@ class Image:
success = success and p.writePixels(self) success = success and p.writePixels(self)
p.writeClose() p.writeClose()
return success 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

View File

@ -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))