Python: Add map view
Vicki Pfau vi@endrift.com
Sun, 24 Sep 2017 22:30:53 -0700
6 files changed,
72 insertions(+),
4 deletions(-)
M
include/mgba/core/map-cache.h
→
include/mgba/core/map-cache.h
@@ -71,6 +71,7 @@
bool mMapCacheCheckTile(struct mMapCache* cache, const struct mMapCacheEntry* entry, unsigned x, unsigned y); void mMapCacheCleanTile(struct mMapCache* cache, struct mMapCacheEntry* entry, unsigned x, unsigned y); +void mMapCacheCleanRow(struct mMapCache* cache, unsigned y); const color_t* mMapCacheGetRow(struct mMapCache* cache, unsigned y); CXX_GUARD_END
M
src/core/map-cache.c
→
src/core/map-cache.c
@@ -171,6 +171,34 @@ }
return false; } +void mMapCacheCleanRow(struct mMapCache* cache, unsigned y) { + // TODO: Cache + int tilesWide = 1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig); + int macroTile = (1 << mMapCacheSystemInfoGetMacroTileSize(cache->sysConfig)) - 1; + size_t stride = 8 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig); + int location = 0; + int x; + for (x = 0; x < tilesWide; ++x) { + if (!(x & macroTile)) { + location = mMapCacheTileId(cache, x, y); + } else { + ++location; + } + struct mMapCacheEntry* status = &cache->status[location]; + if (!mMapCacheEntryFlagsIsVramClean(status->flags)) { + status->flags = mMapCacheEntryFlagsFillVramClean(status->flags); + cache->mapParser(cache, status, &cache->vram[cache->mapStart + (location << mMapCacheSystemInfoGetMapAlign(cache->sysConfig))]); + } + unsigned tileId = status->tileId + cache->tileStart; + if (tileId >= mTileCacheSystemInfoGetMaxTiles(cache->tileCache->sysConfig)) { + tileId = 0; + } + const color_t* tile = mTileCacheGetTile(cache->tileCache, tileId, mMapCacheEntryFlagsGetPaletteId(status->flags)); + color_t* mapOut = &cache->cache[(y * stride + x) * 8]; + _cleanTile(cache, tile, mapOut, status); + } +} + const color_t* mMapCacheGetRow(struct mMapCache* cache, unsigned y) { size_t stride = 8 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig); return &cache->cache[y * stride];
M
src/platform/python/_builder.h
→
src/platform/python/_builder.h
@@ -8,8 +8,14 @@ #define _TIME_H_
#define ATTRIBUTE_FORMAT(X, Y, Z) #define DECL_BITFIELD(newtype, oldtype) typedef oldtype newtype -#define DECL_BIT(type, name, bit) -#define DECL_BITS(type, name, bit, nbits) +#define DECL_BIT(type, field, bit) DECL_BITS(type, field, bit, 1) +#define DECL_BITS(TYPE, FIELD, START, SIZE) \ + TYPE TYPE ## Is ## FIELD (TYPE); \ + TYPE TYPE ## Get ## FIELD (TYPE); \ + TYPE TYPE ## Clear ## FIELD (TYPE); \ + TYPE TYPE ## Fill ## FIELD (TYPE); \ + TYPE TYPE ## Set ## FIELD (TYPE, TYPE); \ + TYPE TYPE ## TestFill ## FIELD (TYPE, bool); #define CXX_GUARD_START #define CXX_GUARD_END@@ -29,9 +35,10 @@ #include <limits.h>
#include "flags.h" +#include <mgba/core/cache-set.h> #include <mgba/core/core.h> +#include <mgba/core/map-cache.h> #include <mgba/core/mem-search.h> -#include <mgba/core/cache-set.h> #include <mgba/core/thread.h> #include <mgba/core/version.h>
M
src/platform/python/_builder.py
→
src/platform/python/_builder.py
@@ -17,14 +17,17 @@ cppflags.extend(sys.argv[1:])
cppflags.extend(["-I" + incdir, "-I" + srcdir, "-I" + bindir]) ffi.set_source("mgba._pylib", """ +#define static +#define inline #include "flags.h" #define OPAQUE_THREADING +#include <mgba/core/cache-set.h> #include <mgba-util/common.h> #include <mgba/core/core.h> +#include <mgba/core/map-cache.h> #include <mgba/core/log.h> #include <mgba/core/mem-search.h> #include <mgba/core/thread.h> -#include <mgba/core/cache-set.h> #include <mgba/core/version.h> #include <mgba/debugger/debugger.h> #include <mgba/internal/arm/arm.h>
M
src/platform/python/mgba/core.py
→
src/platform/python/mgba/core.py
@@ -117,6 +117,14 @@ for i in range(lib.mTileCacheSetSize(ts)):
t.append(tile.TileView(lib.mTileCacheSetGetPointer(ts, i))) return t + @cached_property + def maps(self): + m = [] + ms = ffi.addressof(self.graphicsCache.cache.maps) + for i in range(lib.mMapCacheSetSize(ms)): + m.append(tile.MapView(lib.mMapCacheSetGetPointer(ms, i))) + return m + @classmethod def _init(cls, native): core = ffi.gc(native, native.deinit)
M
src/platform/python/mgba/tile.py
→
src/platform/python/mgba/tile.py
@@ -32,6 +32,27 @@
def getTile(self, tile, palette): return Tile(lib.mTileCacheGetTile(self.cache, tile, palette)) +class MapView: + def __init__(self, cache): + self.cache = cache + + @property + def width(self): + return 1 << lib.mMapCacheSystemInfoGetTilesWide(self.cache.sysConfig) + + @property + def height(self): + return 1 << lib.mMapCacheSystemInfoGetTilesHigh(self.cache.sysConfig) + + @property + def image(self): + i = image.Image(self.width * 8, self.height * 8, alpha=True) + for y in range(self.height * 8): + if not y & 7: + lib.mMapCacheCleanRow(self.cache, y >> 3) + row = lib.mMapCacheGetRow(self.cache, y) + ffi.memmove(ffi.addressof(i.buffer, i.stride * y), row, self.width * 8 * ffi.sizeof("color_t")) + return i class Sprite(object): def constitute(self, tileView, tilePitch):