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 +++ 11 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 backend/print.cpp create mode 100644 backend/print.h (limited to 'backend') 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" -- cgit v1.2.3