diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 20:31:18 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-30 20:31:18 +0100 |
commit | d8daa3e045ca2f41edcbed533bc5a9fef1363a17 (patch) | |
tree | 6a4c0883d05607476b609e68bcac7d854e281aea /backend | |
parent | 6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (diff) |
print to backend
Diffstat (limited to 'backend')
-rw-r--r-- | backend/CMakeLists.txt | 1 | ||||
-rw-r--r-- | backend/Location.cpp | 34 | ||||
-rw-r--r-- | backend/Location.h | 16 | ||||
-rw-r--r-- | backend/Object.cpp | 7 | ||||
-rw-r--r-- | backend/Object.h | 5 | ||||
-rw-r--r-- | backend/String.cpp | 11 | ||||
-rw-r--r-- | backend/String.h | 2 | ||||
-rw-r--r-- | backend/print.cpp | 67 | ||||
-rw-r--r-- | backend/print.h | 26 | ||||
-rw-r--r-- | backend/util.cpp | 4 | ||||
-rw-r--r-- | backend/util.h | 3 |
11 files changed, 150 insertions, 26 deletions
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<Object *> Location::get_objects() const { - return this->objects.range(); +ListRange<Object *> 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<Object *> 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<Object *> get_objects() const; + void add_visible_object(Object *); + void remove_visible_object(Object *); + ListRange<Object *> get_visible_objects() const; + + void add_hidden_object(Object *); + void remove_hidden_object(Object *); + ListRange<Object *> 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<Enemy*> enemies; - List<Object*> objects; + List<Object*> hidden_objects; + List<Object*> 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<char *>(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 <stddef.h> +#include <stdarg.h> 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 <stdlib.h> +#include <string.h> +#include <stdarg.h> +#include <unistd.h> + +#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 <stdio.h> + +#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<void *>(ptr); safe_free(x); } +void safe_free(FILE * & ptr) { + auto x = static_cast<void *>(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 <stdio.h> + #include "List.h" void safe_free(void * & ptr); @@ -7,6 +9,7 @@ void safe_free(char * & ptr); void safe_free(const char * & ptr); template <typename T> void safe_free(List<T *> & ptr_list); +void safe_free(FILE * & ptr); #include "util.hpp" |