From d8daa3e045ca2f41edcbed533bc5a9fef1363a17 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 30 Oct 2024 20:31:18 +0100 Subject: print to backend --- backend/CMakeLists.txt | 1 + backend/Location.cpp | 34 +++++++++++++++----- backend/Location.h | 16 +++++++--- backend/Object.cpp | 7 ----- backend/Object.h | 5 --- backend/String.cpp | 11 +++++-- backend/String.h | 2 ++ backend/print.cpp | 67 +++++++++++++++++++++++++++++++++++++++ backend/print.h | 26 +++++++++++++++ backend/util.cpp | 4 +++ backend/util.h | 3 ++ frontend/CMakeLists.txt | 1 - frontend/Player.cpp | 1 - frontend/cmd/cheat.cpp | 3 +- frontend/cmd/get.cpp | 7 ++--- frontend/cmd/go.cpp | 2 +- frontend/cmd/help.cpp | 3 +- frontend/cmd/query.cpp | 5 ++- frontend/cmd/search.cpp | 9 +++--- frontend/cmd/view.cpp | 3 +- frontend/generate_dungeon.cpp | 2 +- frontend/load_dungeon.cpp | 6 ++-- frontend/main.cpp | 2 +- frontend/print.cpp | 73 ------------------------------------------- frontend/print.h | 27 ---------------- frontend/rl.cpp | 4 +-- frontend/strings.cpp | 3 +- 27 files changed, 174 insertions(+), 153 deletions(-) create mode 100644 backend/print.cpp create mode 100644 backend/print.h delete mode 100644 frontend/print.cpp delete mode 100644 frontend/print.h diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 3c6991a..1f872e4 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -13,5 +13,6 @@ target_sources(main PUBLIC WeaponObject.cpp Enemy.cpp String.cpp + print.cpp ) diff --git a/backend/Location.cpp b/backend/Location.cpp index d3227cf..7246246 100644 --- a/backend/Location.cpp +++ b/backend/Location.cpp @@ -22,7 +22,8 @@ Location::Location(const String & name, const String & description) : name(name) Location::~Location() { safe_free(this->enemies); - safe_free(this->objects); + safe_free(this->hidden_objects); + safe_free(this->visible_objects); } void Location::set_name(const String & name) { this->name = name; } @@ -38,14 +39,33 @@ Location * Location::get_exit(Direction dir) const { return this->edges[dir]; } -void Location::add_object(Object * object) { - this->objects.push_back(object); +void Location::add_visible_object(Object * object) { + this->visible_objects.push_back(object); } -void Location::remove_object(Object * object) { - this->objects.remove(object); +void Location::remove_visible_object(Object * object) { + this->visible_objects.remove(object); } -ListRange Location::get_objects() const { - return this->objects.range(); +ListRange Location::get_visible_objects() const { + return this->visible_objects.range(); +} + +void Location::add_hidden_object(Object * object) { + this->hidden_objects.push_back(object); +} +void Location::remove_hidden_object(Object * object) { + this->hidden_objects.remove(object); +} +ListRange Location::get_hidden_objects() const { + return this->hidden_objects.range(); +} + +void Location::hide_object(Object * object) { + this->visible_objects.remove(object); + this->hidden_objects.push_back(object); +} +void Location::unhide_object(Object * object) { + this->hidden_objects.remove(object); + this->visible_objects.push_back(object); } void Location::add_enemy(Enemy * enemy) { diff --git a/backend/Location.h b/backend/Location.h index b139da8..464328b 100644 --- a/backend/Location.h +++ b/backend/Location.h @@ -30,9 +30,16 @@ public: void set_exit(Direction dir, Location * location = nullptr); Location * get_exit(Direction dir) const; - void add_object(Object *); - void remove_object(Object *); - ListRange get_objects() const; + void add_visible_object(Object *); + void remove_visible_object(Object *); + ListRange get_visible_objects() const; + + void add_hidden_object(Object *); + void remove_hidden_object(Object *); + ListRange get_hidden_objects() const; + + void hide_object(Object *); + void unhide_object(Object *); void add_enemy(Enemy *); void remove_enemy(Enemy *); @@ -48,7 +55,8 @@ private: String name; String description; List enemies; - List objects; + List hidden_objects; + List visible_objects; Location * edges[4] = { nullptr, diff --git a/backend/Object.cpp b/backend/Object.cpp index 2640607..da1738c 100644 --- a/backend/Object.cpp +++ b/backend/Object.cpp @@ -20,10 +20,3 @@ const String & Object::get_description() const { return this->description; } -void Object::set_hidden(bool hidden) { - this->hidden = hidden; -} -bool Object::get_hidden() { - return this->hidden; -} - diff --git a/backend/Object.h b/backend/Object.h index 07b7b74..0cebd64 100644 --- a/backend/Object.h +++ b/backend/Object.h @@ -13,8 +13,6 @@ public: virtual const String & get_displayname() const; void set_description(const String & description); const String & get_description() const; - void set_hidden(bool hidden); - bool get_hidden(); protected: friend class ObjectFactory; @@ -22,8 +20,5 @@ protected: public: virtual ~Object() = default; -protected: - bool hidden = false; - }; diff --git a/backend/String.cpp b/backend/String.cpp index 2b40c88..e387589 100644 --- a/backend/String.cpp +++ b/backend/String.cpp @@ -20,17 +20,22 @@ String::String(const char * c_str) { } String String::fmt(const char * fmt, ...) { - String out; - va_list args, args_copy; + va_list args; va_start(args, fmt); + String out = String::va_fmt(args, fmt); + va_end(args); + return out; +} +String String::va_fmt(va_list args, const char * fmt) { + String out; + va_list args_copy; va_copy(args_copy, args); out._data_len = vsnprintf(NULL, 0, fmt, args_copy); va_end(args_copy); out._data = static_cast(malloc(out._data_len + 1)); vsnprintf(out._data, out._data_len + 1, fmt, args); - va_end(args); return out; } diff --git a/backend/String.h b/backend/String.h index b4025be..e41222e 100644 --- a/backend/String.h +++ b/backend/String.h @@ -1,6 +1,7 @@ #pragma once #include +#include class String { public: @@ -9,6 +10,7 @@ public: String(const String &); ~String(); public: + static String va_fmt(va_list args, const char * fmt); static String fmt(const char * fmt, ...); public: diff --git a/backend/print.cpp b/backend/print.cpp new file mode 100644 index 0000000..fc8ad9b --- /dev/null +++ b/backend/print.cpp @@ -0,0 +1,67 @@ +#include +#include +#include +#include + +#include "print.h" +#include "util.h" + +static String va_stringf(va_list args, const char * fmt) { + va_list args_copy; + va_copy(args_copy, args); + + size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, fmt, args); + + String out = msg; + free(msg); + + va_end(args); + return out; +} + +void lprtf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + String formatted = String::va_fmt(args, fmt); + va_end(args); + + fwrite(formatted.c_str(), 1, formatted.size(), stdout); + fflush(stdout); + + SessionLog::get().append(formatted); +} + +SessionLog & SessionLog::get() { + static SessionLog instance; + return instance; +} + +SessionLog::SessionLog() { + if (!this->enable) return; + + String filename = String::fmt("%lu.log", getpid()); + FILE * file = fopen(filename.c_str(), "w+"); +} + +SessionLog::~SessionLog() { + safe_free(this->file); +} + +void SessionLog::append(const String & str) const { + this->append(str.data(), str.size()); +} + +void SessionLog::append(const char * str) const { + this->append(str, strlen(str)); +} + +void SessionLog::append(const char * buf, size_t buf_size) const { + if (this->file == nullptr) return; + if (buf_size == 0) return; + fwrite(buf, 1, buf_size, this->file); +} + diff --git a/backend/print.h b/backend/print.h new file mode 100644 index 0000000..8fa0501 --- /dev/null +++ b/backend/print.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +#include "String.h" + +void lprtf(const char * fmt, ...); + +class SessionLog { +public: + static SessionLog & get(); + +private: + SessionLog(); + virtual ~SessionLog(); + +public: + virtual void append(const String & str) const; + virtual void append(const char * str) const; + virtual void append(const char * buf, size_t buf_size) const; + +private: + FILE * file = nullptr; + static constexpr const bool enable = false; +}; + diff --git a/backend/util.cpp b/backend/util.cpp index 85b6950..afac06e 100644 --- a/backend/util.cpp +++ b/backend/util.cpp @@ -15,4 +15,8 @@ void safe_free(char * & ptr) { auto x = static_cast(ptr); safe_free(x); } +void safe_free(FILE * & ptr) { + auto x = static_cast(ptr); + safe_free(x); +} diff --git a/backend/util.h b/backend/util.h index bbe307b..e544e19 100644 --- a/backend/util.h +++ b/backend/util.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "List.h" void safe_free(void * & ptr); @@ -7,6 +9,7 @@ void safe_free(char * & ptr); void safe_free(const char * & ptr); template void safe_free(List & ptr_list); +void safe_free(FILE * & ptr); #include "util.hpp" diff --git a/frontend/CMakeLists.txt b/frontend/CMakeLists.txt index d60b44f..92d03bc 100644 --- a/frontend/CMakeLists.txt +++ b/frontend/CMakeLists.txt @@ -2,7 +2,6 @@ target_sources(main PUBLIC main.cpp rl.cpp strings.cpp - print.cpp Player.cpp load_dungeon.cpp generate_dungeon.cpp diff --git a/frontend/Player.cpp b/frontend/Player.cpp index c579f83..27c660c 100644 --- a/frontend/Player.cpp +++ b/frontend/Player.cpp @@ -1,6 +1,5 @@ #include "strings.h" #include "Player.h" -#include "print.h" #include "backend/Dungeon.h" diff --git a/frontend/cmd/cheat.cpp b/frontend/cmd/cheat.cpp index aeed5f4..7a020a3 100644 --- a/frontend/cmd/cheat.cpp +++ b/frontend/cmd/cheat.cpp @@ -1,5 +1,6 @@ +#include "backend/print.h" + #include "../Player.h" -#include "../print.h" using namespace std; diff --git a/frontend/cmd/get.cpp b/frontend/cmd/get.cpp index 12c3353..8aa2d75 100644 --- a/frontend/cmd/get.cpp +++ b/frontend/cmd/get.cpp @@ -1,7 +1,7 @@ #include "../Player.h" #include "../strings.h" -#include "../print.h" +#include "backend/print.h" #include "backend/GoldObject.h" #include "backend/Location.h" @@ -9,11 +9,10 @@ using namespace std; FollowupAction Player::cmd_get(string & target_name) { unique_ptr target = nullptr; - for (Object * object : this->location.get_objects()) { - if (object->get_hidden() == true) continue; + for (Object * object : this->location.get_visible_objects()) { if (str_lower(object->get_name().c_str()) != str_lower(target_name)) continue; target = unique_ptr(object); - this->location.remove_object(object); + this->location.remove_visible_object(object); break; } if (target == nullptr) { diff --git a/frontend/cmd/go.cpp b/frontend/cmd/go.cpp index 624bf6c..9565e99 100644 --- a/frontend/cmd/go.cpp +++ b/frontend/cmd/go.cpp @@ -1,7 +1,7 @@ #include "backend/Location.h" +#include "backend/print.h" #include "../Player.h" -#include "../print.h" #include "../strings.h" using namespace std; diff --git a/frontend/cmd/help.cpp b/frontend/cmd/help.cpp index e614f28..b655a45 100644 --- a/frontend/cmd/help.cpp +++ b/frontend/cmd/help.cpp @@ -1,4 +1,5 @@ -#include "../print.h" +#include "backend/print.h" + #include "../Player.h" using namespace std; diff --git a/frontend/cmd/query.cpp b/frontend/cmd/query.cpp index acd6cee..19f2584 100644 --- a/frontend/cmd/query.cpp +++ b/frontend/cmd/query.cpp @@ -1,9 +1,9 @@ #include "backend/Location.h" #include "backend/Object.h" #include "backend/Enemy.h" +#include "backend/print.h" #include "../Player.h" -#include "../print.h" using namespace std; @@ -21,8 +21,7 @@ FollowupAction Player::cmd_query(string &) { { lprtf("Zichtbare objecten: "); size_t objects = 0; - for (Object * obj : this->location.get_objects()) { - if (obj->get_hidden() == true) continue; + for (Object * obj : this->location.get_visible_objects()) { if (objects > 0) lprtf(", "); lprtf("%s", obj->get_displayname().c_str()); objects++; diff --git a/frontend/cmd/search.cpp b/frontend/cmd/search.cpp index 67b0bf1..f10709a 100644 --- a/frontend/cmd/search.cpp +++ b/frontend/cmd/search.cpp @@ -1,17 +1,16 @@ #include "../Player.h" -#include "../print.h" +#include "backend/print.h" #include "backend/Location.h" using namespace std; FollowupAction Player::cmd_search(string &) { bool found = false; - for (Object * object : this->location.get_objects()) { - if (object->get_hidden() == false) continue; + for (Object * object : this->location.get_hidden_objects()) { if (!found) lprtf("Je vindt:\n"); - lprtf("- %s\n", object->get_displayname()); - object->set_hidden(false); + lprtf("- %s\n", object->get_displayname().c_str()); + this->location.unhide_object(object); found = true; } if (!found) diff --git a/frontend/cmd/view.cpp b/frontend/cmd/view.cpp index b9d179b..a252715 100644 --- a/frontend/cmd/view.cpp +++ b/frontend/cmd/view.cpp @@ -1,7 +1,8 @@ -#include "../print.h" #include "../Player.h" #include "../strings.h" +#include "backend/print.h" + using namespace std; FollowupAction Player::cmd_view(string & target) { diff --git a/frontend/generate_dungeon.cpp b/frontend/generate_dungeon.cpp index 80eb0cb..868fe43 100644 --- a/frontend/generate_dungeon.cpp +++ b/frontend/generate_dungeon.cpp @@ -1,9 +1,9 @@ #include #include "backend/Dungeon.h" +#include "backend/print.h" #include "rl.h" -#include "print.h" #include "generate_dungeon.h" #include "GameData.h" #include "Exception.h" diff --git a/frontend/load_dungeon.cpp b/frontend/load_dungeon.cpp index 23b4094..c671fe7 100644 --- a/frontend/load_dungeon.cpp +++ b/frontend/load_dungeon.cpp @@ -56,14 +56,12 @@ unique_ptr load_dungeon(const string & filename) { vector objects_hidden = str_split(tag.attribute("objectenverborgen").as_string(), ";"); for (string & name : objects_hidden) { Object * object = gamedata.create_object(name); - object->set_hidden(true); - location->add_object(object); + location->add_hidden_object(object); } vector objects_visible = str_split(tag.attribute("objectenzichtbaar").as_string(), ";"); for (string & name : objects_visible) { Object * object = gamedata.create_object(name); - object->set_hidden(false); - location->add_object(object); + location->add_visible_object(object); } vector enemies = str_split(tag.attribute("vijand").as_string(), ";"); diff --git a/frontend/main.cpp b/frontend/main.cpp index 366473d..56743c1 100644 --- a/frontend/main.cpp +++ b/frontend/main.cpp @@ -2,11 +2,11 @@ #include #include +#include "backend/print.h" #include "backend/Dungeon.h" #include "Player.h" #include "Exception.h" -#include "print.h" #include "load_dungeon.h" #include "generate_dungeon.h" #include "rl.h" diff --git a/frontend/print.cpp b/frontend/print.cpp deleted file mode 100644 index 0c60892..0000000 --- a/frontend/print.cpp +++ /dev/null @@ -1,73 +0,0 @@ -#include -#include -#include - -#include "print.h" - -using namespace std; - -static string va_stringf(va_list args, const char * fmt) { - va_list args_copy; - va_copy(args_copy, args); - - size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; - char * msg = (char *) malloc(sz); - va_end(args_copy); - - vsnprintf(msg, sz, fmt, args); - - string out = msg; - free(msg); - - va_end(args); - - return out; -} - -static string stringf(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - string out = va_stringf(args, fmt); - va_end(args); - return out; -} - -void lprtf(const char * fmt, ...) { - va_list args; - va_start(args, fmt); - string formatted = va_stringf(args, fmt); - va_end(args); - - fwrite(formatted.c_str(), 1, formatted.size(), stdout); - fflush(stdout); - - SessionLog::get().append(formatted); -} - -SessionLog & SessionLog::get() { - static SessionLog instance; - return instance; -} - -SessionLog::SessionLog() { - if (!this->enable) return; - - string filename = stringf("%lu.log", getpid()); - FILE * file = fopen(filename.c_str(), "w+"); - this->file = { file, [] (FILE * file) { fclose(file); } }; -} - -void SessionLog::append(const string & str) const { - this->append(str.data(), str.size()); -} - -void SessionLog::append(const char * str) const { - this->append(str, strlen(str)); -} - -void SessionLog::append(const char * buf, size_t buf_size) const { - if (this->file == nullptr) return; - if (buf_size == 0) return; - fwrite(buf, 1, buf_size, this->file.get()); -} - diff --git a/frontend/print.h b/frontend/print.h deleted file mode 100644 index b8034f2..0000000 --- a/frontend/print.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -void lprtf(const char * fmt, ...); - -class SessionLog { -public: - static SessionLog & get(); - -private: - SessionLog(); - virtual ~SessionLog() = default; - -public: - virtual void append(const std::string & str) const; - virtual void append(const char * str) const; - virtual void append(const char * buf, size_t buf_size) const; - -private: - std::unique_ptr> file = nullptr; - static constexpr const bool enable = false; -}; - diff --git a/frontend/rl.cpp b/frontend/rl.cpp index 1c0df0e..3ce360f 100644 --- a/frontend/rl.cpp +++ b/frontend/rl.cpp @@ -5,7 +5,7 @@ #include #include "rl.h" -#include "print.h" +#include "backend/print.h" using namespace std; @@ -19,7 +19,7 @@ string rl() { if (out.size() > 0) add_history(input); free(input); - SessionLog::get().append(out); + SessionLog::get().append(out.c_str()); SessionLog::get().append("\n"); return out; diff --git a/frontend/strings.cpp b/frontend/strings.cpp index 687309d..05c1548 100644 --- a/frontend/strings.cpp +++ b/frontend/strings.cpp @@ -1,7 +1,8 @@ #include #include "strings.h" -#include "print.h" + +#include "backend/print.h" using namespace std; -- cgit v1.2.3