Skip to content

Commit

Permalink
Append tileset constants to numeric tileset names
Browse files Browse the repository at this point in the history
  • Loading branch information
roukaour committed Oct 2, 2017
1 parent 47d3408 commit 5567f7a
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ A map and tileset editor for [Pokémon Polished Crystal](https://github.com/rouk

Inspired by [crowdmap](https://github.com/yenatch/crowdmap), but implemented with C++ and [FLTK](http://www.fltk.org/), and focused on graphics instead of script editing.

Latest release: [**1.6.0**](https://github.com/roukaour/polished-map/releases/tag/v1.6.0)
Latest release: [**1.7.0**](https://github.com/roukaour/polished-map/releases/tag/v1.7.0)
5 changes: 5 additions & 0 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void Config::blk_path_from_project_path(const char *project_path, char *blk_path
}

void Config::palette_map_path(char *dest, const char *root, const char *tileset) {
if (monochrome()) { return; }
sprintf(dest, "%stilesets" DIR_SEP "%s_palette_map.asm", root, tileset);
}

Expand Down Expand Up @@ -112,6 +113,10 @@ void Config::map_constants_path(char *dest, const char *root) {
sprintf(dest, "%sconstants" DIR_SEP "map_constants.asm", root);
}

void Config::tileset_constants_path(char *dest, const char *root) {
sprintf(dest, "%sconstants" DIR_SEP "tilemap_constants.asm", root);
}

bool Config::monochrome() {
return global_project == Project::POKERED;
}
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Config {
static void tileset_path(char *dest, const char *root, const char *tileset);
static void metatileset_path(char *dest, const char *root, const char *tileset);
static void map_constants_path(char *dest, const char *root);
static void tileset_constants_path(char *dest, const char *root);
static bool monochrome(void);
static bool skip_tiles_60_to_7f(void);
};
Expand Down
72 changes: 60 additions & 12 deletions src/option-dialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ Map_Options_Dialog::~Map_Options_Dialog() {
delete _lighting;
}

const char *Map_Options_Dialog::tileset(void) const {
if (!_tileset->mvalue()) { return NULL; }
const char *t = _tileset->mvalue()->label();
Dictionary::const_iterator it = _original_names.find(t);
if (it == _original_names.end()) { return t; }
return it->second.c_str();
}

inline static bool isupperordigit(int c) { return isupper(c) || isdigit(c); }
inline static int toconstant(int c) { return isalnum(c) ? toupper(c) : '_'; }

Expand Down Expand Up @@ -157,6 +165,53 @@ bool Map_Options_Dialog::guess_map_size(const char *filename, const char *direct
return false;
}

Dictionary Map_Options_Dialog::guess_tileset_names(const char *directory) {
Dictionary pretty_names;

char tileset_constants[FL_PATH_MAX] = {};
Config::tileset_constants_path(tileset_constants, directory);

std::ifstream ifs(tileset_constants);
if (!ifs.good()) { return pretty_names; }

int id = 1;
char original[16] = {};
while (ifs.good()) {
std::string line;
std::getline(ifs, line);
std::istringstream lss(line);
std::string token;
lss >> token;
if (token != "const") { continue; }
lss >> token;
if (starts_with(token, "TILESET_")) { token.erase(0, 8); }
sprintf(original, "%02d", id++);
token = original + (": " + token);
pretty_names[original] = token;
}

return pretty_names;
}

void Map_Options_Dialog::add_tileset(const char *t, int ext_len, const Dictionary &pretty_names) {
std::string v(t);
v.erase(v.size() - ext_len, ext_len);

if (v.length() == 2 && isdigit(v[0]) && isdigit(v[1])) {
Dictionary::const_iterator it = pretty_names.find(v);
if (it != pretty_names.end()) {
v = it->second;
_original_names[v] = it->first;
}
}

if (_tileset->find_index(v.c_str()) != -1) { return; }

_tileset->add(v.c_str());
int m = text_width(v.c_str(), 6);
_max_tileset_name_length = MAX(m, _max_tileset_name_length);
}

bool Map_Options_Dialog::limit_blk_options(const char *filename, const char *directory) {
initialize();

Expand All @@ -179,24 +234,15 @@ bool Map_Options_Dialog::limit_blk_options(const char *filename, const char *dir
_max_tileset_name_length = 0;
_tileset->clear();

Dictionary pretty_names = guess_tileset_names(directory);
for (int i = 0; i < n; i++) {
const char *name = list[i]->d_name;
if (ends_with(name, ".colored.png")) { continue; } // ignore utils/metatiles.py renders
else if (ends_with(name, ".2bpp.lz")) {
std::string v(name);
v.erase(v.size() - 8, 8);
_tileset->add(v.c_str());
int m = text_width(v.c_str(), 6);
_max_tileset_name_length = MAX(m, _max_tileset_name_length);
add_tileset(name, 8, pretty_names);
}
else if (ends_with(name, ".png")) {
std::string v(name);
v.erase(v.size() - 4, 4);
if (_tileset->find_index(v.c_str()) == -1) {
_tileset->add(v.c_str());
int m = text_width(v.c_str(), 6);
_max_tileset_name_length = MAX(m, _max_tileset_name_length);
}
add_tileset(name, 4, pretty_names);
}
}
_tileset->value(0);
Expand All @@ -221,6 +267,8 @@ void Map_Options_Dialog::initialize_content() {
_lighting->add("Night"); // NITE
_lighting->add("Indoor"); // INDOOR
_lighting->value((Tileset::Lighting)Config::get("map-lighting", (int)Tileset::Lighting::DAY));
// Initialize data
_original_names.clear();
}

int Map_Options_Dialog::refresh_content(int ww, int dy) {
Expand Down
10 changes: 9 additions & 1 deletion src/option-dialogs.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifndef OPTION_DIALOGS_H
#define OPTION_DIALOGS_H

#include <unordered_map>
#include <string>

#include "utils.h"
#include "widgets.h"
#include "tileset.h"
Expand Down Expand Up @@ -38,21 +41,26 @@ class Option_Dialog {
static void cancel_cb(Fl_Widget *, Option_Dialog *od);
};

typedef std::unordered_map<std::string, std::string> Dictionary;

class Map_Options_Dialog : public Option_Dialog {
private:
int _max_tileset_name_length;
OS_Spinner *_map_width, *_map_height;
Dropdown *_tileset, *_lighting;
Dictionary _original_names;
public:
Map_Options_Dialog(const char *t);
~Map_Options_Dialog();
bool limit_blk_options(const char *filename, const char *directory);
inline uint8_t map_width(void) const { return (uint8_t)_map_width->value(); }
inline uint8_t map_height(void) const { return (uint8_t)_map_height->value(); }
inline const char *tileset(void) const { return _tileset->mvalue() ? _tileset->mvalue()->label() : NULL; }
const char *tileset(void) const;
inline Tileset::Lighting lighting(void) const { return (Tileset::Lighting)_lighting->value(); }
private:
bool guess_map_size(const char *filename, const char *directory);
Dictionary guess_tileset_names(const char *directory);
void add_tileset(const char *t, int ext_len, const Dictionary &pretty_names);
protected:
void initialize_content(void);
int refresh_content(int ww, int dy);
Expand Down
4 changes: 0 additions & 4 deletions src/tileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ void Tileset::clear() {
_result = GFX_NULL;
}

Palette_Map::Result Tileset::read_palette_map(const char *f) {
return _palette_map.read_from(f);
}

Tileset::Result Tileset::read_graphics(const char *f, Lighting l) {
if (!_palette_map.size()) { return (_result = GFX_NO_PALETTE); } // no colors

Expand Down
2 changes: 1 addition & 1 deletion src/tileset.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Tileset {
inline size_t num_tiles(void) const { return _num_tiles; }
inline Result result(void) const { return _result; }
void clear(void);
Palette_Map::Result read_palette_map(const char *f);
inline Palette_Map::Result read_palette_map(const char *f) { return _palette_map.read_from(f); }
Result read_graphics(const char *f, Lighting l);
static const char *error_message(Result result);
};
Expand Down
6 changes: 3 additions & 3 deletions src/version.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef VERSION_H
#define VERSION_H

#define PROGRAM_VERSION 1,6,0
#define PROGRAM_VERSION 1,7,0
#ifdef _DEBUG
#define PROGRAM_VERSION_STRING "1.6.0 [DEBUG]"
#define PROGRAM_VERSION_STRING "1.7.0 [DEBUG]"
#else
#define PROGRAM_VERSION_STRING "1.6.0"
#define PROGRAM_VERSION_STRING "1.7.0"
#endif

#define PROGRAM_NAME "Polished Map"
Expand Down

0 comments on commit 5567f7a

Please sign in to comment.