gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
gbdk-support/png2asset/metasprites.cpp
1 #include <vector>
2 #include <string>
3 #include <algorithm>
4 #include <cstring>
5 #include <set>
6 #include <fstream>
7 #include <cstdint>
8
9 #include "mttile.h"
10
11 #include "cmp_int_color.h"
12 #include "metasprites.h"
13 #include "tiles.h"
14
15 #include "png2asset.h"
16 #include "image_utils.h"
17 #include "process_arguments.h"
18
19 using namespace std;
20
21 void GetMetaSprite(int _x, int _y, int _w, int _h, int pivot_x, int pivot_y, PNG2AssetData* assetData)
22 {
23 int last_x = _x + pivot_x;
24 int last_y = _y + pivot_y;
25
26 assetData->sprites.push_back(MetaSprite());
27 MetaSprite& mt_sprite = assetData->sprites.back();
28 for(int y = _y; y < _y + _h && y < (int)assetData->image.h; y += assetData->image.tile_h)
29 {
30 for(int x = _x; x < _x + _w && x < (int)assetData->image.w; x += assetData->image.tile_w)
31 {
32 Tile tile(assetData->image.tile_h * assetData->image.tile_w);
33 // For sprites, unlike maps, Tiles are only kept and processed if NOT Empty
34 // This default behavior can be overridden with -spr_no_optimize (which sets keep_empty_sprite_tiles and keep_duplicate_tiles to TRUE)
35 bool tile_not_empty = (assetData->image.ExtractTile(x, y, tile, assetData->args->sprite_mode, assetData->args->export_as_map, assetData->args->use_map_attributes));
36 if (tile_not_empty || (assetData->args->keep_empty_sprite_tiles))
37 {
38 size_t idx;
39 unsigned char props;
40 unsigned char pal_idx = tile.pal;
41
42 // When both -keep_duplicate_tiles and source tilesets are used then
43 // keep_duplicate_tiles should only apply to source tilesets, not the main image
44 if ((assetData->args->keep_duplicate_tiles) && (assetData->args->has_source_tilesets == false)) {
45 assetData->tiles.push_back(tile);
46 idx = assetData->tiles.size() - 1;
47 props = assetData->args->props_default;
48 }
49 else
50 {
51 if(!FindTile(tile, idx, props, assetData->tiles, assetData))
52 {
53 if (assetData->args->has_source_tilesets) {
54 printf("found a tile not in the source tileset at %d,%d\n", x, y);
55 assetData->args->includeTileData = true;
56 }
57 assetData->tiles.push_back(tile);
58 idx = assetData->tiles.size() - 1;
59 props = assetData->args->props_default;
60 }
61 }
62
63 props |= pal_idx;
64
65 // Scale up index based on 8x8 tiles-per-hardware sprite
66 if(assetData->args->sprite_mode == SPR_8x16)
67 idx *= 2;
68 else if(assetData->args->sprite_mode == SPR_16x16_MSX)
69 idx *= 4;
70
71 mt_sprite.push_back(MTTile(x - last_x, y - last_y, (unsigned char)idx, props));
72 last_x = x;
73 last_y = y;
74 }
75 }
76 }
77 }
78
79
80 void GetAllMetasprites(PNG2AssetData* assetData) {
81 //Extract metasprites
82 for(int y = 0; y < (int)assetData->image.h; y += (unsigned int)assetData->args->spriteSize.height)
83 {
84 for(int x = 0; x < (int)assetData->image.w; x += (unsigned int)assetData->args->spriteSize.width)
85 {
86 GetMetaSprite(x, y, (unsigned int)assetData->args->spriteSize.width, (unsigned int)assetData->args->spriteSize.height, assetData->args->pivot.x, assetData->args->pivot.y, assetData);
87 }
88 }
89 }
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.