aboutsummaryrefslogtreecommitdiff
path: root/frontend
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 20:31:18 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-30 20:31:18 +0100
commitd8daa3e045ca2f41edcbed533bc5a9fef1363a17 (patch)
tree6a4c0883d05607476b609e68bcac7d854e281aea /frontend
parent6e1d62955c7a7f39bc9126d709a42a70e02a1d30 (diff)
print to backend
Diffstat (limited to 'frontend')
-rw-r--r--frontend/CMakeLists.txt1
-rw-r--r--frontend/Player.cpp1
-rw-r--r--frontend/cmd/cheat.cpp3
-rw-r--r--frontend/cmd/get.cpp7
-rw-r--r--frontend/cmd/go.cpp2
-rw-r--r--frontend/cmd/help.cpp3
-rw-r--r--frontend/cmd/query.cpp5
-rw-r--r--frontend/cmd/search.cpp9
-rw-r--r--frontend/cmd/view.cpp3
-rw-r--r--frontend/generate_dungeon.cpp2
-rw-r--r--frontend/load_dungeon.cpp6
-rw-r--r--frontend/main.cpp2
-rw-r--r--frontend/print.cpp73
-rw-r--r--frontend/print.h27
-rw-r--r--frontend/rl.cpp4
-rw-r--r--frontend/strings.cpp3
16 files changed, 24 insertions, 127 deletions
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<Object> 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>(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 <memory>
#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<Dungeon> load_dungeon(const string & filename) {
vector<string> 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<string> 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<string> 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 <cstdio>
#include <memory>
+#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 <cstdarg>
-#include <string>
-#include <unistd.h>
-
-#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 <cstring>
-#include <memory>
-#include <functional>
-#include <string>
-
-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, std::function<void(FILE*)>> 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 <readline/history.h>
#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 <algorithm>
#include "strings.h"
-#include "print.h"
+
+#include "backend/print.h"
using namespace std;