From 4fce641cc077d18f972e250d2fe3be5067618127 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 10 Jun 2020 18:54:01 +0200 Subject: [PATCH] register scene generators --- Makefile.am | 1 + src/generators.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++ src/generators.h | 13 ++++++++----- src/main.cpp | 3 ++- src/mods.cpp | 31 ++++++++++++++++++------------ src/mods.h | 3 ++- src/paths.cpp | 9 ++++++--- src/paths.h | 3 ++- 8 files changed, 88 insertions(+), 23 deletions(-) create mode 100644 src/generators.cpp diff --git a/Makefile.am b/Makefile.am index eb984b7..05cea95 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,6 @@ bin_PROGRAMS = dmaster dmaster_SOURCES = \ + src/generators.cpp \ src/main.cpp \ src/mods.cpp \ src/paths.cpp diff --git a/src/generators.cpp b/src/generators.cpp new file mode 100644 index 0000000..b82377b --- /dev/null +++ b/src/generators.cpp @@ -0,0 +1,48 @@ +/* Dungeon Master --- RPG Adventure Generator + Copyright © 2019 Javier Sancho + + Dungeon Master is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Dungeon Master is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with Dungeon Master. If not, see . +*/ + +#include +#include +#include +#include "generators.h" + +struct compare_strings +{ + bool operator() (const char* a, const char* b) const + { + return strcmp (a, b) < 0; + } +}; + +std::map, + compare_strings> registered_generators; + +SceneGenerator* +register_generator (char* name, + char* type, + SCM proc) +{ + SceneGenerator* generator = (SceneGenerator*) malloc (sizeof (SceneGenerator)); + generator->name = name; + generator->type = type; + generator->proc = proc; + + registered_generators[type].push_back (generator); + + return generator; +} diff --git a/src/generators.h b/src/generators.h index cc89770..ab044bc 100644 --- a/src/generators.h +++ b/src/generators.h @@ -17,11 +17,14 @@ #include -struct SceneGenerator +typedef struct { - std::string name; - std::string type; + char* name; + char* type; SCM proc; -}; +} SceneGenerator; -int register_generator (SceneGenerator generator); +SceneGenerator* +register_generator (char* name, + char* type, + SCM proc); diff --git a/src/main.cpp b/src/main.cpp index 4a522aa..16c6fac 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -23,7 +23,8 @@ using namespace core; using namespace video; using namespace gui; -int main () +int +main () { load_dmaster_mods (); diff --git a/src/mods.cpp b/src/mods.cpp index dea09b6..7b9115e 100644 --- a/src/mods.cpp +++ b/src/mods.cpp @@ -16,41 +16,48 @@ */ #include -#include #include #include "generators.h" #include "mods.h" #include "paths.h" - -SCM register_scene_generator (SCM name, SCM type, SCM proc) +SCM +register_scene_generator (SCM name, + SCM type, + SCM proc) { - SceneGenerator generator {scm_to_locale_string (name), scm_to_locale_string (type), proc }; - //register_generator (generator); - printf ("Register: %s (%s)\n", generator.name.c_str (), generator.type.c_str ()); - scm_call_1 (proc, scm_from_int (-1)); + SceneGenerator* generator = + register_generator (scm_to_utf8_string (name), + scm_to_utf8_string (scm_symbol_to_string (type)), + proc); + printf ("Register: %s (%s)\n", generator->name, generator->type); + scm_call_1 (generator->proc, scm_from_int (-1)); return SCM_UNSPECIFIED; } -void init_dungeon_master_module (void *unused) +void +init_dungeon_master_module (void *unused) { scm_c_define_gsubr ("register-scene-generator", 3, 0, 0, (scm_t_subr) register_scene_generator); scm_c_export ("register-scene-generator", NULL); } -void scm_init_dungeon_master_module () +void +scm_init_dungeon_master_module () { scm_c_define_module ("dungeon-master", init_dungeon_master_module, NULL); } -void add_to_load_path (std::string path) +void +add_to_load_path (std::string path) { - // Add path to %load-path variable, needed for modules created in mods + // Add path to %load-path variable, needed for mods structured like modules std::string exp = "(add-to-load-path \"" + path + "\")"; scm_c_eval_string (exp.c_str ()); } -void load_dmaster_mods () +void +load_dmaster_mods () { scm_init_guile (); scm_init_dungeon_master_module (); diff --git a/src/mods.h b/src/mods.h index e5fd4a5..f8cd154 100644 --- a/src/mods.h +++ b/src/mods.h @@ -15,4 +15,5 @@ along with Dungeon Master. If not, see . */ -void load_dmaster_mods (); +void +load_dmaster_mods (); diff --git a/src/paths.cpp b/src/paths.cpp index 02aee3f..b7a8374 100644 --- a/src/paths.cpp +++ b/src/paths.cpp @@ -18,7 +18,8 @@ #include #include "paths.h" -std::string get_exec_path () +std::string +get_exec_path () { char pBuf[256]; size_t len = sizeof (pBuf); @@ -35,12 +36,14 @@ std::string get_exec_path () return exec_path.substr (0, exec_path.rfind (PATH_DELIM) + 1); } -std::string get_home_path () +std::string +get_home_path () { return (std::string) getenv ("HOME") + PATH_DELIM + "." + PROGRAM_NAME; } -std::set get_dmaster_paths () { +std::set +get_dmaster_paths () { std::set paths; paths.insert (get_exec_path ()); paths.insert (DATA_PATH); diff --git a/src/paths.h b/src/paths.h index 38cb13a..6603373 100644 --- a/src/paths.h +++ b/src/paths.h @@ -28,4 +28,5 @@ #define PROGRAM_NAME "dmaster" #endif -std::set get_dmaster_paths (); +std::set +get_dmaster_paths (); -- 2.39.2