Skip to content

Commit

Permalink
Generalize Prism support to include pokered support
Browse files Browse the repository at this point in the history
  • Loading branch information
roukaour committed Sep 26, 2017
1 parent 4bee183 commit ee37ca2
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 103 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.4.3**](https://github.com/roukaour/polished-map/releases/tag/v1.4.3)
Latest release: [**1.5.0**](https://github.com/roukaour/polished-map/releases/tag/v1.5.0)
82 changes: 70 additions & 12 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,28 @@
#include <libgen.h>
#endif

Fl_Preferences global_config(Fl_Preferences::USER, PROGRAM_AUTHOR, PROGRAM_NAME);
Fl_Preferences Config::global_config(Fl_Preferences::USER, PROGRAM_AUTHOR, PROGRAM_NAME);
Config::Project Config::global_project(Config::Project::POKECRYSTAL);

const char *gfx_tileset_dir() {
int Config::get(const char *key, int default) {
int value;
global_config.get(key, value, default);
return value;
}

void Config::set(const char *key, int value) {
global_config.set(key, value);
}

const char *Config::gfx_tileset_dir() {
return "gfx" DIR_SEP "tilesets" DIR_SEP;
}

bool project_path_from_blk_path(const char *blk_path, char *project_path, bool prism) {
const char *Config::map_macro() {
return global_project == POKERED ? "mapconst" : "mapgroup";
}

bool Config::project_path_from_blk_path(const char *blk_path, char *project_path) {
#ifdef _WIN32
if (_splitpath_s(blk_path, NULL, 0, project_path, FL_PATH_MAX, NULL, 0, NULL, 0)) { return false; }
#else
Expand All @@ -29,20 +44,41 @@ bool project_path_from_blk_path(const char *blk_path, char *project_path, bool p
free(blk);
strcat(project_path, DIR_SEP);
#endif
strcat(project_path, prism ? ".." DIR_SEP ".." DIR_SEP : ".." DIR_SEP); // go up from maps/
return true;
switch (global_project) {
default:
case POKECRYSTAL:
case POKERED:
case POLISHED:
strcat(project_path, ".." DIR_SEP); // go up from maps/
return true;
case PRISM:
strcat(project_path, ".." DIR_SEP ".." DIR_SEP); // go up from maps/blk/
return true;
}
}

void blk_path_from_project_path(const char *project_path, char *blk_path, bool prism) {
void Config::blk_path_from_project_path(const char *project_path, char *blk_path) {
strcpy(blk_path, project_path);
strcat(blk_path, prism ? "maps" DIR_SEP "blk" DIR_SEP : "maps" DIR_SEP);
switch (global_project) {
default:
case POKECRYSTAL:
case POLISHED:
strcat(blk_path, "maps" DIR_SEP);
return;
case POKERED:
strcat(blk_path, "gfx" DIR_SEP "blocksets" DIR_SEP);
return;
case PRISM:
strcat(blk_path, "maps" DIR_SEP "blk" DIR_SEP);
return;
}
}

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

void tileset_path(char *dest, const char *root, const char *tileset) {
void Config::tileset_path(char *dest, const char *root, const char *tileset) {
// prefer png, then 2bpp, then 2bpp.lz
sprintf(dest, "%s%s%s.png", root, gfx_tileset_dir(), tileset);
if (file_exists(dest)) { return; }
Expand All @@ -51,13 +87,35 @@ void tileset_path(char *dest, const char *root, const char *tileset) {
sprintf(dest, "%s%s%s.2bpp.lz", root, gfx_tileset_dir(), tileset);
}

void metatileset_path(char *dest, const char *root, const char *tileset) {
sprintf(dest, "%stilesets" DIR_SEP "%s_metatiles.bin", root, tileset);
void Config::metatileset_path(char *dest, const char *root, const char *tileset) {
if (global_project == Project::POKERED) {
// remove trailing ".t#", e.g. tileset "overworld.t2" -> name "overworld"
#ifdef _WIN32
char *name = _strdup(tileset);
#else
char *name = strdup(tileset);
#endif
char *dot = strchr(name, '.');
if (dot) { *dot = '\0'; }
sprintf(dest, "%sgfx" DIR_SEP "blocksets" DIR_SEP "%s.bst", root, name);
free(name);
}
else {
sprintf(dest, "%stilesets" DIR_SEP "%s_metatiles.bin", root, tileset);
}
}

void map_constants_path(char *dest, const char *root) {
void Config::map_constants_path(char *dest, const char *root) {
// prefer map_dimension_constants.asm to map_constants.asm
sprintf(dest, "%sconstants" DIR_SEP "map_dimension_constants.asm", root);
if (file_exists(dest)) { return; }
sprintf(dest, "%sconstants" DIR_SEP "map_constants.asm", root);
}

bool Config::monochrome() {
return global_project == Project::POKERED;
}

bool Config::skip_tiles_60_to_7f() {
return global_project == Project::POKECRYSTAL || global_project == Project::PRISM;
}
32 changes: 23 additions & 9 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,28 @@

#define NEW_BLK_NAME "NewMap.blk"

extern Fl_Preferences global_config;

const char *gfx_tileset_dir(void);
bool project_path_from_blk_path(const char *blk_path, char *project_path, bool prism);
void blk_path_from_project_path(const char *project_path, char *blk_path, bool prism);
void palette_map_path(char *dest, const char *root, const char *tileset);
void tileset_path(char *dest, const char *root, const char *tileset);
void metatileset_path(char *dest, const char *root, const char *tileset);
void map_constants_path(char *dest, const char *root);
class Config {
public:
enum Project { POKECRYSTAL, POKERED, POLISHED, PRISM };
private:
static Fl_Preferences global_config;
static Project global_project;
public:
inline static Fl_Preferences config(void) { return global_config; }
inline static Project project(void) { return global_project; }
inline static void project(Project p) { global_project = p; }
static int get(const char *key, int default = 0);
static void set(const char *key, int value);
static const char *gfx_tileset_dir(void);
static const char *map_macro(void);
static bool project_path_from_blk_path(const char *blk_path, char *project_path);
static void blk_path_from_project_path(const char *project_path, char *blk_path);
static void palette_map_path(char *dest, const char *root, const char *tileset);
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 bool monochrome(void);
static bool skip_tiles_60_to_7f(void);
};

#endif
108 changes: 69 additions & 39 deletions src/main-window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
#endif

Main_Window::Main_Window(int x, int y, int w, int h, const char *) : Fl_Double_Window(x, y, w, h, PROGRAM_NAME),
_grid_mi(NULL), _zoom_mi(NULL), _ids_mi(NULL), _hex_mi(NULL), _prism_mi(NULL),
_grid_mi(NULL), _zoom_mi(NULL), _ids_mi(NULL), _hex_mi(NULL),
_pokecrystal_project_mi(NULL), _pokered_project_mi(NULL), _polished_project_mi(NULL), _prism_project_mi(NULL),
_directory(), _blk_file(), _metatileset(), _map(), _metatile_buttons(), _selected(NULL),
_unsaved(false), _wx(x), _wy(y), _ww(w), _wh(h) {
// Populate window
Expand Down Expand Up @@ -114,12 +115,10 @@ Main_Window::Main_Window(int x, int y, int w, int h, const char *) : Fl_Double_W
_dnd_receiver->user_data(this);

// Get global configs
int grid_config, zoom_config, ids_config, hex_config, prism_config;
global_config.get("grid", grid_config, 1);
global_config.get("zoom", zoom_config, 0);
global_config.get("ids", ids_config, 0);
global_config.get("hex", hex_config, 1);
global_config.get("options-prism", prism_config, 0);
int grid_config = Config::get("grid", 1);
int zoom_config = Config::get("zoom", 0);
int ids_config = Config::get("ids", 0);
int hex_config = Config::get("hex", 1);

// Configure window
size_range(384, 256);
Expand Down Expand Up @@ -186,8 +185,16 @@ Main_Window::Main_Window(int x, int y, int w, int h, const char *) : Fl_Double_W
OS_MENU_ITEM("Full &Screen", FL_F + 11, (Fl_Callback *)full_screen_cb, this, FL_MENU_TOGGLE),
{},
OS_SUBMENU("&Options"),
OS_MENU_ITEM("&Prism Compatibility", 0, (Fl_Callback *)prism_cb, this,
FL_MENU_TOGGLE | (prism_config ? FL_MENU_VALUE : 0)),
OS_MENU_ITEM("&Project Type", 0, NULL, NULL, FL_SUBMENU),
OS_MENU_ITEM("poke&crystal", 0, (Fl_Callback *)pokecrystal_project_cb, this,
FL_MENU_RADIO | (Config::project() == Config::Project::POKECRYSTAL ? FL_MENU_VALUE : 0)),
OS_MENU_ITEM("poke&red", 0, (Fl_Callback *)pokered_project_cb, this,
FL_MENU_RADIO | (Config::project() == Config::Project::POKERED ? FL_MENU_VALUE : 0)),
OS_MENU_ITEM("&Polished Crystal", 0, (Fl_Callback *)polished_project_cb, this,
FL_MENU_RADIO | (Config::project() == Config::Project::POLISHED ? FL_MENU_VALUE : 0)),
OS_MENU_ITEM("P&rism", 0, (Fl_Callback *)prism_project_cb, this,
FL_MENU_RADIO | (Config::project() == Config::Project::PRISM ? FL_MENU_VALUE : 0)),
{},
{},
OS_SUBMENU("&Help"),
OS_MENU_ITEM("&Help", FL_F + 1, (Fl_Callback *)help_cb, this, FL_MENU_DIVIDER),
Expand All @@ -208,8 +215,11 @@ Main_Window::Main_Window(int x, int y, int w, int h, const char *) : Fl_Double_W
_zoom_mi = PM_FIND_MENU_ITEM_CB(zoom_cb);
_ids_mi = PM_FIND_MENU_ITEM_CB(ids_cb);
_hex_mi = PM_FIND_MENU_ITEM_CB(hex_cb);
_prism_mi = PM_FIND_MENU_ITEM_CB(prism_cb);
_full_screen_mi = PM_FIND_MENU_ITEM_CB(full_screen_cb);
_pokecrystal_project_mi = PM_FIND_MENU_ITEM_CB(pokecrystal_project_cb);
_pokered_project_mi = PM_FIND_MENU_ITEM_CB(pokered_project_cb);
_polished_project_mi = PM_FIND_MENU_ITEM_CB(polished_project_cb);
_prism_project_mi = PM_FIND_MENU_ITEM_CB(prism_project_cb);
#undef PM_FIND_MENU_ITEM_CB

// Configure toolbar buttons
Expand Down Expand Up @@ -278,7 +288,7 @@ Main_Window::Main_Window(int x, int y, int w, int h, const char *) : Fl_Double_W
_blk_save_chooser->filter("BLK Files\t*.blk\n");
_blk_save_chooser->preset_file(NEW_BLK_NAME);

_new_dir_chooser->title("Choose pokecrystal project directory:");
_new_dir_chooser->title("Choose project directory:");

_png_chooser->title("Print Screenshot");
_png_chooser->filter("PNG Files\t*.png\n");
Expand Down Expand Up @@ -354,7 +364,7 @@ const char *Main_Window::modified_filename() {
return fl_filename_name(_blk_file.c_str());
}
static char buffer[FL_PATH_MAX] = {};
metatileset_path(buffer, _directory.c_str(), _metatileset.tileset()->name());
Config::metatileset_path(buffer, _directory.c_str(), _metatileset.tileset()->name());
return fl_filename_name(buffer);
}

Expand Down Expand Up @@ -423,9 +433,10 @@ void Main_Window::open_map(const char *filename) {
const char *basename = fl_filename_name(filename);

char directory[FL_PATH_MAX] = {};
if (!project_path_from_blk_path(filename, directory, prism())) {
if (!Config::project_path_from_blk_path(filename, directory)) {
std::string msg = "Could not get project directory for ";
msg = msg + basename + "!";
msg = msg + basename + "!\n\n"
"Make sure Options->Project Type is correct.";
_error_dialog->message(msg);
_error_dialog->show(this);
return;
Expand All @@ -438,7 +449,7 @@ void Main_Window::open_map(const char *directory, const char *filename) {
// get map options
if (!_map_options_dialog->limit_blk_options(filename, directory)) {
std::string msg = "Wrong project directory structure!\n\n"
"Double-check Options->Prism Compatibility.";
"Make sure Options->Project Type is correct.";
_error_dialog->message(msg);
_error_dialog->show(this);
return;
Expand All @@ -465,7 +476,7 @@ void Main_Window::open_map(const char *directory, const char *filename) {
}
else {
char new_filename[FL_PATH_MAX] = {};
blk_path_from_project_path(directory, new_filename, prism());
Config::blk_path_from_project_path(directory, new_filename);
strcat(new_filename, NEW_BLK_NAME);
_blk_file = new_filename;
}
Expand All @@ -477,30 +488,30 @@ void Main_Window::open_map(const char *directory, const char *filename) {
Tileset *tileset = _metatileset.tileset();
tileset->name(tileset_name);

palette_map_path(buffer, directory, tileset_name);
Config::palette_map_path(buffer, directory, tileset_name);
if (Palette_Map::Result r = tileset->read_palette_map(buffer)) {
std::string msg = "Error reading ";
palette_map_path(buffer, "", tileset_name);
Config::palette_map_path(buffer, "", tileset_name);
msg = msg + buffer + "!\n\n" + Palette_Map::error_message(r);
_error_dialog->message(msg);
_error_dialog->show(this);
return;
}

tileset_path(buffer, directory, tileset_name);
if (Tileset::Result r = tileset->read_graphics(buffer, _map_options_dialog->lighting(), _map_options_dialog->skip_60_7f())) {
Config::tileset_path(buffer, directory, tileset_name);
if (Tileset::Result r = tileset->read_graphics(buffer, _map_options_dialog->lighting())) {
std::string msg = "Error reading ";
tileset_path(buffer, "", tileset_name);
Config::tileset_path(buffer, "", tileset_name);
msg = msg + buffer + "!\n\n" + Tileset::error_message(r);
_error_dialog->message(msg);
_error_dialog->show(this);
return;
}

metatileset_path(buffer, directory, tileset_name);
Config::metatileset_path(buffer, directory, tileset_name);
if (Metatileset::Result r = _metatileset.read_metatiles(buffer)) {
std::string msg = "Error reading ";
metatileset_path(buffer, "", tileset_name);
Config::metatileset_path(buffer, "", tileset_name);
msg = msg + buffer + "!\n\n" + Metatileset::error_message(r);
_error_dialog->message(msg);
_error_dialog->show(this);
Expand Down Expand Up @@ -743,7 +754,7 @@ bool Main_Window::save_metatileset() {
char filename[FL_PATH_MAX] = {};
const char *directory = _directory.c_str();
const char *tileset_name = _metatileset.tileset()->name();
metatileset_path(filename, directory, tileset_name);
Config::metatileset_path(filename, directory, tileset_name);

if (_metatileset.modified()) {
FILE *file = fl_fopen(filename, "wb");
Expand Down Expand Up @@ -920,7 +931,7 @@ void Main_Window::save_as_cb(Fl_Widget *, Main_Window *mw) {
}

char directory[FL_PATH_MAX] = {};
if (!project_path_from_blk_path(filename, directory, mw->prism())) {
if (!Config::project_path_from_blk_path(filename, directory)) {
std::string msg = "Could not get project directory for ";
msg = msg + basename + "!";
mw->_error_dialog->message(msg);
Expand Down Expand Up @@ -1020,22 +1031,21 @@ void Main_Window::exit_cb(Fl_Widget *, Main_Window *mw) {
}

// Save global config
global_config.set("theme", OS::current_theme());
global_config.set("x", mw->x());
global_config.set("y", mw->y());
global_config.set("w", mw->w());
global_config.set("h", mw->h());
global_config.set("grid", mw->grid());
global_config.set("zoom", mw->zoom());
global_config.set("ids", mw->ids());
global_config.set("hex", mw->hex());
global_config.set("options-prism", mw->prism());
Config::set("theme", OS::current_theme());
Config::set("project", Config::project());
Config::set("x", mw->x());
Config::set("y", mw->y());
Config::set("w", mw->w());
Config::set("h", mw->h());
Config::set("grid", mw->grid());
Config::set("zoom", mw->zoom());
Config::set("ids", mw->ids());
Config::set("hex", mw->hex());
if (mw->_map_options_dialog->initialized()) {
global_config.set("map-lighting", mw->_map_options_dialog->lighting());
global_config.set("map-skip", mw->_map_options_dialog->skip_60_7f());
Config::set("map-lighting", mw->_map_options_dialog->lighting());
}
if (mw->_resize_dialog->initialized()) {
global_config.set("resize-anchor", mw->_resize_dialog->anchor());
Config::set("resize-anchor", mw->_resize_dialog->anchor());
}

exit(EXIT_SUCCESS);
Expand Down Expand Up @@ -1136,7 +1146,27 @@ void Main_Window::full_screen_cb(Fl_Menu_ *m, Main_Window *mw) {
}
}

void Main_Window::prism_cb(Fl_Menu_ *, Main_Window *mw) {
void Main_Window::pokecrystal_project_cb(Fl_Menu_ *, Main_Window *mw) {
Config::project(Config::Project::POKECRYSTAL);
mw->_pokecrystal_project_mi->setonly();
mw->redraw();
}

void Main_Window::pokered_project_cb(Fl_Menu_ *, Main_Window *mw) {
Config::project(Config::Project::POKERED);
mw->_pokered_project_mi->setonly();
mw->redraw();
}

void Main_Window::polished_project_cb(Fl_Menu_ *, Main_Window *mw) {
Config::project(Config::Project::POLISHED);
mw->_polished_project_mi->setonly();
mw->redraw();
}

void Main_Window::prism_project_cb(Fl_Menu_ *, Main_Window *mw) {
Config::project(Config::Project::PRISM);
mw->_prism_project_mi->setonly();
mw->redraw();
}

Expand Down
Loading

0 comments on commit ee37ca2

Please sign in to comment.